/
petit-sibench.c
240 lines (217 loc) · 5.56 KB
/
petit-sibench.c
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
/*
* A simple implementation of something like SIBENCH as described in
* Michael Cahill's thesis[1].
*
* Generate a mixture of select-the-whole table and update-one-random-row
* queries from some number of threads.
*
* Thomas Munro
*
* [1] https://ses.library.usyd.edu.au/bitstream/2123/5353/1/michael-cahill-2009-thesis.pdf
*/
#include "libpq-fe.h"
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
struct thread_context
{
pthread_t thread_handle;
const char *conn_info;
struct timeval finish_time;
int queries_per_update;
int rows;
bool ssi;
int thread_number;
int transactions;
int failures;
int cycle;
};
static void *
thread_main(void *arg)
{
struct timeval now;
struct thread_context *context = arg;
PGconn *conn;
PGresult *result;
int i;
unsigned seed = context->thread_number;
conn = PQconnectdb(context->conn_info);
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "thread %d failed to connect\n", context->thread_number);
return NULL;
}
result = PQexec(conn,
context->ssi ? "set default_transaction_isolation to serializable"
: "set default_transaction_isolation to \"repeatable read\"");
if (PQresultStatus(result) != PGRES_COMMAND_OK)
{
fprintf(stderr, "thread %d failed to set isolation level\n", context->thread_number);
goto fail;
}
PQclear(result);
/* Start each thread at a different phase. */
i = context->thread_number;
for (;;)
{
/* Generate mix of update and select. */
if (i++ % context->cycle == 0)
{
char buffer[80];
snprintf(buffer,
sizeof(buffer),
"update sibench set i = i where i = %d",
rand_r(&seed) % context->rows);
result = PQexec(conn, buffer);
if (PQresultStatus(result) != PGRES_COMMAND_OK)
++context->failures;
++context->transactions;
PQclear(result);
}
else
{
result = PQexec(conn, "select count(*) from sibench");
if (PQresultStatus(result) != PGRES_TUPLES_OK)
++context->failures;
++context->transactions;
PQclear(result);
}
gettimeofday(&now, NULL);
if (now.tv_sec > context->finish_time.tv_sec ||
(now.tv_sec == context->finish_time.tv_sec &&
now.tv_usec >= context->finish_time.tv_usec))
break;
}
fail:
PQfinish(conn);
return NULL;
}
int
main(int argc, char *argv[])
{
struct timeval finish_time;
struct thread_context *thread_contexts;
const char *conn_info;
int queries_per_update;
int rows;
int seconds;
bool ssi;
char buffer[256];
PGconn *conn;
PGresult *result;
int threads;
int total_transactions;
int total_failures;
int i;
/* Defaults. */
conn_info = "dbname=postgres";
queries_per_update = 1;
rows = 10;
seconds = 60;
ssi = false;
threads = 2;
/* Scan arguments. */
for (i = 1; i < argc; ++i)
{
const char *arg = argv[i];
bool more = (i + 1) < argc;
if (strcmp(arg, "--conn-info") == 0 && more)
conn_info = argv[++i];
else if (strcmp(arg, "--queries-per-update") == 0 && more)
queries_per_update = atoi(argv[++i]);
else if (strcmp(arg, "--rows") == 0 && more)
rows = atoi(argv[++i]);
else if (strcmp(arg, "--seconds") == 0 && more)
seconds = atoi(argv[++i]);
else if (strcmp(arg, "--threads") == 0 && more)
threads = atoi(argv[++i]);
else if (strcmp(arg, "--ssi") == 0)
ssi = true;
else
{
fprintf(stderr, "<help wanted>\n");
return EXIT_FAILURE;
}
}
/* Compute the finish time. */
gettimeofday(&finish_time, NULL);
finish_time.tv_sec += seconds;
/* Initialize schema. */
conn = PQconnectdb(conn_info);
if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "Failed to connect.\n");
return EXIT_FAILURE;
}
result = PQexec(conn, "drop table if exists sibench");
if (PQresultStatus(result) != PGRES_COMMAND_OK)
goto fail;
PQclear(result);
snprintf(buffer,
sizeof(buffer),
"create table sibench (i int primary key, x int); insert into sibench select generate_series(1, %d), 42; analyze",
rows);
result = PQexec(conn, buffer);
if (PQresultStatus(result) != PGRES_COMMAND_OK)
goto fail;
PQclear(result);
PQfinish(conn);
/* Prepare thread contexts and launch. */
thread_contexts = malloc(sizeof(struct thread_context) * threads);
if (thread_contexts == NULL)
{
fprintf(stderr, "out of memory\n");
return EXIT_FAILURE;
}
for (i = 0; i < threads; ++i)
{
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
thread_contexts[i].conn_info = conn_info;
thread_contexts[i].finish_time = finish_time;
thread_contexts[i].queries_per_update = queries_per_update;
thread_contexts[i].ssi = ssi;
thread_contexts[i].thread_number = i;
thread_contexts[i].rows = rows;
thread_contexts[i].transactions = 0;
thread_contexts[i].failures = 0;
thread_contexts[i].cycle = queries_per_update + 1;
if (pthread_create(&thread_contexts[i].thread_handle,
&thread_attr,
thread_main,
&thread_contexts[i]) < 0)
{
perror("pthread_create");
return EXIT_FAILURE;
}
}
/* Wait for them to finish. */
for (i = 0; i < threads; ++i)
{
if (pthread_join(thread_contexts[i].thread_handle, NULL) < 0)
{
perror("pthread_join");
return EXIT_FAILURE;
}
}
/* Count up the total tps. */
total_transactions = 0;
total_failures = 0;
for (i = 0; i < threads; ++i)
{
total_transactions += thread_contexts[i].transactions;
total_failures += thread_contexts[i].failures;
}
printf("TPS = %f, failures = %d\n",
(double) total_transactions / (double) seconds,
total_failures);
return EXIT_SUCCESS;
fail:
fprintf(stderr, "Failed: %s\n", PQerrorMessage(conn));
PQfinish(conn);
return EXIT_FAILURE;
}