MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / pfselftestCommand

Function pfselftestCommand

src/hyperloglog.cpp:1399–1502  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1397 * Something that is not easy to test from within the outside. */
1398#define HLL_TEST_CYCLES 1000
1399void pfselftestCommand(client *c) {
1400 unsigned int j, i;
1401 sds bitcounters = sdsnewlen(NULL,HLL_DENSE_SIZE);
1402 struct hllhdr *hdr = (struct hllhdr*) bitcounters, *hdr2;
1403 robj *o = NULL;
1404 uint8_t bytecounters[HLL_REGISTERS];
1405 int64_t checkpoint = 1;
1406 uint64_t seed = 0;
1407 uint64_t ele;
1408 double relerr = 0;
1409
1410 /* Test 1: access registers.
1411 * The test is conceived to test that the different counters of our data
1412 * structure are accessible and that setting their values both result in
1413 * the correct value to be retained and not affect adjacent values. */
1414 for (j = 0; j < HLL_TEST_CYCLES; j++) {
1415 /* Set the HLL counters and an array of unsigned byes of the
1416 * same size to the same set of random values. */
1417 for (i = 0; i < HLL_REGISTERS; i++) {
1418 unsigned int r = rand() & HLL_REGISTER_MAX;
1419
1420 bytecounters[i] = r;
1421 HLL_DENSE_SET_REGISTER(hdr->registers(),i,r);
1422 }
1423 /* Check that we are able to retrieve the same values. */
1424 for (i = 0; i < HLL_REGISTERS; i++) {
1425 unsigned int val;
1426
1427 HLL_DENSE_GET_REGISTER(val,hdr->registers(),i);
1428 if (val != bytecounters[i]) {
1429 addReplyErrorFormat(c,
1430 "TESTFAILED Register %d should be %d but is %d",
1431 i, (int) bytecounters[i], (int) val);
1432 goto cleanup;
1433 }
1434 }
1435 }
1436
1437 /* Test 2: approximation error.
1438 * The test adds unique elements and check that the estimated value
1439 * is always reasonable bounds.
1440 *
1441 * We check that the error is smaller than a few times than the expected
1442 * standard error, to make it very unlikely for the test to fail because
1443 * of a "bad" run.
1444 *
1445 * The test is performed with both dense and sparse HLLs at the same
1446 * time also verifying that the computed cardinality is the same. */
1447 memset(hdr->registers(),0,HLL_DENSE_SIZE-HLL_HDR_SIZE);
1448 o = createHLLObject();
1449 relerr = 1.04/sqrt(HLL_REGISTERS);
1450 checkpoint = 1;
1451 seed = (uint64_t)rand() | (uint64_t)rand() << 32;
1452 for (j = 1; j <= 10000000; j++) {
1453 ele = j ^ seed;
1454 hllDenseAdd(hdr->registers(),(unsigned char*)&ele,sizeof(ele));
1455 hllAdd(o,(unsigned char*)&ele,sizeof(ele));
1456

Callers

nothing calls this directly

Calls 12

sdsnewlenFunction · 0.85
addReplyErrorFormatFunction · 0.85
createHLLObjectFunction · 0.85
hllDenseAddFunction · 0.85
hllAddFunction · 0.85
ptrFromObjFunction · 0.85
addReplyErrorFunction · 0.85
hllCountFunction · 0.85
addReplyFunction · 0.85
sdsfreeFunction · 0.85
decrRefCountFunction · 0.85
registersMethod · 0.80

Tested by

no test coverage detected