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 = str2l(buffer);
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 RRD_ALGORITHM_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 RRD_ALGORITHM_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 RRD_ALGORITHM_INCREMENTAL, // algorithm
360 test3_results, // results
365 // --------------------------------------------------------------------------------------------------------------------
368 struct feed_values test4_feed[] = {
381 calculated_number test4_results[] = {
382 10, 10, 10, 10, 10, 10, 10, 10, 10
385 struct test test4 = {
387 "test incremental values stored in the middle of second boundaries",
391 RRD_ALGORITHM_INCREMENTAL, // algorithm
395 test4_results, // results
400 // --------------------------------------------------------------------------------------------------------------------
403 struct feed_values test5_feed[] = {
416 calculated_number test5_results[] = {
417 1000, 500, 0, 500, 500, 0, 0, 0, 0
420 struct test test5 = {
422 "test incremental values ups and downs",
426 RRD_ALGORITHM_INCREMENTAL, // algorithm
430 test5_results, // results
435 // --------------------------------------------------------------------------------------------------------------------
438 struct feed_values test6_feed[] = {
457 calculated_number test6_results[] = {
458 4000, 4000, 4000, 4000
461 struct test test6 = {
463 "test incremental values updated within the same second",
467 RRD_ALGORITHM_INCREMENTAL, // algorithm
471 test6_results, // results
476 // --------------------------------------------------------------------------------------------------------------------
479 struct feed_values test7_feed[] = {
492 calculated_number test7_results[] = {
493 500, 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 RRD_ALGORITHM_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 RRD_ALGORITHM_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 RRD_ALGORITHM_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 1000, 1000, 1000, 1000, 1000, 1000, 1000
603 struct test test10 = {
605 "test incremental values updated in short and long durations",
609 RRD_ALGORITHM_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 RRD_ALGORITHM_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 RRD_ALGORITHM_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 RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL, // algorithm
734 test13_results, // results
739 // --------------------------------------------------------------------------------------------------------------------
742 struct feed_values test14_feed[] = {
743 { 0, 0x015397dc42151c41ULL },
744 { 13573000, 0x015397e612e3ff5dULL },
745 { 29969000, 0x015397f905ecdaa8ULL },
746 { 29958000, 0x0153980c2a6cb5e4ULL },
747 { 30054000, 0x0153981f4032fb83ULL },
748 { 34952000, 0x015398355efadaccULL },
749 { 25046000, 0x01539845ba4b09f8ULL },
750 { 29947000, 0x0153985948bf381dULL },
751 { 30054000, 0x0153986c5b9c27e2ULL },
752 { 29942000, 0x0153987f888982d0ULL },
755 calculated_number test14_results[] = {
756 23.1383300, 21.8515600, 21.8804600, 21.7788000, 22.0112200, 22.4386100, 22.0906100, 21.9150800
759 struct test test14 = {
761 "issue #981 with real data",
764 1000000000, // divisor
765 RRD_ALGORITHM_INCREMENTAL, // algorithm
769 test14_results, // results
774 struct feed_values test14b_feed[] = {
776 { 13573000, 13573000 },
777 { 29969000, 13573000 + 29969000 },
778 { 29958000, 13573000 + 29969000 + 29958000 },
779 { 30054000, 13573000 + 29969000 + 29958000 + 30054000 },
780 { 34952000, 13573000 + 29969000 + 29958000 + 30054000 + 34952000 },
781 { 25046000, 13573000 + 29969000 + 29958000 + 30054000 + 34952000 + 25046000 },
782 { 29947000, 13573000 + 29969000 + 29958000 + 30054000 + 34952000 + 25046000 + 29947000 },
783 { 30054000, 13573000 + 29969000 + 29958000 + 30054000 + 34952000 + 25046000 + 29947000 + 30054000 },
784 { 29942000, 13573000 + 29969000 + 29958000 + 30054000 + 34952000 + 25046000 + 29947000 + 30054000 + 29942000 },
787 calculated_number test14b_results[] = {
788 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000
791 struct test test14b = {
793 "issue #981 with dummy data",
797 RRD_ALGORITHM_INCREMENTAL, // algorithm
800 test14b_feed, // feed
801 test14b_results, // results
806 struct feed_values test14c_feed[] = {
807 { 29000000, 29000000 },
808 { 1000000, 29000000 + 1000000 },
809 { 30000000, 29000000 + 1000000 + 30000000 },
810 { 30000000, 29000000 + 1000000 + 30000000 + 30000000 },
811 { 30000000, 29000000 + 1000000 + 30000000 + 30000000 + 30000000 },
812 { 30000000, 29000000 + 1000000 + 30000000 + 30000000 + 30000000 + 30000000 },
813 { 30000000, 29000000 + 1000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 },
814 { 30000000, 29000000 + 1000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 },
815 { 30000000, 29000000 + 1000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 },
816 { 30000000, 29000000 + 1000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 },
819 calculated_number test14c_results[] = {
820 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000
823 struct test test14c = {
825 "issue #981 with dummy data, checking for late start",
829 RRD_ALGORITHM_INCREMENTAL, // algorithm
832 test14c_feed, // feed
833 test14c_results, // results
838 // --------------------------------------------------------------------------------------------------------------------
841 struct feed_values test15_feed[] = {
843 { 1008752, 1068822698 },
844 { 993809, 1069573072 },
845 { 995911, 1070324135 },
846 { 1014562, 1071078166 },
847 { 994684, 1071831349 },
848 { 993128, 1072235739 },
849 { 1010332, 1072958871 },
850 { 1003394, 1073707019 },
851 { 995201, 1074460255 },
854 collected_number test15_feed2[] = {
855 178825286, 178825286, 178825286, 178825286, 178825498, 178825498, 179165652, 179202964, 179203282, 179204130
858 calculated_number test15_results[] = {
859 5857.4080000, 5898.4540000, 5891.6590000, 5806.3160000, 5914.2640000, 3202.2630000, 5589.6560000, 5822.5260000, 5911.7520000
862 calculated_number test15_results2[] = {
863 0.0000000, 0.0000000, 0.0024944, 1.6324779, 0.0212777, 2655.1890000, 290.5387000, 5.6733610, 6.5960220
866 struct test test15 = {
868 "test incremental with 2 dimensions",
872 RRD_ALGORITHM_INCREMENTAL, // algorithm
876 test15_results, // results
877 test15_feed2, // feed2
878 test15_results2 // results2
881 // --------------------------------------------------------------------------------------------------------------------
883 int run_test(struct test *test)
885 fprintf(stderr, "\nRunning test '%s':\n%s\n", test->name, test->description);
887 default_rrd_memory_mode = RRD_MEMORY_MODE_RAM;
888 default_rrd_update_every = test->update_every;
891 snprintfz(name, 100, "unittest-%s", test->name);
894 RRDSET *st = rrdset_create_localhost("netdata", name, name, "netdata", NULL, "Unit Testing", "a value", 1
895 , test->update_every, RRDSET_TYPE_LINE);
896 RRDDIM *rd = rrddim_add(st, "dim1", NULL, test->multiplier, test->divisor, test->algorithm);
900 rd2 = rrddim_add(st, "dim2", NULL, test->multiplier, test->divisor, test->algorithm);
902 rrdset_flag_set(st, RRDSET_FLAG_DEBUG);
904 // feed it with the test data
905 time_t time_now = 0, time_start = now_realtime_sec();
907 collected_number last = 0;
908 for(c = 0; c < test->feed_entries; c++) {
909 if(debug_flags) fprintf(stderr, "\n\n");
912 time_now += test->feed[c].microseconds;
913 fprintf(stderr, " > %s: feeding position %lu, after %0.3f seconds (%0.3f seconds from start), delta " CALCULATED_NUMBER_FORMAT ", rate " CALCULATED_NUMBER_FORMAT "\n",
915 (float)test->feed[c].microseconds / 1000000.0,
916 (float)time_now / 1000000.0,
917 ((calculated_number)test->feed[c].value - (calculated_number)last) * (calculated_number)test->multiplier / (calculated_number)test->divisor,
918 (((calculated_number)test->feed[c].value - (calculated_number)last) * (calculated_number)test->multiplier / (calculated_number)test->divisor) / (calculated_number)test->feed[c].microseconds * (calculated_number)1000000);
919 rrdset_next_usec_unfiltered(st, test->feed[c].microseconds);
922 fprintf(stderr, " > %s: feeding position %lu\n", test->name, c+1);
925 fprintf(stderr, " >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd->name, test->feed[c].value);
926 rrddim_set(st, "dim1", test->feed[c].value);
927 last = test->feed[c].value;
930 fprintf(stderr, " >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd2->name, test->feed2[c]);
931 rrddim_set(st, "dim2", test->feed2[c]);
936 // align the first entry to second boundary
938 fprintf(stderr, " > %s: fixing first collection time to be %llu microseconds to second boundary\n", test->name, test->feed[c].microseconds);
939 rd->last_collected_time.tv_usec = st->last_collected_time.tv_usec = st->last_updated.tv_usec = test->feed[c].microseconds;
940 // time_start = st->last_collected_time.tv_sec;
947 if(st->counter != test->result_entries) {
948 fprintf(stderr, " %s stored %lu entries, but we were expecting %lu, ### E R R O R ###\n", test->name, st->counter, test->result_entries);
952 unsigned long max = (st->counter < test->result_entries)?st->counter:test->result_entries;
953 for(c = 0 ; c < max ; c++) {
954 calculated_number v = unpack_storage_number(rd->values[c]);
955 calculated_number n = test->results[c];
956 int same = (roundl(v * 10000000.0) == roundl(n * 10000000.0))?1:0;
957 fprintf(stderr, " %s/%s: checking position %lu (at %lu secs), expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n",
958 test->name, rd->name, c+1,
959 (rrdset_first_entry_t(st) + c * st->update_every) - time_start,
960 n, v, (same)?"OK":"### E R R O R ###");
965 v = unpack_storage_number(rd2->values[c]);
966 n = test->results2[c];
967 same = (roundl(v * 10000000.0) == roundl(n * 10000000.0))?1:0;
968 fprintf(stderr, " %s/%s: checking position %lu (at %lu secs), expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n",
969 test->name, rd2->name, c+1,
970 (rrdset_first_entry_t(st) + c * st->update_every) - time_start,
971 n, v, (same)?"OK":"### E R R O R ###");
979 static int test_variable_renames(void) {
980 fprintf(stderr, "Creating chart\n");
981 RRDSET *st = rrdset_create_localhost("chart", "ID", NULL, "family", "context", "Unit Testing", "a value", 1, 1
983 fprintf(stderr, "Created chart with id '%s', name '%s'\n", st->id, st->name);
985 fprintf(stderr, "Creating dimension DIM1\n");
986 RRDDIM *rd1 = rrddim_add(st, "DIM1", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
987 fprintf(stderr, "Created dimension with id '%s', name '%s'\n", rd1->id, rd1->name);
989 fprintf(stderr, "Creating dimension DIM2\n");
990 RRDDIM *rd2 = rrddim_add(st, "DIM2", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
991 fprintf(stderr, "Created dimension with id '%s', name '%s'\n", rd2->id, rd2->name);
993 fprintf(stderr, "Renaming chart to CHARTNAME1\n");
994 rrdset_set_name(st, "CHARTNAME1");
995 fprintf(stderr, "Renamed chart with id '%s' to name '%s'\n", st->id, st->name);
997 fprintf(stderr, "Renaming chart to CHARTNAME2\n");
998 rrdset_set_name(st, "CHARTNAME2");
999 fprintf(stderr, "Renamed chart with id '%s' to name '%s'\n", st->id, st->name);
1001 fprintf(stderr, "Renaming dimension DIM1 to DIM1NAME1\n");
1002 rrddim_set_name(st, rd1, "DIM1NAME1");
1003 fprintf(stderr, "Renamed dimension with id '%s' to name '%s'\n", rd1->id, rd1->name);
1005 fprintf(stderr, "Renaming dimension DIM1 to DIM1NAME2\n");
1006 rrddim_set_name(st, rd1, "DIM1NAME2");
1007 fprintf(stderr, "Renamed dimension with id '%s' to name '%s'\n", rd1->id, rd1->name);
1009 fprintf(stderr, "Renaming dimension DIM2 to DIM2NAME1\n");
1010 rrddim_set_name(st, rd2, "DIM2NAME1");
1011 fprintf(stderr, "Renamed dimension with id '%s' to name '%s'\n", rd2->id, rd2->name);
1013 fprintf(stderr, "Renaming dimension DIM2 to DIM2NAME2\n");
1014 rrddim_set_name(st, rd2, "DIM2NAME2");
1015 fprintf(stderr, "Renamed dimension with id '%s' to name '%s'\n", rd2->id, rd2->name);
1017 BUFFER *buf = buffer_create(1);
1018 health_api_v1_chart_variables2json(st, buf);
1019 fprintf(stderr, "%s", buffer_tostring(buf));
1024 int run_all_mockup_tests(void)
1026 if(!test_variable_renames())
1029 if(run_test(&test1))
1032 if(run_test(&test2))
1035 if(run_test(&test3))
1038 if(run_test(&test4))
1041 if(run_test(&test5))
1044 if(run_test(&test6))
1047 if(run_test(&test7))
1050 if(run_test(&test8))
1053 if(run_test(&test9))
1056 if(run_test(&test10))
1059 if(run_test(&test11))
1062 if(run_test(&test12))
1065 if(run_test(&test13))
1068 if(run_test(&test14))
1071 if(run_test(&test14b))
1074 if(run_test(&test14c))
1077 if(run_test(&test15))
1085 int unit_test(long delay, long shift)
1087 static int repeat = 0;
1091 snprintfz(name, 100, "unittest-%d-%ld-%ld", repeat, delay, shift);
1093 //debug_flags = 0xffffffff;
1094 default_rrd_memory_mode = RRD_MEMORY_MODE_RAM;
1095 default_rrd_update_every = 1;
1102 RRDSET *st = rrdset_create_localhost("netdata", name, name, "netdata", NULL, "Unit Testing", "a value", 1, 1
1103 , RRDSET_TYPE_LINE);
1104 rrdset_flag_set(st, RRDSET_FLAG_DEBUG);
1106 RRDDIM *rdabs = NULL;
1107 RRDDIM *rdinc = NULL;
1108 RRDDIM *rdabst = NULL;
1109 RRDDIM *rdabsi = NULL;
1111 if(do_abs) rdabs = rrddim_add(st, "absolute", "absolute", 1, 1, RRD_ALGORITHM_ABSOLUTE);
1112 if(do_inc) rdinc = rrddim_add(st, "incremental", "incremental", 1, 1, RRD_ALGORITHM_INCREMENTAL);
1113 if(do_abst) rdabst = rrddim_add(st, "percentage-of-absolute-row", "percentage-of-absolute-row", 1, 1, RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL);
1114 if(do_absi) rdabsi = rrddim_add(st, "percentage-of-incremental-row", "percentage-of-incremental-row", 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
1116 long increment = 1000;
1117 collected_number i = 0;
1119 unsigned long c, dimensions = 0;
1121 for(rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1123 for(c = 0; c < 20 ;c++) {
1126 fprintf(stderr, "\n\nLOOP = %lu, DELAY = %ld, VALUE = " COLLECTED_NUMBER_FORMAT "\n", c, delay, i);
1128 rrdset_next_usec_unfiltered(st, delay);
1130 if(do_abs) rrddim_set(st, "absolute", i);
1131 if(do_inc) rrddim_set(st, "incremental", i);
1132 if(do_abst) rrddim_set(st, "percentage-of-absolute-row", i);
1133 if(do_absi) rrddim_set(st, "percentage-of-incremental-row", i);
1136 now_realtime_timeval(&st->last_collected_time);
1137 st->last_collected_time.tv_usec = shift;
1140 // prevent it from deleting the dimensions
1141 for(rd = st->dimensions ; rd ; rd = rd->next)
1142 rd->last_collected_time.tv_sec = st->last_collected_time.tv_sec;
1147 unsigned long oincrement = increment;
1148 increment = increment * st->update_every * 1000000 / delay;
1149 fprintf(stderr, "\n\nORIGINAL INCREMENT: %lu, INCREMENT %ld, DELAY %ld, SHIFT %ld\n", oincrement * 10, increment * 10, delay, shift);
1153 calculated_number cn, v;
1154 for(c = 0 ; c < st->counter ; c++) {
1155 fprintf(stderr, "\nPOSITION: c = %lu, EXPECTED VALUE %lu\n", c, (oincrement + c * increment + increment * (1000000 - shift) / 1000000 )* 10);
1157 for(rd = st->dimensions ; rd ; rd = rd->next) {
1159 cn = unpack_storage_number(sn);
1160 fprintf(stderr, "\t %s " CALCULATED_NUMBER_FORMAT " (PACKED AS " STORAGE_NUMBER_FORMAT ") -> ", rd->id, cn, sn);
1164 // + (increment * (1000000 - shift) / 1000000)
1165 + (c + 1) * increment
1168 else if(rd == rdinc) v = (c?(increment):(increment * (1000000 - shift) / 1000000));
1169 else if(rd == rdabst) v = oincrement / dimensions / 10;
1170 else if(rd == rdabsi) v = oincrement / dimensions / 10;
1173 if(v == cn) fprintf(stderr, "passed.\n");
1175 fprintf(stderr, "ERROR! (expected " CALCULATED_NUMBER_FORMAT ")\n", v);
1182 fprintf(stderr, "\n\nUNIT TEST(%ld, %ld) FAILED\n\n", delay, shift);