]> arthur.barton.de Git - netdata.git/blob - src/unit_test.c
nonzero option should return all selected dimensions if they are all zero; fixes...
[netdata.git] / src / unit_test.c
1 #include "common.h"
2
3 int check_storage_number(calculated_number n, int debug) {
4     char buffer[100];
5     uint32_t flags = SN_EXISTS;
6
7     storage_number s = pack_storage_number(n, flags);
8     calculated_number d = unpack_storage_number(s);
9
10     if(!does_storage_number_exist(s)) {
11         fprintf(stderr, "Exists flags missing for number " CALCULATED_NUMBER_FORMAT "!\n", n);
12         return 5;
13     }
14
15     calculated_number ddiff = d - n;
16     calculated_number dcdiff = ddiff * 100.0 / n;
17
18     if(dcdiff < 0) dcdiff = -dcdiff;
19
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;
25
26     if(debug) {
27         fprintf(stderr,
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",
32             n,
33             d, s, ddiff, dcdiff,
34             buffer,
35             len, p, pdiff, pcdiff
36         );
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);
40     }
41
42     if(len != strlen(buffer)) return 1;
43     if(dcdiff > ACCURACY_LOSS) return 3;
44     if(pcdiff > ACCURACY_LOSS) return 4;
45     return 0;
46 }
47
48 void benchmark_storage_number(int loop, int multiplier) {
49     int i, j;
50     calculated_number n, d;
51     storage_number s;
52     unsigned long long user, system, total, mine, their;
53
54     char buffer[100];
55
56     struct rusage now, last;
57
58     fprintf(stderr, "\n\nBenchmarking %d numbers, please wait...\n\n", loop);
59
60     // ------------------------------------------------------------------------
61
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));
64
65     mine = (calculated_number)sizeof(storage_number) * (calculated_number)loop;
66     their = (calculated_number)sizeof(calculated_number) * (calculated_number)loop;
67
68     if(mine > their) {
69         fprintf(stderr, "\nNETDATA NEEDS %0.2Lf TIMES MORE MEMORY. Sorry!\n", (long double)(mine / their));
70     }
71     else {
72         fprintf(stderr, "\nNETDATA INTERNAL FLOATING POINT ARITHMETICS NEEDS %0.2Lf TIMES LESS MEMORY.\n", (long double)(their / mine));
73     }
74
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);
81
82     // ------------------------------------------------------------------------
83
84     fprintf(stderr, "INTERNAL LONG DOUBLE PRINTING: ");
85     getrusage(RUSAGE_SELF, &last);
86
87     // do the job
88     for(j = 1; j < 11 ;j++) {
89         n = STORAGE_NUMBER_POSITIVE_MIN * j;
90
91         for(i = 0; i < loop ;i++) {
92             n *= multiplier;
93             if(n > STORAGE_NUMBER_POSITIVE_MAX) n = STORAGE_NUMBER_POSITIVE_MIN;
94
95             print_calculated_number(buffer, n);
96         }
97     }
98
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;
103     mine = total;
104
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));
106
107     // ------------------------------------------------------------------------
108
109     fprintf(stderr, "SYSTEM   LONG DOUBLE PRINTING: ");
110     getrusage(RUSAGE_SELF, &last);
111
112     // do the job
113     for(j = 1; j < 11 ;j++) {
114         n = STORAGE_NUMBER_POSITIVE_MIN * j;
115
116         for(i = 0; i < loop ;i++) {
117             n *= multiplier;
118             if(n > STORAGE_NUMBER_POSITIVE_MAX) n = STORAGE_NUMBER_POSITIVE_MIN;
119             snprintfz(buffer, 100, CALCULATED_NUMBER_FORMAT, n);
120         }
121     }
122
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;
127     their = total;
128
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));
130
131     if(mine > total) {
132         fprintf(stderr, "NETDATA CODE IS SLOWER %0.2Lf %%\n", (long double)(mine * 100.0 / their - 100.0));
133     }
134     else {
135         fprintf(stderr, "NETDATA CODE IS  F A S T E R  %0.2Lf %%\n", (long double)(their * 100.0 / mine - 100.0));
136     }
137
138     // ------------------------------------------------------------------------
139
140     fprintf(stderr, "\nINTERNAL LONG DOUBLE PRINTING WITH PACK / UNPACK: ");
141     getrusage(RUSAGE_SELF, &last);
142
143     // do the job
144     for(j = 1; j < 11 ;j++) {
145         n = STORAGE_NUMBER_POSITIVE_MIN * j;
146
147         for(i = 0; i < loop ;i++) {
148             n *= multiplier;
149             if(n > STORAGE_NUMBER_POSITIVE_MAX) n = STORAGE_NUMBER_POSITIVE_MIN;
150
151             s = pack_storage_number(n, 1);
152             d = unpack_storage_number(s);
153             print_calculated_number(buffer, d);
154         }
155     }
156
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;
161     mine = total;
162
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));
164
165     if(mine > their) {
166         fprintf(stderr, "WITH PACKING UNPACKING NETDATA CODE IS SLOWER %0.2Lf %%\n", (long double)(mine * 100.0 / their - 100.0));
167     }
168     else {
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));
170     }
171
172     // ------------------------------------------------------------------------
173
174 }
175
176 static int check_storage_number_exists() {
177     uint32_t flags = SN_EXISTS;
178
179
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));
183             return 1;
184         }
185     }
186
187     flags = SN_EXISTS;
188     calculated_number n = 0.0;
189
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));
194         return 1;
195     }
196     if(n != d) {
197         fprintf(stderr, "Wrong number returned. Expected " CALCULATED_NUMBER_FORMAT ", returned " CALCULATED_NUMBER_FORMAT "!\n", n, d);
198         return 1;
199     }
200
201     return 0;
202 }
203
204 int unit_test_storage()
205 {
206     if(check_storage_number_exists()) return 0;
207
208     calculated_number c, a = 0;
209     int i, j, g, r = 0;
210
211     for(g = -1; g <= 1 ; g++) {
212         a = 0;
213
214         if(!g) continue;
215
216         for(j = 0; j < 9 ;j++) {
217             a += 0.0000001;
218             c = a * g;
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;
222
223                 if(check_storage_number(c, 1)) return 1;
224             }
225         }
226     }
227
228     benchmark_storage_number(1000000, 2);
229     return r;
230 }
231
232
233 // --------------------------------------------------------------------------------------------------------------------
234
235 struct feed_values {
236         unsigned long long microseconds;
237         collected_number value;
238 };
239
240 struct test {
241     char name[100];
242     char description[1024];
243
244     int update_every;
245     unsigned long long multiplier;
246     unsigned long long divisor;
247     int algorithm;
248
249     unsigned long feed_entries;
250     unsigned long result_entries;
251     struct feed_values *feed;
252     calculated_number *results;
253
254     collected_number *feed2;
255     calculated_number *results2;
256 };
257
258 // --------------------------------------------------------------------------------------------------------------------
259 // test1
260 // test absolute values stored
261
262 struct feed_values test1_feed[] = {
263         { 0, 10 },
264         { 1000000, 20 },
265         { 1000000, 30 },
266         { 1000000, 40 },
267         { 1000000, 50 },
268         { 1000000, 60 },
269         { 1000000, 70 },
270         { 1000000, 80 },
271         { 1000000, 90 },
272         { 1000000, 100 },
273 };
274
275 calculated_number test1_results[] = {
276         20, 30, 40, 50, 60, 70, 80, 90, 100
277 };
278
279 struct test test1 = {
280         "test1",            // name
281         "test absolute values stored at exactly second boundaries",
282         1,                  // update_every
283         1,                  // multiplier
284         1,                  // divisor
285         RRDDIM_ABSOLUTE,    // algorithm
286         10,                 // feed entries
287         9,                  // result entries
288         test1_feed,         // feed
289         test1_results,      // results
290         NULL,               // feed2
291         NULL                // results2
292 };
293
294 // --------------------------------------------------------------------------------------------------------------------
295 // test2
296 // test absolute values stored in the middle of second boundaries
297
298 struct feed_values test2_feed[] = {
299         { 500000, 10 },
300         { 1000000, 20 },
301         { 1000000, 30 },
302         { 1000000, 40 },
303         { 1000000, 50 },
304         { 1000000, 60 },
305         { 1000000, 70 },
306         { 1000000, 80 },
307         { 1000000, 90 },
308         { 1000000, 100 },
309 };
310
311 calculated_number test2_results[] = {
312         20, 30, 40, 50, 60, 70, 80, 90, 100
313 };
314
315 struct test test2 = {
316         "test2",            // name
317         "test absolute values stored in the middle of second boundaries",
318         1,                  // update_every
319         1,                  // multiplier
320         1,                  // divisor
321         RRDDIM_ABSOLUTE,    // algorithm
322         10,                 // feed entries
323         9,                  // result entries
324         test2_feed,         // feed
325         test2_results,      // results
326         NULL,               // feed2
327         NULL                // results2
328 };
329
330 // --------------------------------------------------------------------------------------------------------------------
331 // test3
332
333 struct feed_values test3_feed[] = {
334         { 0, 10 },
335         { 1000000, 20 },
336         { 1000000, 30 },
337         { 1000000, 40 },
338         { 1000000, 50 },
339         { 1000000, 60 },
340         { 1000000, 70 },
341         { 1000000, 80 },
342         { 1000000, 90 },
343         { 1000000, 100 },
344 };
345
346 calculated_number test3_results[] = {
347         10, 10, 10, 10, 10, 10, 10, 10, 10
348 };
349
350 struct test test3 = {
351         "test3",            // name
352         "test incremental values stored at exactly second boundaries",
353         1,                  // update_every
354         1,                  // multiplier
355         1,                  // divisor
356         RRDDIM_INCREMENTAL, // algorithm
357         10,                 // feed entries
358         9,                  // result entries
359         test3_feed,         // feed
360         test3_results,      // results
361         NULL,               // feed2
362         NULL                // results2
363 };
364
365 // --------------------------------------------------------------------------------------------------------------------
366 // test4
367
368 struct feed_values test4_feed[] = {
369         { 500000, 10 },
370         { 1000000, 20 },
371         { 1000000, 30 },
372         { 1000000, 40 },
373         { 1000000, 50 },
374         { 1000000, 60 },
375         { 1000000, 70 },
376         { 1000000, 80 },
377         { 1000000, 90 },
378         { 1000000, 100 },
379 };
380
381 calculated_number test4_results[] = {
382         10, 10, 10, 10, 10, 10, 10, 10, 10
383 };
384
385 struct test test4 = {
386         "test4",            // name
387         "test incremental values stored in the middle of second boundaries",
388         1,                  // update_every
389         1,                  // multiplier
390         1,                  // divisor
391         RRDDIM_INCREMENTAL, // algorithm
392         10,                 // feed entries
393         9,                  // result entries
394         test4_feed,         // feed
395         test4_results,      // results
396         NULL,               // feed2
397         NULL                // results2
398 };
399
400 // --------------------------------------------------------------------------------------------------------------------
401 // test5
402
403 struct feed_values test5_feed[] = {
404         { 500000, 1000 },
405         { 1000000, 2000 },
406         { 1000000, 2000 },
407         { 1000000, 2000 },
408         { 1000000, 3000 },
409         { 1000000, 2000 },
410         { 1000000, 2000 },
411         { 1000000, 2000 },
412         { 1000000, 2000 },
413         { 1000000, 2000 },
414 };
415
416 calculated_number test5_results[] = {
417         1000, 500, 0, 500, 500, 0, 0, 0, 0
418 };
419
420 struct test test5 = {
421         "test5",            // name
422         "test incremental values ups and downs",
423         1,                  // update_every
424         1,                  // multiplier
425         1,                  // divisor
426         RRDDIM_INCREMENTAL, // algorithm
427         10,                 // feed entries
428         9,                  // result entries
429         test5_feed,         // feed
430         test5_results,      // results
431         NULL,               // feed2
432         NULL                // results2
433 };
434
435 // --------------------------------------------------------------------------------------------------------------------
436 // test6
437
438 struct feed_values test6_feed[] = {
439         { 250000, 1000 },
440         { 250000, 2000 },
441         { 250000, 3000 },
442         { 250000, 4000 },
443         { 250000, 5000 },
444         { 250000, 6000 },
445         { 250000, 7000 },
446         { 250000, 8000 },
447         { 250000, 9000 },
448         { 250000, 10000 },
449         { 250000, 11000 },
450         { 250000, 12000 },
451         { 250000, 13000 },
452         { 250000, 14000 },
453         { 250000, 15000 },
454         { 250000, 16000 },
455 };
456
457 calculated_number test6_results[] = {
458         4000, 4000, 4000, 4000
459 };
460
461 struct test test6 = {
462         "test6",            // name
463         "test incremental values updated within the same second",
464         1,                  // update_every
465         1,                  // multiplier
466         1,                  // divisor
467         RRDDIM_INCREMENTAL, // algorithm
468         16,                 // feed entries
469         4,                  // result entries
470         test6_feed,         // feed
471         test6_results,      // results
472         NULL,               // feed2
473         NULL                // results2
474 };
475
476 // --------------------------------------------------------------------------------------------------------------------
477 // test7
478
479 struct feed_values test7_feed[] = {
480         { 500000, 1000 },
481         { 2000000, 2000 },
482         { 2000000, 3000 },
483         { 2000000, 4000 },
484         { 2000000, 5000 },
485         { 2000000, 6000 },
486         { 2000000, 7000 },
487         { 2000000, 8000 },
488         { 2000000, 9000 },
489         { 2000000, 10000 },
490 };
491
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
494 };
495
496 struct test test7 = {
497         "test7",            // name
498         "test incremental values updated in long durations",
499         1,                  // update_every
500         1,                  // multiplier
501         1,                  // divisor
502         RRDDIM_INCREMENTAL, // algorithm
503         10,                 // feed entries
504         18,                 // result entries
505         test7_feed,         // feed
506         test7_results,      // results
507         NULL,               // feed2
508         NULL                // results2
509 };
510
511 // --------------------------------------------------------------------------------------------------------------------
512 // test8
513
514 struct feed_values test8_feed[] = {
515         { 500000, 1000 },
516         { 2000000, 2000 },
517         { 2000000, 3000 },
518         { 2000000, 4000 },
519         { 2000000, 5000 },
520         { 2000000, 6000 },
521 };
522
523 calculated_number test8_results[] = {
524         1250, 2000, 2250, 3000, 3250, 4000, 4250, 5000, 5250, 6000
525 };
526
527 struct test test8 = {
528         "test8",            // name
529         "test absolute values updated in long durations",
530         1,                  // update_every
531         1,                  // multiplier
532         1,                  // divisor
533         RRDDIM_ABSOLUTE,    // algorithm
534         6,                  // feed entries
535         10,                 // result entries
536         test8_feed,         // feed
537         test8_results,      // results
538         NULL,               // feed2
539         NULL                // results2
540 };
541
542 // --------------------------------------------------------------------------------------------------------------------
543 // test9
544
545 struct feed_values test9_feed[] = {
546         { 250000, 1000 },
547         { 250000, 2000 },
548         { 250000, 3000 },
549         { 250000, 4000 },
550         { 250000, 5000 },
551         { 250000, 6000 },
552         { 250000, 7000 },
553         { 250000, 8000 },
554         { 250000, 9000 },
555         { 250000, 10000 },
556         { 250000, 11000 },
557         { 250000, 12000 },
558         { 250000, 13000 },
559         { 250000, 14000 },
560         { 250000, 15000 },
561         { 250000, 16000 },
562 };
563
564 calculated_number test9_results[] = {
565         4000, 8000, 12000, 16000
566 };
567
568 struct test test9 = {
569         "test9",            // name
570         "test absolute values updated within the same second",
571         1,                  // update_every
572         1,                  // multiplier
573         1,                  // divisor
574         RRDDIM_ABSOLUTE,    // algorithm
575         16,                 // feed entries
576         4,                  // result entries
577         test9_feed,         // feed
578         test9_results,      // results
579         NULL,               // feed2
580         NULL                // results2
581 };
582
583 // --------------------------------------------------------------------------------------------------------------------
584 // test10
585
586 struct feed_values test10_feed[] = {
587         { 500000,  1000 },
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 },
597 };
598
599 calculated_number test10_results[] = {
600         1000, 1000, 1000, 1000, 1000, 1000, 1000
601 };
602
603 struct test test10 = {
604         "test10",           // name
605         "test incremental values updated in short and long durations",
606         1,                  // update_every
607         1,                  // multiplier
608         1,                  // divisor
609         RRDDIM_INCREMENTAL, // algorithm
610         10,                 // feed entries
611         7,                  // result entries
612         test10_feed,        // feed
613         test10_results,     // results
614         NULL,               // feed2
615         NULL                // results2
616 };
617
618 // --------------------------------------------------------------------------------------------------------------------
619 // test11
620
621 struct feed_values test11_feed[] = {
622         { 0, 10 },
623         { 1000000, 20 },
624         { 1000000, 30 },
625         { 1000000, 40 },
626         { 1000000, 50 },
627         { 1000000, 60 },
628         { 1000000, 70 },
629         { 1000000, 80 },
630         { 1000000, 90 },
631         { 1000000, 100 },
632 };
633
634 collected_number test11_feed2[] = {
635     10, 20, 30, 40, 50, 60, 70, 80, 90, 100
636 };
637
638 calculated_number test11_results[] = {
639         50, 50, 50, 50, 50, 50, 50, 50, 50
640 };
641
642 calculated_number test11_results2[] = {
643         50, 50, 50, 50, 50, 50, 50, 50, 50
644 };
645
646 struct test test11 = {
647         "test11",           // name
648         "test percentage-of-incremental-row with equal values",
649         1,                  // update_every
650         1,                  // multiplier
651         1,                  // divisor
652         RRDDIM_PCENT_OVER_DIFF_TOTAL,   // algorithm
653         10,                 // feed entries
654         9,                  // result entries
655         test11_feed,        // feed
656         test11_results,     // results
657         test11_feed2,       // feed2
658         test11_results2     // results2
659 };
660
661 // --------------------------------------------------------------------------------------------------------------------
662 // test12
663
664 struct feed_values test12_feed[] = {
665         { 0, 10 },
666         { 1000000, 20 },
667         { 1000000, 30 },
668         { 1000000, 40 },
669         { 1000000, 50 },
670         { 1000000, 60 },
671         { 1000000, 70 },
672         { 1000000, 80 },
673         { 1000000, 90 },
674         { 1000000, 100 },
675 };
676
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
679 };
680
681 calculated_number test12_results[] = {
682         25, 25, 25, 25, 25, 25, 25, 25, 25
683 };
684
685 calculated_number test12_results2[] = {
686         75, 75, 75, 75, 75, 75, 75, 75, 75
687 };
688
689 struct test test12 = {
690         "test12",           // name
691         "test percentage-of-incremental-row with equal values",
692         1,                  // update_every
693         1,                  // multiplier
694         1,                  // divisor
695         RRDDIM_PCENT_OVER_DIFF_TOTAL,   // algorithm
696         10,                 // feed entries
697         9,                  // result entries
698         test12_feed,        // feed
699         test12_results,     // results
700         test12_feed2,       // feed2
701         test12_results2     // results2
702 };
703
704 // --------------------------------------------------------------------------------------------------------------------
705 // test13
706
707 struct feed_values test13_feed[] = {
708         { 500000,  1000 },
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 },
718 };
719
720 calculated_number test13_results[] = {
721         83.3333300, 100, 100, 100, 100, 100, 100
722 };
723
724 struct test test13 = {
725         "test13",           // name
726         "test incremental values updated in short and long durations",
727         1,                  // update_every
728         1,                  // multiplier
729         1,                  // divisor
730         RRDDIM_PCENT_OVER_DIFF_TOTAL,   // algorithm
731         10,                 // feed entries
732         7,                  // result entries
733         test13_feed,        // feed
734         test13_results,     // results
735         NULL,               // feed2
736         NULL                // results2
737 };
738
739 // --------------------------------------------------------------------------------------------------------------------
740 // test14
741
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 },
753 };
754
755 calculated_number test14_results[] = {
756         23.1383300, 21.8515600, 21.8804600, 21.7788000, 22.0112200, 22.4386100, 22.0906100, 21.9150800
757 };
758
759 struct test test14 = {
760         "test14",            // name
761         "issue #981 with real data",
762         30,                 // update_every
763         8,                  // multiplier
764         1000000000,         // divisor
765         RRDDIM_INCREMENTAL, // algorithm
766         10,                 // feed entries
767         8,                  // result entries
768         test14_feed,        // feed
769         test14_results,     // results
770         NULL,               // feed2
771         NULL                // results2
772 };
773
774 struct feed_values test14b_feed[] = {
775         {        0, 0 },
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 },
785 };
786
787 calculated_number test14b_results[] = {
788         1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000
789 };
790
791 struct test test14b = {
792         "test14b",            // name
793         "issue #981 with dummy data",
794         30,                 // update_every
795         1,                  // multiplier
796         1,                  // divisor
797         RRDDIM_INCREMENTAL, // algorithm
798         10,                 // feed entries
799         8,                  // result entries
800         test14b_feed,        // feed
801         test14b_results,     // results
802         NULL,               // feed2
803         NULL                // results2
804 };
805
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 },
817 };
818
819 calculated_number test14c_results[] = {
820         1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000
821 };
822
823 struct test test14c = {
824         "test14c",            // name
825         "issue #981 with dummy data, checking for late start",
826         30,                 // update_every
827         1,                  // multiplier
828         1,                  // divisor
829         RRDDIM_INCREMENTAL, // algorithm
830         10,                 // feed entries
831         9,                  // result entries
832         test14c_feed,        // feed
833         test14c_results,     // results
834         NULL,               // feed2
835         NULL                // results2
836 };
837
838 // --------------------------------------------------------------------------------------------------------------------
839 // test15
840
841 struct feed_values test15_feed[] = {
842         {       0, 1068066388 },
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 },
852 };
853
854 collected_number test15_feed2[] = {
855     178825286, 178825286, 178825286, 178825286, 178825498, 178825498, 179165652, 179202964, 179203282, 179204130
856 };
857
858 calculated_number test15_results[] = {
859         5857.4080000, 5898.4540000, 5891.6590000, 5806.3160000, 5914.2640000, 3202.2630000, 5589.6560000, 5822.5260000, 5911.7520000
860 };
861
862 calculated_number test15_results2[] = {
863         0.0000000, 0.0000000, 0.0024944, 1.6324779, 0.0212777, 2655.1890000, 290.5387000, 5.6733610, 6.5960220
864 };
865
866 struct test test15 = {
867         "test15",           // name
868         "test incremental with 2 dimensions",
869         1,                  // update_every
870         8,                  // multiplier
871         1024,               // divisor
872         RRDDIM_INCREMENTAL, // algorithm
873         10,                 // feed entries
874         9,                  // result entries
875         test15_feed,        // feed
876         test15_results,     // results
877         test15_feed2,       // feed2
878         test15_results2     // results2
879 };
880
881 // --------------------------------------------------------------------------------------------------------------------
882
883 int run_test(struct test *test)
884 {
885     fprintf(stderr, "\nRunning test '%s':\n%s\n", test->name, test->description);
886
887     rrd_memory_mode = RRD_MEMORY_MODE_RAM;
888     rrd_update_every = test->update_every;
889
890     char name[101];
891     snprintfz(name, 100, "unittest-%s", test->name);
892
893     // create the chart
894     RRDSET *st = rrdset_create("netdata", name, name, "netdata", NULL, "Unit Testing", "a value", 1, test->update_every, RRDSET_TYPE_LINE);
895     RRDDIM *rd = rrddim_add(st, "dim1", NULL, test->multiplier, test->divisor, test->algorithm);
896     
897     RRDDIM *rd2 = NULL;
898     if(test->feed2)
899         rd2 = rrddim_add(st, "dim2", NULL, test->multiplier, test->divisor, test->algorithm);
900
901     st->debug = 1;
902
903     // feed it with the test data
904     time_t time_now = 0, time_start = now_realtime_sec();
905     unsigned long c;
906     collected_number last = 0;
907     for(c = 0; c < test->feed_entries; c++) {
908         if(debug_flags) fprintf(stderr, "\n\n");
909
910         if(c) {
911             time_now += test->feed[c].microseconds;
912             fprintf(stderr, "    > %s: feeding position %lu, after %0.3f seconds (%0.3f seconds from start), delta " CALCULATED_NUMBER_FORMAT ", rate " CALCULATED_NUMBER_FORMAT "\n", 
913                 test->name, c+1,
914                 (float)test->feed[c].microseconds / 1000000.0,
915                 (float)time_now / 1000000.0,
916                 ((calculated_number)test->feed[c].value - (calculated_number)last) * (calculated_number)test->multiplier / (calculated_number)test->divisor,
917                 (((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);
918             rrdset_next_usec_unfiltered(st, test->feed[c].microseconds);
919         }
920         else {
921             fprintf(stderr, "    > %s: feeding position %lu\n", test->name, c+1);
922         }
923
924         fprintf(stderr, "       >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd->name, test->feed[c].value);
925         rrddim_set(st, "dim1", test->feed[c].value);
926         last = test->feed[c].value;
927
928         if(rd2) {
929             fprintf(stderr, "       >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd2->name, test->feed2[c]);
930             rrddim_set(st, "dim2", test->feed2[c]);
931         }
932
933         rrdset_done(st);
934
935         // align the first entry to second boundary
936         if(!c) {
937             fprintf(stderr, "    > %s: fixing first collection time to be %llu microseconds to second boundary\n", test->name, test->feed[c].microseconds);
938             rd->last_collected_time.tv_usec = st->last_collected_time.tv_usec = st->last_updated.tv_usec = test->feed[c].microseconds;
939             // time_start = st->last_collected_time.tv_sec;
940         }
941     }
942
943     // check the result
944     int errors = 0;
945
946     if(st->counter != test->result_entries) {
947         fprintf(stderr, "    %s stored %lu entries, but we were expecting %lu, ### E R R O R ###\n", test->name, st->counter, test->result_entries);
948         errors++;
949     }
950
951     unsigned long max = (st->counter < test->result_entries)?st->counter:test->result_entries;
952     for(c = 0 ; c < max ; c++) {
953         calculated_number v = unpack_storage_number(rd->values[c]);
954         calculated_number n = test->results[c];
955         int same = (roundl(v * 10000000.0) == roundl(n * 10000000.0))?1:0;
956         fprintf(stderr, "    %s/%s: checking position %lu (at %lu secs), expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n",
957             test->name, rd->name, c+1,
958             (rrdset_first_entry_t(st) + c * st->update_every) - time_start,
959             n, v, (same)?"OK":"### E R R O R ###");
960
961         if(!same) errors++;
962
963         if(rd2) {
964             v = unpack_storage_number(rd2->values[c]);
965             n = test->results2[c];
966             same = (roundl(v * 10000000.0) == roundl(n * 10000000.0))?1:0;
967             fprintf(stderr, "    %s/%s: checking position %lu (at %lu secs), expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n",
968                 test->name, rd2->name, c+1,
969                 (rrdset_first_entry_t(st) + c * st->update_every) - time_start,
970                 n, v, (same)?"OK":"### E R R O R ###");
971             if(!same) errors++;
972         }
973     }
974
975     return errors;
976 }
977
978 static int test_variable_renames(void) {
979     fprintf(stderr, "Creating chart\n");
980     RRDSET *st = rrdset_create("chart", "ID", NULL, "family", "context", "Unit Testing", "a value", 1, 1, RRDSET_TYPE_LINE);
981     fprintf(stderr, "Created chart with id '%s', name '%s'\n", st->id, st->name);
982
983     fprintf(stderr, "Creating dimension DIM1\n");
984     RRDDIM *rd1 = rrddim_add(st, "DIM1", NULL, 1, 1, RRDDIM_INCREMENTAL);
985     fprintf(stderr, "Created dimension with id '%s', name '%s'\n", rd1->id, rd1->name);
986
987     fprintf(stderr, "Creating dimension DIM2\n");
988     RRDDIM *rd2 = rrddim_add(st, "DIM2", NULL, 1, 1, RRDDIM_INCREMENTAL);
989     fprintf(stderr, "Created dimension with id '%s', name '%s'\n", rd2->id, rd2->name);
990
991     fprintf(stderr, "Renaming chart to CHARTNAME1\n");
992     rrdset_set_name(st, "CHARTNAME1");
993     fprintf(stderr, "Renamed chart with id '%s' to name '%s'\n", st->id, st->name);
994
995     fprintf(stderr, "Renaming chart to CHARTNAME2\n");
996     rrdset_set_name(st, "CHARTNAME2");
997     fprintf(stderr, "Renamed chart with id '%s' to name '%s'\n", st->id, st->name);
998
999     fprintf(stderr, "Renaming dimension DIM1 to DIM1NAME1\n");
1000     rrddim_set_name(st, rd1, "DIM1NAME1");
1001     fprintf(stderr, "Renamed dimension with id '%s' to name '%s'\n", rd1->id, rd1->name);
1002
1003     fprintf(stderr, "Renaming dimension DIM1 to DIM1NAME2\n");
1004     rrddim_set_name(st, rd1, "DIM1NAME2");
1005     fprintf(stderr, "Renamed dimension with id '%s' to name '%s'\n", rd1->id, rd1->name);
1006
1007     fprintf(stderr, "Renaming dimension DIM2 to DIM2NAME1\n");
1008     rrddim_set_name(st, rd2, "DIM2NAME1");
1009     fprintf(stderr, "Renamed dimension with id '%s' to name '%s'\n", rd2->id, rd2->name);
1010
1011     fprintf(stderr, "Renaming dimension DIM2 to DIM2NAME2\n");
1012     rrddim_set_name(st, rd2, "DIM2NAME2");
1013     fprintf(stderr, "Renamed dimension with id '%s' to name '%s'\n", rd2->id, rd2->name);
1014
1015     BUFFER *buf = buffer_create(1);
1016     health_api_v1_chart_variables2json(st, buf);
1017     fprintf(stderr, "%s", buffer_tostring(buf));
1018     buffer_free(buf);
1019     return 1;
1020 }
1021
1022 int run_all_mockup_tests(void)
1023 {
1024     if(!test_variable_renames())
1025         return 1;
1026
1027     if(run_test(&test1))
1028         return 1;
1029
1030     if(run_test(&test2))
1031         return 1;
1032
1033     if(run_test(&test3))
1034         return 1;
1035
1036     if(run_test(&test4))
1037         return 1;
1038
1039     if(run_test(&test5))
1040         return 1;
1041
1042     if(run_test(&test6))
1043         return 1;
1044
1045     if(run_test(&test7))
1046         return 1;
1047
1048     if(run_test(&test8))
1049         return 1;
1050
1051     if(run_test(&test9))
1052         return 1;
1053
1054     if(run_test(&test10))
1055         return 1;
1056
1057     if(run_test(&test11))
1058         return 1;
1059
1060     if(run_test(&test12))
1061         return 1;
1062
1063     if(run_test(&test13))
1064         return 1;
1065
1066     if(run_test(&test14))
1067         return 1;
1068
1069     if(run_test(&test14b))
1070         return 1;
1071
1072     if(run_test(&test14c))
1073         return 1;
1074
1075     if(run_test(&test15))
1076         return 1;
1077
1078
1079
1080     return 0;
1081 }
1082
1083 int unit_test(long delay, long shift)
1084 {
1085     static int repeat = 0;
1086     repeat++;
1087
1088     char name[101];
1089     snprintfz(name, 100, "unittest-%d-%ld-%ld", repeat, delay, shift);
1090
1091     //debug_flags = 0xffffffff;
1092     rrd_memory_mode = RRD_MEMORY_MODE_RAM;
1093     rrd_update_every = 1;
1094
1095     int do_abs = 1;
1096     int do_inc = 1;
1097     int do_abst = 0;
1098     int do_absi = 0;
1099
1100     RRDSET *st = rrdset_create("netdata", name, name, "netdata", NULL, "Unit Testing", "a value", 1, 1, RRDSET_TYPE_LINE);
1101     st->debug = 1;
1102
1103     RRDDIM *rdabs = NULL;
1104     RRDDIM *rdinc = NULL;
1105     RRDDIM *rdabst = NULL;
1106     RRDDIM *rdabsi = NULL;
1107
1108     if(do_abs) rdabs = rrddim_add(st, "absolute", "absolute", 1, 1, RRDDIM_ABSOLUTE);
1109     if(do_inc) rdinc = rrddim_add(st, "incremental", "incremental", 1, 1, RRDDIM_INCREMENTAL);
1110     if(do_abst) rdabst = rrddim_add(st, "percentage-of-absolute-row", "percentage-of-absolute-row", 1, 1, RRDDIM_PCENT_OVER_ROW_TOTAL);
1111     if(do_absi) rdabsi = rrddim_add(st, "percentage-of-incremental-row", "percentage-of-incremental-row", 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
1112
1113     long increment = 1000;
1114     collected_number i = 0;
1115
1116     unsigned long c, dimensions = 0;
1117     RRDDIM *rd;
1118     for(rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
1119
1120     for(c = 0; c < 20 ;c++) {
1121         i += increment;
1122
1123         fprintf(stderr, "\n\nLOOP = %lu, DELAY = %ld, VALUE = " COLLECTED_NUMBER_FORMAT "\n", c, delay, i);
1124         if(c) {
1125             rrdset_next_usec_unfiltered(st, delay);
1126         }
1127         if(do_abs) rrddim_set(st, "absolute", i);
1128         if(do_inc) rrddim_set(st, "incremental", i);
1129         if(do_abst) rrddim_set(st, "percentage-of-absolute-row", i);
1130         if(do_absi) rrddim_set(st, "percentage-of-incremental-row", i);
1131
1132         if(!c) {
1133             now_realtime_timeval(&st->last_collected_time);
1134             st->last_collected_time.tv_usec = shift;
1135         }
1136
1137         // prevent it from deleting the dimensions
1138         for(rd = st->dimensions ; rd ; rd = rd->next)
1139             rd->last_collected_time.tv_sec = st->last_collected_time.tv_sec;
1140
1141         rrdset_done(st);
1142     }
1143
1144     unsigned long oincrement = increment;
1145     increment = increment * st->update_every * 1000000 / delay;
1146     fprintf(stderr, "\n\nORIGINAL INCREMENT: %lu, INCREMENT %ld, DELAY %ld, SHIFT %ld\n", oincrement * 10, increment * 10, delay, shift);
1147
1148     int ret = 0;
1149     storage_number sn;
1150     calculated_number cn, v;
1151     for(c = 0 ; c < st->counter ; c++) {
1152         fprintf(stderr, "\nPOSITION: c = %lu, EXPECTED VALUE %lu\n", c, (oincrement + c * increment + increment * (1000000 - shift) / 1000000 )* 10);
1153
1154         for(rd = st->dimensions ; rd ; rd = rd->next) {
1155             sn = rd->values[c];
1156             cn = unpack_storage_number(sn);
1157             fprintf(stderr, "\t %s " CALCULATED_NUMBER_FORMAT " (PACKED AS " STORAGE_NUMBER_FORMAT ")   ->   ", rd->id, cn, sn);
1158
1159             if(rd == rdabs) v =
1160                 (     oincrement
1161                     // + (increment * (1000000 - shift) / 1000000)
1162                     + (c + 1) * increment
1163                 );
1164
1165             else if(rd == rdinc) v = (c?(increment):(increment * (1000000 - shift) / 1000000));
1166             else if(rd == rdabst) v = oincrement / dimensions / 10;
1167             else if(rd == rdabsi) v = oincrement / dimensions / 10;
1168             else v = 0;
1169
1170             if(v == cn) fprintf(stderr, "passed.\n");
1171             else {
1172                 fprintf(stderr, "ERROR! (expected " CALCULATED_NUMBER_FORMAT ")\n", v);
1173                 ret = 1;
1174             }
1175         }
1176     }
1177
1178     if(ret)
1179         fprintf(stderr, "\n\nUNIT TEST(%ld, %ld) FAILED\n\n", delay, shift);
1180
1181     return ret;
1182 }