7 #include <sys/resource.h>
10 #include "storage_number.h"
13 #include "web_buffer.h"
15 int check_storage_number(calculated_number n, int debug) {
17 uint32_t flags = SN_EXISTS;
19 storage_number s = pack_storage_number(n, flags);
20 calculated_number d = unpack_storage_number(s);
22 if(!does_storage_number_exist(s)) {
23 fprintf(stderr, "Exists flags missing for number " CALCULATED_NUMBER_FORMAT "!\n", n);
27 calculated_number ddiff = d - n;
28 calculated_number dcdiff = ddiff * 100.0 / n;
30 if(dcdiff < 0) dcdiff = -dcdiff;
32 size_t len = print_calculated_number(buffer, d);
33 calculated_number p = strtold(buffer, NULL);
34 calculated_number pdiff = n - p;
35 calculated_number pcdiff = pdiff * 100.0 / n;
36 if(pcdiff < 0) pcdiff = -pcdiff;
40 CALCULATED_NUMBER_FORMAT " original\n"
41 CALCULATED_NUMBER_FORMAT " packed and unpacked, (stored as 0x%08X, diff " CALCULATED_NUMBER_FORMAT ", " CALCULATED_NUMBER_FORMAT "%%)\n"
42 "%s printed after unpacked (%zu bytes)\n"
43 CALCULATED_NUMBER_FORMAT " re-parsed from printed (diff " CALCULATED_NUMBER_FORMAT ", " CALCULATED_NUMBER_FORMAT "%%)\n\n",
49 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));
50 if(dcdiff > ACCURACY_LOSS) fprintf(stderr, "WARNING: packing number " CALCULATED_NUMBER_FORMAT " has accuracy loss %0.7Lf %%\n", n, dcdiff);
51 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);
54 if(len != strlen(buffer)) return 1;
55 if(dcdiff > ACCURACY_LOSS) return 3;
56 if(pcdiff > ACCURACY_LOSS) return 4;
60 void benchmark_storage_number(int loop, int multiplier) {
62 calculated_number n, d;
64 unsigned long long user, system, total, mine, their;
68 struct rusage now, last;
70 fprintf(stderr, "\n\nBenchmarking %d numbers, please wait...\n\n", loop);
72 // ------------------------------------------------------------------------
74 fprintf(stderr, "SYSTEM LONG DOUBLE SIZE: %zu bytes\n", sizeof(calculated_number));
75 fprintf(stderr, "NETDATA FLOATING POINT SIZE: %zu bytes\n", sizeof(storage_number));
77 mine = (calculated_number)sizeof(storage_number) * (calculated_number)loop;
78 their = (calculated_number)sizeof(calculated_number) * (calculated_number)loop;
81 fprintf(stderr, "\nNETDATA NEEDS %0.2Lf TIMES MORE MEMORY. Sorry!\n", (long double)(mine / their));
84 fprintf(stderr, "\nNETDATA INTERNAL FLOATING POINT ARITHMETICS NEEDS %0.2Lf TIMES LESS MEMORY.\n", (long double)(their / mine));
87 fprintf(stderr, "\nNETDATA FLOATING POINT\n");
88 fprintf(stderr, "MIN POSITIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", (calculated_number)STORAGE_NUMBER_POSITIVE_MIN);
89 fprintf(stderr, "MAX POSITIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", (calculated_number)STORAGE_NUMBER_POSITIVE_MAX);
90 fprintf(stderr, "MIN NEGATIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", (calculated_number)STORAGE_NUMBER_NEGATIVE_MIN);
91 fprintf(stderr, "MAX NEGATIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", (calculated_number)STORAGE_NUMBER_NEGATIVE_MAX);
92 fprintf(stderr, "Maximum accuracy loss: " CALCULATED_NUMBER_FORMAT "%%\n\n\n", (calculated_number)ACCURACY_LOSS);
94 // ------------------------------------------------------------------------
96 fprintf(stderr, "INTERNAL LONG DOUBLE PRINTING: ");
97 getrusage(RUSAGE_SELF, &last);
100 for(j = 1; j < 11 ;j++) {
101 n = STORAGE_NUMBER_POSITIVE_MIN * j;
103 for(i = 0; i < loop ;i++) {
105 if(n > STORAGE_NUMBER_POSITIVE_MAX) n = STORAGE_NUMBER_POSITIVE_MIN;
107 print_calculated_number(buffer, n);
111 getrusage(RUSAGE_SELF, &now);
112 user = now.ru_utime.tv_sec * 1000000ULL + now.ru_utime.tv_usec - last.ru_utime.tv_sec * 1000000ULL + last.ru_utime.tv_usec;
113 system = now.ru_stime.tv_sec * 1000000ULL + now.ru_stime.tv_usec - last.ru_stime.tv_sec * 1000000ULL + last.ru_stime.tv_usec;
114 total = user + system;
117 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));
119 // ------------------------------------------------------------------------
121 fprintf(stderr, "SYSTEM LONG DOUBLE PRINTING: ");
122 getrusage(RUSAGE_SELF, &last);
125 for(j = 1; j < 11 ;j++) {
126 n = STORAGE_NUMBER_POSITIVE_MIN * j;
128 for(i = 0; i < loop ;i++) {
130 if(n > STORAGE_NUMBER_POSITIVE_MAX) n = STORAGE_NUMBER_POSITIVE_MIN;
131 snprintf(buffer, 100, CALCULATED_NUMBER_FORMAT, n);
135 getrusage(RUSAGE_SELF, &now);
136 user = now.ru_utime.tv_sec * 1000000ULL + now.ru_utime.tv_usec - last.ru_utime.tv_sec * 1000000ULL + last.ru_utime.tv_usec;
137 system = now.ru_stime.tv_sec * 1000000ULL + now.ru_stime.tv_usec - last.ru_stime.tv_sec * 1000000ULL + last.ru_stime.tv_usec;
138 total = user + system;
141 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));
144 fprintf(stderr, "NETDATA CODE IS SLOWER %0.2Lf %%\n", (long double)(mine * 100.0 / their - 100.0));
147 fprintf(stderr, "NETDATA CODE IS F A S T E R %0.2Lf %%\n", (long double)(their * 100.0 / mine - 100.0));
150 // ------------------------------------------------------------------------
152 fprintf(stderr, "\nINTERNAL LONG DOUBLE PRINTING WITH PACK / UNPACK: ");
153 getrusage(RUSAGE_SELF, &last);
156 for(j = 1; j < 11 ;j++) {
157 n = STORAGE_NUMBER_POSITIVE_MIN * j;
159 for(i = 0; i < loop ;i++) {
161 if(n > STORAGE_NUMBER_POSITIVE_MAX) n = STORAGE_NUMBER_POSITIVE_MIN;
163 s = pack_storage_number(n, 1);
164 d = unpack_storage_number(s);
165 print_calculated_number(buffer, d);
169 getrusage(RUSAGE_SELF, &now);
170 user = now.ru_utime.tv_sec * 1000000ULL + now.ru_utime.tv_usec - last.ru_utime.tv_sec * 1000000ULL + last.ru_utime.tv_usec;
171 system = now.ru_stime.tv_sec * 1000000ULL + now.ru_stime.tv_usec - last.ru_stime.tv_sec * 1000000ULL + last.ru_stime.tv_usec;
172 total = user + system;
175 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));
178 fprintf(stderr, "WITH PACKING UNPACKING NETDATA CODE IS SLOWER %0.2Lf %%\n", (long double)(mine * 100.0 / their - 100.0));
181 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));
184 // ------------------------------------------------------------------------
188 static int check_storage_number_exists() {
189 uint32_t flags = SN_EXISTS;
192 for(flags = 0; flags < 7 ; flags++) {
193 if(get_storage_number_flags(flags << 24) != flags << 24) {
194 fprintf(stderr, "Flag 0x%08x is not checked correctly. It became 0x%08x\n", flags << 24, get_storage_number_flags(flags << 24));
200 calculated_number n = 0.0;
202 storage_number s = pack_storage_number(n, flags);
203 calculated_number d = unpack_storage_number(s);
204 if(get_storage_number_flags(s) != flags) {
205 fprintf(stderr, "Wrong flags. Given %08x, Got %08x!\n", flags, get_storage_number_flags(s));
209 fprintf(stderr, "Wrong number returned. Expected " CALCULATED_NUMBER_FORMAT ", returned " CALCULATED_NUMBER_FORMAT "!\n", n, d);
216 int unit_test_storage()
218 if(check_storage_number_exists()) return 0;
220 calculated_number c, a = 0;
223 for(g = -1; g <= 1 ; g++) {
228 for(j = 0; j < 9 ;j++) {
231 for(i = 0; i < 21 ;i++, c *= 10) {
232 if(c > 0 && c < STORAGE_NUMBER_POSITIVE_MIN) continue;
233 if(c < 0 && c > STORAGE_NUMBER_NEGATIVE_MAX) continue;
235 if(check_storage_number(c, 1)) return 1;
240 benchmark_storage_number(1000000, 2);
245 // --------------------------------------------------------------------------------------------------------------------
248 unsigned long long microseconds;
249 calculated_number value;
254 char description[1024];
257 unsigned long long multiplier;
258 unsigned long long divisor;
261 unsigned long feed_entries;
262 unsigned long result_entries;
263 struct feed_values *feed;
264 calculated_number *results;
267 // --------------------------------------------------------------------------------------------------------------------
269 // test absolute values stored
271 struct feed_values test1_feed[] = {
284 calculated_number test1_results[] = {
285 20, 30, 40, 50, 60, 70, 80, 90, 100
288 struct test test1 = {
290 "test absolute values stored at exactly second boundaries",
294 RRDDIM_ABSOLUTE, // algorithm
298 test1_results // results
301 // --------------------------------------------------------------------------------------------------------------------
303 // test absolute values stored in the middle of second boundaries
305 struct feed_values test2_feed[] = {
318 calculated_number test2_results[] = {
319 20, 30, 40, 50, 60, 70, 80, 90, 100
322 struct test test2 = {
324 "test absolute values stored in the middle of second boundaries",
328 RRDDIM_ABSOLUTE, // algorithm
332 test2_results // results
335 // --------------------------------------------------------------------------------------------------------------------
338 struct feed_values test3_feed[] = {
351 calculated_number test3_results[] = {
352 10, 10, 10, 10, 10, 10, 10, 10, 10
355 struct test test3 = {
357 "test incremental values stored at exactly second boundaries",
361 RRDDIM_INCREMENTAL, // algorithm
365 test3_results // results
368 // --------------------------------------------------------------------------------------------------------------------
371 struct feed_values test4_feed[] = {
384 calculated_number test4_results[] = {
385 5, 10, 10, 10, 10, 10, 10, 10, 10
388 struct test test4 = {
390 "test incremental values stored in the middle of second boundaries",
394 RRDDIM_INCREMENTAL, // algorithm
398 test4_results // results
401 // --------------------------------------------------------------------------------------------------------------------
404 struct feed_values test5_feed[] = {
417 calculated_number test5_results[] = {
418 500, 500, 0, 500, 500, 0, 0, 0, 0
421 struct test test5 = {
423 "test incremental values ups and downs",
427 RRDDIM_INCREMENTAL, // algorithm
431 test5_results // results
434 // --------------------------------------------------------------------------------------------------------------------
437 struct feed_values test6_feed[] = {
456 calculated_number test6_results[] = {
457 3000, 4000, 4000, 4000
460 struct test test6 = {
462 "test incremental values updated within the same second",
466 RRDDIM_INCREMENTAL, // algorithm
470 test6_results // results
473 // --------------------------------------------------------------------------------------------------------------------
476 struct feed_values test7_feed[] = {
489 calculated_number test7_results[] = {
490 250, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500
493 struct test test7 = {
495 "test incremental values updated in long durations",
499 RRDDIM_INCREMENTAL, // algorithm
501 18, // result entries
503 test7_results // results
506 // --------------------------------------------------------------------------------------------------------------------
509 struct feed_values test8_feed[] = {
518 calculated_number test8_results[] = {
519 1250, 2000, 2250, 3000, 3250, 4000, 4250, 5000, 5250, 6000
522 struct test test8 = {
524 "test absolute values updated in long durations",
528 RRDDIM_ABSOLUTE, // algorithm
530 10, // result entries
532 test8_results // results
535 // --------------------------------------------------------------------------------------------------------------------
538 struct feed_values test9_feed[] = {
557 calculated_number test9_results[] = {
558 4000, 8000, 12000, 16000
561 struct test test9 = {
563 "test absolute values updated within the same second",
567 RRDDIM_ABSOLUTE, // algorithm
571 test9_results // results
574 // --------------------------------------------------------------------------------------------------------------------
576 int run_test(struct test *test)
578 fprintf(stderr, "\nRunning test '%s':\n%s\n", test->name, test->description);
580 rrd_memory_mode = RRD_MEMORY_MODE_RAM;
581 rrd_update_every = test->update_every;
584 snprintf(name, 100, "unittest-%s", test->name);
587 RRDSET *st = rrdset_create("netdata", name, name, "netdata", "Unit Testing", "a value", 1, 1, RRDSET_TYPE_LINE);
588 RRDDIM *rd = rrddim_add(st, "dimension", NULL, test->multiplier, test->divisor, test->algorithm);
591 // feed it with the test data
593 for(c = 0; c < test->feed_entries; c++) {
594 if(debug_flags) fprintf(stderr, "\n\n");
597 fprintf(stderr, " > %s: feeding position %lu, after %llu microseconds, with value " CALCULATED_NUMBER_FORMAT "\n", test->name, c+1, test->feed[c].microseconds, test->feed[c].value);
598 rrdset_next_usec(st, test->feed[c].microseconds);
601 fprintf(stderr, " > %s: feeding position %lu with value " CALCULATED_NUMBER_FORMAT "\n", test->name, c+1, test->feed[c].value);
604 rrddim_set(st, "dimension", test->feed[c].value);
607 // align the first entry to second boundary
609 fprintf(stderr, " > %s: fixing first collection time to be %llu microseconds to second boundary\n", test->name, test->feed[c].microseconds);
610 rd->last_collected_time.tv_usec = st->last_collected_time.tv_usec = st->last_updated.tv_usec = test->feed[c].microseconds;
617 if(st->counter != test->result_entries) {
618 fprintf(stderr, " %s stored %lu entries, but we were expecting %lu, ### E R R O R ###\n", test->name, st->counter, test->result_entries);
622 unsigned long max = (st->counter < test->result_entries)?st->counter:test->result_entries;
623 for(c = 0 ; c < max ; c++) {
624 calculated_number v = unpack_storage_number(rd->values[c]), n = test->results[c];
625 fprintf(stderr, " %s: checking position %lu, expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n", test->name, c+1, n, v, (v == n)?"OK":"### E R R O R ###");
632 int run_all_mockup_tests(void)
664 int unit_test(long delay, long shift)
666 static int repeat = 0;
670 snprintf(name, 100, "unittest-%d-%ld-%ld", repeat, delay, shift);
672 //debug_flags = 0xffffffff;
673 rrd_memory_mode = RRD_MEMORY_MODE_RAM;
674 rrd_update_every = 1;
681 RRDSET *st = rrdset_create("netdata", name, name, "netdata", "Unit Testing", "a value", 1, 1, RRDSET_TYPE_LINE);
684 RRDDIM *rdabs = NULL;
685 RRDDIM *rdinc = NULL;
686 RRDDIM *rdabst = NULL;
687 RRDDIM *rdabsi = NULL;
689 if(do_abs) rdabs = rrddim_add(st, "absolute", "absolute", 1, 1, RRDDIM_ABSOLUTE);
690 if(do_inc) rdinc = rrddim_add(st, "incremental", "incremental", 1, 1, RRDDIM_INCREMENTAL);
691 if(do_abst) rdabst = rrddim_add(st, "percentage-of-absolute-row", "percentage-of-absolute-row", 1, 1, RRDDIM_PCENT_OVER_ROW_TOTAL);
692 if(do_absi) rdabsi = rrddim_add(st, "percentage-of-incremental-row", "percentage-of-incremental-row", 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
694 long increment = 1000;
695 collected_number i = 0;
697 unsigned long c, dimensions = 0;
699 for(rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
701 for(c = 0; c < 20 ;c++) {
704 fprintf(stderr, "\n\nLOOP = %lu, DELAY = %ld, VALUE = " COLLECTED_NUMBER_FORMAT "\n", c, delay, i);
706 rrdset_next_usec(st, delay);
708 if(do_abs) rrddim_set(st, "absolute", i);
709 if(do_inc) rrddim_set(st, "incremental", i);
710 if(do_abst) rrddim_set(st, "percentage-of-absolute-row", i);
711 if(do_absi) rrddim_set(st, "percentage-of-incremental-row", i);
714 gettimeofday(&st->last_collected_time, NULL);
715 st->last_collected_time.tv_usec = shift;
718 // prevent it from deleting the dimensions
719 for(rd = st->dimensions ; rd ; rd = rd->next)
720 rd->last_collected_time.tv_sec = st->last_collected_time.tv_sec;
725 unsigned long oincrement = increment;
726 increment = increment * st->update_every * 1000000 / delay;
727 fprintf(stderr, "\n\nORIGINAL INCREMENT: %lu, INCREMENT %lu, DELAY %lu, SHIFT %lu\n", oincrement * 10, increment * 10, delay, shift);
731 calculated_number cn, v;
732 for(c = 0 ; c < st->counter ; c++) {
733 fprintf(stderr, "\nPOSITION: c = %lu, EXPECTED VALUE %lu\n", c, (oincrement + c * increment + increment * (1000000 - shift) / 1000000 )* 10);
735 for(rd = st->dimensions ; rd ; rd = rd->next) {
737 cn = unpack_storage_number(sn);
738 fprintf(stderr, "\t %s " CALCULATED_NUMBER_FORMAT " (PACKED AS " STORAGE_NUMBER_FORMAT ") -> ", rd->id, cn, sn);
742 // + (increment * (1000000 - shift) / 1000000)
743 + (c + 1) * increment
746 else if(rd == rdinc) v = (c?(increment):(increment * (1000000 - shift) / 1000000));
747 else if(rd == rdabst) v = oincrement / dimensions / 10;
748 else if(rd == rdabsi) v = oincrement / dimensions / 10;
751 if(v == cn) fprintf(stderr, "passed.\n");
753 fprintf(stderr, "ERROR! (expected " CALCULATED_NUMBER_FORMAT ")\n", v);
760 fprintf(stderr, "\n\nUNIT TEST(%ld, %ld) FAILED\n\n", delay, shift);