📄 testblobs.cpp
字号:
m_on = m_ms = m_cnt = m_time[0] = m_text[0] = 0; } void on() { assert(m_on == 0); m_on = NdbTick_CurrentMillisecond(); } void off(unsigned cnt = 0) { NDB_TICKS off = NdbTick_CurrentMillisecond(); assert(m_on != 0 && off >= m_on); m_ms += off - m_on; m_cnt += cnt; m_on = 0; } const char* time() { if (m_cnt == 0) sprintf(m_time, "%u ms", m_ms); else sprintf(m_time, "%u ms per %u ( %u ms per 1000 )", m_ms, m_cnt, (1000 * m_ms) / m_cnt); return m_time; } const char* pct (const Tmr& t1) { if (0 < t1.m_ms) sprintf(m_text, "%u pct", (100 * m_ms) / t1.m_ms); else sprintf(m_text, "[cannot measure]"); return m_text; } const char* over(const Tmr& t1) { if (0 < t1.m_ms) { if (t1.m_ms <= m_ms) sprintf(m_text, "%u pct", (100 * (m_ms - t1.m_ms)) / t1.m_ms); else sprintf(m_text, "-%u pct", (100 * (t1.m_ms - m_ms)) / t1.m_ms); } else sprintf(m_text, "[cannot measure]"); return m_text; } NDB_TICKS m_on; unsigned m_ms; unsigned m_cnt; char m_time[100]; char m_text[100];};static inttestperf(){ if (! testcase('p')) return 0; DBG("=== perf test ==="); g_bh1 = g_bh2 = 0; g_ndb = new Ndb(g_ncc, "TEST_DB"); CHK(g_ndb->init() == 0); CHK(g_ndb->waitUntilReady() == 0); g_dic = g_ndb->getDictionary(); NdbDictionary::Table tab(g_opt.m_tnameperf); if (g_dic->getTable(tab.getName()) != 0) CHK(g_dic->dropTable(tab) == 0); // col A - pk { NdbDictionary::Column col("A"); col.setType(NdbDictionary::Column::Unsigned); col.setPrimaryKey(true); tab.addColumn(col); } // col B - char 20 { NdbDictionary::Column col("B"); col.setType(NdbDictionary::Column::Char); col.setLength(20); col.setNullable(true); tab.addColumn(col); } // col C - text { NdbDictionary::Column col("C"); col.setType(NdbDictionary::Column::Text); col.setInlineSize(20); col.setPartSize(512); col.setStripeSize(1); col.setNullable(true); tab.addColumn(col); } // create CHK(g_dic->createTable(tab) == 0); Uint32 cA = 0, cB = 1, cC = 2; // timers Tmr t1; Tmr t2; // insert char (one trans) { DBG("--- insert char ---"); char b[20]; t1.on(); CHK((g_con = g_ndb->startTransaction()) != 0); for (Uint32 k = 0; k < g_opt.m_rowsperf; k++) { CHK((g_opr = g_con->getNdbOperation(tab.getName())) != 0); CHK(g_opr->insertTuple() == 0); CHK(g_opr->equal(cA, (char*)&k) == 0); memset(b, 0x20, sizeof(b)); b[0] = 'b'; CHK(g_opr->setValue(cB, b) == 0); CHK(g_con->execute(NoCommit) == 0); } t1.off(g_opt.m_rowsperf); CHK(g_con->execute(Rollback) == 0); DBG(t1.time()); g_opr = 0; g_con = 0; } // insert text (one trans) { DBG("--- insert text ---"); t2.on(); CHK((g_con = g_ndb->startTransaction()) != 0); for (Uint32 k = 0; k < g_opt.m_rowsperf; k++) { CHK((g_opr = g_con->getNdbOperation(tab.getName())) != 0); CHK(g_opr->insertTuple() == 0); CHK(g_opr->equal(cA, (char*)&k) == 0); CHK((g_bh1 = g_opr->getBlobHandle(cC)) != 0); CHK((g_bh1->setValue("c", 1) == 0)); CHK(g_con->execute(NoCommit) == 0); } t2.off(g_opt.m_rowsperf); CHK(g_con->execute(Rollback) == 0); DBG(t2.time()); g_bh1 = 0; g_opr = 0; g_con = 0; } // insert overhead DBG("insert overhead: " << t2.over(t1)); t1.clr(); t2.clr(); // insert { DBG("--- insert for read test ---"); unsigned n = 0; char b[20]; CHK((g_con = g_ndb->startTransaction()) != 0); for (Uint32 k = 0; k < g_opt.m_rowsperf; k++) { CHK((g_opr = g_con->getNdbOperation(tab.getName())) != 0); CHK(g_opr->insertTuple() == 0); CHK(g_opr->equal(cA, (char*)&k) == 0); memset(b, 0x20, sizeof(b)); b[0] = 'b'; CHK(g_opr->setValue(cB, b) == 0); CHK((g_bh1 = g_opr->getBlobHandle(cC)) != 0); CHK((g_bh1->setValue("c", 1) == 0)); if (++n == g_opt.m_batch) { CHK(g_con->execute(Commit) == 0); g_ndb->closeTransaction(g_con); CHK((g_con = g_ndb->startTransaction()) != 0); n = 0; } } if (n != 0) { CHK(g_con->execute(Commit) == 0); g_ndb->closeTransaction(g_con); g_con = 0; n = 0; } g_bh1 = 0; g_opr = 0; } // pk read char (one trans) { DBG("--- pk read char ---"); CHK((g_con = g_ndb->startTransaction()) != 0); Uint32 a; char b[20]; t1.on(); for (Uint32 k = 0; k < g_opt.m_rowsperf; k++) { CHK((g_opr = g_con->getNdbOperation(tab.getName())) != 0); CHK(g_opr->readTuple() == 0); CHK(g_opr->equal(cA, (char*)&k) == 0); CHK(g_opr->getValue(cA, (char*)&a) != 0); CHK(g_opr->getValue(cB, b) != 0); a = (Uint32)-1; b[0] = 0; CHK(g_con->execute(NoCommit) == 0); CHK(a == k && b[0] == 'b'); } CHK(g_con->execute(Commit) == 0); t1.off(g_opt.m_rowsperf); DBG(t1.time()); g_opr = 0; g_ndb->closeTransaction(g_con); g_con = 0; } // pk read text (one trans) { DBG("--- pk read text ---"); CHK((g_con = g_ndb->startTransaction()) != 0); Uint32 a; char c[20]; t2.on(); for (Uint32 k = 0; k < g_opt.m_rowsperf; k++) { CHK((g_opr = g_con->getNdbOperation(tab.getName())) != 0); CHK(g_opr->readTuple() == 0); CHK(g_opr->equal(cA, (char*)&k) == 0); CHK(g_opr->getValue(cA, (char*)&a) != 0); CHK((g_bh1 = g_opr->getBlobHandle(cC)) != 0); a = (Uint32)-1; c[0] = 0; CHK(g_con->execute(NoCommit) == 0); Uint32 m = 20; CHK(g_bh1->readData(c, m) == 0); CHK(a == k && m == 1 && c[0] == 'c'); } CHK(g_con->execute(Commit) == 0); t2.off(g_opt.m_rowsperf); DBG(t2.time()); g_ndb->closeTransaction(g_con); g_opr = 0; g_con = 0; } // pk read overhead DBG("pk read overhead: " << t2.over(t1)); t1.clr(); t2.clr(); // scan read char { DBG("--- scan read char ---"); Uint32 a; char b[20]; CHK((g_con = g_ndb->startTransaction()) != 0); CHK((g_ops = g_con->getNdbScanOperation(tab.getName())) != 0); CHK(g_ops->readTuples(NdbScanOperation::LM_Read) == 0); CHK(g_ops->getValue(cA, (char*)&a) != 0); CHK(g_ops->getValue(cB, b) != 0); CHK(g_con->execute(NoCommit) == 0); unsigned n = 0; t1.on(); while (1) { a = (Uint32)-1; b[0] = 0; int ret; CHK((ret = g_ops->nextResult(true)) == 0 || ret == 1); if (ret == 1) break; CHK(a < g_opt.m_rowsperf && b[0] == 'b'); n++; } CHK(n == g_opt.m_rowsperf); t1.off(g_opt.m_rowsperf); DBG(t1.time()); g_ndb->closeTransaction(g_con); g_ops = 0; g_con = 0; } // scan read text { DBG("--- read text ---"); Uint32 a; char c[20]; CHK((g_con = g_ndb->startTransaction()) != 0); CHK((g_ops = g_con->getNdbScanOperation(tab.getName())) != 0); CHK(g_ops->readTuples(NdbScanOperation::LM_Read) == 0); CHK(g_ops->getValue(cA, (char*)&a) != 0); CHK((g_bh1 = g_ops->getBlobHandle(cC)) != 0); CHK(g_con->execute(NoCommit) == 0); unsigned n = 0; t2.on(); while (1) { a = (Uint32)-1; c[0] = 0; int ret; CHK((ret = g_ops->nextResult(true)) == 0 || ret == 1); if (ret == 1) break; Uint32 m = 20; CHK(g_bh1->readData(c, m) == 0); CHK(a < g_opt.m_rowsperf && m == 1 && c[0] == 'c'); n++; } CHK(n == g_opt.m_rowsperf); t2.off(g_opt.m_rowsperf); DBG(t2.time()); g_bh1 = 0; g_ops = 0; g_ndb->closeTransaction(g_con); g_con = 0; } // scan read overhead DBG("scan read overhead: " << t2.over(t1)); t1.clr(); t2.clr(); delete g_ndb; return 0;}// bug testsstatic intbugtest_4088(){ unsigned i; DBG("bug test 4088 - ndb api hang with mixed ops on index table"); // insert rows calcTups(false); CHK(insertPk(false) == 0); // new trans CHK((g_con = g_ndb->startTransaction()) != 0); for (unsigned k = 0; k < g_opt.m_rows; k++) { Tup& tup = g_tups[k]; // read table pk via index as a table const unsigned pkcnt = 2; Tup pktup[pkcnt]; for (i = 0; i < pkcnt; i++) { char name[20]; // XXX guess table id sprintf(name, "%d/%s", 4, g_opt.m_x1name); CHK((g_opr = g_con->getNdbOperation(name)) != 0); CHK(g_opr->readTuple() == 0); CHK(g_opr->equal("PK2", tup.m_pk2) == 0); CHK(g_opr->getValue("NDB$PK", (char*)&pktup[i].m_pk1) != 0); } // read blob inline via index as an index CHK((g_opx = g_con->getNdbIndexOperation(g_opt.m_x1name, g_opt.m_tname)) != 0); CHK(g_opx->readTuple() == 0); CHK(g_opx->equal("PK2", tup.m_pk2) == 0); assert(tup.m_blob1.m_buf != 0); CHK(g_opx->getValue("BL1", (char*)tup.m_blob1.m_buf) != 0); // execute // BUG 4088: gets 1 tckeyconf, 1 tcindxconf, then hangs CHK(g_con->execute(Commit) == 0); // verify for (i = 0; i < pkcnt; i++) { CHK(pktup[i].m_pk1 == tup.m_pk1); CHK(memcmp(pktup[i].m_pk2, tup.m_pk2, g_opt.m_pk2len) == 0); } CHK(memcmp(tup.m_blob1.m_val, tup.m_blob1.m_buf, 8 + g_opt.m_blob1.m_inline) == 0); } return 0;}static intbugtest_2222(){ return 0;}static intbugtest_3333(){ return 0;}static struct { int m_bug; int (*m_test)();} g_bugtest[] = { { 4088, bugtest_4088 }};NDB_COMMAND(testOdbcDriver, "testBlobs", "testBlobs", "testBlobs", 65535){ ndb_init(); while (++argv, --argc > 0) { const char* arg = argv[0]; if (strcmp(arg, "-batch") == 0) { if (++argv, --argc > 0) { g_opt.m_batch = atoi(argv[0]); continue; } } if (strcmp(arg, "-core") == 0) { g_opt.m_core = true; continue; } if (strcmp(arg, "-dbg") == 0) { g_opt.m_dbg = true; continue; } if (strcmp(arg, "-dbgall") == 0) { g_opt.m_dbg = true; g_opt.m_dbgall = true; putenv(strdup("NDB_BLOB_DEBUG=1")); continue; } if (strcmp(arg, "-dbug") == 0) { if (++argv, --argc > 0) { g_opt.m_dbug = strdup(argv[0]); continue; } } if (strcmp(arg, "-full") == 0) { g_opt.m_full = true; continue; } if (strcmp(arg, "-loop") == 0) { if (++argv, --argc > 0) { g_opt.m_loop = atoi(argv[0]); continue; } } if (strcmp(arg, "-parts") == 0) { if (++argv, --argc > 0) { g_opt.m_parts = atoi(argv[0]); continue; } } if (strcmp(arg, "-rows") == 0) { if (++argv, --argc > 0) { g_opt.m_rows = atoi(argv[0]); continue; } } if (strcmp(arg, "-rowsperf") == 0) { if (++argv, --argc > 0) { g_opt.m_rowsperf = atoi(argv[0]); continue; } } if (strcmp(arg, "-seed") == 0) { if (++argv, --argc > 0) { g_opt.m_seed = atoi(argv[0]); continue; } } if (strcmp(arg, "-skip") == 0) { if (++argv, --argc > 0) { g_opt.m_skip = strdup(argv[0]); continue; } } if (strcmp(arg, "-test") == 0) { if (++argv, --argc > 0) { g_opt.m_test = strdup(argv[0]); continue; } } // metadata if (strcmp(arg, "-pk2len") == 0) { if (++argv, --argc > 0) { g_opt.m_pk2len = atoi(argv[0]); if (g_opt.m_pk2len <= g_max_pk2len) continue; } } if (strcmp(arg, "-oneblob") == 0) { g_opt.m_oneblob = true; continue; } // bugs if (strcmp(arg, "-bug") == 0) { if (++argv, --argc > 0) { g_opt.m_bug = atoi(argv[0]); for (unsigned i = 0; i < sizeof(g_bugtest)/sizeof(g_bugtest[0]); i++) { if (g_opt.m_bug == g_bugtest[i].m_bug) { g_opt.m_bugtest = g_bugtest[i].m_test; break; } } if (g_opt.m_bugtest != 0) continue; } } ndbout << "testOIBasic: unknown option " << arg << endl; printusage(); return NDBT_ProgramExit(NDBT_WRONGARGS); } if (g_opt.m_dbug != 0) { DBUG_PUSH(g_opt.m_dbug); } if (g_opt.m_pk2len == 0) { char b[100]; b[0] = 0; if (g_opt.m_skip != 0) strcpy(b, g_opt.m_skip); strcat(b, "i"); strcat(b, "r"); g_opt.m_skip = strdup(b); } g_ncc = new Ndb_cluster_connection(); if (g_ncc->connect(30) != 0 || testmain() == -1 || testperf() == -1) { ndbout << "line " << __LINE__ << " FAIL loop=" << g_loop << endl; return NDBT_ProgramExit(NDBT_FAILED); } delete g_ncc; g_ncc = 0; return NDBT_ProgramExit(NDBT_OK);}// vim: set sw=2 et:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -