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
|
/*
* Copyright (c) 2010-2012 Postgres-XC Development Group
*/
#include <sys/types.h>
#include <unistd.h>
#include "gtm/gtm_c.h"
#include "gtm/libpq-fe.h"
#include "gtm/gtm_client.h"
#include <sys/time.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/wait.h>
#define client_log(x)
extern int optind;
extern char *optarg;
/* Calculate time difference */
static void
diffTime(struct timeval *t1, struct timeval *t2, struct timeval *result)
{
int sec = t1->tv_sec - t2->tv_sec;
int usec = t1->tv_usec - t2->tv_usec;
if (usec < 0)
{
usec += 1000000;
sec--;
}
result->tv_sec = sec;
result->tv_usec = usec;
}
/*
* Help display should match
*/
static void
help(const char *progname)
{
printf(_("Usage:\n %s [OPTION]...\n\n"), progname);
printf(_("Options:\n"));
printf(_(" -h hostname GTM proxy/server hostname/IP\n"));
printf(_(" -p port GTM proxy/serevr port number\n"));
printf(_(" -c count Number of clients\n"));
printf(_(" -n count Number of transactions per client\n"));
printf(_(" -s count Number of statements per transaction\n"));
printf(_(" -i id Coordinator ID\n"));
}
int
main(int argc, char *argv[])
{
int ii;
int jj;
int kk;
char connect_string[100];
int gtmport;
char *tmp_name;
int nclients;
int ntxns_per_cli;
int nstmts_per_txn;
char *gtmhost;
char opt;
struct timeval starttime, endtime, diff;
FILE *fp;
FILE *fp2;
char buf[1024];
int testid, this_testid, max_testid;
int snapsize = 0;
float avg_sanpsize = 0;
pid_t child_pids[1024];
pid_t parent_pid;
#define TXN_COUNT 1000
GlobalTransactionId gxid[TXN_COUNT];
GTM_Conn *conn;
char test_output[256], test_end[256], test_output_csv[256];
char system_cmd[1024];
/*
* Catch standard options before doing much else
*/
if (argc > 1)
{
if (strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-?") == 0)
{
help(argv[0]);
exit(0);
}
}
/*
* Parse the command like options and set variables
*/
while ((opt = getopt(argc, argv, "h:p:c:n:s:i:")) != -1)
{
switch (opt)
{
case 'h':
gtmhost = strdup(optarg);
break;
case 'p':
gtmport = atoi(optarg);
break;
case 'c':
nclients = atoi(optarg);
break;
case 'n':
ntxns_per_cli = atoi(optarg);
break;
case 's':
nstmts_per_txn = atoi(optarg);
break;
case 'i':
tmp_name = strdup(optarg);
sprintf(test_output, "TEST_OUTPUT_%s\0", tmp_name);
sprintf(test_end, "TEST_END_%s\0", tmp_name);
sprintf(test_output_csv, "TEST_OUTPUT_%s.CSV\0", tmp_name);
break;
default:
fprintf(stderr, "Unrecognized option %c\n", opt);
help(argv[0]);
exit(0);
}
}
sprintf(connect_string, "host=%s port=%d node_name=%s remote_type=%d", gtmhost, gtmport, tmp_name, GTM_NODE_COORDINATOR);
sprintf(system_cmd, "echo -------------------------------------------------------- >> %s", test_output);
system(system_cmd);
sprintf(system_cmd, "date >> %s", test_output);
system(system_cmd);
sprintf(system_cmd, "echo -------------------------------------------------------- >> %s", test_output);
system(system_cmd);
fp = fopen(test_output, "a+");
fp2 = fopen(test_output_csv, "a+");
max_testid = 0;
while (fgets(buf, 1024, fp) != NULL)
{
if (sscanf(buf, "TEST-ID: %d", &testid) == 1)
{
if (max_testid < testid)
max_testid = testid;
}
}
this_testid = max_testid + 1;
fprintf(fp, "TEST-ID: %d", this_testid);
fprintf(fp, "\n\n");
fflush(fp);
parent_pid = getpid();
gettimeofday(&starttime, NULL);
/*
* Start as many clients
*/
for (ii = 1; ii < nclients; ii++)
{
int cpid;
if ((cpid = fork()) == 0)
break;
else
child_pids[ii-1] = cpid;
}
if (getpid() == parent_pid)
fprintf(stderr, "started %d clients\n", nclients);
conn = PQconnectGTM(connect_string);
if (conn == NULL)
{
client_log(("Error in connection\n"));
exit(1);
}
if (getpid() != parent_pid)
gettimeofday(&starttime, NULL);
snapsize = 0;
for (jj = 0; jj <= ntxns_per_cli / TXN_COUNT; jj++)
{
for (ii = 0; ii < TXN_COUNT; ii++)
{
if ((jj * TXN_COUNT) + ii >= ntxns_per_cli)
break;
gxid[ii] = begin_transaction(conn, GTM_ISOLATION_RC);
if (gxid[ii] != InvalidGlobalTransactionId)
client_log(("Started a new transaction (GXID:%u)\n", gxid[ii]));
else
client_log(("BEGIN transaction failed for ii=%d\n", ii));
for (kk = 0; kk < nstmts_per_txn; kk++)
{
GTM_Snapshot snapshot = get_snapshot(conn, gxid[ii], true);
snapsize += snapshot->sn_xcnt;
}
if (!prepare_transaction(conn, gxid[ii]))
client_log(("PREPARE successful (GXID:%u)\n", gxid[ii]));
else
client_log(("PREPARE failed (GXID:%u)\n", gxid[ii]));
if (ii % 2 == 0)
{
if (!abort_transaction(conn, gxid[ii]))
client_log(("ROLLBACK successful (GXID:%u)\n", gxid[ii]));
else
client_log(("ROLLBACK failed (GXID:%u)\n", gxid[ii]));
}
else
{
if (!commit_transaction(conn, gxid[ii]))
client_log(("COMMIT successful (GXID:%u)\n", gxid[ii]));
else
client_log(("COMMIT failed (GXID:%u)\n", gxid[ii]));
}
}
fprintf(stderr, "client [%d] finished %d transactions\n", getpid(), (jj * TXN_COUNT) + ii);
}
GTMPQfinish(conn);
if (parent_pid == getpid())
{
for (ii = 1; ii < nclients; ii++)
wait(NULL);
gettimeofday(&endtime, NULL);
diffTime(&endtime, &starttime, &diff);
avg_sanpsize = ((float) snapsize) / (ntxns_per_cli * nstmts_per_txn);
fprintf(fp, "\n");
fprintf(fp, "Num of client: %d\n", nclients);
fprintf(fp, "Num of txns/client: %d\n", ntxns_per_cli);
fprintf(fp, "Num of statements/txn: %d\n", nstmts_per_txn);
fprintf(fp, "TPS: %2f\n", (ntxns_per_cli * nclients) / ((float)((diff.tv_sec * 1000000) + diff.tv_usec)/1000000));
fprintf(fp, "Total snapshot size: %d\n", snapsize);
fprintf(fp, "Average snapshot size: %f\n", avg_sanpsize);
fprintf(fp, "Time: %d.%d\n", diff.tv_sec, diff.tv_usec);
fprintf(fp, "\n");
sprintf(system_cmd, "touch %s\0", test_end);
system(system_cmd);
}
else
{
gettimeofday(&endtime, NULL);
diffTime(&endtime, &starttime, &diff);
avg_sanpsize = ((float) snapsize) / (ntxns_per_cli * nstmts_per_txn);
}
flock(fileno(fp2), LOCK_EX);
if (parent_pid != getpid())
fprintf(fp2, "%d,%d,%d,%d,%d,%d,%d,%f,false\n", this_testid, nclients, ntxns_per_cli, nstmts_per_txn, diff.tv_sec, diff.tv_usec, snapsize, avg_sanpsize);
else
fprintf(fp2, "%d,%d,%d,%d,%d,%d,%d,%f,true\n", this_testid, nclients, ntxns_per_cli, nstmts_per_txn, diff.tv_sec, diff.tv_usec, snapsize, avg_sanpsize);
flock(fileno(fp2), LOCK_UN);
fclose(fp2);
fclose(fp);
return 0;
}
|