5
5
#include < inttypes.h>
6
6
#include < sys/time.h>
7
7
#include < pthread.h>
8
+ #include < unistd.h>
8
9
9
10
#include < string>
10
11
#include < vector>
13
14
#include < pqxx/transaction>
14
15
#include < pqxx/nontransaction>
15
16
#include < pqxx/pipeline>
17
+ #include < pqxx/version>
16
18
17
19
using namespace std ;
18
20
using namespace pqxx ;
@@ -39,6 +41,8 @@ struct config
39
41
int nIndexes;
40
42
int nIterations;
41
43
int transactionSize;
44
+ bool useSystemTime;
45
+ bool noPK;
42
46
string connection;
43
47
44
48
config () {
@@ -47,6 +51,8 @@ struct config
47
51
nIndexes = 8 ;
48
52
nIterations = 10000 ;
49
53
transactionSize = 100 ;
54
+ useSystemTime = false ;
55
+ noPK = false ;
50
56
}
51
57
};
52
58
@@ -55,6 +61,7 @@ bool running;
55
61
int nIndexUpdates;
56
62
time_t maxIndexUpdateTime;
57
63
time_t totalIndexUpdateTime;
64
+ time_t currTimestamp;
58
65
59
66
#define USEC 1000000
60
67
@@ -79,14 +86,30 @@ void exec(transaction_base& txn, char const* sql, ...)
79
86
void * inserter (void * arg)
80
87
{
81
88
connection con (cfg.connection );
82
- con.prepare (" insert" , " insert into t values ($1,$2,$3,$4,$5,$6,$7,$8,$9)" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" );
89
+ if (cfg.useSystemTime )
90
+ {
91
+ #if PQXX_VERSION_MAJOR >= 4
92
+ con.prepare (" insert" , " insert into t values ($1,$2,$3,$4,$5,$6,$7,$8,$9)" );
93
+ #else
94
+ con.prepare (" insert" , " insert into t values ($1,$2,$3,$4,$5,$6,$7,$8,$9)" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" )(" bigint" );
95
+ #endif
96
+ } else {
97
+ con.prepare (" insert" , " insert into t (select generate_series($1::integer,$2::integer),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000),ceil(random()*1000000000))" );
98
+ }
99
+
83
100
for (int i = 0 ; i < cfg.nIterations ; i++)
84
101
{
85
102
work txn (con);
86
- for (int j = 0 ; j < cfg.transactionSize ; j++)
87
- {
88
- txn.prepared (" insert" )(getCurrentTime ())(random ())(random ())(random ())(random ())(random ())(random ())(random ())(random ()).exec ();
89
- }
103
+ if (cfg.useSystemTime )
104
+ {
105
+ for (int j = 0 ; j < cfg.transactionSize ; j++)
106
+ {
107
+ txn.prepared (" insert" )(getCurrentTime ())(random ())(random ())(random ())(random ())(random ())(random ())(random ())(random ()).exec ();
108
+ }
109
+ } else {
110
+ currTimestamp = i*cfg.transactionSize ;
111
+ txn.prepared (" insert" )(i*cfg.transactionSize )((i+1 )*cfg.transactionSize -1 ).exec ();
112
+ }
90
113
txn.commit ();
91
114
}
92
115
return NULL ;
@@ -97,14 +120,16 @@ void* indexUpdater(void* arg)
97
120
connection con (cfg.connection );
98
121
while (running) {
99
122
sleep (cfg.indexUpdateInterval );
123
+ printf (" Alter indexes\n " );
100
124
time_t now = getCurrentTime ();
101
125
{
102
126
work txn (con);
103
127
for (int i = 0 ; i < cfg.nIndexes ; i++) {
104
- exec (txn, " alter index idx%d where pk<%lu" , i, now);
128
+ exec (txn, " alter index idx%d where pk<%lu" , i, cfg. useSystemTime ? now : currTimestamp );
105
129
}
106
130
txn.commit ();
107
131
}
132
+ printf (" End alter indexes\n " );
108
133
nIndexUpdates += 1 ;
109
134
time_t elapsed = getCurrentTime () - now;
110
135
totalIndexUpdateTime += elapsed;
@@ -122,12 +147,16 @@ void initializeDatabase()
122
147
time_t now = getCurrentTime ();
123
148
exec (txn, " drop table if exists t" );
124
149
exec (txn, " create table t (pk bigint, k1 bigint, k2 bigint, k3 bigint, k4 bigint, k5 bigint, k6 bigint, k7 bigint, k8 bigint)" );
125
- exec (txn, " create index pk on t(pk)" );
150
+ if (!cfg.noPK ) {
151
+ exec (txn, " create index pk on t(pk)" );
152
+ }
126
153
for (int i = 0 ; i < cfg.nIndexes ; i++) {
127
154
if (cfg.indexUpdateInterval == 0 ) {
128
155
exec (txn, " create index idx%d on t(k%d)" , i, i+1 );
129
- } else {
156
+ } else if (cfg. useSystemTime ) {
130
157
exec (txn, " create index idx%d on t(k%d) where pk<%ld" , i, i+1 , now);
158
+ } else {
159
+ exec (txn, " create index idx%d on t(k%d) where pk<%ld" , i, i+1 , 0 );
131
160
}
132
161
}
133
162
txn.commit ();
@@ -162,6 +191,12 @@ int main (int argc, char* argv[])
162
191
case ' c' :
163
192
cfg.connection = string (argv[++i]);
164
193
continue ;
194
+ case ' q' :
195
+ cfg.useSystemTime = true ;
196
+ continue ;
197
+ case ' p' :
198
+ cfg.noPK = true ;
199
+ continue ;
165
200
}
166
201
}
167
202
printf (" Options:\n "
@@ -170,6 +205,8 @@ int main (int argc, char* argv[])
170
205
" \t -u N\t index update interval (0)\n "
171
206
" \t -n N\t number of iterations (10000)\n "
172
207
" \t -i N\t number of indexes (8)\n "
208
+ " \t -q\t use system time and libpq\n "
209
+ " \t -p\t no primary key\n "
173
210
" \t -c STR\t database connection string\n " );
174
211
return 1 ;
175
212
}
0 commit comments