· 7 years ago · Dec 27, 2018, 02:34 AM
1// NOTE: This file is to provide context into the numbers I got
2// This code probably won't compile as is.
3// It was solely so that I could run the benchmark easily
4
5#include <stdio.h>
6
7#include "sqlite_c/sqlite3.h"
8
9#define WIN32_LEAN_AND_MEAN
10#include <Windows.h>
11
12#include <stdint.h>
13#include <stdlib.h>
14#include <time.h>
15
16double GetCurrTime() {
17
18 static bool firstTime = true;
19 static double frequencyCoeff = 1.0;
20 if (firstTime) {
21 LARGE_INTEGER freq;
22 QueryPerformanceFrequency(&freq);
23 firstTime = false;
24
25 frequencyCoeff = 1.0 / freq.QuadPart;
26 }
27
28 LARGE_INTEGER currTime;
29 QueryPerformanceCounter(&currTime);
30
31 double time = currTime.QuadPart;
32 return time * frequencyCoeff;
33}
34
35#define LogError(msg, ...) printf("[%s:%d]" msg, __FILE__, __LINE__, ## __VA_ARGS__)
36
37void GetRandomString(char* str, int len) {
38 static const char* alphabet = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890";
39 for (int i = 0; i < len; i++) {
40 str[i] = alphabet[rand() % 36];
41 }
42
43 str[len] = '\0';
44}
45
46int main() {
47 srand(time(NULL));
48
49 sqlite3* db = nullptr;
50
51 int rc = sqlite3_open("test_insert_new.db", &db);
52 if(rc != 0){
53 LogError("Can't open database: %s\n", sqlite3_errmsg(db));
54 sqlite3_close(db);
55 return -1;
56 }
57
58 {
59 char* errorMsg = nullptr;
60 const char* setupCmd =
61 "CREATE TABLE If NOT EXISTS Test(strVar VARCHAR(255) PRIMARY KEY);";
62 int rc = sqlite3_exec(db, setupCmd, NULL, NULL, &errorMsg);
63
64 if (rc){
65 LogError("Sqlite table setup failed: '%s'\n", errorMsg);
66 return false;
67 }
68 }
69
70 for (int iter = 0; iter < 10*1000; iter++) {
71
72 double t0 = GetCurrTime();
73
74 char* errorMsg;
75
76 if(1){
77 rc = sqlite3_exec(db, "BEGIN TRANSACTION;", NULL, NULL, &errorMsg);
78
79 if (rc){
80 LogError("Sqlite transaction failed: '%s'\n", errorMsg);
81 return false;
82 }
83 }
84
85 {
86 sqlite3_stmt* stmt;
87 rc = sqlite3_prepare_v2(db,
88 "INSERT OR IGNORE INTO Test(strVar) VALUES(?);",
89 1000, &stmt, (const char**)&errorMsg);
90
91 if (rc){
92 LogError("Error in prep stmt: '%s' err '%s'", errorMsg, sqlite3_errstr(rc));
93 return -1;
94 }
95
96 char strBuff[16] = {};
97 for (int i = 0; i < 1000; i++) {
98 //printf("Round %d\n", i);
99 GetRandomString(strBuff, 15);
100
101 rc = sqlite3_bind_text(stmt, 1, strBuff, -1, NULL);
102
103 if (rc){
104 LogError("Error in prep stmt: '%s' err '%s'", errorMsg, sqlite3_errstr(rc));
105 return -1;
106 }
107
108 int stmtRc = sqlite3_step(stmt);
109 if (stmtRc != SQLITE_DONE){
110 LogError("Error when executing insert: '%s'", sqlite3_errstr(rc));
111 return false;
112 }
113
114 sqlite3_reset(stmt);
115 }
116 sqlite3_finalize(stmt);
117 }
118
119 if(1){
120 rc = sqlite3_exec(db, "COMMIT;", NULL, NULL, &errorMsg);
121
122 if (rc){
123 LogError("Sqlite transaction failed: '%s'\n", errorMsg);
124 return false;
125 }
126 }
127
128 double t1 = GetCurrTime();
129
130 printf("Iter %d Time elapsed: %f\n", iter, (t1 - t0));
131
132 }
133
134
135 return 0;
136}