-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqlite-bench.c
More file actions
293 lines (249 loc) · 8.15 KB
/
sqlite-bench.c
File metadata and controls
293 lines (249 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright 2022-2023 CrossDB.ORG. All rights reserved.
*
* https://crossdb.org
* https://github.com/crossdb-org/crossdb
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
gcc -o sqlite3-bench sqlite3-bench.c -lsqlite3 -O3 -Wall
*/
#include "benchmark.h"
#include <sqlite3.h>
static void error_check(int status) {
if (status != SQLITE_OK) {
printf("sqlite3 error: status = %d\n", status);
exit(1);
}
}
static void exec_error_check(int status, char *err_msg) {
if (status != SQLITE_OK) {
printf("SQL error: %s\n", err_msg);
sqlite3_free(err_msg);
exit(1);
}
}
static void step_error_check(int status) {
if (status != SQLITE_DONE) {
printf("SQL step error: status = %d\n", status);
exit(1);
}
}
static sqlite3* pDb = NULL;
static sqlite3_stmt *insert_stmt, *query_stmt, *update_stmt, *delete_stmt, *begin_trans_stmt, *end_trans_stmt;
const char* sqlite_create (char *dbname, dbtype_e type, keytype_e key, uint32_t flags)
{
int status;
char* err_msg = NULL;
char name[512];
if (!s_quiet && (KEY_DEFAULT != key) && (KEY_TREE != key)) {
printf ("*** Warning: Doesn't support key type %s, use default to do test\n", keyTypeStr(key));
}
if (DB_INMEM == type) {
dbname = ":memory:";
} else {
sprintf (name, "%s/%s", dbname, "sqlite3bench.db");
dbname = name;
}
status = sqlite3_open(dbname, &pDb);
if (status) {
printf("open error: %s\n", sqlite3_errmsg(pDb));
exit(1);
}
char * sqlite3_cfg[] = {
"PRAGMA synchronous = NORMAL",
"PRAGMA journal_mode = WAL",
"PRAGMA temp_store = memory",
"PRAGMA optimize",
NULL
};
if (DB_ONDISK != type) {
sqlite3_cfg[0] = "PRAGMA synchronous = OFF";
if (DB_INMEM == type) {
sqlite3_cfg[1] = "PRAGMA journal_mode = OFF";
}
}
for (int i = 0; NULL != sqlite3_cfg[i]; ++i) {
if (!s_quiet) {
printf (" %s\n", sqlite3_cfg[i]);
}
status = sqlite3_exec(pDb, sqlite3_cfg[i], NULL, NULL, &err_msg);
exec_error_check(status, err_msg);
}
// sqlite3_exec(pDb, "DROP TABLE route", NULL, NULL, &err_msg);
status = sqlite3_exec(pDb, "CREATE TABLE IF NOT EXISTS route("
"prefix INT, "
"mask INT, "
"nexthop INT, "
"metric INT, "
"intf TEXT, "
"birth INT, "
"flags INT, "
"PRIMARY KEY (prefix,mask))",
NULL, NULL, &err_msg);
exec_error_check(status, err_msg);
const char* insert_str = "INSERT INTO route VALUES (?, ?, ?, ?, ?, ?, ?)";
const char *query_str = "SELECT * FROM route WHERE prefix = ? AND mask = ?";
const char *update_str = "UPDATE route SET birth = ? WHERE prefix = ? AND mask = ?";
const char *delete_str = "DELETE FROM route WHERE prefix = ? AND mask = ?";
// Preparing sqlite3 statements
status = sqlite3_prepare_v2(pDb, insert_str, -1, &insert_stmt, NULL);
error_check(status);
status = sqlite3_prepare_v2(pDb, query_str, -1, &query_stmt, NULL);
error_check(status);
status = sqlite3_prepare_v2(pDb, update_str, -1, &update_stmt, NULL);
error_check(status);
status = sqlite3_prepare_v2(pDb, delete_str, -1, &delete_stmt, NULL);
error_check(status);
status = sqlite3_prepare_v2(pDb, "BEGIN TRANSACTION", -1, &begin_trans_stmt, NULL);
error_check(status);
status = sqlite3_prepare_v2(pDb, "END TRANSACTION", -1, &end_trans_stmt, NULL);
error_check(status);
return "SQLite";
}
void sqlite_close ()
{
int status = sqlite3_finalize(insert_stmt);
error_check(status);
status = sqlite3_finalize(query_stmt);
error_check(status);
status = sqlite3_finalize(update_stmt);
error_check(status);
status = sqlite3_finalize(delete_stmt);
error_check(status);
status = sqlite3_finalize(begin_trans_stmt);
error_check(status);
status = sqlite3_finalize(end_trans_stmt);
error_check(status);
status = sqlite3_close(pDb);
error_check(status);
}
void sqlite_begin ()
{
int status;
// Begin write transaction
status = sqlite3_step(begin_trans_stmt);
step_error_check(status);
status = sqlite3_reset(begin_trans_stmt);
error_check(status);
}
void sqlite_commit ()
{
int status;
// End write transaction
status = sqlite3_step(end_trans_stmt);
step_error_check(status);
status = sqlite3_reset(end_trans_stmt);
error_check(status);
}
void sqlite_insert (route_t *pRoute)
{
int status;
// Bind values into insert_stmt
status = sqlite3_bind_int64(insert_stmt, 1, pRoute->prefix);
error_check(status);
status = sqlite3_bind_int(insert_stmt, 2, pRoute->mask);
error_check(status);
status = sqlite3_bind_int64(insert_stmt, 3, pRoute->nexthop);
error_check(status);
status = sqlite3_bind_int(insert_stmt, 4, pRoute->metric);
error_check(status);
status = sqlite3_bind_text(insert_stmt, 5, pRoute->intf, strlen(pRoute->intf), SQLITE_STATIC);
error_check(status);
status = sqlite3_bind_int64(insert_stmt, 6, pRoute->birth);
error_check(status);
status = sqlite3_bind_int64(insert_stmt, 7, pRoute->flags);
error_check(status);
// Execute insert_stmt
status = sqlite3_step(insert_stmt);
step_error_check(status);
// Reset SQLite statement for another use
status = sqlite3_clear_bindings(insert_stmt);
error_check(status);
status = sqlite3_reset(insert_stmt);
error_check(status);
}
bool sqlite_query (route_t *pRoute)
{
int status;
// Bind key value into query_stmt
status = sqlite3_bind_int64(query_stmt, 1, pRoute->prefix);
error_check(status);
status = sqlite3_bind_int(query_stmt, 2, pRoute->mask);
error_check(status);
// Execute query_stmt
if ((status = sqlite3_step(query_stmt)) == SQLITE_ROW) {
pRoute->nexthop = sqlite3_column_int64 (query_stmt, 2);
pRoute->metric = sqlite3_column_int (query_stmt, 3);
strcpy (pRoute->intf, (const char*)sqlite3_column_text (query_stmt, 4));
pRoute->birth = sqlite3_column_int64 (query_stmt, 5);
pRoute->flags = sqlite3_column_int64 (query_stmt, 6);
} else {
printf("sqlite3 error: status = %d, no row found\n", status);
return false;
}
// Reset SQLite statement for another use
status = sqlite3_clear_bindings(query_stmt);
error_check(status);
status = sqlite3_reset(query_stmt);
error_check(status);
return true;
}
void sqlite_update (route_t *pRoute)
{
int status;
// Bind key value into update_stmt
status = sqlite3_bind_int64(update_stmt, 1, pRoute->birth);
error_check(status);
status = sqlite3_bind_int64(update_stmt, 2, pRoute->prefix);
error_check(status);
status = sqlite3_bind_int(update_stmt, 3, pRoute->mask);
error_check(status);
// Execute update_stmt
status = sqlite3_step(update_stmt);
step_error_check(status);
// Reset SQLite statement for another use
status = sqlite3_clear_bindings(update_stmt);
error_check(status);
status = sqlite3_reset(update_stmt);
error_check(status);
}
void sqlite_delete (route_t *pRoute)
{
int status;
// Bind key value into delete_stmt
status = sqlite3_bind_int64(delete_stmt, 1, pRoute->prefix);
error_check(status);
status = sqlite3_bind_int(delete_stmt, 2, pRoute->mask);
error_check(status);
// Execute delete_stmt
status = sqlite3_step(delete_stmt);
step_error_check(status);
// Reset SQLite statement for another use
status = sqlite3_clear_bindings(delete_stmt);
error_check(status);
status = sqlite3_reset(delete_stmt);
error_check(status);
}
#define db_name "SQLite"
#define db_create sqlite_create
#define db_close sqlite_close
#define db_insert sqlite_insert
#define db_query sqlite_query
#define db_update sqlite_update
#define db_delete sqlite_delete
#define db_begin sqlite_begin
#define db_commit sqlite_commit
#define DFT_ROW_COUNT 100000
#include "benchmark.c"