|
| 1 | + |
| 2 | +#define BENCH_DBNAME "CrossDB" |
| 3 | + |
| 4 | +#define LKUP_COUNT 10000000 |
| 5 | +#define SQL_LKUP_COUNT LKUP_COUNT/5 |
| 6 | +#define UPD_COUNT LKUP_COUNT/10 |
| 7 | + |
| 8 | +#include "bench.h" |
| 9 | +#include <crossdb.h> |
| 10 | + |
| 11 | +void* bench_create () |
| 12 | +{ |
| 13 | + xdb_conn_t* pConn = xdb_open (":memory:"); |
| 14 | + XDB_CHECK (NULL != pConn, printf ("Can't open connection:\n"); return NULL;); |
| 15 | + |
| 16 | + xdb_res_t* pRes = xdb_exec (pConn, "CREATE TABLE student (id INT PRIMARY KEY, name CHAR(16), age INT, class CHAR(16), score INT)"); |
| 17 | + XDB_RESCHK (pRes, printf ("Can't create table student\n"); xdb_close(pConn); return NULL;); |
| 18 | + |
| 19 | + return pConn; |
| 20 | +} |
| 21 | + |
| 22 | +void bench_close (void *pConn) |
| 23 | +{ |
| 24 | + xdb_close (pConn); |
| 25 | +} |
| 26 | + |
| 27 | +void bench_sql_test (void *pConn, int STU_COUNT, bool bRand, bench_result_t *pResult) |
| 28 | +{ |
| 29 | + xdb_res_t* pRes; |
| 30 | + xdb_row_t *pRow; |
| 31 | + |
| 32 | + bench_print ("\n[============= SQL Test =============]\n\n"); |
| 33 | + |
| 34 | + |
| 35 | + bench_print ("------------ INSERT %d ------------\n", STU_COUNT); |
| 36 | + bench_ts_beg(); |
| 37 | + for (int i = 0; i < STU_COUNT; ++i) { |
| 38 | + pRes = xdb_bexec (pConn, "INSERT INTO student (id,name,age,class,score) VALUES (?,?,?,?,?)", |
| 39 | + i, STU_NAME(i), STU_AGE(i), STU_CLASS(i), STU_SCORE(i)); |
| 40 | + XDB_RESCHK (pRes, bench_print ("Can't insert table student id=%d\n", i); return;); |
| 41 | + } |
| 42 | + pResult->insert_qps += bench_ts_end (STU_COUNT); |
| 43 | + |
| 44 | + |
| 45 | + bench_print ("------------ %s LKUP %d ------------\n", ORDER_STR(bRand), SQL_LKUP_COUNT); |
| 46 | + uint64_t qps_sum = 0; |
| 47 | + for (int t = 0; t < 5; ++t) { |
| 48 | + int count = 0; |
| 49 | + bench_ts_beg(); |
| 50 | + for (int i = 0; i < SQL_LKUP_COUNT; ++i) { |
| 51 | + pRes = xdb_bexec (pConn, "SELECT * FROM student WHERE id=?", STU_ID(i)); |
| 52 | + pRow = xdb_fetch_row (pRes); |
| 53 | + if (NULL != pRow) { |
| 54 | + int id = *(int*)pRow[0]; |
| 55 | + const char *name = (char*)pRow[1]; |
| 56 | + int age = *(int*)pRow[2]; |
| 57 | + const char *class = (char*)pRow[3]; |
| 58 | + int score = *(int*)pRow[4]; |
| 59 | + (void)id; (void)name; (void)age; (void)class; (void)score; |
| 60 | + count++; |
| 61 | + } |
| 62 | + xdb_free_result (pRes); |
| 63 | + } |
| 64 | + qps_sum += bench_ts_end (SQL_LKUP_COUNT); |
| 65 | + XDB_CHECK (count == SQL_LKUP_COUNT, bench_print ("OK %d != LKUP %d\n", count, SQL_LKUP_COUNT);); |
| 66 | + } |
| 67 | + pResult->query_qps += qps_sum / 5; |
| 68 | + |
| 69 | + |
| 70 | + bench_print ("------------ %s UPDATE %d ------------\n", ORDER_STR(bRand), UPD_COUNT); |
| 71 | + bench_ts_beg(); |
| 72 | + for (int i = 0; i < UPD_COUNT; ++i) { |
| 73 | + pRes = xdb_bexec (pConn, "UPDATE student SET age=age+? WHERE id=?", 1, STU_ID(i)); |
| 74 | + XDB_RESCHK (pRes, bench_print ("Can't update table student id%d\n", STU_ID(i)); return;); |
| 75 | + } |
| 76 | + pResult->update_qps += bench_ts_end (UPD_COUNT); |
| 77 | + |
| 78 | + |
| 79 | + bench_print ("------------ %s DELETE %d ------------\n", ORDER_STR(bRand), STU_COUNT); |
| 80 | + bench_ts_beg(); |
| 81 | + for (int i = 0; i < STU_COUNT; ++i) { |
| 82 | + pRes = xdb_bexec (pConn, "DELETE FROM student WHERE id=?", STU_ID(i)); |
| 83 | + XDB_RESCHK (pRes, bench_print ("Can't delete table student id=%d\n", STU_ID(i)); return;); |
| 84 | + } |
| 85 | + pResult->delete_qps += bench_ts_end (STU_COUNT); |
| 86 | +} |
| 87 | + |
| 88 | +void bench_pstmt_test (void *pConn, int STU_COUNT, bool bRand, bench_result_t *pResult) |
| 89 | +{ |
| 90 | + xdb_res_t* pRes; |
| 91 | + xdb_row_t *pRow; |
| 92 | + xdb_stmt_t *pStmt = NULL; |
| 93 | + |
| 94 | + bench_print ("\n[============= Prepared STMT Test =============]\n"); |
| 95 | + |
| 96 | + |
| 97 | + bench_print ("\n------------ INSERT %d ------------\n", STU_COUNT); |
| 98 | + pStmt = xdb_stmt_prepare (pConn, "INSERT INTO student (id,name,age,class,score) VALUES (?,?,?,?,?)"); |
| 99 | + bench_ts_beg(); |
| 100 | + for (int i = 0; i < STU_COUNT; ++i) { |
| 101 | + pRes = xdb_stmt_bexec (pStmt, i, STU_NAME(i), STU_AGE(i), STU_CLASS(i), STU_SCORE(i)); |
| 102 | + XDB_RESCHK (pRes, bench_print ("Can't insert table student id%d\n", i); goto error;); |
| 103 | + } |
| 104 | + pResult->insert_qps += bench_ts_end (STU_COUNT); |
| 105 | + xdb_stmt_close (pStmt); |
| 106 | + |
| 107 | + |
| 108 | + bench_print ("------------ %s LKUP %d ------------\n", ORDER_STR(bRand), LKUP_COUNT); |
| 109 | + pStmt = xdb_stmt_prepare (pConn, "SELECT * FROM student WHERE id=?"); |
| 110 | + XDB_CHECK (pStmt != NULL, goto error); |
| 111 | + uint64_t qps_sum = 0; |
| 112 | + for (int t = 0; t < 5; ++t) { |
| 113 | + int count = 0; |
| 114 | + bench_ts_beg(); |
| 115 | + for (int i = 0; i < LKUP_COUNT; ++i) { |
| 116 | + pRes = xdb_stmt_bexec (pStmt, STU_ID(i)); |
| 117 | + pRow = xdb_fetch_row (pRes); |
| 118 | + if (NULL != pRow) { |
| 119 | + int id = *(int*)pRow[0]; |
| 120 | + const char *name = (char*)pRow[1]; |
| 121 | + int age = *(int*)pRow[2]; |
| 122 | + const char *class = (char*)pRow[3]; |
| 123 | + int score = *(int*)pRow[4]; |
| 124 | + (void)id; (void)name; (void)age; (void)class; (void)score; |
| 125 | + count++; |
| 126 | + } |
| 127 | + xdb_free_result (pRes); |
| 128 | + } |
| 129 | + qps_sum += bench_ts_end (LKUP_COUNT); |
| 130 | + XDB_CHECK (count == LKUP_COUNT, bench_print ("OK %d != LKUP %d\n", count, LKUP_COUNT);); |
| 131 | + } |
| 132 | + xdb_stmt_close (pStmt); |
| 133 | + pResult->query_qps += qps_sum / 5; |
| 134 | + |
| 135 | + |
| 136 | + bench_print ("------------ %s UPDATE %d ------------\n", ORDER_STR(bRand), UPD_COUNT); |
| 137 | + pStmt = xdb_stmt_prepare (pConn, "UPDATE student SET age=age+? WHERE id=?"); |
| 138 | + XDB_CHECK (pStmt != NULL, goto error); |
| 139 | + bench_ts_beg(); |
| 140 | + for (int i = 0; i < UPD_COUNT; ++i) { |
| 141 | + pRes = xdb_stmt_bexec (pStmt, 1, STU_ID(i)); |
| 142 | + XDB_RESCHK (pRes, bench_print ("Can't update table student id%d\n", STU_ID(i)); goto error;); |
| 143 | + } |
| 144 | + pResult->update_qps = bench_ts_end (UPD_COUNT); |
| 145 | + xdb_stmt_close (pStmt); |
| 146 | + |
| 147 | + |
| 148 | + bench_print ("------------ %s DELETE %d ------------\n", ORDER_STR(bRand), STU_COUNT); |
| 149 | + pStmt = xdb_stmt_prepare (pConn, "DELETE FROM student WHERE id=?"); |
| 150 | + XDB_CHECK (pStmt != NULL, goto error); |
| 151 | + bench_ts_beg(); |
| 152 | + for (int i = 0; i < STU_COUNT; ++i) { |
| 153 | + pRes = xdb_stmt_bexec (pStmt, STU_ID(i)); |
| 154 | + XDB_RESCHK (pRes, bench_print ("Can't delete table student id%d\n", STU_ID(i)); goto error;); |
| 155 | + } |
| 156 | + pResult->delete_qps += bench_ts_end (STU_COUNT); |
| 157 | + |
| 158 | +error: |
| 159 | + xdb_stmt_close (pStmt); |
| 160 | +} |
0 commit comments