7 #include <sys/resource.h>
11 #include "storage_number.h"
14 #include "web_buffer.h"
16 int check_storage_number(calculated_number n, int debug) {
18 uint32_t flags = SN_EXISTS;
20 storage_number s = pack_storage_number(n, flags);
21 calculated_number d = unpack_storage_number(s);
23 if(!does_storage_number_exist(s)) {
24 fprintf(stderr, "Exists flags missing for number " CALCULATED_NUMBER_FORMAT "!\n", n);
28 calculated_number ddiff = d - n;
29 calculated_number dcdiff = ddiff * 100.0 / n;
31 if(dcdiff < 0) dcdiff = -dcdiff;
33 size_t len = print_calculated_number(buffer, d);
34 calculated_number p = strtold(buffer, NULL);
35 calculated_number pdiff = n - p;
36 calculated_number pcdiff = pdiff * 100.0 / n;
37 if(pcdiff < 0) pcdiff = -pcdiff;
41 CALCULATED_NUMBER_FORMAT " original\n"
42 CALCULATED_NUMBER_FORMAT " packed and unpacked, (stored as 0x%08X, diff " CALCULATED_NUMBER_FORMAT ", " CALCULATED_NUMBER_FORMAT "%%)\n"
43 "%s printed after unpacked (%zu bytes)\n"
44 CALCULATED_NUMBER_FORMAT " re-parsed from printed (diff " CALCULATED_NUMBER_FORMAT ", " CALCULATED_NUMBER_FORMAT "%%)\n\n",
50 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));
51 if(dcdiff > ACCURACY_LOSS) fprintf(stderr, "WARNING: packing number " CALCULATED_NUMBER_FORMAT " has accuracy loss %0.7Lf %%\n", n, dcdiff);
52 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);
55 if(len != strlen(buffer)) return 1;
56 if(dcdiff > ACCURACY_LOSS) return 3;
57 if(pcdiff > ACCURACY_LOSS) return 4;
61 void benchmark_storage_number(int loop, int multiplier) {
63 calculated_number n, d;
65 unsigned long long user, system, total, mine, their;
69 struct rusage now, last;
71 fprintf(stderr, "\n\nBenchmarking %d numbers, please wait...\n\n", loop);
73 // ------------------------------------------------------------------------
75 fprintf(stderr, "SYSTEM LONG DOUBLE SIZE: %zu bytes\n", sizeof(calculated_number));
76 fprintf(stderr, "NETDATA FLOATING POINT SIZE: %zu bytes\n", sizeof(storage_number));
78 mine = (calculated_number)sizeof(storage_number) * (calculated_number)loop;
79 their = (calculated_number)sizeof(calculated_number) * (calculated_number)loop;
82 fprintf(stderr, "\nNETDATA NEEDS %0.2Lf TIMES MORE MEMORY. Sorry!\n", (long double)(mine / their));
85 fprintf(stderr, "\nNETDATA INTERNAL FLOATING POINT ARITHMETICS NEEDS %0.2Lf TIMES LESS MEMORY.\n", (long double)(their / mine));
88 fprintf(stderr, "\nNETDATA FLOATING POINT\n");
89 fprintf(stderr, "MIN POSITIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", (calculated_number)STORAGE_NUMBER_POSITIVE_MIN);
90 fprintf(stderr, "MAX POSITIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", (calculated_number)STORAGE_NUMBER_POSITIVE_MAX);
91 fprintf(stderr, "MIN NEGATIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", (calculated_number)STORAGE_NUMBER_NEGATIVE_MIN);
92 fprintf(stderr, "MAX NEGATIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", (calculated_number)STORAGE_NUMBER_NEGATIVE_MAX);
93 fprintf(stderr, "Maximum accuracy loss: " CALCULATED_NUMBER_FORMAT "%%\n\n\n", (calculated_number)ACCURACY_LOSS);
95 // ------------------------------------------------------------------------
97 fprintf(stderr, "INTERNAL LONG DOUBLE PRINTING: ");
98 getrusage(RUSAGE_SELF, &last);
101 for(j = 1; j < 11 ;j++) {
102 n = STORAGE_NUMBER_POSITIVE_MIN * j;
104 for(i = 0; i < loop ;i++) {
106 if(n > STORAGE_NUMBER_POSITIVE_MAX) n = STORAGE_NUMBER_POSITIVE_MIN;
108 print_calculated_number(buffer, n);
112 getrusage(RUSAGE_SELF, &now);
113 user = now.ru_utime.tv_sec * 1000000ULL + now.ru_utime.tv_usec - last.ru_utime.tv_sec * 1000000ULL + last.ru_utime.tv_usec;
114 system = now.ru_stime.tv_sec * 1000000ULL + now.ru_stime.tv_usec - last.ru_stime.tv_sec * 1000000ULL + last.ru_stime.tv_usec;
115 total = user + system;
118 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));
120 // ------------------------------------------------------------------------
122 fprintf(stderr, "SYSTEM LONG DOUBLE PRINTING: ");
123 getrusage(RUSAGE_SELF, &last);
126 for(j = 1; j < 11 ;j++) {
127 n = STORAGE_NUMBER_POSITIVE_MIN * j;
129 for(i = 0; i < loop ;i++) {
131 if(n > STORAGE_NUMBER_POSITIVE_MAX) n = STORAGE_NUMBER_POSITIVE_MIN;
132 snprintfz(buffer, 100, CALCULATED_NUMBER_FORMAT, n);
136 getrusage(RUSAGE_SELF, &now);
137 user = now.ru_utime.tv_sec * 1000000ULL + now.ru_utime.tv_usec - last.ru_utime.tv_sec * 1000000ULL + last.ru_utime.tv_usec;
138 system = now.ru_stime.tv_sec * 1000000ULL + now.ru_stime.tv_usec - last.ru_stime.tv_sec * 1000000ULL + last.ru_stime.tv_usec;
139 total = user + system;
142 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));
145 fprintf(stderr, "NETDATA CODE IS SLOWER %0.2Lf %%\n", (long double)(mine * 100.0 / their - 100.0));
148 fprintf(stderr, "NETDATA CODE IS F A S T E R %0.2Lf %%\n", (long double)(their * 100.0 / mine - 100.0));
151 // ------------------------------------------------------------------------
153 fprintf(stderr, "\nINTERNAL LONG DOUBLE PRINTING WITH PACK / UNPACK: ");
154 getrusage(RUSAGE_SELF, &last);
157 for(j = 1; j < 11 ;j++) {
158 n = STORAGE_NUMBER_POSITIVE_MIN * j;
160 for(i = 0; i < loop ;i++) {
162 if(n > STORAGE_NUMBER_POSITIVE_MAX) n = STORAGE_NUMBER_POSITIVE_MIN;
164 s = pack_storage_number(n, 1);
165 d = unpack_storage_number(s);
166 print_calculated_number(buffer, d);
170 getrusage(RUSAGE_SELF, &now);
171 user = now.ru_utime.tv_sec * 1000000ULL + now.ru_utime.tv_usec - last.ru_utime.tv_sec * 1000000ULL + last.ru_utime.tv_usec;
172 system = now.ru_stime.tv_sec * 1000000ULL + now.ru_stime.tv_usec - last.ru_stime.tv_sec * 1000000ULL + last.ru_stime.tv_usec;
173 total = user + system;
176 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));
179 fprintf(stderr, "WITH PACKING UNPACKING NETDATA CODE IS SLOWER %0.2Lf %%\n", (long double)(mine * 100.0 / their - 100.0));
182 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));
185 // ------------------------------------------------------------------------
189 static int check_storage_number_exists() {
190 uint32_t flags = SN_EXISTS;
193 for(flags = 0; flags < 7 ; flags++) {
194 if(get_storage_number_flags(flags << 24) != flags << 24) {
195 fprintf(stderr, "Flag 0x%08x is not checked correctly. It became 0x%08x\n", flags << 24, get_storage_number_flags(flags << 24));
201 calculated_number n = 0.0;
203 storage_number s = pack_storage_number(n, flags);
204 calculated_number d = unpack_storage_number(s);
205 if(get_storage_number_flags(s) != flags) {
206 fprintf(stderr, "Wrong flags. Given %08x, Got %08x!\n", flags, get_storage_number_flags(s));
210 fprintf(stderr, "Wrong number returned. Expected " CALCULATED_NUMBER_FORMAT ", returned " CALCULATED_NUMBER_FORMAT "!\n", n, d);
217 int unit_test_storage()
219 if(check_storage_number_exists()) return 0;
221 calculated_number c, a = 0;
224 for(g = -1; g <= 1 ; g++) {
229 for(j = 0; j < 9 ;j++) {
232 for(i = 0; i < 21 ;i++, c *= 10) {
233 if(c > 0 && c < STORAGE_NUMBER_POSITIVE_MIN) continue;
234 if(c < 0 && c > STORAGE_NUMBER_NEGATIVE_MAX) continue;
236 if(check_storage_number(c, 1)) return 1;
241 benchmark_storage_number(1000000, 2);
246 // --------------------------------------------------------------------------------------------------------------------
249 unsigned long long microseconds;
250 collected_number value;
255 char description[1024];
258 unsigned long long multiplier;
259 unsigned long long divisor;
262 unsigned long feed_entries;
263 unsigned long result_entries;
264 struct feed_values *feed;
265 calculated_number *results;
267 collected_number *feed2;
268 calculated_number *results2;
271 // --------------------------------------------------------------------------------------------------------------------
273 // test absolute values stored
275 struct feed_values test1_feed[] = {
288 calculated_number test1_results[] = {
289 20, 30, 40, 50, 60, 70, 80, 90, 100
292 struct test test1 = {
294 "test absolute values stored at exactly second boundaries",
298 RRDDIM_ABSOLUTE, // algorithm
302 test1_results, // results
307 // --------------------------------------------------------------------------------------------------------------------
309 // test absolute values stored in the middle of second boundaries
311 struct feed_values test2_feed[] = {
324 calculated_number test2_results[] = {
325 20, 30, 40, 50, 60, 70, 80, 90, 100
328 struct test test2 = {
330 "test absolute values stored in the middle of second boundaries",
334 RRDDIM_ABSOLUTE, // algorithm
338 test2_results, // results
343 // --------------------------------------------------------------------------------------------------------------------
346 struct feed_values test3_feed[] = {
359 calculated_number test3_results[] = {
360 10, 10, 10, 10, 10, 10, 10, 10, 10
363 struct test test3 = {
365 "test incremental values stored at exactly second boundaries",
369 RRDDIM_INCREMENTAL, // algorithm
373 test3_results, // results
378 // --------------------------------------------------------------------------------------------------------------------
381 struct feed_values test4_feed[] = {
394 calculated_number test4_results[] = {
395 5, 10, 10, 10, 10, 10, 10, 10, 10
398 struct test test4 = {
400 "test incremental values stored in the middle of second boundaries",
404 RRDDIM_INCREMENTAL, // algorithm
408 test4_results, // results
413 // --------------------------------------------------------------------------------------------------------------------
416 struct feed_values test5_feed[] = {
429 calculated_number test5_results[] = {
430 500, 500, 0, 500, 500, 0, 0, 0, 0
433 struct test test5 = {
435 "test incremental values ups and downs",
439 RRDDIM_INCREMENTAL, // algorithm
443 test5_results, // results
448 // --------------------------------------------------------------------------------------------------------------------
451 struct feed_values test6_feed[] = {
470 calculated_number test6_results[] = {
471 3000, 4000, 4000, 4000
474 struct test test6 = {
476 "test incremental values updated within the same second",
480 RRDDIM_INCREMENTAL, // algorithm
484 test6_results, // results
489 // --------------------------------------------------------------------------------------------------------------------
492 struct feed_values test7_feed[] = {
505 calculated_number test7_results[] = {
506 250, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500
509 struct test test7 = {
511 "test incremental values updated in long durations",
515 RRDDIM_INCREMENTAL, // algorithm
517 18, // result entries
519 test7_results, // results
524 // --------------------------------------------------------------------------------------------------------------------
527 struct feed_values test8_feed[] = {
536 calculated_number test8_results[] = {
537 1250, 2000, 2250, 3000, 3250, 4000, 4250, 5000, 5250, 6000
540 struct test test8 = {
542 "test absolute values updated in long durations",
546 RRDDIM_ABSOLUTE, // algorithm
548 10, // result entries
550 test8_results, // results
555 // --------------------------------------------------------------------------------------------------------------------
558 struct feed_values test9_feed[] = {
577 calculated_number test9_results[] = {
578 4000, 8000, 12000, 16000
581 struct test test9 = {
583 "test absolute values updated within the same second",
587 RRDDIM_ABSOLUTE, // algorithm
591 test9_results, // results
596 // --------------------------------------------------------------------------------------------------------------------
599 struct feed_values test10_feed[] = {
601 { 600000, 1000 + 600 },
602 { 200000, 1600 + 200 },
603 { 1000000, 1800 + 1000 },
604 { 200000, 2800 + 200 },
605 { 2000000, 3000 + 2000 },
606 { 600000, 5000 + 600 },
607 { 400000, 5600 + 400 },
608 { 900000, 6000 + 900 },
609 { 1000000, 6900 + 1000 },
612 calculated_number test10_results[] = {
613 500, 1000, 1000, 1000, 1000, 1000, 1000
616 struct test test10 = {
618 "test incremental values updated in short and long durations",
622 RRDDIM_INCREMENTAL, // algorithm
626 test10_results, // results
631 // --------------------------------------------------------------------------------------------------------------------
634 struct feed_values test11_feed[] = {
647 collected_number test11_feed2[] = {
648 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
651 calculated_number test11_results[] = {
652 50, 50, 50, 50, 50, 50, 50, 50, 50
655 calculated_number test11_results2[] = {
656 50, 50, 50, 50, 50, 50, 50, 50, 50
659 struct test test11 = {
661 "test percentage-of-incremental-row with equal values",
665 RRDDIM_PCENT_OVER_DIFF_TOTAL, // algorithm
669 test11_results, // results
670 test11_feed2, // feed2
671 test11_results2 // results2
674 // --------------------------------------------------------------------------------------------------------------------
677 struct feed_values test12_feed[] = {
690 collected_number test12_feed2[] = {
691 10*3, 20*3, 30*3, 40*3, 50*3, 60*3, 70*3, 80*3, 90*3, 100*3
694 calculated_number test12_results[] = {
695 25, 25, 25, 25, 25, 25, 25, 25, 25
698 calculated_number test12_results2[] = {
699 75, 75, 75, 75, 75, 75, 75, 75, 75
702 struct test test12 = {
704 "test percentage-of-incremental-row with equal values",
708 RRDDIM_PCENT_OVER_DIFF_TOTAL, // algorithm
712 test12_results, // results
713 test12_feed2, // feed2
714 test12_results2 // results2
717 // --------------------------------------------------------------------------------------------------------------------
720 struct feed_values test13_feed[] = {
722 { 600000, 1000 + 600 },
723 { 200000, 1600 + 200 },
724 { 1000000, 1800 + 1000 },
725 { 200000, 2800 + 200 },
726 { 2000000, 3000 + 2000 },
727 { 600000, 5000 + 600 },
728 { 400000, 5600 + 400 },
729 { 900000, 6000 + 900 },
730 { 1000000, 6900 + 1000 },
733 calculated_number test13_results[] = {
734 83.3333300, 100, 100, 100, 100, 100, 100
737 struct test test13 = {
739 "test incremental values updated in short and long durations",
743 RRDDIM_PCENT_OVER_DIFF_TOTAL, // algorithm
747 test13_results, // results
752 // --------------------------------------------------------------------------------------------------------------------
754 int run_test(struct test *test)
756 fprintf(stderr, "\nRunning test '%s':\n%s\n", test->name, test->description);
758 rrd_memory_mode = RRD_MEMORY_MODE_RAM;
759 rrd_update_every = test->update_every;
762 snprintfz(name, 100, "unittest-%s", test->name);
765 RRDSET *st = rrdset_create("netdata", name, name, "netdata", NULL, "Unit Testing", "a value", 1, 1, RRDSET_TYPE_LINE);
766 RRDDIM *rd = rrddim_add(st, "dim1", NULL, test->multiplier, test->divisor, test->algorithm);
770 rd2 = rrddim_add(st, "dim2", NULL, test->multiplier, test->divisor, test->algorithm);
774 // feed it with the test data
776 for(c = 0; c < test->feed_entries; c++) {
777 if(debug_flags) fprintf(stderr, "\n\n");
780 fprintf(stderr, " > %s: feeding position %lu, after %llu microseconds\n", test->name, c+1, test->feed[c].microseconds);
781 rrdset_next_usec(st, test->feed[c].microseconds);
784 fprintf(stderr, " > %s: feeding position %lu\n", test->name, c+1);
787 fprintf(stderr, " >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd->name, test->feed[c].value);
788 rrddim_set(st, "dim1", test->feed[c].value);
791 fprintf(stderr, " >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd2->name, test->feed2[c]);
792 rrddim_set(st, "dim2", test->feed2[c]);
797 // align the first entry to second boundary
799 fprintf(stderr, " > %s: fixing first collection time to be %llu microseconds to second boundary\n", test->name, test->feed[c].microseconds);
800 rd->last_collected_time.tv_usec = st->last_collected_time.tv_usec = st->last_updated.tv_usec = test->feed[c].microseconds;
807 if(st->counter != test->result_entries) {
808 fprintf(stderr, " %s stored %lu entries, but we were expecting %lu, ### E R R O R ###\n", test->name, st->counter, test->result_entries);
812 unsigned long max = (st->counter < test->result_entries)?st->counter:test->result_entries;
813 for(c = 0 ; c < max ; c++) {
814 calculated_number v = unpack_storage_number(rd->values[c]);
815 calculated_number n = test->results[c];
816 int same = (roundl(v * 10000000.0) == roundl(n * 10000000.0))?1:0;
817 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 ###");
821 v = unpack_storage_number(rd2->values[c]);
822 n = test->results2[c];
823 same = (roundl(v * 10000000.0) == roundl(n * 10000000.0))?1:0;
824 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 ###");
832 int run_all_mockup_tests(void)
861 if(run_test(&test10))
864 if(run_test(&test11))
867 if(run_test(&test12))
870 if(run_test(&test13))
876 int unit_test(long delay, long shift)
878 static int repeat = 0;
882 snprintfz(name, 100, "unittest-%d-%ld-%ld", repeat, delay, shift);
884 //debug_flags = 0xffffffff;
885 rrd_memory_mode = RRD_MEMORY_MODE_RAM;
886 rrd_update_every = 1;
893 RRDSET *st = rrdset_create("netdata", name, name, "netdata", NULL, "Unit Testing", "a value", 1, 1, RRDSET_TYPE_LINE);
896 RRDDIM *rdabs = NULL;
897 RRDDIM *rdinc = NULL;
898 RRDDIM *rdabst = NULL;
899 RRDDIM *rdabsi = NULL;
901 if(do_abs) rdabs = rrddim_add(st, "absolute", "absolute", 1, 1, RRDDIM_ABSOLUTE);
902 if(do_inc) rdinc = rrddim_add(st, "incremental", "incremental", 1, 1, RRDDIM_INCREMENTAL);
903 if(do_abst) rdabst = rrddim_add(st, "percentage-of-absolute-row", "percentage-of-absolute-row", 1, 1, RRDDIM_PCENT_OVER_ROW_TOTAL);
904 if(do_absi) rdabsi = rrddim_add(st, "percentage-of-incremental-row", "percentage-of-incremental-row", 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
906 long increment = 1000;
907 collected_number i = 0;
909 unsigned long c, dimensions = 0;
911 for(rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
913 for(c = 0; c < 20 ;c++) {
916 fprintf(stderr, "\n\nLOOP = %lu, DELAY = %ld, VALUE = " COLLECTED_NUMBER_FORMAT "\n", c, delay, i);
918 rrdset_next_usec(st, delay);
920 if(do_abs) rrddim_set(st, "absolute", i);
921 if(do_inc) rrddim_set(st, "incremental", i);
922 if(do_abst) rrddim_set(st, "percentage-of-absolute-row", i);
923 if(do_absi) rrddim_set(st, "percentage-of-incremental-row", i);
926 gettimeofday(&st->last_collected_time, NULL);
927 st->last_collected_time.tv_usec = shift;
930 // prevent it from deleting the dimensions
931 for(rd = st->dimensions ; rd ; rd = rd->next)
932 rd->last_collected_time.tv_sec = st->last_collected_time.tv_sec;
937 unsigned long oincrement = increment;
938 increment = increment * st->update_every * 1000000 / delay;
939 fprintf(stderr, "\n\nORIGINAL INCREMENT: %lu, INCREMENT %ld, DELAY %ld, SHIFT %ld\n", oincrement * 10, increment * 10, delay, shift);
943 calculated_number cn, v;
944 for(c = 0 ; c < st->counter ; c++) {
945 fprintf(stderr, "\nPOSITION: c = %lu, EXPECTED VALUE %lu\n", c, (oincrement + c * increment + increment * (1000000 - shift) / 1000000 )* 10);
947 for(rd = st->dimensions ; rd ; rd = rd->next) {
949 cn = unpack_storage_number(sn);
950 fprintf(stderr, "\t %s " CALCULATED_NUMBER_FORMAT " (PACKED AS " STORAGE_NUMBER_FORMAT ") -> ", rd->id, cn, sn);
954 // + (increment * (1000000 - shift) / 1000000)
955 + (c + 1) * increment
958 else if(rd == rdinc) v = (c?(increment):(increment * (1000000 - shift) / 1000000));
959 else if(rd == rdabst) v = oincrement / dimensions / 10;
960 else if(rd == rdabsi) v = oincrement / dimensions / 10;
963 if(v == cn) fprintf(stderr, "passed.\n");
965 fprintf(stderr, "ERROR! (expected " CALCULATED_NUMBER_FORMAT ")\n", v);
972 fprintf(stderr, "\n\nUNIT TEST(%ld, %ld) FAILED\n\n", delay, shift);