3 int check_storage_number(calculated_number n, int debug) {
5 uint32_t flags = SN_EXISTS;
7 storage_number s = pack_storage_number(n, flags);
8 calculated_number d = unpack_storage_number(s);
10 if(!does_storage_number_exist(s)) {
11 fprintf(stderr, "Exists flags missing for number " CALCULATED_NUMBER_FORMAT "!\n", n);
15 calculated_number ddiff = d - n;
16 calculated_number dcdiff = ddiff * 100.0 / n;
18 if(dcdiff < 0) dcdiff = -dcdiff;
20 size_t len = print_calculated_number(buffer, d);
21 calculated_number p = strtold(buffer, NULL);
22 calculated_number pdiff = n - p;
23 calculated_number pcdiff = pdiff * 100.0 / n;
24 if(pcdiff < 0) pcdiff = -pcdiff;
28 CALCULATED_NUMBER_FORMAT " original\n"
29 CALCULATED_NUMBER_FORMAT " packed and unpacked, (stored as 0x%08X, diff " CALCULATED_NUMBER_FORMAT ", " CALCULATED_NUMBER_FORMAT "%%)\n"
30 "%s printed after unpacked (%zu bytes)\n"
31 CALCULATED_NUMBER_FORMAT " re-parsed from printed (diff " CALCULATED_NUMBER_FORMAT ", " CALCULATED_NUMBER_FORMAT "%%)\n\n",
37 if(len != strlen(buffer)) fprintf(stderr, "ERROR: printed number %s is reported to have length %zu but it has %zu\n", buffer, len, strlen(buffer));
38 if(dcdiff > ACCURACY_LOSS) fprintf(stderr, "WARNING: packing number " CALCULATED_NUMBER_FORMAT " has accuracy loss %0.7Lf %%\n", n, dcdiff);
39 if(pcdiff > ACCURACY_LOSS) fprintf(stderr, "WARNING: re-parsing the packed, unpacked and printed number " CALCULATED_NUMBER_FORMAT " has accuracy loss %0.7Lf %%\n", n, pcdiff);
42 if(len != strlen(buffer)) return 1;
43 if(dcdiff > ACCURACY_LOSS) return 3;
44 if(pcdiff > ACCURACY_LOSS) return 4;
48 void benchmark_storage_number(int loop, int multiplier) {
50 calculated_number n, d;
52 unsigned long long user, system, total, mine, their;
56 struct rusage now, last;
58 fprintf(stderr, "\n\nBenchmarking %d numbers, please wait...\n\n", loop);
60 // ------------------------------------------------------------------------
62 fprintf(stderr, "SYSTEM LONG DOUBLE SIZE: %zu bytes\n", sizeof(calculated_number));
63 fprintf(stderr, "NETDATA FLOATING POINT SIZE: %zu bytes\n", sizeof(storage_number));
65 mine = (calculated_number)sizeof(storage_number) * (calculated_number)loop;
66 their = (calculated_number)sizeof(calculated_number) * (calculated_number)loop;
69 fprintf(stderr, "\nNETDATA NEEDS %0.2Lf TIMES MORE MEMORY. Sorry!\n", (long double)(mine / their));
72 fprintf(stderr, "\nNETDATA INTERNAL FLOATING POINT ARITHMETICS NEEDS %0.2Lf TIMES LESS MEMORY.\n", (long double)(their / mine));
75 fprintf(stderr, "\nNETDATA FLOATING POINT\n");
76 fprintf(stderr, "MIN POSITIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", (calculated_number)STORAGE_NUMBER_POSITIVE_MIN);
77 fprintf(stderr, "MAX POSITIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", (calculated_number)STORAGE_NUMBER_POSITIVE_MAX);
78 fprintf(stderr, "MIN NEGATIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", (calculated_number)STORAGE_NUMBER_NEGATIVE_MIN);
79 fprintf(stderr, "MAX NEGATIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", (calculated_number)STORAGE_NUMBER_NEGATIVE_MAX);
80 fprintf(stderr, "Maximum accuracy loss: " CALCULATED_NUMBER_FORMAT "%%\n\n\n", (calculated_number)ACCURACY_LOSS);
82 // ------------------------------------------------------------------------
84 fprintf(stderr, "INTERNAL LONG DOUBLE PRINTING: ");
85 getrusage(RUSAGE_SELF, &last);
88 for(j = 1; j < 11 ;j++) {
89 n = STORAGE_NUMBER_POSITIVE_MIN * j;
91 for(i = 0; i < loop ;i++) {
93 if(n > STORAGE_NUMBER_POSITIVE_MAX) n = STORAGE_NUMBER_POSITIVE_MIN;
95 print_calculated_number(buffer, n);
99 getrusage(RUSAGE_SELF, &now);
100 user = now.ru_utime.tv_sec * 1000000ULL + now.ru_utime.tv_usec - last.ru_utime.tv_sec * 1000000ULL + last.ru_utime.tv_usec;
101 system = now.ru_stime.tv_sec * 1000000ULL + now.ru_stime.tv_usec - last.ru_stime.tv_sec * 1000000ULL + last.ru_stime.tv_usec;
102 total = user + system;
105 fprintf(stderr, "user %0.5Lf, system %0.5Lf, total %0.5Lf\n", (long double)(user / 1000000.0), (long double)(system / 1000000.0), (long double)(total / 1000000.0));
107 // ------------------------------------------------------------------------
109 fprintf(stderr, "SYSTEM LONG DOUBLE PRINTING: ");
110 getrusage(RUSAGE_SELF, &last);
113 for(j = 1; j < 11 ;j++) {
114 n = STORAGE_NUMBER_POSITIVE_MIN * j;
116 for(i = 0; i < loop ;i++) {
118 if(n > STORAGE_NUMBER_POSITIVE_MAX) n = STORAGE_NUMBER_POSITIVE_MIN;
119 snprintfz(buffer, 100, CALCULATED_NUMBER_FORMAT, n);
123 getrusage(RUSAGE_SELF, &now);
124 user = now.ru_utime.tv_sec * 1000000ULL + now.ru_utime.tv_usec - last.ru_utime.tv_sec * 1000000ULL + last.ru_utime.tv_usec;
125 system = now.ru_stime.tv_sec * 1000000ULL + now.ru_stime.tv_usec - last.ru_stime.tv_sec * 1000000ULL + last.ru_stime.tv_usec;
126 total = user + system;
129 fprintf(stderr, "user %0.5Lf, system %0.5Lf, total %0.5Lf\n", (long double)(user / 1000000.0), (long double)(system / 1000000.0), (long double)(total / 1000000.0));
132 fprintf(stderr, "NETDATA CODE IS SLOWER %0.2Lf %%\n", (long double)(mine * 100.0 / their - 100.0));
135 fprintf(stderr, "NETDATA CODE IS F A S T E R %0.2Lf %%\n", (long double)(their * 100.0 / mine - 100.0));
138 // ------------------------------------------------------------------------
140 fprintf(stderr, "\nINTERNAL LONG DOUBLE PRINTING WITH PACK / UNPACK: ");
141 getrusage(RUSAGE_SELF, &last);
144 for(j = 1; j < 11 ;j++) {
145 n = STORAGE_NUMBER_POSITIVE_MIN * j;
147 for(i = 0; i < loop ;i++) {
149 if(n > STORAGE_NUMBER_POSITIVE_MAX) n = STORAGE_NUMBER_POSITIVE_MIN;
151 s = pack_storage_number(n, 1);
152 d = unpack_storage_number(s);
153 print_calculated_number(buffer, d);
157 getrusage(RUSAGE_SELF, &now);
158 user = now.ru_utime.tv_sec * 1000000ULL + now.ru_utime.tv_usec - last.ru_utime.tv_sec * 1000000ULL + last.ru_utime.tv_usec;
159 system = now.ru_stime.tv_sec * 1000000ULL + now.ru_stime.tv_usec - last.ru_stime.tv_sec * 1000000ULL + last.ru_stime.tv_usec;
160 total = user + system;
163 fprintf(stderr, "user %0.5Lf, system %0.5Lf, total %0.5Lf\n", (long double)(user / 1000000.0), (long double)(system / 1000000.0), (long double)(total / 1000000.0));
166 fprintf(stderr, "WITH PACKING UNPACKING NETDATA CODE IS SLOWER %0.2Lf %%\n", (long double)(mine * 100.0 / their - 100.0));
169 fprintf(stderr, "EVEN WITH PACKING AND UNPACKING, NETDATA CODE IS F A S T E R %0.2Lf %%\n", (long double)(their * 100.0 / mine - 100.0));
172 // ------------------------------------------------------------------------
176 static int check_storage_number_exists() {
177 uint32_t flags = SN_EXISTS;
180 for(flags = 0; flags < 7 ; flags++) {
181 if(get_storage_number_flags(flags << 24) != flags << 24) {
182 fprintf(stderr, "Flag 0x%08x is not checked correctly. It became 0x%08x\n", flags << 24, get_storage_number_flags(flags << 24));
188 calculated_number n = 0.0;
190 storage_number s = pack_storage_number(n, flags);
191 calculated_number d = unpack_storage_number(s);
192 if(get_storage_number_flags(s) != flags) {
193 fprintf(stderr, "Wrong flags. Given %08x, Got %08x!\n", flags, get_storage_number_flags(s));
197 fprintf(stderr, "Wrong number returned. Expected " CALCULATED_NUMBER_FORMAT ", returned " CALCULATED_NUMBER_FORMAT "!\n", n, d);
204 int unit_test_storage()
206 if(check_storage_number_exists()) return 0;
208 calculated_number c, a = 0;
211 for(g = -1; g <= 1 ; g++) {
216 for(j = 0; j < 9 ;j++) {
219 for(i = 0; i < 21 ;i++, c *= 10) {
220 if(c > 0 && c < STORAGE_NUMBER_POSITIVE_MIN) continue;
221 if(c < 0 && c > STORAGE_NUMBER_NEGATIVE_MAX) continue;
223 if(check_storage_number(c, 1)) return 1;
228 benchmark_storage_number(1000000, 2);
233 // --------------------------------------------------------------------------------------------------------------------
236 unsigned long long microseconds;
237 collected_number value;
242 char description[1024];
245 unsigned long long multiplier;
246 unsigned long long divisor;
249 unsigned long feed_entries;
250 unsigned long result_entries;
251 struct feed_values *feed;
252 calculated_number *results;
254 collected_number *feed2;
255 calculated_number *results2;
258 // --------------------------------------------------------------------------------------------------------------------
260 // test absolute values stored
262 struct feed_values test1_feed[] = {
275 calculated_number test1_results[] = {
276 20, 30, 40, 50, 60, 70, 80, 90, 100
279 struct test test1 = {
281 "test absolute values stored at exactly second boundaries",
285 RRDDIM_ABSOLUTE, // algorithm
289 test1_results, // results
294 // --------------------------------------------------------------------------------------------------------------------
296 // test absolute values stored in the middle of second boundaries
298 struct feed_values test2_feed[] = {
311 calculated_number test2_results[] = {
312 20, 30, 40, 50, 60, 70, 80, 90, 100
315 struct test test2 = {
317 "test absolute values stored in the middle of second boundaries",
321 RRDDIM_ABSOLUTE, // algorithm
325 test2_results, // results
330 // --------------------------------------------------------------------------------------------------------------------
333 struct feed_values test3_feed[] = {
346 calculated_number test3_results[] = {
347 10, 10, 10, 10, 10, 10, 10, 10, 10
350 struct test test3 = {
352 "test incremental values stored at exactly second boundaries",
356 RRDDIM_INCREMENTAL, // algorithm
360 test3_results, // results
365 // --------------------------------------------------------------------------------------------------------------------
368 struct feed_values test4_feed[] = {
381 calculated_number test4_results[] = {
382 5, 10, 10, 10, 10, 10, 10, 10, 10
385 struct test test4 = {
387 "test incremental values stored in the middle of second boundaries",
391 RRDDIM_INCREMENTAL, // algorithm
395 test4_results, // results
400 // --------------------------------------------------------------------------------------------------------------------
403 struct feed_values test5_feed[] = {
416 calculated_number test5_results[] = {
417 500, 500, 0, 500, 500, 0, 0, 0, 0
420 struct test test5 = {
422 "test incremental values ups and downs",
426 RRDDIM_INCREMENTAL, // algorithm
430 test5_results, // results
435 // --------------------------------------------------------------------------------------------------------------------
438 struct feed_values test6_feed[] = {
457 calculated_number test6_results[] = {
458 3000, 4000, 4000, 4000
461 struct test test6 = {
463 "test incremental values updated within the same second",
467 RRDDIM_INCREMENTAL, // algorithm
471 test6_results, // results
476 // --------------------------------------------------------------------------------------------------------------------
479 struct feed_values test7_feed[] = {
492 calculated_number test7_results[] = {
493 250, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500
496 struct test test7 = {
498 "test incremental values updated in long durations",
502 RRDDIM_INCREMENTAL, // algorithm
504 18, // result entries
506 test7_results, // results
511 // --------------------------------------------------------------------------------------------------------------------
514 struct feed_values test8_feed[] = {
523 calculated_number test8_results[] = {
524 1250, 2000, 2250, 3000, 3250, 4000, 4250, 5000, 5250, 6000
527 struct test test8 = {
529 "test absolute values updated in long durations",
533 RRDDIM_ABSOLUTE, // algorithm
535 10, // result entries
537 test8_results, // results
542 // --------------------------------------------------------------------------------------------------------------------
545 struct feed_values test9_feed[] = {
564 calculated_number test9_results[] = {
565 4000, 8000, 12000, 16000
568 struct test test9 = {
570 "test absolute values updated within the same second",
574 RRDDIM_ABSOLUTE, // algorithm
578 test9_results, // results
583 // --------------------------------------------------------------------------------------------------------------------
586 struct feed_values test10_feed[] = {
588 { 600000, 1000 + 600 },
589 { 200000, 1600 + 200 },
590 { 1000000, 1800 + 1000 },
591 { 200000, 2800 + 200 },
592 { 2000000, 3000 + 2000 },
593 { 600000, 5000 + 600 },
594 { 400000, 5600 + 400 },
595 { 900000, 6000 + 900 },
596 { 1000000, 6900 + 1000 },
599 calculated_number test10_results[] = {
600 500, 1000, 1000, 1000, 1000, 1000, 1000
603 struct test test10 = {
605 "test incremental values updated in short and long durations",
609 RRDDIM_INCREMENTAL, // algorithm
613 test10_results, // results
618 // --------------------------------------------------------------------------------------------------------------------
621 struct feed_values test11_feed[] = {
634 collected_number test11_feed2[] = {
635 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
638 calculated_number test11_results[] = {
639 50, 50, 50, 50, 50, 50, 50, 50, 50
642 calculated_number test11_results2[] = {
643 50, 50, 50, 50, 50, 50, 50, 50, 50
646 struct test test11 = {
648 "test percentage-of-incremental-row with equal values",
652 RRDDIM_PCENT_OVER_DIFF_TOTAL, // algorithm
656 test11_results, // results
657 test11_feed2, // feed2
658 test11_results2 // results2
661 // --------------------------------------------------------------------------------------------------------------------
664 struct feed_values test12_feed[] = {
677 collected_number test12_feed2[] = {
678 10*3, 20*3, 30*3, 40*3, 50*3, 60*3, 70*3, 80*3, 90*3, 100*3
681 calculated_number test12_results[] = {
682 25, 25, 25, 25, 25, 25, 25, 25, 25
685 calculated_number test12_results2[] = {
686 75, 75, 75, 75, 75, 75, 75, 75, 75
689 struct test test12 = {
691 "test percentage-of-incremental-row with equal values",
695 RRDDIM_PCENT_OVER_DIFF_TOTAL, // algorithm
699 test12_results, // results
700 test12_feed2, // feed2
701 test12_results2 // results2
704 // --------------------------------------------------------------------------------------------------------------------
707 struct feed_values test13_feed[] = {
709 { 600000, 1000 + 600 },
710 { 200000, 1600 + 200 },
711 { 1000000, 1800 + 1000 },
712 { 200000, 2800 + 200 },
713 { 2000000, 3000 + 2000 },
714 { 600000, 5000 + 600 },
715 { 400000, 5600 + 400 },
716 { 900000, 6000 + 900 },
717 { 1000000, 6900 + 1000 },
720 calculated_number test13_results[] = {
721 83.3333300, 100, 100, 100, 100, 100, 100
724 struct test test13 = {
726 "test incremental values updated in short and long durations",
730 RRDDIM_PCENT_OVER_DIFF_TOTAL, // algorithm
734 test13_results, // results
739 // --------------------------------------------------------------------------------------------------------------------
741 int run_test(struct test *test)
743 fprintf(stderr, "\nRunning test '%s':\n%s\n", test->name, test->description);
745 rrd_memory_mode = RRD_MEMORY_MODE_RAM;
746 rrd_update_every = test->update_every;
749 snprintfz(name, 100, "unittest-%s", test->name);
752 RRDSET *st = rrdset_create("netdata", name, name, "netdata", NULL, "Unit Testing", "a value", 1, 1, RRDSET_TYPE_LINE);
753 RRDDIM *rd = rrddim_add(st, "dim1", NULL, test->multiplier, test->divisor, test->algorithm);
757 rd2 = rrddim_add(st, "dim2", NULL, test->multiplier, test->divisor, test->algorithm);
761 // feed it with the test data
763 for(c = 0; c < test->feed_entries; c++) {
764 if(debug_flags) fprintf(stderr, "\n\n");
767 fprintf(stderr, " > %s: feeding position %lu, after %llu microseconds\n", test->name, c+1, test->feed[c].microseconds);
768 rrdset_next_usec(st, test->feed[c].microseconds);
771 fprintf(stderr, " > %s: feeding position %lu\n", test->name, c+1);
774 fprintf(stderr, " >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd->name, test->feed[c].value);
775 rrddim_set(st, "dim1", test->feed[c].value);
778 fprintf(stderr, " >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd2->name, test->feed2[c]);
779 rrddim_set(st, "dim2", test->feed2[c]);
784 // align the first entry to second boundary
786 fprintf(stderr, " > %s: fixing first collection time to be %llu microseconds to second boundary\n", test->name, test->feed[c].microseconds);
787 rd->last_collected_time.tv_usec = st->last_collected_time.tv_usec = st->last_updated.tv_usec = test->feed[c].microseconds;
794 if(st->counter != test->result_entries) {
795 fprintf(stderr, " %s stored %lu entries, but we were expecting %lu, ### E R R O R ###\n", test->name, st->counter, test->result_entries);
799 unsigned long max = (st->counter < test->result_entries)?st->counter:test->result_entries;
800 for(c = 0 ; c < max ; c++) {
801 calculated_number v = unpack_storage_number(rd->values[c]);
802 calculated_number n = test->results[c];
803 int same = (roundl(v * 10000000.0) == roundl(n * 10000000.0))?1:0;
804 fprintf(stderr, " %s/%s: checking position %lu, expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n", test->name, rd->name, c+1, n, v, (same)?"OK":"### E R R O R ###");
808 v = unpack_storage_number(rd2->values[c]);
809 n = test->results2[c];
810 same = (roundl(v * 10000000.0) == roundl(n * 10000000.0))?1:0;
811 fprintf(stderr, " %s/%s: checking position %lu, expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n", test->name, rd2->name, c+1, n, v, (same)?"OK":"### E R R O R ###");
819 int run_all_mockup_tests(void)
848 if(run_test(&test10))
851 if(run_test(&test11))
854 if(run_test(&test12))
857 if(run_test(&test13))
863 int unit_test(long delay, long shift)
865 static int repeat = 0;
869 snprintfz(name, 100, "unittest-%d-%ld-%ld", repeat, delay, shift);
871 //debug_flags = 0xffffffff;
872 rrd_memory_mode = RRD_MEMORY_MODE_RAM;
873 rrd_update_every = 1;
880 RRDSET *st = rrdset_create("netdata", name, name, "netdata", NULL, "Unit Testing", "a value", 1, 1, RRDSET_TYPE_LINE);
883 RRDDIM *rdabs = NULL;
884 RRDDIM *rdinc = NULL;
885 RRDDIM *rdabst = NULL;
886 RRDDIM *rdabsi = NULL;
888 if(do_abs) rdabs = rrddim_add(st, "absolute", "absolute", 1, 1, RRDDIM_ABSOLUTE);
889 if(do_inc) rdinc = rrddim_add(st, "incremental", "incremental", 1, 1, RRDDIM_INCREMENTAL);
890 if(do_abst) rdabst = rrddim_add(st, "percentage-of-absolute-row", "percentage-of-absolute-row", 1, 1, RRDDIM_PCENT_OVER_ROW_TOTAL);
891 if(do_absi) rdabsi = rrddim_add(st, "percentage-of-incremental-row", "percentage-of-incremental-row", 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
893 long increment = 1000;
894 collected_number i = 0;
896 unsigned long c, dimensions = 0;
898 for(rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
900 for(c = 0; c < 20 ;c++) {
903 fprintf(stderr, "\n\nLOOP = %lu, DELAY = %ld, VALUE = " COLLECTED_NUMBER_FORMAT "\n", c, delay, i);
905 rrdset_next_usec(st, delay);
907 if(do_abs) rrddim_set(st, "absolute", i);
908 if(do_inc) rrddim_set(st, "incremental", i);
909 if(do_abst) rrddim_set(st, "percentage-of-absolute-row", i);
910 if(do_absi) rrddim_set(st, "percentage-of-incremental-row", i);
913 gettimeofday(&st->last_collected_time, NULL);
914 st->last_collected_time.tv_usec = shift;
917 // prevent it from deleting the dimensions
918 for(rd = st->dimensions ; rd ; rd = rd->next)
919 rd->last_collected_time.tv_sec = st->last_collected_time.tv_sec;
924 unsigned long oincrement = increment;
925 increment = increment * st->update_every * 1000000 / delay;
926 fprintf(stderr, "\n\nORIGINAL INCREMENT: %lu, INCREMENT %ld, DELAY %ld, SHIFT %ld\n", oincrement * 10, increment * 10, delay, shift);
930 calculated_number cn, v;
931 for(c = 0 ; c < st->counter ; c++) {
932 fprintf(stderr, "\nPOSITION: c = %lu, EXPECTED VALUE %lu\n", c, (oincrement + c * increment + increment * (1000000 - shift) / 1000000 )* 10);
934 for(rd = st->dimensions ; rd ; rd = rd->next) {
936 cn = unpack_storage_number(sn);
937 fprintf(stderr, "\t %s " CALCULATED_NUMBER_FORMAT " (PACKED AS " STORAGE_NUMBER_FORMAT ") -> ", rd->id, cn, sn);
941 // + (increment * (1000000 - shift) / 1000000)
942 + (c + 1) * increment
945 else if(rd == rdinc) v = (c?(increment):(increment * (1000000 - shift) / 1000000));
946 else if(rd == rdabst) v = oincrement / dimensions / 10;
947 else if(rd == rdabsi) v = oincrement / dimensions / 10;
950 if(v == cn) fprintf(stderr, "passed.\n");
952 fprintf(stderr, "ERROR! (expected " CALCULATED_NUMBER_FORMAT ")\n", v);
959 fprintf(stderr, "\n\nUNIT TEST(%ld, %ld) FAILED\n\n", delay, shift);