unit_test.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "common.h"
  3. static int check_number_printing(void) {
  4. struct {
  5. calculated_number n;
  6. const char *correct;
  7. } values[] = {
  8. { .n = 0, .correct = "0" },
  9. { .n = 0.0000001, .correct = "0.0000001" },
  10. { .n = 0.00000009, .correct = "0.0000001" },
  11. { .n = 0.000000001, .correct = "0" },
  12. { .n = 99.99999999999999999, .correct = "100" },
  13. { .n = -99.99999999999999999, .correct = "-100" },
  14. { .n = 123.4567890123456789, .correct = "123.456789" },
  15. { .n = 9999.9999999, .correct = "9999.9999999" },
  16. { .n = -9999.9999999, .correct = "-9999.9999999" },
  17. { .n = 0, .correct = NULL },
  18. };
  19. char netdata[50], system[50];
  20. int i, failed = 0;
  21. for(i = 0; values[i].correct ; i++) {
  22. print_calculated_number(netdata, values[i].n);
  23. snprintfz(system, 49, "%0.12" LONG_DOUBLE_MODIFIER, (LONG_DOUBLE)values[i].n);
  24. int ok = 1;
  25. if(strcmp(netdata, values[i].correct) != 0) {
  26. ok = 0;
  27. failed++;
  28. }
  29. fprintf(stderr, "'%s' (system) printed as '%s' (netdata): %s\n", system, netdata, ok?"OK":"FAILED");
  30. }
  31. if(failed) return 1;
  32. return 0;
  33. }
  34. static int check_rrdcalc_comparisons(void) {
  35. RRDCALC_STATUS a, b;
  36. // make sure calloc() sets the status to UNINITIALIZED
  37. memset(&a, 0, sizeof(RRDCALC_STATUS));
  38. if(a != RRDCALC_STATUS_UNINITIALIZED) {
  39. fprintf(stderr, "%s is not zero.\n", rrdcalc_status2string(RRDCALC_STATUS_UNINITIALIZED));
  40. return 1;
  41. }
  42. a = RRDCALC_STATUS_REMOVED;
  43. b = RRDCALC_STATUS_UNDEFINED;
  44. if(!(a < b)) {
  45. fprintf(stderr, "%s is not less than %s\n", rrdcalc_status2string(a), rrdcalc_status2string(b));
  46. return 1;
  47. }
  48. a = RRDCALC_STATUS_UNDEFINED;
  49. b = RRDCALC_STATUS_UNINITIALIZED;
  50. if(!(a < b)) {
  51. fprintf(stderr, "%s is not less than %s\n", rrdcalc_status2string(a), rrdcalc_status2string(b));
  52. return 1;
  53. }
  54. a = RRDCALC_STATUS_UNINITIALIZED;
  55. b = RRDCALC_STATUS_CLEAR;
  56. if(!(a < b)) {
  57. fprintf(stderr, "%s is not less than %s\n", rrdcalc_status2string(a), rrdcalc_status2string(b));
  58. return 1;
  59. }
  60. a = RRDCALC_STATUS_CLEAR;
  61. b = RRDCALC_STATUS_RAISED;
  62. if(!(a < b)) {
  63. fprintf(stderr, "%s is not less than %s\n", rrdcalc_status2string(a), rrdcalc_status2string(b));
  64. return 1;
  65. }
  66. a = RRDCALC_STATUS_RAISED;
  67. b = RRDCALC_STATUS_WARNING;
  68. if(!(a < b)) {
  69. fprintf(stderr, "%s is not less than %s\n", rrdcalc_status2string(a), rrdcalc_status2string(b));
  70. return 1;
  71. }
  72. a = RRDCALC_STATUS_WARNING;
  73. b = RRDCALC_STATUS_CRITICAL;
  74. if(!(a < b)) {
  75. fprintf(stderr, "%s is not less than %s\n", rrdcalc_status2string(a), rrdcalc_status2string(b));
  76. return 1;
  77. }
  78. fprintf(stderr, "RRDCALC_STATUSes are sortable.\n");
  79. return 0;
  80. }
  81. int check_storage_number(calculated_number n, int debug) {
  82. char buffer[100];
  83. uint32_t flags = SN_DEFAULT_FLAGS;
  84. storage_number s = pack_storage_number(n, flags);
  85. calculated_number d = unpack_storage_number(s);
  86. if(!does_storage_number_exist(s)) {
  87. fprintf(stderr, "Exists flags missing for number " CALCULATED_NUMBER_FORMAT "!\n", n);
  88. return 5;
  89. }
  90. calculated_number ddiff = d - n;
  91. calculated_number dcdiff = ddiff * 100.0 / n;
  92. if(dcdiff < 0) dcdiff = -dcdiff;
  93. size_t len = (size_t)print_calculated_number(buffer, d);
  94. calculated_number p = str2ld(buffer, NULL);
  95. calculated_number pdiff = n - p;
  96. calculated_number pcdiff = pdiff * 100.0 / n;
  97. if(pcdiff < 0) pcdiff = -pcdiff;
  98. if(debug) {
  99. fprintf(stderr,
  100. CALCULATED_NUMBER_FORMAT " original\n"
  101. CALCULATED_NUMBER_FORMAT " packed and unpacked, (stored as 0x%08X, diff " CALCULATED_NUMBER_FORMAT ", " CALCULATED_NUMBER_FORMAT "%%)\n"
  102. "%s printed after unpacked (%zu bytes)\n"
  103. CALCULATED_NUMBER_FORMAT " re-parsed from printed (diff " CALCULATED_NUMBER_FORMAT ", " CALCULATED_NUMBER_FORMAT "%%)\n\n",
  104. n,
  105. d, s, ddiff, dcdiff,
  106. buffer, len,
  107. p, pdiff, pcdiff
  108. );
  109. 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));
  110. if(dcdiff > ACCURACY_LOSS_ACCEPTED_PERCENT)
  111. fprintf(stderr, "WARNING: packing number " CALCULATED_NUMBER_FORMAT " has accuracy loss " CALCULATED_NUMBER_FORMAT " %%\n", n, dcdiff);
  112. if(pcdiff > ACCURACY_LOSS_ACCEPTED_PERCENT)
  113. fprintf(stderr, "WARNING: re-parsing the packed, unpacked and printed number " CALCULATED_NUMBER_FORMAT " has accuracy loss " CALCULATED_NUMBER_FORMAT " %%\n", n, pcdiff);
  114. }
  115. if(len != strlen(buffer)) return 1;
  116. if(dcdiff > ACCURACY_LOSS_ACCEPTED_PERCENT) return 3;
  117. if(pcdiff > ACCURACY_LOSS_ACCEPTED_PERCENT) return 4;
  118. return 0;
  119. }
  120. calculated_number storage_number_min(calculated_number n) {
  121. calculated_number r = 1, last;
  122. do {
  123. last = n;
  124. n /= 2.0;
  125. storage_number t = pack_storage_number(n, SN_DEFAULT_FLAGS);
  126. r = unpack_storage_number(t);
  127. } while(r != 0.0 && r != last);
  128. return last;
  129. }
  130. void benchmark_storage_number(int loop, int multiplier) {
  131. int i, j;
  132. calculated_number n, d;
  133. storage_number s;
  134. unsigned long long user, system, total, mine, their;
  135. calculated_number storage_number_positive_min = unpack_storage_number(STORAGE_NUMBER_POSITIVE_MIN_RAW);
  136. calculated_number storage_number_positive_max = unpack_storage_number(STORAGE_NUMBER_POSITIVE_MAX_RAW);
  137. char buffer[100];
  138. struct rusage now, last;
  139. fprintf(stderr, "\n\nBenchmarking %d numbers, please wait...\n\n", loop);
  140. // ------------------------------------------------------------------------
  141. fprintf(stderr, "SYSTEM LONG DOUBLE SIZE: %zu bytes\n", sizeof(calculated_number));
  142. fprintf(stderr, "NETDATA FLOATING POINT SIZE: %zu bytes\n", sizeof(storage_number));
  143. mine = (calculated_number)sizeof(storage_number) * (calculated_number)loop;
  144. their = (calculated_number)sizeof(calculated_number) * (calculated_number)loop;
  145. if(mine > their) {
  146. fprintf(stderr, "\nNETDATA NEEDS %0.2" LONG_DOUBLE_MODIFIER " TIMES MORE MEMORY. Sorry!\n", (LONG_DOUBLE)(mine / their));
  147. }
  148. else {
  149. fprintf(stderr, "\nNETDATA INTERNAL FLOATING POINT ARITHMETICS NEEDS %0.2" LONG_DOUBLE_MODIFIER " TIMES LESS MEMORY.\n", (LONG_DOUBLE)(their / mine));
  150. }
  151. fprintf(stderr, "\nNETDATA FLOATING POINT\n");
  152. fprintf(stderr, "MIN POSITIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", unpack_storage_number(STORAGE_NUMBER_POSITIVE_MIN_RAW));
  153. fprintf(stderr, "MAX POSITIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", unpack_storage_number(STORAGE_NUMBER_POSITIVE_MAX_RAW));
  154. fprintf(stderr, "MIN NEGATIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", unpack_storage_number(STORAGE_NUMBER_NEGATIVE_MIN_RAW));
  155. fprintf(stderr, "MAX NEGATIVE VALUE " CALCULATED_NUMBER_FORMAT "\n", unpack_storage_number(STORAGE_NUMBER_NEGATIVE_MAX_RAW));
  156. fprintf(stderr, "Maximum accuracy loss accepted: " CALCULATED_NUMBER_FORMAT "%%\n\n\n", (calculated_number)ACCURACY_LOSS_ACCEPTED_PERCENT);
  157. // ------------------------------------------------------------------------
  158. fprintf(stderr, "INTERNAL LONG DOUBLE PRINTING: ");
  159. getrusage(RUSAGE_SELF, &last);
  160. // do the job
  161. for(j = 1; j < 11 ;j++) {
  162. n = storage_number_positive_min * j;
  163. for(i = 0; i < loop ;i++) {
  164. n *= multiplier;
  165. if(n > storage_number_positive_max) n = storage_number_positive_min;
  166. print_calculated_number(buffer, n);
  167. }
  168. }
  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;
  173. mine = total;
  174. fprintf(stderr, "user %0.5" LONG_DOUBLE_MODIFIER", system %0.5" LONG_DOUBLE_MODIFIER ", total %0.5" LONG_DOUBLE_MODIFIER "\n", (LONG_DOUBLE)(user / 1000000.0), (LONG_DOUBLE)(system / 1000000.0), (LONG_DOUBLE)(total / 1000000.0));
  175. // ------------------------------------------------------------------------
  176. fprintf(stderr, "SYSTEM LONG DOUBLE PRINTING: ");
  177. getrusage(RUSAGE_SELF, &last);
  178. // do the job
  179. for(j = 1; j < 11 ;j++) {
  180. n = storage_number_positive_min * j;
  181. for(i = 0; i < loop ;i++) {
  182. n *= multiplier;
  183. if(n > storage_number_positive_max) n = storage_number_positive_min;
  184. snprintfz(buffer, 100, CALCULATED_NUMBER_FORMAT, n);
  185. }
  186. }
  187. getrusage(RUSAGE_SELF, &now);
  188. user = now.ru_utime.tv_sec * 1000000ULL + now.ru_utime.tv_usec - last.ru_utime.tv_sec * 1000000ULL + last.ru_utime.tv_usec;
  189. system = now.ru_stime.tv_sec * 1000000ULL + now.ru_stime.tv_usec - last.ru_stime.tv_sec * 1000000ULL + last.ru_stime.tv_usec;
  190. total = user + system;
  191. their = total;
  192. fprintf(stderr, "user %0.5" LONG_DOUBLE_MODIFIER ", system %0.5" LONG_DOUBLE_MODIFIER ", total %0.5" LONG_DOUBLE_MODIFIER "\n", (LONG_DOUBLE)(user / 1000000.0), (LONG_DOUBLE)(system / 1000000.0), (LONG_DOUBLE)(total / 1000000.0));
  193. if(mine > total) {
  194. fprintf(stderr, "NETDATA CODE IS SLOWER %0.2" LONG_DOUBLE_MODIFIER " %%\n", (LONG_DOUBLE)(mine * 100.0 / their - 100.0));
  195. }
  196. else {
  197. fprintf(stderr, "NETDATA CODE IS F A S T E R %0.2" LONG_DOUBLE_MODIFIER " %%\n", (LONG_DOUBLE)(their * 100.0 / mine - 100.0));
  198. }
  199. // ------------------------------------------------------------------------
  200. fprintf(stderr, "\nINTERNAL LONG DOUBLE PRINTING WITH PACK / UNPACK: ");
  201. getrusage(RUSAGE_SELF, &last);
  202. // do the job
  203. for(j = 1; j < 11 ;j++) {
  204. n = storage_number_positive_min * j;
  205. for(i = 0; i < loop ;i++) {
  206. n *= multiplier;
  207. if(n > storage_number_positive_max) n = storage_number_positive_min;
  208. s = pack_storage_number(n, SN_DEFAULT_FLAGS);
  209. d = unpack_storage_number(s);
  210. print_calculated_number(buffer, d);
  211. }
  212. }
  213. getrusage(RUSAGE_SELF, &now);
  214. user = now.ru_utime.tv_sec * 1000000ULL + now.ru_utime.tv_usec - last.ru_utime.tv_sec * 1000000ULL + last.ru_utime.tv_usec;
  215. system = now.ru_stime.tv_sec * 1000000ULL + now.ru_stime.tv_usec - last.ru_stime.tv_sec * 1000000ULL + last.ru_stime.tv_usec;
  216. total = user + system;
  217. mine = total;
  218. fprintf(stderr, "user %0.5" LONG_DOUBLE_MODIFIER ", system %0.5" LONG_DOUBLE_MODIFIER ", total %0.5" LONG_DOUBLE_MODIFIER "\n", (LONG_DOUBLE)(user / 1000000.0), (LONG_DOUBLE)(system / 1000000.0), (LONG_DOUBLE)(total / 1000000.0));
  219. if(mine > their) {
  220. fprintf(stderr, "WITH PACKING UNPACKING NETDATA CODE IS SLOWER %0.2" LONG_DOUBLE_MODIFIER " %%\n", (LONG_DOUBLE)(mine * 100.0 / their - 100.0));
  221. }
  222. else {
  223. fprintf(stderr, "EVEN WITH PACKING AND UNPACKING, NETDATA CODE IS F A S T E R %0.2" LONG_DOUBLE_MODIFIER " %%\n", (LONG_DOUBLE)(their * 100.0 / mine - 100.0));
  224. }
  225. // ------------------------------------------------------------------------
  226. }
  227. static int check_storage_number_exists() {
  228. uint32_t flags = SN_DEFAULT_FLAGS;
  229. calculated_number n = 0.0;
  230. storage_number s = pack_storage_number(n, flags);
  231. calculated_number d = unpack_storage_number(s);
  232. if(n != d) {
  233. fprintf(stderr, "Wrong number returned. Expected " CALCULATED_NUMBER_FORMAT ", returned " CALCULATED_NUMBER_FORMAT "!\n", n, d);
  234. return 1;
  235. }
  236. return 0;
  237. }
  238. int unit_test_storage() {
  239. if(check_storage_number_exists()) return 0;
  240. calculated_number storage_number_positive_min = unpack_storage_number(STORAGE_NUMBER_POSITIVE_MIN_RAW);
  241. calculated_number storage_number_negative_max = unpack_storage_number(STORAGE_NUMBER_NEGATIVE_MAX_RAW);
  242. calculated_number c, a = 0;
  243. int i, j, g, r = 0;
  244. for(g = -1; g <= 1 ; g++) {
  245. a = 0;
  246. if(!g) continue;
  247. for(j = 0; j < 9 ;j++) {
  248. a += 0.0000001;
  249. c = a * g;
  250. for(i = 0; i < 21 ;i++, c *= 10) {
  251. if(c > 0 && c < storage_number_positive_min) continue;
  252. if(c < 0 && c > storage_number_negative_max) continue;
  253. if(check_storage_number(c, 1)) return 1;
  254. }
  255. }
  256. }
  257. // if(check_storage_number(858993459.1234567, 1)) return 1;
  258. benchmark_storage_number(1000000, 2);
  259. return r;
  260. }
  261. int unit_test_str2ld() {
  262. char *values[] = {
  263. "1.2345678", "-35.6", "0.00123", "23842384234234.2", ".1", "1.2e-10",
  264. "hello", "1wrong", "nan", "inf", NULL
  265. };
  266. int i;
  267. for(i = 0; values[i] ; i++) {
  268. char *e_mine = "hello", *e_sys = "world";
  269. LONG_DOUBLE mine = str2ld(values[i], &e_mine);
  270. LONG_DOUBLE sys = strtold(values[i], &e_sys);
  271. if(isnan(mine)) {
  272. if(!isnan(sys)) {
  273. fprintf(stderr, "Value '%s' is parsed as %" LONG_DOUBLE_MODIFIER ", but system believes it is %" LONG_DOUBLE_MODIFIER ".\n", values[i], mine, sys);
  274. return -1;
  275. }
  276. }
  277. else if(isinf(mine)) {
  278. if(!isinf(sys)) {
  279. fprintf(stderr, "Value '%s' is parsed as %" LONG_DOUBLE_MODIFIER ", but system believes it is %" LONG_DOUBLE_MODIFIER ".\n", values[i], mine, sys);
  280. return -1;
  281. }
  282. }
  283. else if(mine != sys && ABS(mine-sys) > 0.000001) {
  284. fprintf(stderr, "Value '%s' is parsed as %" LONG_DOUBLE_MODIFIER ", but system believes it is %" LONG_DOUBLE_MODIFIER ", delta %" LONG_DOUBLE_MODIFIER ".\n", values[i], mine, sys, sys-mine);
  285. return -1;
  286. }
  287. if(e_mine != e_sys) {
  288. fprintf(stderr, "Value '%s' is parsed correctly, but endptr is not right\n", values[i]);
  289. return -1;
  290. }
  291. fprintf(stderr, "str2ld() parsed value '%s' exactly the same way with strtold(), returned %" LONG_DOUBLE_MODIFIER " vs %" LONG_DOUBLE_MODIFIER "\n", values[i], mine, sys);
  292. }
  293. return 0;
  294. }
  295. int unit_test_buffer() {
  296. BUFFER *wb = buffer_create(1);
  297. char string[2048 + 1];
  298. char final[9000 + 1];
  299. int i;
  300. for(i = 0; i < 2048; i++)
  301. string[i] = (char)((i % 24) + 'a');
  302. string[2048] = '\0';
  303. const char *fmt = "string1: %s\nstring2: %s\nstring3: %s\nstring4: %s";
  304. buffer_sprintf(wb, fmt, string, string, string, string);
  305. snprintfz(final, 9000, fmt, string, string, string, string);
  306. const char *s = buffer_tostring(wb);
  307. if(buffer_strlen(wb) != strlen(final) || strcmp(s, final) != 0) {
  308. fprintf(stderr, "\nbuffer_sprintf() is faulty.\n");
  309. fprintf(stderr, "\nstring : %s (length %zu)\n", string, strlen(string));
  310. fprintf(stderr, "\nbuffer : %s (length %zu)\n", s, buffer_strlen(wb));
  311. fprintf(stderr, "\nexpected: %s (length %zu)\n", final, strlen(final));
  312. buffer_free(wb);
  313. return -1;
  314. }
  315. fprintf(stderr, "buffer_sprintf() works as expected.\n");
  316. buffer_free(wb);
  317. return 0;
  318. }
  319. int unit_test_static_threads() {
  320. struct netdata_static_thread *static_threads = static_threads_get();
  321. /*
  322. * make sure enough static threads have been registered
  323. */
  324. if (!static_threads) {
  325. fprintf(stderr, "empty static_threads array\n");
  326. return 1;
  327. }
  328. int n;
  329. for (n = 0; static_threads[n].start_routine != NULL; n++) {}
  330. if (n < 2) {
  331. fprintf(stderr, "only %d static threads registered", n);
  332. freez(static_threads);
  333. return 1;
  334. }
  335. /*
  336. * verify that each thread's start routine is unique.
  337. */
  338. for (int i = 0; i != n - 1; i++) {
  339. for (int j = i + 1; j != n; j++) {
  340. if (static_threads[i].start_routine != static_threads[j].start_routine)
  341. continue;
  342. fprintf(stderr, "Found duplicate threads with name: %s\n", static_threads[i].name);
  343. freez(static_threads);
  344. return 1;
  345. }
  346. }
  347. freez(static_threads);
  348. return 0;
  349. }
  350. // --------------------------------------------------------------------------------------------------------------------
  351. struct feed_values {
  352. unsigned long long microseconds;
  353. collected_number value;
  354. };
  355. struct test {
  356. char name[100];
  357. char description[1024];
  358. int update_every;
  359. unsigned long long multiplier;
  360. unsigned long long divisor;
  361. RRD_ALGORITHM algorithm;
  362. unsigned long feed_entries;
  363. unsigned long result_entries;
  364. struct feed_values *feed;
  365. calculated_number *results;
  366. collected_number *feed2;
  367. calculated_number *results2;
  368. };
  369. // --------------------------------------------------------------------------------------------------------------------
  370. // test1
  371. // test absolute values stored
  372. struct feed_values test1_feed[] = {
  373. { 0, 10 },
  374. { 1000000, 20 },
  375. { 1000000, 30 },
  376. { 1000000, 40 },
  377. { 1000000, 50 },
  378. { 1000000, 60 },
  379. { 1000000, 70 },
  380. { 1000000, 80 },
  381. { 1000000, 90 },
  382. { 1000000, 100 },
  383. };
  384. calculated_number test1_results[] = {
  385. 20, 30, 40, 50, 60, 70, 80, 90, 100
  386. };
  387. struct test test1 = {
  388. "test1", // name
  389. "test absolute values stored at exactly second boundaries",
  390. 1, // update_every
  391. 1, // multiplier
  392. 1, // divisor
  393. RRD_ALGORITHM_ABSOLUTE, // algorithm
  394. 10, // feed entries
  395. 9, // result entries
  396. test1_feed, // feed
  397. test1_results, // results
  398. NULL, // feed2
  399. NULL // results2
  400. };
  401. // --------------------------------------------------------------------------------------------------------------------
  402. // test2
  403. // test absolute values stored in the middle of second boundaries
  404. struct feed_values test2_feed[] = {
  405. { 500000, 10 },
  406. { 1000000, 20 },
  407. { 1000000, 30 },
  408. { 1000000, 40 },
  409. { 1000000, 50 },
  410. { 1000000, 60 },
  411. { 1000000, 70 },
  412. { 1000000, 80 },
  413. { 1000000, 90 },
  414. { 1000000, 100 },
  415. };
  416. calculated_number test2_results[] = {
  417. 20, 30, 40, 50, 60, 70, 80, 90, 100
  418. };
  419. struct test test2 = {
  420. "test2", // name
  421. "test absolute values stored in the middle of second boundaries",
  422. 1, // update_every
  423. 1, // multiplier
  424. 1, // divisor
  425. RRD_ALGORITHM_ABSOLUTE, // algorithm
  426. 10, // feed entries
  427. 9, // result entries
  428. test2_feed, // feed
  429. test2_results, // results
  430. NULL, // feed2
  431. NULL // results2
  432. };
  433. // --------------------------------------------------------------------------------------------------------------------
  434. // test3
  435. struct feed_values test3_feed[] = {
  436. { 0, 10 },
  437. { 1000000, 20 },
  438. { 1000000, 30 },
  439. { 1000000, 40 },
  440. { 1000000, 50 },
  441. { 1000000, 60 },
  442. { 1000000, 70 },
  443. { 1000000, 80 },
  444. { 1000000, 90 },
  445. { 1000000, 100 },
  446. };
  447. calculated_number test3_results[] = {
  448. 10, 10, 10, 10, 10, 10, 10, 10, 10
  449. };
  450. struct test test3 = {
  451. "test3", // name
  452. "test incremental values stored at exactly second boundaries",
  453. 1, // update_every
  454. 1, // multiplier
  455. 1, // divisor
  456. RRD_ALGORITHM_INCREMENTAL, // algorithm
  457. 10, // feed entries
  458. 9, // result entries
  459. test3_feed, // feed
  460. test3_results, // results
  461. NULL, // feed2
  462. NULL // results2
  463. };
  464. // --------------------------------------------------------------------------------------------------------------------
  465. // test4
  466. struct feed_values test4_feed[] = {
  467. { 500000, 10 },
  468. { 1000000, 20 },
  469. { 1000000, 30 },
  470. { 1000000, 40 },
  471. { 1000000, 50 },
  472. { 1000000, 60 },
  473. { 1000000, 70 },
  474. { 1000000, 80 },
  475. { 1000000, 90 },
  476. { 1000000, 100 },
  477. };
  478. calculated_number test4_results[] = {
  479. 10, 10, 10, 10, 10, 10, 10, 10, 10
  480. };
  481. struct test test4 = {
  482. "test4", // name
  483. "test incremental values stored in the middle of second boundaries",
  484. 1, // update_every
  485. 1, // multiplier
  486. 1, // divisor
  487. RRD_ALGORITHM_INCREMENTAL, // algorithm
  488. 10, // feed entries
  489. 9, // result entries
  490. test4_feed, // feed
  491. test4_results, // results
  492. NULL, // feed2
  493. NULL // results2
  494. };
  495. // --------------------------------------------------------------------------------------------------------------------
  496. // test5 - 32 bit overflows
  497. struct feed_values test5_feed[] = {
  498. { 0, 0x00000000FFFFFFFFULL / 15 * 0 },
  499. { 1000000, 0x00000000FFFFFFFFULL / 15 * 7 },
  500. { 1000000, 0x00000000FFFFFFFFULL / 15 * 14 },
  501. { 1000000, 0x00000000FFFFFFFFULL / 15 * 0 },
  502. { 1000000, 0x00000000FFFFFFFFULL / 15 * 7 },
  503. { 1000000, 0x00000000FFFFFFFFULL / 15 * 14 },
  504. { 1000000, 0x00000000FFFFFFFFULL / 15 * 0 },
  505. { 1000000, 0x00000000FFFFFFFFULL / 15 * 7 },
  506. { 1000000, 0x00000000FFFFFFFFULL / 15 * 14 },
  507. { 1000000, 0x00000000FFFFFFFFULL / 15 * 0 },
  508. };
  509. calculated_number test5_results[] = {
  510. 0x00000000FFFFFFFFULL / 15 * 7,
  511. 0x00000000FFFFFFFFULL / 15 * 7,
  512. 0x00000000FFFFFFFFULL / 15,
  513. 0x00000000FFFFFFFFULL / 15 * 7,
  514. 0x00000000FFFFFFFFULL / 15 * 7,
  515. 0x00000000FFFFFFFFULL / 15,
  516. 0x00000000FFFFFFFFULL / 15 * 7,
  517. 0x00000000FFFFFFFFULL / 15 * 7,
  518. 0x00000000FFFFFFFFULL / 15,
  519. };
  520. struct test test5 = {
  521. "test5", // name
  522. "test 32-bit incremental values overflow",
  523. 1, // update_every
  524. 1, // multiplier
  525. 1, // divisor
  526. RRD_ALGORITHM_INCREMENTAL, // algorithm
  527. 10, // feed entries
  528. 9, // result entries
  529. test5_feed, // feed
  530. test5_results, // results
  531. NULL, // feed2
  532. NULL // results2
  533. };
  534. // --------------------------------------------------------------------------------------------------------------------
  535. // test5b - 64 bit overflows
  536. struct feed_values test5b_feed[] = {
  537. { 0, 0xFFFFFFFFFFFFFFFFULL / 15 * 0 },
  538. { 1000000, 0xFFFFFFFFFFFFFFFFULL / 15 * 7 },
  539. { 1000000, 0xFFFFFFFFFFFFFFFFULL / 15 * 14 },
  540. { 1000000, 0xFFFFFFFFFFFFFFFFULL / 15 * 0 },
  541. { 1000000, 0xFFFFFFFFFFFFFFFFULL / 15 * 7 },
  542. { 1000000, 0xFFFFFFFFFFFFFFFFULL / 15 * 14 },
  543. { 1000000, 0xFFFFFFFFFFFFFFFFULL / 15 * 0 },
  544. { 1000000, 0xFFFFFFFFFFFFFFFFULL / 15 * 7 },
  545. { 1000000, 0xFFFFFFFFFFFFFFFFULL / 15 * 14 },
  546. { 1000000, 0xFFFFFFFFFFFFFFFFULL / 15 * 0 },
  547. };
  548. calculated_number test5b_results[] = {
  549. 0xFFFFFFFFFFFFFFFFULL / 15 * 7,
  550. 0xFFFFFFFFFFFFFFFFULL / 15 * 7,
  551. 0xFFFFFFFFFFFFFFFFULL / 15,
  552. 0xFFFFFFFFFFFFFFFFULL / 15 * 7,
  553. 0xFFFFFFFFFFFFFFFFULL / 15 * 7,
  554. 0xFFFFFFFFFFFFFFFFULL / 15,
  555. 0xFFFFFFFFFFFFFFFFULL / 15 * 7,
  556. 0xFFFFFFFFFFFFFFFFULL / 15 * 7,
  557. 0xFFFFFFFFFFFFFFFFULL / 15,
  558. };
  559. struct test test5b = {
  560. "test5b", // name
  561. "test 64-bit incremental values overflow",
  562. 1, // update_every
  563. 1, // multiplier
  564. 1, // divisor
  565. RRD_ALGORITHM_INCREMENTAL, // algorithm
  566. 10, // feed entries
  567. 9, // result entries
  568. test5b_feed, // feed
  569. test5b_results, // results
  570. NULL, // feed2
  571. NULL // results2
  572. };
  573. // --------------------------------------------------------------------------------------------------------------------
  574. // test6
  575. struct feed_values test6_feed[] = {
  576. { 250000, 1000 },
  577. { 250000, 2000 },
  578. { 250000, 3000 },
  579. { 250000, 4000 },
  580. { 250000, 5000 },
  581. { 250000, 6000 },
  582. { 250000, 7000 },
  583. { 250000, 8000 },
  584. { 250000, 9000 },
  585. { 250000, 10000 },
  586. { 250000, 11000 },
  587. { 250000, 12000 },
  588. { 250000, 13000 },
  589. { 250000, 14000 },
  590. { 250000, 15000 },
  591. { 250000, 16000 },
  592. };
  593. calculated_number test6_results[] = {
  594. 4000, 4000, 4000, 4000
  595. };
  596. struct test test6 = {
  597. "test6", // name
  598. "test incremental values updated within the same second",
  599. 1, // update_every
  600. 1, // multiplier
  601. 1, // divisor
  602. RRD_ALGORITHM_INCREMENTAL, // algorithm
  603. 16, // feed entries
  604. 4, // result entries
  605. test6_feed, // feed
  606. test6_results, // results
  607. NULL, // feed2
  608. NULL // results2
  609. };
  610. // --------------------------------------------------------------------------------------------------------------------
  611. // test7
  612. struct feed_values test7_feed[] = {
  613. { 500000, 1000 },
  614. { 2000000, 2000 },
  615. { 2000000, 3000 },
  616. { 2000000, 4000 },
  617. { 2000000, 5000 },
  618. { 2000000, 6000 },
  619. { 2000000, 7000 },
  620. { 2000000, 8000 },
  621. { 2000000, 9000 },
  622. { 2000000, 10000 },
  623. };
  624. calculated_number test7_results[] = {
  625. 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500, 500
  626. };
  627. struct test test7 = {
  628. "test7", // name
  629. "test incremental values updated in long durations",
  630. 1, // update_every
  631. 1, // multiplier
  632. 1, // divisor
  633. RRD_ALGORITHM_INCREMENTAL, // algorithm
  634. 10, // feed entries
  635. 18, // result entries
  636. test7_feed, // feed
  637. test7_results, // results
  638. NULL, // feed2
  639. NULL // results2
  640. };
  641. // --------------------------------------------------------------------------------------------------------------------
  642. // test8
  643. struct feed_values test8_feed[] = {
  644. { 500000, 1000 },
  645. { 2000000, 2000 },
  646. { 2000000, 3000 },
  647. { 2000000, 4000 },
  648. { 2000000, 5000 },
  649. { 2000000, 6000 },
  650. };
  651. calculated_number test8_results[] = {
  652. 1250, 2000, 2250, 3000, 3250, 4000, 4250, 5000, 5250, 6000
  653. };
  654. struct test test8 = {
  655. "test8", // name
  656. "test absolute values updated in long durations",
  657. 1, // update_every
  658. 1, // multiplier
  659. 1, // divisor
  660. RRD_ALGORITHM_ABSOLUTE, // algorithm
  661. 6, // feed entries
  662. 10, // result entries
  663. test8_feed, // feed
  664. test8_results, // results
  665. NULL, // feed2
  666. NULL // results2
  667. };
  668. // --------------------------------------------------------------------------------------------------------------------
  669. // test9
  670. struct feed_values test9_feed[] = {
  671. { 250000, 1000 },
  672. { 250000, 2000 },
  673. { 250000, 3000 },
  674. { 250000, 4000 },
  675. { 250000, 5000 },
  676. { 250000, 6000 },
  677. { 250000, 7000 },
  678. { 250000, 8000 },
  679. { 250000, 9000 },
  680. { 250000, 10000 },
  681. { 250000, 11000 },
  682. { 250000, 12000 },
  683. { 250000, 13000 },
  684. { 250000, 14000 },
  685. { 250000, 15000 },
  686. { 250000, 16000 },
  687. };
  688. calculated_number test9_results[] = {
  689. 4000, 8000, 12000, 16000
  690. };
  691. struct test test9 = {
  692. "test9", // name
  693. "test absolute values updated within the same second",
  694. 1, // update_every
  695. 1, // multiplier
  696. 1, // divisor
  697. RRD_ALGORITHM_ABSOLUTE, // algorithm
  698. 16, // feed entries
  699. 4, // result entries
  700. test9_feed, // feed
  701. test9_results, // results
  702. NULL, // feed2
  703. NULL // results2
  704. };
  705. // --------------------------------------------------------------------------------------------------------------------
  706. // test10
  707. struct feed_values test10_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. calculated_number test10_results[] = {
  720. 1000, 1000, 1000, 1000, 1000, 1000, 1000
  721. };
  722. struct test test10 = {
  723. "test10", // name
  724. "test incremental values updated in short and long durations",
  725. 1, // update_every
  726. 1, // multiplier
  727. 1, // divisor
  728. RRD_ALGORITHM_INCREMENTAL, // algorithm
  729. 10, // feed entries
  730. 7, // result entries
  731. test10_feed, // feed
  732. test10_results, // results
  733. NULL, // feed2
  734. NULL // results2
  735. };
  736. // --------------------------------------------------------------------------------------------------------------------
  737. // test11
  738. struct feed_values test11_feed[] = {
  739. { 0, 10 },
  740. { 1000000, 20 },
  741. { 1000000, 30 },
  742. { 1000000, 40 },
  743. { 1000000, 50 },
  744. { 1000000, 60 },
  745. { 1000000, 70 },
  746. { 1000000, 80 },
  747. { 1000000, 90 },
  748. { 1000000, 100 },
  749. };
  750. collected_number test11_feed2[] = {
  751. 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
  752. };
  753. calculated_number test11_results[] = {
  754. 50, 50, 50, 50, 50, 50, 50, 50, 50
  755. };
  756. calculated_number test11_results2[] = {
  757. 50, 50, 50, 50, 50, 50, 50, 50, 50
  758. };
  759. struct test test11 = {
  760. "test11", // name
  761. "test percentage-of-incremental-row with equal values",
  762. 1, // update_every
  763. 1, // multiplier
  764. 1, // divisor
  765. RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL, // algorithm
  766. 10, // feed entries
  767. 9, // result entries
  768. test11_feed, // feed
  769. test11_results, // results
  770. test11_feed2, // feed2
  771. test11_results2 // results2
  772. };
  773. // --------------------------------------------------------------------------------------------------------------------
  774. // test12
  775. struct feed_values test12_feed[] = {
  776. { 0, 10 },
  777. { 1000000, 20 },
  778. { 1000000, 30 },
  779. { 1000000, 40 },
  780. { 1000000, 50 },
  781. { 1000000, 60 },
  782. { 1000000, 70 },
  783. { 1000000, 80 },
  784. { 1000000, 90 },
  785. { 1000000, 100 },
  786. };
  787. collected_number test12_feed2[] = {
  788. 10*3, 20*3, 30*3, 40*3, 50*3, 60*3, 70*3, 80*3, 90*3, 100*3
  789. };
  790. calculated_number test12_results[] = {
  791. 25, 25, 25, 25, 25, 25, 25, 25, 25
  792. };
  793. calculated_number test12_results2[] = {
  794. 75, 75, 75, 75, 75, 75, 75, 75, 75
  795. };
  796. struct test test12 = {
  797. "test12", // name
  798. "test percentage-of-incremental-row with equal values",
  799. 1, // update_every
  800. 1, // multiplier
  801. 1, // divisor
  802. RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL, // algorithm
  803. 10, // feed entries
  804. 9, // result entries
  805. test12_feed, // feed
  806. test12_results, // results
  807. test12_feed2, // feed2
  808. test12_results2 // results2
  809. };
  810. // --------------------------------------------------------------------------------------------------------------------
  811. // test13
  812. struct feed_values test13_feed[] = {
  813. { 500000, 1000 },
  814. { 600000, 1000 + 600 },
  815. { 200000, 1600 + 200 },
  816. { 1000000, 1800 + 1000 },
  817. { 200000, 2800 + 200 },
  818. { 2000000, 3000 + 2000 },
  819. { 600000, 5000 + 600 },
  820. { 400000, 5600 + 400 },
  821. { 900000, 6000 + 900 },
  822. { 1000000, 6900 + 1000 },
  823. };
  824. calculated_number test13_results[] = {
  825. 83.3333300, 100, 100, 100, 100, 100, 100
  826. };
  827. struct test test13 = {
  828. "test13", // name
  829. "test incremental values updated in short and long durations",
  830. 1, // update_every
  831. 1, // multiplier
  832. 1, // divisor
  833. RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL, // algorithm
  834. 10, // feed entries
  835. 7, // result entries
  836. test13_feed, // feed
  837. test13_results, // results
  838. NULL, // feed2
  839. NULL // results2
  840. };
  841. // --------------------------------------------------------------------------------------------------------------------
  842. // test14
  843. struct feed_values test14_feed[] = {
  844. { 0, 0x015397dc42151c41ULL },
  845. { 13573000, 0x015397e612e3ff5dULL },
  846. { 29969000, 0x015397f905ecdaa8ULL },
  847. { 29958000, 0x0153980c2a6cb5e4ULL },
  848. { 30054000, 0x0153981f4032fb83ULL },
  849. { 34952000, 0x015398355efadaccULL },
  850. { 25046000, 0x01539845ba4b09f8ULL },
  851. { 29947000, 0x0153985948bf381dULL },
  852. { 30054000, 0x0153986c5b9c27e2ULL },
  853. { 29942000, 0x0153987f888982d0ULL },
  854. };
  855. calculated_number test14_results[] = {
  856. 23.1383300, 21.8515600, 21.8804600, 21.7788000, 22.0112200, 22.4386100, 22.0906100, 21.9150800
  857. };
  858. struct test test14 = {
  859. "test14", // name
  860. "issue #981 with real data",
  861. 30, // update_every
  862. 8, // multiplier
  863. 1000000000, // divisor
  864. RRD_ALGORITHM_INCREMENTAL, // algorithm
  865. 10, // feed entries
  866. 8, // result entries
  867. test14_feed, // feed
  868. test14_results, // results
  869. NULL, // feed2
  870. NULL // results2
  871. };
  872. struct feed_values test14b_feed[] = {
  873. { 0, 0 },
  874. { 13573000, 13573000 },
  875. { 29969000, 13573000 + 29969000 },
  876. { 29958000, 13573000 + 29969000 + 29958000 },
  877. { 30054000, 13573000 + 29969000 + 29958000 + 30054000 },
  878. { 34952000, 13573000 + 29969000 + 29958000 + 30054000 + 34952000 },
  879. { 25046000, 13573000 + 29969000 + 29958000 + 30054000 + 34952000 + 25046000 },
  880. { 29947000, 13573000 + 29969000 + 29958000 + 30054000 + 34952000 + 25046000 + 29947000 },
  881. { 30054000, 13573000 + 29969000 + 29958000 + 30054000 + 34952000 + 25046000 + 29947000 + 30054000 },
  882. { 29942000, 13573000 + 29969000 + 29958000 + 30054000 + 34952000 + 25046000 + 29947000 + 30054000 + 29942000 },
  883. };
  884. calculated_number test14b_results[] = {
  885. 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000
  886. };
  887. struct test test14b = {
  888. "test14b", // name
  889. "issue #981 with dummy data",
  890. 30, // update_every
  891. 1, // multiplier
  892. 1, // divisor
  893. RRD_ALGORITHM_INCREMENTAL, // algorithm
  894. 10, // feed entries
  895. 8, // result entries
  896. test14b_feed, // feed
  897. test14b_results, // results
  898. NULL, // feed2
  899. NULL // results2
  900. };
  901. struct feed_values test14c_feed[] = {
  902. { 29000000, 29000000 },
  903. { 1000000, 29000000 + 1000000 },
  904. { 30000000, 29000000 + 1000000 + 30000000 },
  905. { 30000000, 29000000 + 1000000 + 30000000 + 30000000 },
  906. { 30000000, 29000000 + 1000000 + 30000000 + 30000000 + 30000000 },
  907. { 30000000, 29000000 + 1000000 + 30000000 + 30000000 + 30000000 + 30000000 },
  908. { 30000000, 29000000 + 1000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 },
  909. { 30000000, 29000000 + 1000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 },
  910. { 30000000, 29000000 + 1000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 },
  911. { 30000000, 29000000 + 1000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 + 30000000 },
  912. };
  913. calculated_number test14c_results[] = {
  914. 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000, 1000000
  915. };
  916. struct test test14c = {
  917. "test14c", // name
  918. "issue #981 with dummy data, checking for late start",
  919. 30, // update_every
  920. 1, // multiplier
  921. 1, // divisor
  922. RRD_ALGORITHM_INCREMENTAL, // algorithm
  923. 10, // feed entries
  924. 9, // result entries
  925. test14c_feed, // feed
  926. test14c_results, // results
  927. NULL, // feed2
  928. NULL // results2
  929. };
  930. // --------------------------------------------------------------------------------------------------------------------
  931. // test15
  932. struct feed_values test15_feed[] = {
  933. { 0, 1068066388 },
  934. { 1008752, 1068822698 },
  935. { 993809, 1069573072 },
  936. { 995911, 1070324135 },
  937. { 1014562, 1071078166 },
  938. { 994684, 1071831349 },
  939. { 993128, 1072235739 },
  940. { 1010332, 1072958871 },
  941. { 1003394, 1073707019 },
  942. { 995201, 1074460255 },
  943. };
  944. collected_number test15_feed2[] = {
  945. 178825286, 178825286, 178825286, 178825286, 178825498, 178825498, 179165652, 179202964, 179203282, 179204130
  946. };
  947. calculated_number test15_results[] = {
  948. 5857.4080000, 5898.4540000, 5891.6590000, 5806.3160000, 5914.2640000, 3202.2630000, 5589.6560000, 5822.5260000, 5911.7520000
  949. };
  950. calculated_number test15_results2[] = {
  951. 0.0000000, 0.0000000, 0.0024944, 1.6324779, 0.0212777, 2655.1890000, 290.5387000, 5.6733610, 6.5960220
  952. };
  953. struct test test15 = {
  954. "test15", // name
  955. "test incremental with 2 dimensions",
  956. 1, // update_every
  957. 8, // multiplier
  958. 1024, // divisor
  959. RRD_ALGORITHM_INCREMENTAL, // algorithm
  960. 10, // feed entries
  961. 9, // result entries
  962. test15_feed, // feed
  963. test15_results, // results
  964. test15_feed2, // feed2
  965. test15_results2 // results2
  966. };
  967. // --------------------------------------------------------------------------------------------------------------------
  968. int run_test(struct test *test)
  969. {
  970. fprintf(stderr, "\nRunning test '%s':\n%s\n", test->name, test->description);
  971. default_rrd_memory_mode = RRD_MEMORY_MODE_ALLOC;
  972. default_rrd_update_every = test->update_every;
  973. char name[101];
  974. snprintfz(name, 100, "unittest-%s", test->name);
  975. // create the chart
  976. RRDSET *st = rrdset_create_localhost("netdata", name, name, "netdata", NULL, "Unit Testing", "a value", "unittest", NULL, 1
  977. , test->update_every, RRDSET_TYPE_LINE);
  978. RRDDIM *rd = rrddim_add(st, "dim1", NULL, test->multiplier, test->divisor, test->algorithm);
  979. RRDDIM *rd2 = NULL;
  980. if(test->feed2)
  981. rd2 = rrddim_add(st, "dim2", NULL, test->multiplier, test->divisor, test->algorithm);
  982. rrdset_flag_set(st, RRDSET_FLAG_DEBUG);
  983. // feed it with the test data
  984. time_t time_now = 0, time_start = now_realtime_sec();
  985. unsigned long c;
  986. collected_number last = 0;
  987. for(c = 0; c < test->feed_entries; c++) {
  988. if(debug_flags) fprintf(stderr, "\n\n");
  989. if(c) {
  990. time_now += test->feed[c].microseconds;
  991. fprintf(stderr, " > %s: feeding position %lu, after %0.3f seconds (%0.3f seconds from start), delta " CALCULATED_NUMBER_FORMAT ", rate " CALCULATED_NUMBER_FORMAT "\n",
  992. test->name, c+1,
  993. (float)test->feed[c].microseconds / 1000000.0,
  994. (float)time_now / 1000000.0,
  995. ((calculated_number)test->feed[c].value - (calculated_number)last) * (calculated_number)test->multiplier / (calculated_number)test->divisor,
  996. (((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);
  997. // rrdset_next_usec_unfiltered(st, test->feed[c].microseconds);
  998. st->usec_since_last_update = test->feed[c].microseconds;
  999. }
  1000. else {
  1001. fprintf(stderr, " > %s: feeding position %lu\n", test->name, c+1);
  1002. }
  1003. fprintf(stderr, " >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd->name, test->feed[c].value);
  1004. rrddim_set(st, "dim1", test->feed[c].value);
  1005. last = test->feed[c].value;
  1006. if(rd2) {
  1007. fprintf(stderr, " >> %s with value " COLLECTED_NUMBER_FORMAT "\n", rd2->name, test->feed2[c]);
  1008. rrddim_set(st, "dim2", test->feed2[c]);
  1009. }
  1010. rrdset_done(st);
  1011. // align the first entry to second boundary
  1012. if(!c) {
  1013. fprintf(stderr, " > %s: fixing first collection time to be %llu microseconds to second boundary\n", test->name, test->feed[c].microseconds);
  1014. rd->last_collected_time.tv_usec = st->last_collected_time.tv_usec = st->last_updated.tv_usec = test->feed[c].microseconds;
  1015. // time_start = st->last_collected_time.tv_sec;
  1016. }
  1017. }
  1018. // check the result
  1019. int errors = 0;
  1020. if(st->counter != test->result_entries) {
  1021. fprintf(stderr, " %s stored %zu entries, but we were expecting %lu, ### E R R O R ###\n", test->name, st->counter, test->result_entries);
  1022. errors++;
  1023. }
  1024. unsigned long max = (st->counter < test->result_entries)?st->counter:test->result_entries;
  1025. for(c = 0 ; c < max ; c++) {
  1026. calculated_number v = unpack_storage_number(rd->values[c]);
  1027. calculated_number n = unpack_storage_number(pack_storage_number(test->results[c], SN_DEFAULT_FLAGS));
  1028. int same = (calculated_number_round(v * 10000000.0) == calculated_number_round(n * 10000000.0))?1:0;
  1029. fprintf(stderr, " %s/%s: checking position %lu (at %"PRId64" secs), expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n",
  1030. test->name, rd->name, c+1,
  1031. (int64_t)((rrdset_first_entry_t(st) + c * st->update_every) - time_start),
  1032. n, v, (same)?"OK":"### E R R O R ###");
  1033. if(!same) errors++;
  1034. if(rd2) {
  1035. v = unpack_storage_number(rd2->values[c]);
  1036. n = test->results2[c];
  1037. same = (calculated_number_round(v * 10000000.0) == calculated_number_round(n * 10000000.0))?1:0;
  1038. fprintf(stderr, " %s/%s: checking position %lu (at %"PRId64" secs), expecting value " CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", %s\n",
  1039. test->name, rd2->name, c+1,
  1040. (int64_t)((rrdset_first_entry_t(st) + c * st->update_every) - time_start),
  1041. n, v, (same)?"OK":"### E R R O R ###");
  1042. if(!same) errors++;
  1043. }
  1044. }
  1045. return errors;
  1046. }
  1047. static int test_variable_renames(void) {
  1048. fprintf(stderr, "Creating chart\n");
  1049. RRDSET *st = rrdset_create_localhost("chart", "ID", NULL, "family", "context", "Unit Testing", "a value", "unittest", NULL, 1, 1, RRDSET_TYPE_LINE);
  1050. fprintf(stderr, "Created chart with id '%s', name '%s'\n", st->id, st->name);
  1051. fprintf(stderr, "Creating dimension DIM1\n");
  1052. RRDDIM *rd1 = rrddim_add(st, "DIM1", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1053. fprintf(stderr, "Created dimension with id '%s', name '%s'\n", rd1->id, rd1->name);
  1054. fprintf(stderr, "Creating dimension DIM2\n");
  1055. RRDDIM *rd2 = rrddim_add(st, "DIM2", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1056. fprintf(stderr, "Created dimension with id '%s', name '%s'\n", rd2->id, rd2->name);
  1057. fprintf(stderr, "Renaming chart to CHARTNAME1\n");
  1058. rrdset_set_name(st, "CHARTNAME1");
  1059. fprintf(stderr, "Renamed chart with id '%s' to name '%s'\n", st->id, st->name);
  1060. fprintf(stderr, "Renaming chart to CHARTNAME2\n");
  1061. rrdset_set_name(st, "CHARTNAME2");
  1062. fprintf(stderr, "Renamed chart with id '%s' to name '%s'\n", st->id, st->name);
  1063. fprintf(stderr, "Renaming dimension DIM1 to DIM1NAME1\n");
  1064. rrddim_set_name(st, rd1, "DIM1NAME1");
  1065. fprintf(stderr, "Renamed dimension with id '%s' to name '%s'\n", rd1->id, rd1->name);
  1066. fprintf(stderr, "Renaming dimension DIM1 to DIM1NAME2\n");
  1067. rrddim_set_name(st, rd1, "DIM1NAME2");
  1068. fprintf(stderr, "Renamed dimension with id '%s' to name '%s'\n", rd1->id, rd1->name);
  1069. fprintf(stderr, "Renaming dimension DIM2 to DIM2NAME1\n");
  1070. rrddim_set_name(st, rd2, "DIM2NAME1");
  1071. fprintf(stderr, "Renamed dimension with id '%s' to name '%s'\n", rd2->id, rd2->name);
  1072. fprintf(stderr, "Renaming dimension DIM2 to DIM2NAME2\n");
  1073. rrddim_set_name(st, rd2, "DIM2NAME2");
  1074. fprintf(stderr, "Renamed dimension with id '%s' to name '%s'\n", rd2->id, rd2->name);
  1075. BUFFER *buf = buffer_create(1);
  1076. health_api_v1_chart_variables2json(st, buf);
  1077. fprintf(stderr, "%s", buffer_tostring(buf));
  1078. buffer_free(buf);
  1079. return 1;
  1080. }
  1081. int check_strdupz_path_subpath() {
  1082. struct strdupz_path_subpath_checks {
  1083. const char *path;
  1084. const char *subpath;
  1085. const char *result;
  1086. } checks[] = {
  1087. { "", "", "." },
  1088. { "/", "", "/" },
  1089. { "/etc/netdata", "", "/etc/netdata" },
  1090. { "/etc/netdata///", "", "/etc/netdata" },
  1091. { "/etc/netdata///", "health.d", "/etc/netdata/health.d" },
  1092. { "/etc/netdata///", "///health.d", "/etc/netdata/health.d" },
  1093. { "/etc/netdata", "///health.d", "/etc/netdata/health.d" },
  1094. { "", "///health.d", "./health.d" },
  1095. { "/", "///health.d", "/health.d" },
  1096. // terminator
  1097. { NULL, NULL, NULL }
  1098. };
  1099. size_t i;
  1100. for(i = 0; checks[i].result ; i++) {
  1101. char *s = strdupz_path_subpath(checks[i].path, checks[i].subpath);
  1102. fprintf(stderr, "strdupz_path_subpath(\"%s\", \"%s\") = \"%s\": ", checks[i].path, checks[i].subpath, s);
  1103. if(!s || strcmp(s, checks[i].result) != 0) {
  1104. freez(s);
  1105. fprintf(stderr, "FAILED\n");
  1106. return 1;
  1107. }
  1108. else {
  1109. freez(s);
  1110. fprintf(stderr, "OK\n");
  1111. }
  1112. }
  1113. return 0;
  1114. }
  1115. int run_all_mockup_tests(void)
  1116. {
  1117. if(check_strdupz_path_subpath())
  1118. return 1;
  1119. if(check_number_printing())
  1120. return 1;
  1121. if(check_rrdcalc_comparisons())
  1122. return 1;
  1123. if(!test_variable_renames())
  1124. return 1;
  1125. if(run_test(&test1))
  1126. return 1;
  1127. if(run_test(&test2))
  1128. return 1;
  1129. if(run_test(&test3))
  1130. return 1;
  1131. if(run_test(&test4))
  1132. return 1;
  1133. if(run_test(&test5))
  1134. return 1;
  1135. if(run_test(&test5b))
  1136. return 1;
  1137. if(run_test(&test6))
  1138. return 1;
  1139. if(run_test(&test7))
  1140. return 1;
  1141. if(run_test(&test8))
  1142. return 1;
  1143. if(run_test(&test9))
  1144. return 1;
  1145. if(run_test(&test10))
  1146. return 1;
  1147. if(run_test(&test11))
  1148. return 1;
  1149. if(run_test(&test12))
  1150. return 1;
  1151. if(run_test(&test13))
  1152. return 1;
  1153. if(run_test(&test14))
  1154. return 1;
  1155. if(run_test(&test14b))
  1156. return 1;
  1157. if(run_test(&test14c))
  1158. return 1;
  1159. if(run_test(&test15))
  1160. return 1;
  1161. return 0;
  1162. }
  1163. int unit_test(long delay, long shift)
  1164. {
  1165. static int repeat = 0;
  1166. repeat++;
  1167. char name[101];
  1168. snprintfz(name, 100, "unittest-%d-%ld-%ld", repeat, delay, shift);
  1169. //debug_flags = 0xffffffff;
  1170. default_rrd_memory_mode = RRD_MEMORY_MODE_ALLOC;
  1171. default_rrd_update_every = 1;
  1172. int do_abs = 1;
  1173. int do_inc = 1;
  1174. int do_abst = 0;
  1175. int do_absi = 0;
  1176. RRDSET *st = rrdset_create_localhost("netdata", name, name, "netdata", NULL, "Unit Testing", "a value", "unittest", NULL, 1, 1
  1177. , RRDSET_TYPE_LINE);
  1178. rrdset_flag_set(st, RRDSET_FLAG_DEBUG);
  1179. RRDDIM *rdabs = NULL;
  1180. RRDDIM *rdinc = NULL;
  1181. RRDDIM *rdabst = NULL;
  1182. RRDDIM *rdabsi = NULL;
  1183. if(do_abs) rdabs = rrddim_add(st, "absolute", "absolute", 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1184. if(do_inc) rdinc = rrddim_add(st, "incremental", "incremental", 1, 1, RRD_ALGORITHM_INCREMENTAL);
  1185. if(do_abst) rdabst = rrddim_add(st, "percentage-of-absolute-row", "percentage-of-absolute-row", 1, 1, RRD_ALGORITHM_PCENT_OVER_ROW_TOTAL);
  1186. if(do_absi) rdabsi = rrddim_add(st, "percentage-of-incremental-row", "percentage-of-incremental-row", 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  1187. long increment = 1000;
  1188. collected_number i = 0;
  1189. unsigned long c, dimensions = 0;
  1190. RRDDIM *rd;
  1191. for(rd = st->dimensions ; rd ; rd = rd->next) dimensions++;
  1192. for(c = 0; c < 20 ;c++) {
  1193. i += increment;
  1194. fprintf(stderr, "\n\nLOOP = %lu, DELAY = %ld, VALUE = " COLLECTED_NUMBER_FORMAT "\n", c, delay, i);
  1195. if(c) {
  1196. // rrdset_next_usec_unfiltered(st, delay);
  1197. st->usec_since_last_update = delay;
  1198. }
  1199. if(do_abs) rrddim_set(st, "absolute", i);
  1200. if(do_inc) rrddim_set(st, "incremental", i);
  1201. if(do_abst) rrddim_set(st, "percentage-of-absolute-row", i);
  1202. if(do_absi) rrddim_set(st, "percentage-of-incremental-row", i);
  1203. if(!c) {
  1204. now_realtime_timeval(&st->last_collected_time);
  1205. st->last_collected_time.tv_usec = shift;
  1206. }
  1207. // prevent it from deleting the dimensions
  1208. for(rd = st->dimensions ; rd ; rd = rd->next)
  1209. rd->last_collected_time.tv_sec = st->last_collected_time.tv_sec;
  1210. rrdset_done(st);
  1211. }
  1212. unsigned long oincrement = increment;
  1213. increment = increment * st->update_every * 1000000 / delay;
  1214. fprintf(stderr, "\n\nORIGINAL INCREMENT: %lu, INCREMENT %ld, DELAY %ld, SHIFT %ld\n", oincrement * 10, increment * 10, delay, shift);
  1215. int ret = 0;
  1216. storage_number sn;
  1217. calculated_number cn, v;
  1218. for(c = 0 ; c < st->counter ; c++) {
  1219. fprintf(stderr, "\nPOSITION: c = %lu, EXPECTED VALUE %lu\n", c, (oincrement + c * increment + increment * (1000000 - shift) / 1000000 )* 10);
  1220. for(rd = st->dimensions ; rd ; rd = rd->next) {
  1221. sn = rd->values[c];
  1222. cn = unpack_storage_number(sn);
  1223. fprintf(stderr, "\t %s " CALCULATED_NUMBER_FORMAT " (PACKED AS " STORAGE_NUMBER_FORMAT ") -> ", rd->id, cn, sn);
  1224. if(rd == rdabs) v =
  1225. ( oincrement
  1226. // + (increment * (1000000 - shift) / 1000000)
  1227. + (c + 1) * increment
  1228. );
  1229. else if(rd == rdinc) v = (c?(increment):(increment * (1000000 - shift) / 1000000));
  1230. else if(rd == rdabst) v = oincrement / dimensions / 10;
  1231. else if(rd == rdabsi) v = oincrement / dimensions / 10;
  1232. else v = 0;
  1233. if(v == cn) fprintf(stderr, "passed.\n");
  1234. else {
  1235. fprintf(stderr, "ERROR! (expected " CALCULATED_NUMBER_FORMAT ")\n", v);
  1236. ret = 1;
  1237. }
  1238. }
  1239. }
  1240. if(ret)
  1241. fprintf(stderr, "\n\nUNIT TEST(%ld, %ld) FAILED\n\n", delay, shift);
  1242. return ret;
  1243. }
  1244. int test_sqlite(void) {
  1245. sqlite3 *db_meta;
  1246. fprintf(stderr, "Testing SQLIte\n");
  1247. int rc = sqlite3_open(":memory:", &db_meta);
  1248. if (rc != SQLITE_OK) {
  1249. fprintf(stderr,"Failed to test SQLite: DB init failed\n");
  1250. return 1;
  1251. }
  1252. rc = sqlite3_exec(db_meta, "CREATE TABLE IF NOT EXISTS mine (id1, id2);", 0, 0, NULL);
  1253. if (rc != SQLITE_OK) {
  1254. fprintf(stderr,"Failed to test SQLite: Create table failed\n");
  1255. return 1;
  1256. }
  1257. rc = sqlite3_exec(db_meta, "DELETE FROM MINE LIMIT 1;", 0, 0, NULL);
  1258. if (rc != SQLITE_OK) {
  1259. fprintf(stderr,"Failed to test SQLite: Delete with LIMIT failed\n");
  1260. return 1;
  1261. }
  1262. rc = sqlite3_exec(db_meta, "UPDATE MINE SET id1=1 LIMIT 1;", 0, 0, NULL);
  1263. if (rc != SQLITE_OK) {
  1264. fprintf(stderr,"Failed to test SQLite: Update with LIMIT failed\n");
  1265. return 1;
  1266. }
  1267. fprintf(stderr,"SQLite is OK\n");
  1268. return 0;
  1269. }
  1270. #ifdef ENABLE_DBENGINE
  1271. static inline void rrddim_set_by_pointer_fake_time(RRDDIM *rd, collected_number value, time_t now)
  1272. {
  1273. rd->last_collected_time.tv_sec = now;
  1274. rd->last_collected_time.tv_usec = 0;
  1275. rd->collected_value = value;
  1276. rd->updated = 1;
  1277. rd->collections_counter++;
  1278. collected_number v = (value >= 0) ? value : -value;
  1279. if(unlikely(v > rd->collected_value_max)) rd->collected_value_max = v;
  1280. }
  1281. static RRDHOST *dbengine_rrdhost_find_or_create(char *name)
  1282. {
  1283. /* We don't want to drop metrics when generating load, we prefer to block data generation itself */
  1284. rrdeng_drop_metrics_under_page_cache_pressure = 0;
  1285. return rrdhost_find_or_create(
  1286. name
  1287. , name
  1288. , name
  1289. , os_type
  1290. , netdata_configured_timezone
  1291. , netdata_configured_abbrev_timezone
  1292. , netdata_configured_utc_offset
  1293. , ""
  1294. , program_name
  1295. , program_version
  1296. , default_rrd_update_every
  1297. , default_rrd_history_entries
  1298. , RRD_MEMORY_MODE_DBENGINE
  1299. , default_health_enabled
  1300. , default_rrdpush_enabled
  1301. , default_rrdpush_destination
  1302. , default_rrdpush_api_key
  1303. , default_rrdpush_send_charts_matching
  1304. , NULL
  1305. );
  1306. }
  1307. // constants for test_dbengine
  1308. static const int CHARTS = 64;
  1309. static const int DIMS = 16; // That gives us 64 * 16 = 1024 metrics
  1310. #define REGIONS (3) // 3 regions of update_every
  1311. // first region update_every is 2, second is 3, third is 1
  1312. static const int REGION_UPDATE_EVERY[REGIONS] = {2, 3, 1};
  1313. static const int REGION_POINTS[REGIONS] = {
  1314. 16384, // This produces 64MiB of metric data for the first region: update_every = 2
  1315. 16384, // This produces 64MiB of metric data for the second region: update_every = 3
  1316. 16384, // This produces 64MiB of metric data for the third region: update_every = 1
  1317. };
  1318. static const int QUERY_BATCH = 4096;
  1319. static void test_dbengine_create_charts(RRDHOST *host, RRDSET *st[CHARTS], RRDDIM *rd[CHARTS][DIMS],
  1320. int update_every)
  1321. {
  1322. int i, j;
  1323. char name[101];
  1324. for (i = 0 ; i < CHARTS ; ++i) {
  1325. snprintfz(name, 100, "dbengine-chart-%d", i);
  1326. // create the chart
  1327. st[i] = rrdset_create(host, "netdata", name, name, "netdata", NULL, "Unit Testing", "a value", "unittest",
  1328. NULL, 1, update_every, RRDSET_TYPE_LINE);
  1329. rrdset_flag_set(st[i], RRDSET_FLAG_DEBUG);
  1330. rrdset_flag_set(st[i], RRDSET_FLAG_STORE_FIRST);
  1331. for (j = 0 ; j < DIMS ; ++j) {
  1332. snprintfz(name, 100, "dim-%d", j);
  1333. rd[i][j] = rrddim_add(st[i], name, NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1334. }
  1335. }
  1336. // Initialize DB with the very first entries
  1337. for (i = 0 ; i < CHARTS ; ++i) {
  1338. for (j = 0 ; j < DIMS ; ++j) {
  1339. rd[i][j]->last_collected_time.tv_sec =
  1340. st[i]->last_collected_time.tv_sec = st[i]->last_updated.tv_sec = 2 * API_RELATIVE_TIME_MAX - 1;
  1341. rd[i][j]->last_collected_time.tv_usec =
  1342. st[i]->last_collected_time.tv_usec = st[i]->last_updated.tv_usec = 0;
  1343. }
  1344. }
  1345. for (i = 0 ; i < CHARTS ; ++i) {
  1346. st[i]->usec_since_last_update = USEC_PER_SEC;
  1347. for (j = 0; j < DIMS; ++j) {
  1348. rrddim_set_by_pointer_fake_time(rd[i][j], 69, 2 * API_RELATIVE_TIME_MAX); // set first value to 69
  1349. }
  1350. rrdset_done(st[i]);
  1351. }
  1352. // Fluh pages for subsequent real values
  1353. for (i = 0 ; i < CHARTS ; ++i) {
  1354. for (j = 0; j < DIMS; ++j) {
  1355. rrdeng_store_metric_flush_current_page(rd[i][j]);
  1356. }
  1357. }
  1358. }
  1359. // Feeds the database region with test data, returns last timestamp of region
  1360. static time_t test_dbengine_create_metrics(RRDSET *st[CHARTS], RRDDIM *rd[CHARTS][DIMS],
  1361. int current_region, time_t time_start)
  1362. {
  1363. time_t time_now;
  1364. int i, j, c, update_every;
  1365. collected_number next;
  1366. update_every = REGION_UPDATE_EVERY[current_region];
  1367. time_now = time_start;
  1368. // feed it with the test data
  1369. for (i = 0 ; i < CHARTS ; ++i) {
  1370. for (j = 0 ; j < DIMS ; ++j) {
  1371. rd[i][j]->last_collected_time.tv_sec =
  1372. st[i]->last_collected_time.tv_sec = st[i]->last_updated.tv_sec = time_now;
  1373. rd[i][j]->last_collected_time.tv_usec =
  1374. st[i]->last_collected_time.tv_usec = st[i]->last_updated.tv_usec = 0;
  1375. }
  1376. }
  1377. for (c = 0; c < REGION_POINTS[current_region] ; ++c) {
  1378. time_now += update_every; // time_now = start + (c + 1) * update_every
  1379. for (i = 0 ; i < CHARTS ; ++i) {
  1380. st[i]->usec_since_last_update = USEC_PER_SEC * update_every;
  1381. for (j = 0; j < DIMS; ++j) {
  1382. next = ((collected_number)i * DIMS) * REGION_POINTS[current_region] +
  1383. j * REGION_POINTS[current_region] + c;
  1384. rrddim_set_by_pointer_fake_time(rd[i][j], next, time_now);
  1385. }
  1386. rrdset_done(st[i]);
  1387. }
  1388. }
  1389. return time_now; //time_end
  1390. }
  1391. // Checks the metric data for the given region, returns number of errors
  1392. static int test_dbengine_check_metrics(RRDSET *st[CHARTS], RRDDIM *rd[CHARTS][DIMS],
  1393. int current_region, time_t time_start)
  1394. {
  1395. uint8_t same;
  1396. time_t time_now, time_retrieved;
  1397. int i, j, k, c, errors, update_every;
  1398. collected_number last;
  1399. calculated_number value, expected;
  1400. storage_number n;
  1401. struct rrddim_query_handle handle;
  1402. update_every = REGION_UPDATE_EVERY[current_region];
  1403. errors = 0;
  1404. // check the result
  1405. for (c = 0; c < REGION_POINTS[current_region] ; c += QUERY_BATCH) {
  1406. time_now = time_start + (c + 1) * update_every;
  1407. for (i = 0 ; i < CHARTS ; ++i) {
  1408. for (j = 0; j < DIMS; ++j) {
  1409. rd[i][j]->state->query_ops.init(rd[i][j], &handle, time_now, time_now + QUERY_BATCH * update_every);
  1410. for (k = 0; k < QUERY_BATCH; ++k) {
  1411. last = ((collected_number)i * DIMS) * REGION_POINTS[current_region] +
  1412. j * REGION_POINTS[current_region] + c + k;
  1413. expected = unpack_storage_number(pack_storage_number((calculated_number)last, SN_DEFAULT_FLAGS));
  1414. n = rd[i][j]->state->query_ops.next_metric(&handle, &time_retrieved);
  1415. value = unpack_storage_number(n);
  1416. same = (calculated_number_round(value) == calculated_number_round(expected)) ? 1 : 0;
  1417. if(!same) {
  1418. fprintf(stderr, " DB-engine unittest %s/%s: at %lu secs, expecting value "
  1419. CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT ", ### E R R O R ###\n",
  1420. st[i]->name, rd[i][j]->name, (unsigned long)time_now + k * update_every, expected, value);
  1421. errors++;
  1422. }
  1423. if(time_retrieved != time_now + k * update_every) {
  1424. fprintf(stderr, " DB-engine unittest %s/%s: at %lu secs, found timestamp %lu ### E R R O R ###\n",
  1425. st[i]->name, rd[i][j]->name, (unsigned long)time_now + k * update_every, (unsigned long)time_retrieved);
  1426. errors++;
  1427. }
  1428. }
  1429. rd[i][j]->state->query_ops.finalize(&handle);
  1430. }
  1431. }
  1432. }
  1433. return errors;
  1434. }
  1435. // Check rrdr transformations
  1436. static int test_dbengine_check_rrdr(RRDSET *st[CHARTS], RRDDIM *rd[CHARTS][DIMS],
  1437. int current_region, time_t time_start, time_t time_end)
  1438. {
  1439. uint8_t same;
  1440. time_t time_now, time_retrieved;
  1441. int i, j, errors, update_every;
  1442. long c;
  1443. collected_number last;
  1444. calculated_number value, expected;
  1445. errors = 0;
  1446. update_every = REGION_UPDATE_EVERY[current_region];
  1447. long points = (time_end - time_start) / update_every;
  1448. for (i = 0 ; i < CHARTS ; ++i) {
  1449. ONEWAYALLOC *owa = onewayalloc_create(0);
  1450. RRDR *r = rrd2rrdr(owa, st[i], points, time_start + update_every, time_end, RRDR_GROUPING_AVERAGE, 0, 0, NULL, NULL, 0);
  1451. if (!r) {
  1452. fprintf(stderr, " DB-engine unittest %s: empty RRDR ### E R R O R ###\n", st[i]->name);
  1453. return ++errors;
  1454. } else {
  1455. assert(r->st == st[i]);
  1456. for (c = 0; c != rrdr_rows(r) ; ++c) {
  1457. RRDDIM *d;
  1458. time_now = time_start + (c + 2) * update_every;
  1459. time_retrieved = r->t[c];
  1460. // for each dimension
  1461. for (j = 0, d = r->st->dimensions ; d && j < r->d ; ++j, d = d->next) {
  1462. calculated_number *cn = &r->v[ c * r->d ];
  1463. value = cn[j];
  1464. assert(rd[i][j] == d);
  1465. last = i * DIMS * REGION_POINTS[current_region] + j * REGION_POINTS[current_region] + c + 1;
  1466. expected = unpack_storage_number(pack_storage_number((calculated_number)last, SN_DEFAULT_FLAGS));
  1467. same = (calculated_number_round(value) == calculated_number_round(expected)) ? 1 : 0;
  1468. if(!same) {
  1469. fprintf(stderr, " DB-engine unittest %s/%s: at %lu secs, expecting value "
  1470. CALCULATED_NUMBER_FORMAT ", RRDR found " CALCULATED_NUMBER_FORMAT ", ### E R R O R ###\n",
  1471. st[i]->name, rd[i][j]->name, (unsigned long)time_now, expected, value);
  1472. errors++;
  1473. }
  1474. if(time_retrieved != time_now) {
  1475. fprintf(stderr, " DB-engine unittest %s/%s: at %lu secs, found RRDR timestamp %lu ### E R R O R ###\n",
  1476. st[i]->name, rd[i][j]->name, (unsigned long)time_now, (unsigned long)time_retrieved);
  1477. errors++;
  1478. }
  1479. }
  1480. }
  1481. rrdr_free(owa, r);
  1482. }
  1483. onewayalloc_destroy(owa);
  1484. }
  1485. return errors;
  1486. }
  1487. int test_dbengine(void)
  1488. {
  1489. int i, j, errors, update_every, current_region;
  1490. RRDHOST *host = NULL;
  1491. RRDSET *st[CHARTS];
  1492. RRDDIM *rd[CHARTS][DIMS];
  1493. time_t time_start[REGIONS], time_end[REGIONS];
  1494. error_log_limit_unlimited();
  1495. fprintf(stderr, "\nRunning DB-engine test\n");
  1496. default_rrd_memory_mode = RRD_MEMORY_MODE_DBENGINE;
  1497. fprintf(stderr, "Initializing localhost with hostname 'unittest-dbengine'");
  1498. host = dbengine_rrdhost_find_or_create("unittest-dbengine");
  1499. if (NULL == host)
  1500. return 1;
  1501. current_region = 0; // this is the first region of data
  1502. update_every = REGION_UPDATE_EVERY[current_region]; // set data collection frequency to 2 seconds
  1503. test_dbengine_create_charts(host, st, rd, update_every);
  1504. time_start[current_region] = 2 * API_RELATIVE_TIME_MAX;
  1505. time_end[current_region] = test_dbengine_create_metrics(st,rd, current_region, time_start[current_region]);
  1506. errors = test_dbengine_check_metrics(st, rd, current_region, time_start[current_region]);
  1507. if (errors)
  1508. goto error_out;
  1509. current_region = 1; //this is the second region of data
  1510. update_every = REGION_UPDATE_EVERY[current_region]; // set data collection frequency to 3 seconds
  1511. // Align pages for frequency change
  1512. for (i = 0 ; i < CHARTS ; ++i) {
  1513. st[i]->update_every = update_every;
  1514. for (j = 0; j < DIMS; ++j) {
  1515. rrdeng_store_metric_flush_current_page(rd[i][j]);
  1516. }
  1517. }
  1518. time_start[current_region] = time_end[current_region - 1] + update_every;
  1519. if (0 != time_start[current_region] % update_every) // align to update_every
  1520. time_start[current_region] += update_every - time_start[current_region] % update_every;
  1521. time_end[current_region] = test_dbengine_create_metrics(st,rd, current_region, time_start[current_region]);
  1522. errors = test_dbengine_check_metrics(st, rd, current_region, time_start[current_region]);
  1523. if (errors)
  1524. goto error_out;
  1525. current_region = 2; //this is the third region of data
  1526. update_every = REGION_UPDATE_EVERY[current_region]; // set data collection frequency to 1 seconds
  1527. // Align pages for frequency change
  1528. for (i = 0 ; i < CHARTS ; ++i) {
  1529. st[i]->update_every = update_every;
  1530. for (j = 0; j < DIMS; ++j) {
  1531. rrdeng_store_metric_flush_current_page(rd[i][j]);
  1532. }
  1533. }
  1534. time_start[current_region] = time_end[current_region - 1] + update_every;
  1535. if (0 != time_start[current_region] % update_every) // align to update_every
  1536. time_start[current_region] += update_every - time_start[current_region] % update_every;
  1537. time_end[current_region] = test_dbengine_create_metrics(st,rd, current_region, time_start[current_region]);
  1538. errors = test_dbengine_check_metrics(st, rd, current_region, time_start[current_region]);
  1539. if (errors)
  1540. goto error_out;
  1541. for (current_region = 0 ; current_region < REGIONS ; ++current_region) {
  1542. errors = test_dbengine_check_rrdr(st, rd, current_region, time_start[current_region], time_end[current_region]);
  1543. if (errors)
  1544. goto error_out;
  1545. }
  1546. current_region = 1;
  1547. update_every = REGION_UPDATE_EVERY[current_region]; // use the maximum update_every = 3
  1548. errors = 0;
  1549. long points = (time_end[REGIONS - 1] - time_start[0]) / update_every; // cover all time regions with RRDR
  1550. long point_offset = (time_start[current_region] - time_start[0]) / update_every;
  1551. for (i = 0 ; i < CHARTS ; ++i) {
  1552. ONEWAYALLOC *owa = onewayalloc_create(0);
  1553. RRDR *r = rrd2rrdr(owa, st[i], points, time_start[0] + update_every, time_end[REGIONS - 1], RRDR_GROUPING_AVERAGE, 0, 0, NULL, NULL, 0);
  1554. if (!r) {
  1555. fprintf(stderr, " DB-engine unittest %s: empty RRDR ### E R R O R ###\n", st[i]->name);
  1556. ++errors;
  1557. } else {
  1558. long c;
  1559. assert(r->st == st[i]);
  1560. // test current region values only, since they must be left unchanged
  1561. for (c = point_offset ; c < point_offset + rrdr_rows(r) / REGIONS / 2 ; ++c) {
  1562. RRDDIM *d;
  1563. time_t time_now = time_start[current_region] + (c - point_offset + 2) * update_every;
  1564. time_t time_retrieved = r->t[c];
  1565. // for each dimension
  1566. for(j = 0, d = r->st->dimensions ; d && j < r->d ; ++j, d = d->next) {
  1567. calculated_number *cn = &r->v[ c * r->d ];
  1568. calculated_number value = cn[j];
  1569. assert(rd[i][j] == d);
  1570. collected_number last = i * DIMS * REGION_POINTS[current_region] + j * REGION_POINTS[current_region] + c - point_offset + 1;
  1571. calculated_number expected = unpack_storage_number(pack_storage_number((calculated_number)last, SN_DEFAULT_FLAGS));
  1572. uint8_t same = (calculated_number_round(value) == calculated_number_round(expected)) ? 1 : 0;
  1573. if(!same) {
  1574. fprintf(stderr, " DB-engine unittest %s/%s: at %lu secs, expecting value "
  1575. CALCULATED_NUMBER_FORMAT ", RRDR found " CALCULATED_NUMBER_FORMAT ", ### E R R O R ###\n",
  1576. st[i]->name, rd[i][j]->name, (unsigned long)time_now, expected, value);
  1577. errors++;
  1578. }
  1579. if(time_retrieved != time_now) {
  1580. fprintf(stderr, " DB-engine unittest %s/%s: at %lu secs, found RRDR timestamp %lu ### E R R O R ###\n",
  1581. st[i]->name, rd[i][j]->name, (unsigned long)time_now, (unsigned long)time_retrieved);
  1582. errors++;
  1583. }
  1584. }
  1585. }
  1586. rrdr_free(owa, r);
  1587. }
  1588. onewayalloc_destroy(owa);
  1589. }
  1590. error_out:
  1591. rrd_wrlock();
  1592. rrdeng_prepare_exit(host->rrdeng_ctx);
  1593. rrdhost_delete_charts(host);
  1594. rrdeng_exit(host->rrdeng_ctx);
  1595. rrd_unlock();
  1596. return errors;
  1597. }
  1598. struct dbengine_chart_thread {
  1599. uv_thread_t thread;
  1600. RRDHOST *host;
  1601. char *chartname; /* Will be prefixed by type, e.g. "example_local1.", "example_local2." etc */
  1602. unsigned dset_charts; /* number of charts */
  1603. unsigned dset_dims; /* dimensions per chart */
  1604. unsigned chart_i; /* current chart offset */
  1605. time_t time_present; /* current virtual time of the benchmark */
  1606. volatile time_t time_max; /* latest timestamp of stored values */
  1607. unsigned history_seconds; /* how far back in the past to go */
  1608. volatile long done; /* initialize to 0, set to 1 to stop thread */
  1609. struct completion charts_initialized;
  1610. unsigned long errors, stored_metrics_nr; /* statistics */
  1611. RRDSET *st;
  1612. RRDDIM *rd[]; /* dset_dims elements */
  1613. };
  1614. collected_number generate_dbengine_chart_value(int chart_i, int dim_i, time_t time_current)
  1615. {
  1616. collected_number value;
  1617. value = ((collected_number)time_current) * (chart_i + 1);
  1618. value += ((collected_number)time_current) * (dim_i + 1);
  1619. value %= 1024LLU;
  1620. return value;
  1621. }
  1622. static void generate_dbengine_chart(void *arg)
  1623. {
  1624. struct dbengine_chart_thread *thread_info = (struct dbengine_chart_thread *)arg;
  1625. RRDHOST *host = thread_info->host;
  1626. char *chartname = thread_info->chartname;
  1627. const unsigned DSET_DIMS = thread_info->dset_dims;
  1628. unsigned history_seconds = thread_info->history_seconds;
  1629. time_t time_present = thread_info->time_present;
  1630. unsigned j, update_every = 1;
  1631. RRDSET *st;
  1632. RRDDIM *rd[DSET_DIMS];
  1633. char name[RRD_ID_LENGTH_MAX + 1];
  1634. time_t time_current;
  1635. // create the chart
  1636. snprintfz(name, RRD_ID_LENGTH_MAX, "example_local%u", thread_info->chart_i + 1);
  1637. thread_info->st = st = rrdset_create(host, name, chartname, chartname, "example", NULL, chartname, chartname,
  1638. chartname, NULL, 1, update_every, RRDSET_TYPE_LINE);
  1639. for (j = 0 ; j < DSET_DIMS ; ++j) {
  1640. snprintfz(name, RRD_ID_LENGTH_MAX, "%s%u", chartname, j + 1);
  1641. thread_info->rd[j] = rd[j] = rrddim_add(st, name, NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1642. }
  1643. completion_mark_complete(&thread_info->charts_initialized);
  1644. // feed it with the test data
  1645. time_current = time_present - history_seconds;
  1646. for (j = 0 ; j < DSET_DIMS ; ++j) {
  1647. rd[j]->last_collected_time.tv_sec =
  1648. st->last_collected_time.tv_sec = st->last_updated.tv_sec = time_current - update_every;
  1649. rd[j]->last_collected_time.tv_usec =
  1650. st->last_collected_time.tv_usec = st->last_updated.tv_usec = 0;
  1651. }
  1652. for( ; !thread_info->done && time_current < time_present ; time_current += update_every) {
  1653. st->usec_since_last_update = USEC_PER_SEC * update_every;
  1654. for (j = 0; j < DSET_DIMS; ++j) {
  1655. collected_number value;
  1656. value = generate_dbengine_chart_value(thread_info->chart_i, j, time_current);
  1657. rrddim_set_by_pointer_fake_time(rd[j], value, time_current);
  1658. ++thread_info->stored_metrics_nr;
  1659. }
  1660. rrdset_done(st);
  1661. thread_info->time_max = time_current;
  1662. }
  1663. for (j = 0; j < DSET_DIMS; ++j) {
  1664. rrdeng_store_metric_finalize(rd[j]);
  1665. }
  1666. }
  1667. void generate_dbengine_dataset(unsigned history_seconds)
  1668. {
  1669. const int DSET_CHARTS = 16;
  1670. const int DSET_DIMS = 128;
  1671. const uint64_t EXPECTED_COMPRESSION_RATIO = 20;
  1672. RRDHOST *host = NULL;
  1673. struct dbengine_chart_thread **thread_info;
  1674. int i;
  1675. time_t time_present;
  1676. default_rrd_memory_mode = RRD_MEMORY_MODE_DBENGINE;
  1677. default_rrdeng_page_cache_mb = 128;
  1678. // Worst case for uncompressible data
  1679. default_rrdeng_disk_quota_mb = (((uint64_t)DSET_DIMS * DSET_CHARTS) * sizeof(storage_number) * history_seconds) /
  1680. (1024 * 1024);
  1681. default_rrdeng_disk_quota_mb -= default_rrdeng_disk_quota_mb * EXPECTED_COMPRESSION_RATIO / 100;
  1682. error_log_limit_unlimited();
  1683. fprintf(stderr, "Initializing localhost with hostname 'dbengine-dataset'");
  1684. host = dbengine_rrdhost_find_or_create("dbengine-dataset");
  1685. if (NULL == host)
  1686. return;
  1687. thread_info = mallocz(sizeof(*thread_info) * DSET_CHARTS);
  1688. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1689. thread_info[i] = mallocz(sizeof(*thread_info[i]) + sizeof(RRDDIM *) * DSET_DIMS);
  1690. }
  1691. fprintf(stderr, "\nRunning DB-engine workload generator\n");
  1692. time_present = now_realtime_sec();
  1693. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1694. thread_info[i]->host = host;
  1695. thread_info[i]->chartname = "random";
  1696. thread_info[i]->dset_charts = DSET_CHARTS;
  1697. thread_info[i]->chart_i = i;
  1698. thread_info[i]->dset_dims = DSET_DIMS;
  1699. thread_info[i]->history_seconds = history_seconds;
  1700. thread_info[i]->time_present = time_present;
  1701. thread_info[i]->time_max = 0;
  1702. thread_info[i]->done = 0;
  1703. completion_init(&thread_info[i]->charts_initialized);
  1704. assert(0 == uv_thread_create(&thread_info[i]->thread, generate_dbengine_chart, thread_info[i]));
  1705. completion_wait_for(&thread_info[i]->charts_initialized);
  1706. completion_destroy(&thread_info[i]->charts_initialized);
  1707. }
  1708. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1709. assert(0 == uv_thread_join(&thread_info[i]->thread));
  1710. }
  1711. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1712. freez(thread_info[i]);
  1713. }
  1714. freez(thread_info);
  1715. rrd_wrlock();
  1716. rrdhost_free(host);
  1717. rrd_unlock();
  1718. }
  1719. struct dbengine_query_thread {
  1720. uv_thread_t thread;
  1721. RRDHOST *host;
  1722. char *chartname; /* Will be prefixed by type, e.g. "example_local1.", "example_local2." etc */
  1723. unsigned dset_charts; /* number of charts */
  1724. unsigned dset_dims; /* dimensions per chart */
  1725. time_t time_present; /* current virtual time of the benchmark */
  1726. unsigned history_seconds; /* how far back in the past to go */
  1727. volatile long done; /* initialize to 0, set to 1 to stop thread */
  1728. unsigned long errors, queries_nr, queried_metrics_nr; /* statistics */
  1729. uint8_t delete_old_data; /* if non zero then data are deleted when disk space is exhausted */
  1730. struct dbengine_chart_thread *chart_threads[]; /* dset_charts elements */
  1731. };
  1732. static void query_dbengine_chart(void *arg)
  1733. {
  1734. struct dbengine_query_thread *thread_info = (struct dbengine_query_thread *)arg;
  1735. const int DSET_CHARTS = thread_info->dset_charts;
  1736. const int DSET_DIMS = thread_info->dset_dims;
  1737. time_t time_after, time_before, time_min, time_approx_min, time_max, duration;
  1738. int i, j, update_every = 1;
  1739. RRDSET *st;
  1740. RRDDIM *rd;
  1741. uint8_t same;
  1742. time_t time_now, time_retrieved;
  1743. collected_number generatedv;
  1744. calculated_number value, expected;
  1745. storage_number n;
  1746. struct rrddim_query_handle handle;
  1747. do {
  1748. // pick a chart and dimension
  1749. i = random() % DSET_CHARTS;
  1750. st = thread_info->chart_threads[i]->st;
  1751. j = random() % DSET_DIMS;
  1752. rd = thread_info->chart_threads[i]->rd[j];
  1753. time_min = thread_info->time_present - thread_info->history_seconds + 1;
  1754. time_max = thread_info->chart_threads[i]->time_max;
  1755. if (thread_info->delete_old_data) {
  1756. /* A time window of twice the disk space is sufficient for compression space savings of up to 50% */
  1757. time_approx_min = time_max - (default_rrdeng_disk_quota_mb * 2 * 1024 * 1024) /
  1758. (((uint64_t) DSET_DIMS * DSET_CHARTS) * sizeof(storage_number));
  1759. time_min = MAX(time_min, time_approx_min);
  1760. }
  1761. if (!time_max) {
  1762. time_before = time_after = time_min;
  1763. } else {
  1764. time_after = time_min + random() % (MAX(time_max - time_min, 1));
  1765. duration = random() % 3600;
  1766. time_before = MIN(time_after + duration, time_max); /* up to 1 hour queries */
  1767. }
  1768. rd->state->query_ops.init(rd, &handle, time_after, time_before);
  1769. ++thread_info->queries_nr;
  1770. for (time_now = time_after ; time_now <= time_before ; time_now += update_every) {
  1771. generatedv = generate_dbengine_chart_value(i, j, time_now);
  1772. expected = unpack_storage_number(pack_storage_number((calculated_number) generatedv, SN_DEFAULT_FLAGS));
  1773. if (unlikely(rd->state->query_ops.is_finished(&handle))) {
  1774. if (!thread_info->delete_old_data) { /* data validation only when we don't delete */
  1775. fprintf(stderr, " DB-engine stresstest %s/%s: at %lu secs, expecting value "
  1776. CALCULATED_NUMBER_FORMAT ", found data gap, ### E R R O R ###\n",
  1777. st->name, rd->name, (unsigned long) time_now, expected);
  1778. ++thread_info->errors;
  1779. }
  1780. break;
  1781. }
  1782. n = rd->state->query_ops.next_metric(&handle, &time_retrieved);
  1783. if (SN_EMPTY_SLOT == n) {
  1784. if (!thread_info->delete_old_data) { /* data validation only when we don't delete */
  1785. fprintf(stderr, " DB-engine stresstest %s/%s: at %lu secs, expecting value "
  1786. CALCULATED_NUMBER_FORMAT ", found data gap, ### E R R O R ###\n",
  1787. st->name, rd->name, (unsigned long) time_now, expected);
  1788. ++thread_info->errors;
  1789. }
  1790. break;
  1791. }
  1792. ++thread_info->queried_metrics_nr;
  1793. value = unpack_storage_number(n);
  1794. same = (calculated_number_round(value) == calculated_number_round(expected)) ? 1 : 0;
  1795. if (!same) {
  1796. if (!thread_info->delete_old_data) { /* data validation only when we don't delete */
  1797. fprintf(stderr, " DB-engine stresstest %s/%s: at %lu secs, expecting value "
  1798. CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT
  1799. ", ### E R R O R ###\n",
  1800. st->name, rd->name, (unsigned long) time_now, expected, value);
  1801. ++thread_info->errors;
  1802. }
  1803. }
  1804. if (time_retrieved != time_now) {
  1805. if (!thread_info->delete_old_data) { /* data validation only when we don't delete */
  1806. fprintf(stderr,
  1807. " DB-engine stresstest %s/%s: at %lu secs, found timestamp %lu ### E R R O R ###\n",
  1808. st->name, rd->name, (unsigned long) time_now, (unsigned long) time_retrieved);
  1809. ++thread_info->errors;
  1810. }
  1811. }
  1812. }
  1813. rd->state->query_ops.finalize(&handle);
  1814. } while(!thread_info->done);
  1815. }
  1816. void dbengine_stress_test(unsigned TEST_DURATION_SEC, unsigned DSET_CHARTS, unsigned QUERY_THREADS,
  1817. unsigned RAMP_UP_SECONDS, unsigned PAGE_CACHE_MB, unsigned DISK_SPACE_MB)
  1818. {
  1819. const unsigned DSET_DIMS = 128;
  1820. const uint64_t EXPECTED_COMPRESSION_RATIO = 20;
  1821. const unsigned HISTORY_SECONDS = 3600 * 24 * 365 * 50; /* 50 year of history */
  1822. RRDHOST *host = NULL;
  1823. struct dbengine_chart_thread **chart_threads;
  1824. struct dbengine_query_thread **query_threads;
  1825. unsigned i, j;
  1826. time_t time_start, test_duration;
  1827. error_log_limit_unlimited();
  1828. if (!TEST_DURATION_SEC)
  1829. TEST_DURATION_SEC = 10;
  1830. if (!DSET_CHARTS)
  1831. DSET_CHARTS = 1;
  1832. if (!QUERY_THREADS)
  1833. QUERY_THREADS = 1;
  1834. if (PAGE_CACHE_MB < RRDENG_MIN_PAGE_CACHE_SIZE_MB)
  1835. PAGE_CACHE_MB = RRDENG_MIN_PAGE_CACHE_SIZE_MB;
  1836. default_rrd_memory_mode = RRD_MEMORY_MODE_DBENGINE;
  1837. default_rrdeng_page_cache_mb = PAGE_CACHE_MB;
  1838. if (DISK_SPACE_MB) {
  1839. fprintf(stderr, "By setting disk space limit data are allowed to be deleted. "
  1840. "Data validation is turned off for this run.\n");
  1841. default_rrdeng_disk_quota_mb = DISK_SPACE_MB;
  1842. } else {
  1843. // Worst case for uncompressible data
  1844. default_rrdeng_disk_quota_mb =
  1845. (((uint64_t) DSET_DIMS * DSET_CHARTS) * sizeof(storage_number) * HISTORY_SECONDS) / (1024 * 1024);
  1846. default_rrdeng_disk_quota_mb -= default_rrdeng_disk_quota_mb * EXPECTED_COMPRESSION_RATIO / 100;
  1847. }
  1848. fprintf(stderr, "Initializing localhost with hostname 'dbengine-stress-test'\n");
  1849. (void) sql_init_database(DB_CHECK_NONE, 1);
  1850. host = dbengine_rrdhost_find_or_create("dbengine-stress-test");
  1851. if (NULL == host)
  1852. return;
  1853. chart_threads = mallocz(sizeof(*chart_threads) * DSET_CHARTS);
  1854. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1855. chart_threads[i] = mallocz(sizeof(*chart_threads[i]) + sizeof(RRDDIM *) * DSET_DIMS);
  1856. }
  1857. query_threads = mallocz(sizeof(*query_threads) * QUERY_THREADS);
  1858. for (i = 0 ; i < QUERY_THREADS ; ++i) {
  1859. query_threads[i] = mallocz(sizeof(*query_threads[i]) + sizeof(struct dbengine_chart_thread *) * DSET_CHARTS);
  1860. }
  1861. fprintf(stderr, "\nRunning DB-engine stress test, %u seconds writers ramp-up time,\n"
  1862. "%u seconds of concurrent readers and writers, %u writer threads, %u reader threads,\n"
  1863. "%u MiB of page cache.\n",
  1864. RAMP_UP_SECONDS, TEST_DURATION_SEC, DSET_CHARTS, QUERY_THREADS, PAGE_CACHE_MB);
  1865. time_start = now_realtime_sec() + HISTORY_SECONDS; /* move history to the future */
  1866. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1867. chart_threads[i]->host = host;
  1868. chart_threads[i]->chartname = "random";
  1869. chart_threads[i]->dset_charts = DSET_CHARTS;
  1870. chart_threads[i]->chart_i = i;
  1871. chart_threads[i]->dset_dims = DSET_DIMS;
  1872. chart_threads[i]->history_seconds = HISTORY_SECONDS;
  1873. chart_threads[i]->time_present = time_start;
  1874. chart_threads[i]->time_max = 0;
  1875. chart_threads[i]->done = 0;
  1876. chart_threads[i]->errors = chart_threads[i]->stored_metrics_nr = 0;
  1877. completion_init(&chart_threads[i]->charts_initialized);
  1878. assert(0 == uv_thread_create(&chart_threads[i]->thread, generate_dbengine_chart, chart_threads[i]));
  1879. }
  1880. /* barrier so that subsequent queries can access valid chart data */
  1881. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1882. completion_wait_for(&chart_threads[i]->charts_initialized);
  1883. completion_destroy(&chart_threads[i]->charts_initialized);
  1884. }
  1885. sleep(RAMP_UP_SECONDS);
  1886. /* at this point data have already began being written to the database */
  1887. for (i = 0 ; i < QUERY_THREADS ; ++i) {
  1888. query_threads[i]->host = host;
  1889. query_threads[i]->chartname = "random";
  1890. query_threads[i]->dset_charts = DSET_CHARTS;
  1891. query_threads[i]->dset_dims = DSET_DIMS;
  1892. query_threads[i]->history_seconds = HISTORY_SECONDS;
  1893. query_threads[i]->time_present = time_start;
  1894. query_threads[i]->done = 0;
  1895. query_threads[i]->errors = query_threads[i]->queries_nr = query_threads[i]->queried_metrics_nr = 0;
  1896. for (j = 0 ; j < DSET_CHARTS ; ++j) {
  1897. query_threads[i]->chart_threads[j] = chart_threads[j];
  1898. }
  1899. query_threads[i]->delete_old_data = DISK_SPACE_MB ? 1 : 0;
  1900. assert(0 == uv_thread_create(&query_threads[i]->thread, query_dbengine_chart, query_threads[i]));
  1901. }
  1902. sleep(TEST_DURATION_SEC);
  1903. /* stop workload */
  1904. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1905. chart_threads[i]->done = 1;
  1906. }
  1907. for (i = 0 ; i < QUERY_THREADS ; ++i) {
  1908. query_threads[i]->done = 1;
  1909. }
  1910. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1911. assert(0 == uv_thread_join(&chart_threads[i]->thread));
  1912. }
  1913. for (i = 0 ; i < QUERY_THREADS ; ++i) {
  1914. assert(0 == uv_thread_join(&query_threads[i]->thread));
  1915. }
  1916. test_duration = now_realtime_sec() - (time_start - HISTORY_SECONDS);
  1917. if (!test_duration)
  1918. test_duration = 1;
  1919. fprintf(stderr, "\nDB-engine stress test finished in %ld seconds.\n", test_duration);
  1920. unsigned long stored_metrics_nr = 0;
  1921. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1922. stored_metrics_nr += chart_threads[i]->stored_metrics_nr;
  1923. }
  1924. unsigned long queried_metrics_nr = 0;
  1925. for (i = 0 ; i < QUERY_THREADS ; ++i) {
  1926. queried_metrics_nr += query_threads[i]->queried_metrics_nr;
  1927. }
  1928. fprintf(stderr, "%u metrics were stored (dataset size of %lu MiB) in %u charts by 1 writer thread per chart.\n",
  1929. DSET_CHARTS * DSET_DIMS, stored_metrics_nr * sizeof(storage_number) / (1024 * 1024), DSET_CHARTS);
  1930. fprintf(stderr, "Metrics were being generated per 1 emulated second and time was accelerated.\n");
  1931. fprintf(stderr, "%lu metric data points were queried by %u reader threads.\n", queried_metrics_nr, QUERY_THREADS);
  1932. fprintf(stderr, "Query starting time is randomly chosen from the beginning of the time-series up to the time of\n"
  1933. "the latest data point, and ending time from 1 second up to 1 hour after the starting time.\n");
  1934. fprintf(stderr, "Performance is %lu written data points/sec and %lu read data points/sec.\n",
  1935. stored_metrics_nr / test_duration, queried_metrics_nr / test_duration);
  1936. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1937. freez(chart_threads[i]);
  1938. }
  1939. freez(chart_threads);
  1940. for (i = 0 ; i < QUERY_THREADS ; ++i) {
  1941. freez(query_threads[i]);
  1942. }
  1943. freez(query_threads);
  1944. rrd_wrlock();
  1945. rrdeng_prepare_exit(host->rrdeng_ctx);
  1946. rrdhost_delete_charts(host);
  1947. rrdeng_exit(host->rrdeng_ctx);
  1948. rrd_unlock();
  1949. }
  1950. #endif