@cryptotaxi247 / netdata-1 / commits / 730ca8115

fix percentage calculation when data collection happens multiple times within the same interval

Costa Tsaousis (ktsaou) committed Jul 5, 2016 at 23:26 UTC 730ca81151fb7fec1bdcd1a51590ed27ff055ee8
2 files changed +196 -20
src/rrd.c
+1 -1
@@ -1247,6 +1247,7 @@ unsigned long long rrdset_done(RRDSET *st)
1247 if(likely(stored_entries || !store_this_entry)) {
1248 st->last_updated.tv_sec = st->last_collected_time.tv_sec;
1249 st->last_updated.tv_usec = st->last_collected_time.tv_usec;
1250 + st->last_collected_total = st->collected_total;
1251 }
1252
1253 for( rd = st->dimensions; likely(rd) ; rd = rd->next ) {
@@ -1276,7 +1277,6 @@ unsigned long long rrdset_done(RRDSET *st)
1277 , rd->calculated_value
1278 );
1279 }
1279 - st->last_collected_total = st->collected_total;
1280
1281 // ALL DONE ABOUT THE DATA UPDATE
1282 // --------------------------------------------------------------------
src/unit_test.c
+195 -19
@@ -5,6 +5,7 @@
5 #include <stdlib.h>
6 #include <string.h>
7 #include <sys/resource.h>
8 +#include <math.h>
9
10 #include "common.h"
11 #include "storage_number.h"
@@ -246,7 +247,7 @@ int unit_test_storage()
247
248 struct feed_values {
249 unsigned long long microseconds;
249 - calculated_number value;
250 + collected_number value;
251 };
252
253 struct test {
@@ -262,6 +263,9 @@ struct test {
263 unsigned long result_entries;
264 struct feed_values *feed;
265 calculated_number *results;
266 +
267 + collected_number *feed2;
268 + calculated_number *results2;
269 };
270
271 // --------------------------------------------------------------------------------------------------------------------
@@ -295,7 +299,9 @@ struct test test1 = {
299 10, // feed entries
300 9, // result entries
301 test1_feed, // feed
298 - test1_results // results
302 + test1_results, // results
303 + NULL, // feed2
304 + NULL // results2
305 };
306
307 // --------------------------------------------------------------------------------------------------------------------
@@ -329,7 +335,9 @@ struct test test2 = {
335 10, // feed entries
336 9, // result entries
337 test2_feed, // feed
332 - test2_results // results
338 + test2_results, // results
339 + NULL, // feed2
340 + NULL // results2
341 };
342
343 // --------------------------------------------------------------------------------------------------------------------
@@ -362,7 +370,9 @@ struct test test3 = {
370 10, // feed entries
371 9, // result entries
372 test3_feed, // feed
365 - test3_results // results
373 + test3_results, // results
374 + NULL, // feed2
375 + NULL // results2
376 };
377
378 // --------------------------------------------------------------------------------------------------------------------
@@ -395,7 +405,9 @@ struct test test4 = {
405 10, // feed entries
406 9, // result entries
407 test4_feed, // feed
398 - test4_results // results
408 + test4_results, // results
409 + NULL, // feed2
410 + NULL // results2
411 };
412
413 // --------------------------------------------------------------------------------------------------------------------
@@ -428,7 +440,9 @@ struct test test5 = {
440 10, // feed entries
441 9, // result entries
442 test5_feed, // feed
431 - test5_results // results
443 + test5_results, // results
444 + NULL, // feed2
445 + NULL // results2
446 };
447
448 // --------------------------------------------------------------------------------------------------------------------
@@ -467,7 +481,9 @@ struct test test6 = {
481 16, // feed entries
482 4, // result entries
483 test6_feed, // feed
470 - test6_results // results
484 + test6_results, // results
485 + NULL, // feed2
486 + NULL // results2
487 };
488
489 // --------------------------------------------------------------------------------------------------------------------
@@ -500,7 +516,9 @@ struct test test7 = {
516 10, // feed entries
517 18, // result entries
518 test7_feed, // feed
503 - test7_results // results
519 + test7_results, // results
520 + NULL, // feed2
521 + NULL // results2
522 };
523
524 // --------------------------------------------------------------------------------------------------------------------
@@ -529,7 +547,9 @@ struct test test8 = {
547 6, // feed entries
548 10, // result entries
549 test8_feed, // feed
532 - test8_results // results
550 + test8_results, // results
551 + NULL, // feed2
552 + NULL // results2
553 };
554
555 // --------------------------------------------------------------------------------------------------------------------
@@ -568,7 +588,9 @@ struct test test9 = {
588 16, // feed entries
589 4, // result entries
590 test9_feed, // feed
571 - test9_results // results
591 + test9_results, // results
592 + NULL, // feed2
593 + NULL // results2
594 };
595
596 // --------------------------------------------------------------------------------------------------------------------
@@ -600,8 +622,131 @@ struct test test10 = {
622 RRDDIM_INCREMENTAL, // algorithm
623 10, // feed entries
624 7, // result entries
603 - test10_feed, // feed
604 - test10_results // results
625 + test10_feed, // feed
626 + test10_results, // results
627 + NULL, // feed2
628 + NULL // results2
629 +};
630 +
631 +// --------------------------------------------------------------------------------------------------------------------
632 +// test11
633 +
634 +struct feed_values test11_feed[] = {
635 + { 0, 10 },
636 + { 1000000, 20 },
637 + { 1000000, 30 },
638 + { 1000000, 40 },
639 + { 1000000, 50 },
640 + { 1000000, 60 },
641 + { 1000000, 70 },
642 + { 1000000, 80 },
643 + { 1000000, 90 },
644 + { 1000000, 100 },
645 +};
646 +
647 +collected_number test11_feed2[] = {
648 + 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
649 +};
650 +
651 +calculated_number test11_results[] = {
652 + 50, 50, 50, 50, 50, 50, 50, 50, 50
653 +};
654 +
655 +calculated_number test11_results2[] = {
656 + 50, 50, 50, 50, 50, 50, 50, 50, 50
657 +};
658 +
659 +struct test test11 = {
660 + "test11", // name
661 + "test percentage-of-incremental-row with equal values",
662 + 1, // update_every
663 + 1, // multiplier
664 + 1, // divisor
665 + RRDDIM_PCENT_OVER_DIFF_TOTAL, // algorithm
666 + 10, // feed entries
667 + 9, // result entries
668 + test11_feed, // feed
669 + test11_results, // results
670 + test11_feed2, // feed2
671 + test11_results2 // results2
672 +};
673 +
674 +// --------------------------------------------------------------------------------------------------------------------
675 +// test12
676 +
677 +struct feed_values test12_feed[] = {
678 + { 0, 10 },
679 + { 1000000, 20 },
680 + { 1000000, 30 },
681 + { 1000000, 40 },
682 + { 1000000, 50 },
683 + { 1000000, 60 },
684 + { 1000000, 70 },
685 + { 1000000, 80 },
686 + { 1000000, 90 },
687 + { 1000000, 100 },
688 +};
689 +
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
692 +};
693 +
694 +calculated_number test12_results[] = {
695 + 25, 25, 25, 25, 25, 25, 25, 25, 25
696 +};
697 +
698 +calculated_number test12_results2[] = {
699 + 75, 75, 75, 75, 75, 75, 75, 75, 75
700 +};
701 +
702 +struct test test12 = {
703 + "test12", // name
704 + "test percentage-of-incremental-row with equal values",
705 + 1, // update_every
706 + 1, // multiplier
707 + 1, // divisor
708 + RRDDIM_PCENT_OVER_DIFF_TOTAL, // algorithm
709 + 10, // feed entries
710 + 9, // result entries
711 + test12_feed, // feed
712 + test12_results, // results
713 + test12_feed2, // feed2
714 + test12_results2 // results2
715 +};
716 +
717 +// --------------------------------------------------------------------------------------------------------------------
718 +// test13
719 +
720 +struct feed_values test13_feed[] = {
721 + { 500000, 1000 },
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 },
731 +};
732 +
733 +calculated_number test13_results[] = {
734 + 83.3333300, 100, 100, 100, 100, 100, 100
735 +};
736 +
737 +struct test test13 = {
738 + "test13", // name
739 + "test incremental values updated in short and long durations",
740 + 1, // update_every
741 + 1, // multiplier
742 + 1, // divisor
743 + RRDDIM_PCENT_OVER_DIFF_TOTAL, // algorithm
744 + 10, // feed entries
745 + 7, // result entries
746 + test13_feed, // feed
747 + test13_results, // results
748 + NULL, // feed2
749 + NULL // results2
750 };
751
752 // --------------------------------------------------------------------------------------------------------------------
@@ -618,7 +763,12 @@ int run_test(struct test *test)
763
764 // create the chart
765 RRDSET *st = rrdset_create("netdata", name, name, "netdata", NULL, "Unit Testing", "a value", 1, 1, RRDSET_TYPE_LINE);
621 - RRDDIM *rd = rrddim_add(st, "dimension", NULL, test->multiplier, test->divisor, test->algorithm);
766 + RRDDIM *rd = rrddim_add(st, "dim1", NULL, test->multiplier, test->divisor, test->algorithm);
767 +
768 + RRDDIM *rd2 = NULL;
769 + if(test->feed2)
770 + rd2 = rrddim_add(st, "dim2", NULL, test->multiplier, test->divisor, test->algorithm);
771 +
772 st->debug = 1;
773
774 // feed it with the test data
@@ -627,14 +777,21 @@ int run_test(struct test *test)
777 if(debug_flags) fprintf(stderr, "\n\n");
778
779 if(c) {
630 - 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);
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);
782 }
783 else {
634 - fprintf(stderr, " > %s: feeding position %lu with value " CALCULATED_NUMBER_FORMAT "\n", test->name, c+1, test->feed[c].value);
784 + fprintf(stderr, " > %s: feeding position %lu\n", test->name, c+1);
785 + }
786 +
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);
789 +
790 + if(rd2) {
791 + fprintf(stderr, " >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd2->name, test->feed2[c]);
792 + rrddim_set(st, "dim2", test->feed2[c]);
793 }
794
637 - rrddim_set(st, "dimension", test->feed[c].value);
795 rrdset_done(st);
796
797 // align the first entry to second boundary
@@ -654,9 +811,19 @@ int run_test(struct test *test)
811
812 unsigned long max = (st->counter < test->result_entries)?st->counter:test->result_entries;
813 for(c = 0 ; c < max ; c++) {
657 - calculated_number v = unpack_storage_number(rd->values[c]), n = test->results[c];
658 - 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 ###");
659 - if(v != n) errors++;
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 ###");
818 + if(!same) errors++;
819 +
820 + if(rd2) {
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 ###");
825 + if(!same) errors++;
826 + }
827 }
828
829 return errors;
@@ -694,6 +861,15 @@ int run_all_mockup_tests(void)
861 if(run_test(&test10))
862 return 1;
863
864 + if(run_test(&test11))
865 + return 1;
866 +
867 + if(run_test(&test12))
868 + return 1;
869 +
870 + if(run_test(&test13))
871 + return 1;
872 +
873 return 0;
874 }
875