unit_test.c 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294
  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. RRDR *r = rrd2rrdr(st[i], points, time_start + update_every, time_end, RRDR_GROUPING_AVERAGE, 0, 0, NULL, NULL, 0);
  1450. if (!r) {
  1451. fprintf(stderr, " DB-engine unittest %s: empty RRDR ### E R R O R ###\n", st[i]->name);
  1452. return ++errors;
  1453. } else {
  1454. assert(r->st == st[i]);
  1455. for (c = 0; c != rrdr_rows(r) ; ++c) {
  1456. RRDDIM *d;
  1457. time_now = time_start + (c + 2) * update_every;
  1458. time_retrieved = r->t[c];
  1459. // for each dimension
  1460. for (j = 0, d = r->st->dimensions ; d && j < r->d ; ++j, d = d->next) {
  1461. calculated_number *cn = &r->v[ c * r->d ];
  1462. value = cn[j];
  1463. assert(rd[i][j] == d);
  1464. last = i * DIMS * REGION_POINTS[current_region] + j * REGION_POINTS[current_region] + c + 1;
  1465. expected = unpack_storage_number(pack_storage_number((calculated_number)last, SN_DEFAULT_FLAGS));
  1466. same = (calculated_number_round(value) == calculated_number_round(expected)) ? 1 : 0;
  1467. if(!same) {
  1468. fprintf(stderr, " DB-engine unittest %s/%s: at %lu secs, expecting value "
  1469. CALCULATED_NUMBER_FORMAT ", RRDR found " CALCULATED_NUMBER_FORMAT ", ### E R R O R ###\n",
  1470. st[i]->name, rd[i][j]->name, (unsigned long)time_now, expected, value);
  1471. errors++;
  1472. }
  1473. if(time_retrieved != time_now) {
  1474. fprintf(stderr, " DB-engine unittest %s/%s: at %lu secs, found RRDR timestamp %lu ### E R R O R ###\n",
  1475. st[i]->name, rd[i][j]->name, (unsigned long)time_now, (unsigned long)time_retrieved);
  1476. errors++;
  1477. }
  1478. }
  1479. }
  1480. rrdr_free(r);
  1481. }
  1482. }
  1483. return errors;
  1484. }
  1485. int test_dbengine(void)
  1486. {
  1487. int i, j, errors, update_every, current_region;
  1488. RRDHOST *host = NULL;
  1489. RRDSET *st[CHARTS];
  1490. RRDDIM *rd[CHARTS][DIMS];
  1491. time_t time_start[REGIONS], time_end[REGIONS];
  1492. error_log_limit_unlimited();
  1493. fprintf(stderr, "\nRunning DB-engine test\n");
  1494. default_rrd_memory_mode = RRD_MEMORY_MODE_DBENGINE;
  1495. fprintf(stderr, "Initializing localhost with hostname 'unittest-dbengine'");
  1496. host = dbengine_rrdhost_find_or_create("unittest-dbengine");
  1497. if (NULL == host)
  1498. return 1;
  1499. current_region = 0; // this is the first region of data
  1500. update_every = REGION_UPDATE_EVERY[current_region]; // set data collection frequency to 2 seconds
  1501. test_dbengine_create_charts(host, st, rd, update_every);
  1502. time_start[current_region] = 2 * API_RELATIVE_TIME_MAX;
  1503. time_end[current_region] = test_dbengine_create_metrics(st,rd, current_region, time_start[current_region]);
  1504. errors = test_dbengine_check_metrics(st, rd, current_region, time_start[current_region]);
  1505. if (errors)
  1506. goto error_out;
  1507. current_region = 1; //this is the second region of data
  1508. update_every = REGION_UPDATE_EVERY[current_region]; // set data collection frequency to 3 seconds
  1509. // Align pages for frequency change
  1510. for (i = 0 ; i < CHARTS ; ++i) {
  1511. st[i]->update_every = update_every;
  1512. for (j = 0; j < DIMS; ++j) {
  1513. rrdeng_store_metric_flush_current_page(rd[i][j]);
  1514. }
  1515. }
  1516. time_start[current_region] = time_end[current_region - 1] + update_every;
  1517. if (0 != time_start[current_region] % update_every) // align to update_every
  1518. time_start[current_region] += update_every - time_start[current_region] % update_every;
  1519. time_end[current_region] = test_dbengine_create_metrics(st,rd, current_region, time_start[current_region]);
  1520. errors = test_dbengine_check_metrics(st, rd, current_region, time_start[current_region]);
  1521. if (errors)
  1522. goto error_out;
  1523. current_region = 2; //this is the third region of data
  1524. update_every = REGION_UPDATE_EVERY[current_region]; // set data collection frequency to 1 seconds
  1525. // Align pages for frequency change
  1526. for (i = 0 ; i < CHARTS ; ++i) {
  1527. st[i]->update_every = update_every;
  1528. for (j = 0; j < DIMS; ++j) {
  1529. rrdeng_store_metric_flush_current_page(rd[i][j]);
  1530. }
  1531. }
  1532. time_start[current_region] = time_end[current_region - 1] + update_every;
  1533. if (0 != time_start[current_region] % update_every) // align to update_every
  1534. time_start[current_region] += update_every - time_start[current_region] % update_every;
  1535. time_end[current_region] = test_dbengine_create_metrics(st,rd, current_region, time_start[current_region]);
  1536. errors = test_dbengine_check_metrics(st, rd, current_region, time_start[current_region]);
  1537. if (errors)
  1538. goto error_out;
  1539. for (current_region = 0 ; current_region < REGIONS ; ++current_region) {
  1540. errors = test_dbengine_check_rrdr(st, rd, current_region, time_start[current_region], time_end[current_region]);
  1541. if (errors)
  1542. goto error_out;
  1543. }
  1544. current_region = 1;
  1545. update_every = REGION_UPDATE_EVERY[current_region]; // use the maximum update_every = 3
  1546. errors = 0;
  1547. long points = (time_end[REGIONS - 1] - time_start[0]) / update_every; // cover all time regions with RRDR
  1548. long point_offset = (time_start[current_region] - time_start[0]) / update_every;
  1549. for (i = 0 ; i < CHARTS ; ++i) {
  1550. RRDR *r = rrd2rrdr(st[i], points, time_start[0] + update_every, time_end[REGIONS - 1], RRDR_GROUPING_AVERAGE, 0, 0, NULL, NULL, 0);
  1551. if (!r) {
  1552. fprintf(stderr, " DB-engine unittest %s: empty RRDR ### E R R O R ###\n", st[i]->name);
  1553. ++errors;
  1554. } else {
  1555. long c;
  1556. assert(r->st == st[i]);
  1557. // test current region values only, since they must be left unchanged
  1558. for (c = point_offset ; c < point_offset + rrdr_rows(r) / REGIONS / 2 ; ++c) {
  1559. RRDDIM *d;
  1560. time_t time_now = time_start[current_region] + (c - point_offset + 2) * update_every;
  1561. time_t time_retrieved = r->t[c];
  1562. // for each dimension
  1563. for(j = 0, d = r->st->dimensions ; d && j < r->d ; ++j, d = d->next) {
  1564. calculated_number *cn = &r->v[ c * r->d ];
  1565. calculated_number value = cn[j];
  1566. assert(rd[i][j] == d);
  1567. collected_number last = i * DIMS * REGION_POINTS[current_region] + j * REGION_POINTS[current_region] + c - point_offset + 1;
  1568. calculated_number expected = unpack_storage_number(pack_storage_number((calculated_number)last, SN_DEFAULT_FLAGS));
  1569. uint8_t same = (calculated_number_round(value) == calculated_number_round(expected)) ? 1 : 0;
  1570. if(!same) {
  1571. fprintf(stderr, " DB-engine unittest %s/%s: at %lu secs, expecting value "
  1572. CALCULATED_NUMBER_FORMAT ", RRDR found " CALCULATED_NUMBER_FORMAT ", ### E R R O R ###\n",
  1573. st[i]->name, rd[i][j]->name, (unsigned long)time_now, expected, value);
  1574. errors++;
  1575. }
  1576. if(time_retrieved != time_now) {
  1577. fprintf(stderr, " DB-engine unittest %s/%s: at %lu secs, found RRDR timestamp %lu ### E R R O R ###\n",
  1578. st[i]->name, rd[i][j]->name, (unsigned long)time_now, (unsigned long)time_retrieved);
  1579. errors++;
  1580. }
  1581. }
  1582. }
  1583. rrdr_free(r);
  1584. }
  1585. }
  1586. error_out:
  1587. rrd_wrlock();
  1588. rrdeng_prepare_exit(host->rrdeng_ctx);
  1589. rrdhost_delete_charts(host);
  1590. rrdeng_exit(host->rrdeng_ctx);
  1591. rrd_unlock();
  1592. return errors;
  1593. }
  1594. struct dbengine_chart_thread {
  1595. uv_thread_t thread;
  1596. RRDHOST *host;
  1597. char *chartname; /* Will be prefixed by type, e.g. "example_local1.", "example_local2." etc */
  1598. unsigned dset_charts; /* number of charts */
  1599. unsigned dset_dims; /* dimensions per chart */
  1600. unsigned chart_i; /* current chart offset */
  1601. time_t time_present; /* current virtual time of the benchmark */
  1602. volatile time_t time_max; /* latest timestamp of stored values */
  1603. unsigned history_seconds; /* how far back in the past to go */
  1604. volatile long done; /* initialize to 0, set to 1 to stop thread */
  1605. struct completion charts_initialized;
  1606. unsigned long errors, stored_metrics_nr; /* statistics */
  1607. RRDSET *st;
  1608. RRDDIM *rd[]; /* dset_dims elements */
  1609. };
  1610. collected_number generate_dbengine_chart_value(int chart_i, int dim_i, time_t time_current)
  1611. {
  1612. collected_number value;
  1613. value = ((collected_number)time_current) * (chart_i + 1);
  1614. value += ((collected_number)time_current) * (dim_i + 1);
  1615. value %= 1024LLU;
  1616. return value;
  1617. }
  1618. static void generate_dbengine_chart(void *arg)
  1619. {
  1620. struct dbengine_chart_thread *thread_info = (struct dbengine_chart_thread *)arg;
  1621. RRDHOST *host = thread_info->host;
  1622. char *chartname = thread_info->chartname;
  1623. const unsigned DSET_DIMS = thread_info->dset_dims;
  1624. unsigned history_seconds = thread_info->history_seconds;
  1625. time_t time_present = thread_info->time_present;
  1626. unsigned j, update_every = 1;
  1627. RRDSET *st;
  1628. RRDDIM *rd[DSET_DIMS];
  1629. char name[RRD_ID_LENGTH_MAX + 1];
  1630. time_t time_current;
  1631. // create the chart
  1632. snprintfz(name, RRD_ID_LENGTH_MAX, "example_local%u", thread_info->chart_i + 1);
  1633. thread_info->st = st = rrdset_create(host, name, chartname, chartname, "example", NULL, chartname, chartname,
  1634. chartname, NULL, 1, update_every, RRDSET_TYPE_LINE);
  1635. for (j = 0 ; j < DSET_DIMS ; ++j) {
  1636. snprintfz(name, RRD_ID_LENGTH_MAX, "%s%u", chartname, j + 1);
  1637. thread_info->rd[j] = rd[j] = rrddim_add(st, name, NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  1638. }
  1639. completion_mark_complete(&thread_info->charts_initialized);
  1640. // feed it with the test data
  1641. time_current = time_present - history_seconds;
  1642. for (j = 0 ; j < DSET_DIMS ; ++j) {
  1643. rd[j]->last_collected_time.tv_sec =
  1644. st->last_collected_time.tv_sec = st->last_updated.tv_sec = time_current - update_every;
  1645. rd[j]->last_collected_time.tv_usec =
  1646. st->last_collected_time.tv_usec = st->last_updated.tv_usec = 0;
  1647. }
  1648. for( ; !thread_info->done && time_current < time_present ; time_current += update_every) {
  1649. st->usec_since_last_update = USEC_PER_SEC * update_every;
  1650. for (j = 0; j < DSET_DIMS; ++j) {
  1651. collected_number value;
  1652. value = generate_dbengine_chart_value(thread_info->chart_i, j, time_current);
  1653. rrddim_set_by_pointer_fake_time(rd[j], value, time_current);
  1654. ++thread_info->stored_metrics_nr;
  1655. }
  1656. rrdset_done(st);
  1657. thread_info->time_max = time_current;
  1658. }
  1659. for (j = 0; j < DSET_DIMS; ++j) {
  1660. rrdeng_store_metric_finalize(rd[j]);
  1661. }
  1662. }
  1663. void generate_dbengine_dataset(unsigned history_seconds)
  1664. {
  1665. const int DSET_CHARTS = 16;
  1666. const int DSET_DIMS = 128;
  1667. const uint64_t EXPECTED_COMPRESSION_RATIO = 20;
  1668. RRDHOST *host = NULL;
  1669. struct dbengine_chart_thread **thread_info;
  1670. int i;
  1671. time_t time_present;
  1672. default_rrd_memory_mode = RRD_MEMORY_MODE_DBENGINE;
  1673. default_rrdeng_page_cache_mb = 128;
  1674. // Worst case for uncompressible data
  1675. default_rrdeng_disk_quota_mb = (((uint64_t)DSET_DIMS * DSET_CHARTS) * sizeof(storage_number) * history_seconds) /
  1676. (1024 * 1024);
  1677. default_rrdeng_disk_quota_mb -= default_rrdeng_disk_quota_mb * EXPECTED_COMPRESSION_RATIO / 100;
  1678. error_log_limit_unlimited();
  1679. fprintf(stderr, "Initializing localhost with hostname 'dbengine-dataset'");
  1680. host = dbengine_rrdhost_find_or_create("dbengine-dataset");
  1681. if (NULL == host)
  1682. return;
  1683. thread_info = mallocz(sizeof(*thread_info) * DSET_CHARTS);
  1684. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1685. thread_info[i] = mallocz(sizeof(*thread_info[i]) + sizeof(RRDDIM *) * DSET_DIMS);
  1686. }
  1687. fprintf(stderr, "\nRunning DB-engine workload generator\n");
  1688. time_present = now_realtime_sec();
  1689. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1690. thread_info[i]->host = host;
  1691. thread_info[i]->chartname = "random";
  1692. thread_info[i]->dset_charts = DSET_CHARTS;
  1693. thread_info[i]->chart_i = i;
  1694. thread_info[i]->dset_dims = DSET_DIMS;
  1695. thread_info[i]->history_seconds = history_seconds;
  1696. thread_info[i]->time_present = time_present;
  1697. thread_info[i]->time_max = 0;
  1698. thread_info[i]->done = 0;
  1699. completion_init(&thread_info[i]->charts_initialized);
  1700. assert(0 == uv_thread_create(&thread_info[i]->thread, generate_dbengine_chart, thread_info[i]));
  1701. completion_wait_for(&thread_info[i]->charts_initialized);
  1702. completion_destroy(&thread_info[i]->charts_initialized);
  1703. }
  1704. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1705. assert(0 == uv_thread_join(&thread_info[i]->thread));
  1706. }
  1707. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1708. freez(thread_info[i]);
  1709. }
  1710. freez(thread_info);
  1711. rrd_wrlock();
  1712. rrdhost_free(host);
  1713. rrd_unlock();
  1714. }
  1715. struct dbengine_query_thread {
  1716. uv_thread_t thread;
  1717. RRDHOST *host;
  1718. char *chartname; /* Will be prefixed by type, e.g. "example_local1.", "example_local2." etc */
  1719. unsigned dset_charts; /* number of charts */
  1720. unsigned dset_dims; /* dimensions per chart */
  1721. time_t time_present; /* current virtual time of the benchmark */
  1722. unsigned history_seconds; /* how far back in the past to go */
  1723. volatile long done; /* initialize to 0, set to 1 to stop thread */
  1724. unsigned long errors, queries_nr, queried_metrics_nr; /* statistics */
  1725. uint8_t delete_old_data; /* if non zero then data are deleted when disk space is exhausted */
  1726. struct dbengine_chart_thread *chart_threads[]; /* dset_charts elements */
  1727. };
  1728. static void query_dbengine_chart(void *arg)
  1729. {
  1730. struct dbengine_query_thread *thread_info = (struct dbengine_query_thread *)arg;
  1731. const int DSET_CHARTS = thread_info->dset_charts;
  1732. const int DSET_DIMS = thread_info->dset_dims;
  1733. time_t time_after, time_before, time_min, time_approx_min, time_max, duration;
  1734. int i, j, update_every = 1;
  1735. RRDSET *st;
  1736. RRDDIM *rd;
  1737. uint8_t same;
  1738. time_t time_now, time_retrieved;
  1739. collected_number generatedv;
  1740. calculated_number value, expected;
  1741. storage_number n;
  1742. struct rrddim_query_handle handle;
  1743. do {
  1744. // pick a chart and dimension
  1745. i = random() % DSET_CHARTS;
  1746. st = thread_info->chart_threads[i]->st;
  1747. j = random() % DSET_DIMS;
  1748. rd = thread_info->chart_threads[i]->rd[j];
  1749. time_min = thread_info->time_present - thread_info->history_seconds + 1;
  1750. time_max = thread_info->chart_threads[i]->time_max;
  1751. if (thread_info->delete_old_data) {
  1752. /* A time window of twice the disk space is sufficient for compression space savings of up to 50% */
  1753. time_approx_min = time_max - (default_rrdeng_disk_quota_mb * 2 * 1024 * 1024) /
  1754. (((uint64_t) DSET_DIMS * DSET_CHARTS) * sizeof(storage_number));
  1755. time_min = MAX(time_min, time_approx_min);
  1756. }
  1757. if (!time_max) {
  1758. time_before = time_after = time_min;
  1759. } else {
  1760. time_after = time_min + random() % (MAX(time_max - time_min, 1));
  1761. duration = random() % 3600;
  1762. time_before = MIN(time_after + duration, time_max); /* up to 1 hour queries */
  1763. }
  1764. rd->state->query_ops.init(rd, &handle, time_after, time_before);
  1765. ++thread_info->queries_nr;
  1766. for (time_now = time_after ; time_now <= time_before ; time_now += update_every) {
  1767. generatedv = generate_dbengine_chart_value(i, j, time_now);
  1768. expected = unpack_storage_number(pack_storage_number((calculated_number) generatedv, SN_DEFAULT_FLAGS));
  1769. if (unlikely(rd->state->query_ops.is_finished(&handle))) {
  1770. if (!thread_info->delete_old_data) { /* data validation only when we don't delete */
  1771. fprintf(stderr, " DB-engine stresstest %s/%s: at %lu secs, expecting value "
  1772. CALCULATED_NUMBER_FORMAT ", found data gap, ### E R R O R ###\n",
  1773. st->name, rd->name, (unsigned long) time_now, expected);
  1774. ++thread_info->errors;
  1775. }
  1776. break;
  1777. }
  1778. n = rd->state->query_ops.next_metric(&handle, &time_retrieved);
  1779. if (SN_EMPTY_SLOT == n) {
  1780. if (!thread_info->delete_old_data) { /* data validation only when we don't delete */
  1781. fprintf(stderr, " DB-engine stresstest %s/%s: at %lu secs, expecting value "
  1782. CALCULATED_NUMBER_FORMAT ", found data gap, ### E R R O R ###\n",
  1783. st->name, rd->name, (unsigned long) time_now, expected);
  1784. ++thread_info->errors;
  1785. }
  1786. break;
  1787. }
  1788. ++thread_info->queried_metrics_nr;
  1789. value = unpack_storage_number(n);
  1790. same = (calculated_number_round(value) == calculated_number_round(expected)) ? 1 : 0;
  1791. if (!same) {
  1792. if (!thread_info->delete_old_data) { /* data validation only when we don't delete */
  1793. fprintf(stderr, " DB-engine stresstest %s/%s: at %lu secs, expecting value "
  1794. CALCULATED_NUMBER_FORMAT ", found " CALCULATED_NUMBER_FORMAT
  1795. ", ### E R R O R ###\n",
  1796. st->name, rd->name, (unsigned long) time_now, expected, value);
  1797. ++thread_info->errors;
  1798. }
  1799. }
  1800. if (time_retrieved != time_now) {
  1801. if (!thread_info->delete_old_data) { /* data validation only when we don't delete */
  1802. fprintf(stderr,
  1803. " DB-engine stresstest %s/%s: at %lu secs, found timestamp %lu ### E R R O R ###\n",
  1804. st->name, rd->name, (unsigned long) time_now, (unsigned long) time_retrieved);
  1805. ++thread_info->errors;
  1806. }
  1807. }
  1808. }
  1809. rd->state->query_ops.finalize(&handle);
  1810. } while(!thread_info->done);
  1811. }
  1812. void dbengine_stress_test(unsigned TEST_DURATION_SEC, unsigned DSET_CHARTS, unsigned QUERY_THREADS,
  1813. unsigned RAMP_UP_SECONDS, unsigned PAGE_CACHE_MB, unsigned DISK_SPACE_MB)
  1814. {
  1815. const unsigned DSET_DIMS = 128;
  1816. const uint64_t EXPECTED_COMPRESSION_RATIO = 20;
  1817. const unsigned HISTORY_SECONDS = 3600 * 24 * 365 * 50; /* 50 year of history */
  1818. RRDHOST *host = NULL;
  1819. struct dbengine_chart_thread **chart_threads;
  1820. struct dbengine_query_thread **query_threads;
  1821. unsigned i, j;
  1822. time_t time_start, test_duration;
  1823. error_log_limit_unlimited();
  1824. if (!TEST_DURATION_SEC)
  1825. TEST_DURATION_SEC = 10;
  1826. if (!DSET_CHARTS)
  1827. DSET_CHARTS = 1;
  1828. if (!QUERY_THREADS)
  1829. QUERY_THREADS = 1;
  1830. if (PAGE_CACHE_MB < RRDENG_MIN_PAGE_CACHE_SIZE_MB)
  1831. PAGE_CACHE_MB = RRDENG_MIN_PAGE_CACHE_SIZE_MB;
  1832. default_rrd_memory_mode = RRD_MEMORY_MODE_DBENGINE;
  1833. default_rrdeng_page_cache_mb = PAGE_CACHE_MB;
  1834. if (DISK_SPACE_MB) {
  1835. fprintf(stderr, "By setting disk space limit data are allowed to be deleted. "
  1836. "Data validation is turned off for this run.\n");
  1837. default_rrdeng_disk_quota_mb = DISK_SPACE_MB;
  1838. } else {
  1839. // Worst case for uncompressible data
  1840. default_rrdeng_disk_quota_mb =
  1841. (((uint64_t) DSET_DIMS * DSET_CHARTS) * sizeof(storage_number) * HISTORY_SECONDS) / (1024 * 1024);
  1842. default_rrdeng_disk_quota_mb -= default_rrdeng_disk_quota_mb * EXPECTED_COMPRESSION_RATIO / 100;
  1843. }
  1844. fprintf(stderr, "Initializing localhost with hostname 'dbengine-stress-test'\n");
  1845. host = dbengine_rrdhost_find_or_create("dbengine-stress-test");
  1846. if (NULL == host)
  1847. return;
  1848. chart_threads = mallocz(sizeof(*chart_threads) * DSET_CHARTS);
  1849. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1850. chart_threads[i] = mallocz(sizeof(*chart_threads[i]) + sizeof(RRDDIM *) * DSET_DIMS);
  1851. }
  1852. query_threads = mallocz(sizeof(*query_threads) * QUERY_THREADS);
  1853. for (i = 0 ; i < QUERY_THREADS ; ++i) {
  1854. query_threads[i] = mallocz(sizeof(*query_threads[i]) + sizeof(struct dbengine_chart_thread *) * DSET_CHARTS);
  1855. }
  1856. fprintf(stderr, "\nRunning DB-engine stress test, %u seconds writers ramp-up time,\n"
  1857. "%u seconds of concurrent readers and writers, %u writer threads, %u reader threads,\n"
  1858. "%u MiB of page cache.\n",
  1859. RAMP_UP_SECONDS, TEST_DURATION_SEC, DSET_CHARTS, QUERY_THREADS, PAGE_CACHE_MB);
  1860. time_start = now_realtime_sec() + HISTORY_SECONDS; /* move history to the future */
  1861. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1862. chart_threads[i]->host = host;
  1863. chart_threads[i]->chartname = "random";
  1864. chart_threads[i]->dset_charts = DSET_CHARTS;
  1865. chart_threads[i]->chart_i = i;
  1866. chart_threads[i]->dset_dims = DSET_DIMS;
  1867. chart_threads[i]->history_seconds = HISTORY_SECONDS;
  1868. chart_threads[i]->time_present = time_start;
  1869. chart_threads[i]->time_max = 0;
  1870. chart_threads[i]->done = 0;
  1871. chart_threads[i]->errors = chart_threads[i]->stored_metrics_nr = 0;
  1872. completion_init(&chart_threads[i]->charts_initialized);
  1873. assert(0 == uv_thread_create(&chart_threads[i]->thread, generate_dbengine_chart, chart_threads[i]));
  1874. }
  1875. /* barrier so that subsequent queries can access valid chart data */
  1876. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1877. completion_wait_for(&chart_threads[i]->charts_initialized);
  1878. completion_destroy(&chart_threads[i]->charts_initialized);
  1879. }
  1880. sleep(RAMP_UP_SECONDS);
  1881. /* at this point data have already began being written to the database */
  1882. for (i = 0 ; i < QUERY_THREADS ; ++i) {
  1883. query_threads[i]->host = host;
  1884. query_threads[i]->chartname = "random";
  1885. query_threads[i]->dset_charts = DSET_CHARTS;
  1886. query_threads[i]->dset_dims = DSET_DIMS;
  1887. query_threads[i]->history_seconds = HISTORY_SECONDS;
  1888. query_threads[i]->time_present = time_start;
  1889. query_threads[i]->done = 0;
  1890. query_threads[i]->errors = query_threads[i]->queries_nr = query_threads[i]->queried_metrics_nr = 0;
  1891. for (j = 0 ; j < DSET_CHARTS ; ++j) {
  1892. query_threads[i]->chart_threads[j] = chart_threads[j];
  1893. }
  1894. query_threads[i]->delete_old_data = DISK_SPACE_MB ? 1 : 0;
  1895. assert(0 == uv_thread_create(&query_threads[i]->thread, query_dbengine_chart, query_threads[i]));
  1896. }
  1897. sleep(TEST_DURATION_SEC);
  1898. /* stop workload */
  1899. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1900. chart_threads[i]->done = 1;
  1901. }
  1902. for (i = 0 ; i < QUERY_THREADS ; ++i) {
  1903. query_threads[i]->done = 1;
  1904. }
  1905. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1906. assert(0 == uv_thread_join(&chart_threads[i]->thread));
  1907. }
  1908. for (i = 0 ; i < QUERY_THREADS ; ++i) {
  1909. assert(0 == uv_thread_join(&query_threads[i]->thread));
  1910. }
  1911. test_duration = now_realtime_sec() - (time_start - HISTORY_SECONDS);
  1912. if (!test_duration)
  1913. test_duration = 1;
  1914. fprintf(stderr, "\nDB-engine stress test finished in %ld seconds.\n", test_duration);
  1915. unsigned long stored_metrics_nr = 0;
  1916. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1917. stored_metrics_nr += chart_threads[i]->stored_metrics_nr;
  1918. }
  1919. unsigned long queries_nr = 0, queried_metrics_nr = 0;
  1920. for (i = 0 ; i < QUERY_THREADS ; ++i) {
  1921. queries_nr += query_threads[i]->queries_nr;
  1922. queried_metrics_nr += query_threads[i]->queried_metrics_nr;
  1923. }
  1924. fprintf(stderr, "%u metrics were stored (dataset size of %lu MiB) in %u charts by 1 writer thread per chart.\n",
  1925. DSET_CHARTS * DSET_DIMS, stored_metrics_nr * sizeof(storage_number) / (1024 * 1024), DSET_CHARTS);
  1926. fprintf(stderr, "Metrics were being generated per 1 emulated second and time was accelerated.\n");
  1927. fprintf(stderr, "%lu metric data points were queried by %u reader threads.\n", queried_metrics_nr, QUERY_THREADS);
  1928. fprintf(stderr, "Query starting time is randomly chosen from the beginning of the time-series up to the time of\n"
  1929. "the latest data point, and ending time from 1 second up to 1 hour after the starting time.\n");
  1930. fprintf(stderr, "Performance is %lu written data points/sec and %lu read data points/sec.\n",
  1931. stored_metrics_nr / test_duration, queried_metrics_nr / test_duration);
  1932. for (i = 0 ; i < DSET_CHARTS ; ++i) {
  1933. freez(chart_threads[i]);
  1934. }
  1935. freez(chart_threads);
  1936. for (i = 0 ; i < QUERY_THREADS ; ++i) {
  1937. freez(query_threads[i]);
  1938. }
  1939. freez(query_threads);
  1940. rrd_wrlock();
  1941. rrdeng_prepare_exit(host->rrdeng_ctx);
  1942. rrdhost_delete_charts(host);
  1943. rrdeng_exit(host->rrdeng_ctx);
  1944. rrd_unlock();
  1945. }
  1946. #endif