|
| 1 | +#include <time.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <stdarg.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <inttypes.h> |
| 6 | +#include <sys/time.h> |
| 7 | +#include <pthread.h> |
| 8 | + |
| 9 | +#include <string> |
| 10 | +#include <vector> |
| 11 | + |
| 12 | +#include <pqxx/connection> |
| 13 | +#include <pqxx/transaction> |
| 14 | +#include <pqxx/nontransaction> |
| 15 | + |
| 16 | +using namespace std; |
| 17 | +using namespace pqxx; |
| 18 | + |
| 19 | +template<class T> |
| 20 | +class unique_ptr |
| 21 | +{ |
| 22 | + T* ptr; |
| 23 | + |
| 24 | + public: |
| 25 | + unique_ptr(T* p = NULL) : ptr(p) {} |
| 26 | + ~unique_ptr() { delete ptr; } |
| 27 | + T& operator*() { return *ptr; } |
| 28 | + T* operator->() { return ptr; } |
| 29 | + void operator=(T* p) { ptr = p; } |
| 30 | + void operator=(unique_ptr& other) { |
| 31 | + ptr = other.ptr; |
| 32 | + other.ptr = NULL; |
| 33 | + } |
| 34 | +}; |
| 35 | + |
| 36 | +typedef void* (*thread_proc_t)(void*); |
| 37 | +typedef int64_t csn_t; |
| 38 | + |
| 39 | +struct thread |
| 40 | +{ |
| 41 | + pthread_t t; |
| 42 | + size_t proceeded; |
| 43 | + int id; |
| 44 | + |
| 45 | + void start(int tid, thread_proc_t proc) { |
| 46 | + id = tid; |
| 47 | + proceeded = 0; |
| 48 | + pthread_create(&t, NULL, proc, this); |
| 49 | + } |
| 50 | + |
| 51 | + void wait() { |
| 52 | + pthread_join(t, NULL); |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +struct config |
| 57 | +{ |
| 58 | + int nReaders; |
| 59 | + int nWriters; |
| 60 | + int nIterations; |
| 61 | + int nAccounts; |
| 62 | + vector<string> connections; |
| 63 | + |
| 64 | + config() { |
| 65 | + nReaders = 1; |
| 66 | + nWriters = 10; |
| 67 | + nIterations = 1000; |
| 68 | + nAccounts = 1000; |
| 69 | + } |
| 70 | +}; |
| 71 | + |
| 72 | +config cfg; |
| 73 | +bool running; |
| 74 | + |
| 75 | +#define USEC 1000000 |
| 76 | + |
| 77 | +static time_t getCurrentTime() |
| 78 | +{ |
| 79 | + struct timeval tv; |
| 80 | + gettimeofday(&tv, NULL); |
| 81 | + return (time_t)tv.tv_sec*USEC + tv.tv_usec; |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +void exec(transaction_base& txn, char const* sql, ...) |
| 86 | +{ |
| 87 | + va_list args; |
| 88 | + va_start(args, sql); |
| 89 | + char buf[1024]; |
| 90 | + vsprintf(buf, sql, args); |
| 91 | + va_end(args); |
| 92 | + txn.exec(buf); |
| 93 | +} |
| 94 | + |
| 95 | +int64_t execQuery( transaction_base& txn, char const* sql, ...) |
| 96 | +{ |
| 97 | + va_list args; |
| 98 | + va_start(args, sql); |
| 99 | + char buf[1024]; |
| 100 | + vsprintf(buf, sql, args); |
| 101 | + va_end(args); |
| 102 | + result r = txn.exec(buf); |
| 103 | + return r[0][0].as(int64_t()); |
| 104 | +} |
| 105 | + |
| 106 | +void* reader(void* arg) |
| 107 | +{ |
| 108 | + thread& t = *(thread*)arg; |
| 109 | + vector< unique_ptr<connection> > conns(cfg.connections.size()); |
| 110 | + for (size_t i = 0; i < conns.size(); i++) { |
| 111 | + conns[i] = new connection(cfg.connections[i]); |
| 112 | + } |
| 113 | + int64_t prevSum = 0; |
| 114 | + |
| 115 | + while (running) { |
| 116 | + csn_t snapshot; |
| 117 | + vector< unique_ptr<work> > txns(conns.size()); |
| 118 | + for (size_t i = 0; i < conns.size(); i++) { |
| 119 | + txns[i] = new work(*conns[i]); |
| 120 | + } |
| 121 | + for (size_t i = 0; i < txns.size(); i++) { |
| 122 | + if (i == 0) { |
| 123 | + snapshot = execQuery(*txns[i], "select dtm_extend()"); |
| 124 | + } else { |
| 125 | + snapshot = execQuery(*txns[i], "select dtm_access(%ld)", snapshot); |
| 126 | + } |
| 127 | + } |
| 128 | + int64_t sum = 0; |
| 129 | + for (size_t i = 0; i < txns.size(); i++) { |
| 130 | + sum += execQuery(*txns[i], "select sum(v) from t"); |
| 131 | + } |
| 132 | + if (sum != prevSum) { |
| 133 | + printf("Total=%ld snapshot=%ld\n", sum, snapshot); |
| 134 | + prevSum = sum; |
| 135 | + } |
| 136 | + t.proceeded += 1; |
| 137 | + } |
| 138 | + return NULL; |
| 139 | +} |
| 140 | + |
| 141 | +void* writer(void* arg) |
| 142 | +{ |
| 143 | + thread& t = *(thread*)arg; |
| 144 | + vector< unique_ptr<connection> > conns(cfg.connections.size()); |
| 145 | + for (size_t i = 0; i < conns.size(); i++) { |
| 146 | + conns[i] = new connection(cfg.connections[i]); |
| 147 | + } |
| 148 | + for (int i = 0; i < cfg.nIterations; i++) |
| 149 | + { |
| 150 | + char gtid[32]; |
| 151 | + int srcCon, dstCon; |
| 152 | + int srcAcc = (random() % ((cfg.nAccounts-cfg.nWriters)/cfg.nWriters))*cfg.nWriters + t.id; |
| 153 | + int dstAcc = (random() % ((cfg.nAccounts-cfg.nWriters)/cfg.nWriters))*cfg.nWriters + t.id; |
| 154 | + |
| 155 | + sprintf(gtid, "%d.%d", t.id, i); |
| 156 | + |
| 157 | + do { |
| 158 | + srcCon = random() % cfg.connections.size(); |
| 159 | + dstCon = random() % cfg.connections.size(); |
| 160 | + } while (srcCon == dstCon); |
| 161 | + |
| 162 | + nontransaction srcTx(*conns[srcCon]); |
| 163 | + nontransaction dstTx(*conns[dstCon]); |
| 164 | + |
| 165 | + exec(srcTx, "begin transaction"); |
| 166 | + exec(dstTx, "begin transaction"); |
| 167 | + |
| 168 | + csn_t snapshot = execQuery(srcTx, "select dtm_extend('%s')", gtid); |
| 169 | + snapshot = execQuery(dstTx, "select dtm_access(%ld, '%s')", snapshot, gtid); |
| 170 | + |
| 171 | + exec(srcTx, "update t set v = v - 1 where u=%d", srcAcc); |
| 172 | + exec(dstTx, "update t set v = v + 1 where u=%d", dstAcc); |
| 173 | + |
| 174 | + exec(srcTx, "prepare transaction '%s'", gtid); |
| 175 | + exec(dstTx, "prepare transaction '%s'", gtid); |
| 176 | + exec(srcTx, "select dtm_begin_prepare('%s')", gtid); |
| 177 | + exec(dstTx, "select dtm_begin_prepare('%s')", gtid); |
| 178 | + csn_t csn = execQuery(srcTx, "select dtm_prepare('%s', 0)", gtid); |
| 179 | + csn = execQuery(dstTx, "select dtm_prepare('%s', %ld)", gtid, csn); |
| 180 | + exec(srcTx, "select dtm_end_prepare('%s', %ld)", gtid, csn); |
| 181 | + exec(dstTx, "select dtm_end_prepare('%s', %ld)", gtid, csn); |
| 182 | + exec(srcTx, "commit prepared '%s'", gtid); |
| 183 | + exec(dstTx, "commit prepared '%s'", gtid); |
| 184 | + |
| 185 | + t.proceeded += 1; |
| 186 | + } |
| 187 | + return NULL; |
| 188 | +} |
| 189 | + |
| 190 | +void initializeDatabase() |
| 191 | +{ |
| 192 | + for (size_t i = 0; i < cfg.connections.size(); i++) { |
| 193 | + connection conn(cfg.connections[i]); |
| 194 | + work txn(conn); |
| 195 | + exec(txn, "drop extension if exists pg_dtm"); |
| 196 | + exec(txn, "create extension pg_dtm"); |
| 197 | + exec(txn, "drop table if exists t"); |
| 198 | + exec(txn, "create table t(u int primary key, v int)"); |
| 199 | + exec(txn, "insert into t (select generate_series(0,%d), %d)", cfg.nAccounts-1, 0); |
| 200 | + txn.commit(); |
| 201 | + |
| 202 | + // nontransaction vacTx(conn); |
| 203 | + // exec(vacTx, "vacuum full"); |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +int main (int argc, char* argv[]) |
| 208 | +{ |
| 209 | + bool initialize = false; |
| 210 | + for (int i = 1; i < argc; i++) { |
| 211 | + if (argv[i][0] == '-') { |
| 212 | + switch (argv[i][1]) { |
| 213 | + case 'r': |
| 214 | + cfg.nReaders = atoi(argv[++i]); |
| 215 | + continue; |
| 216 | + case 'w': |
| 217 | + cfg.nWriters = atoi(argv[++i]); |
| 218 | + continue; |
| 219 | + case 'a': |
| 220 | + cfg.nAccounts = atoi(argv[++i]); |
| 221 | + continue; |
| 222 | + case 'n': |
| 223 | + cfg.nIterations = atoi(argv[++i]); |
| 224 | + continue; |
| 225 | + case 'C': |
| 226 | + cfg.connections.push_back(string(argv[++i])); |
| 227 | + continue; |
| 228 | + case 'i': |
| 229 | + initialize = true; |
| 230 | + continue; |
| 231 | + } |
| 232 | + } |
| 233 | + printf("Options:\n" |
| 234 | + "\t-r N\tnumber of readers (1)\n" |
| 235 | + "\t-w N\tnumber of writers (10)\n" |
| 236 | + "\t-a N\tnumber of accounts (1000)\n" |
| 237 | + "\t-n N\tnumber of iterations (1000)\n" |
| 238 | + "\t-c STR\tdatabase connection string\n" |
| 239 | + "\t-i\tinitialize datanase\n"); |
| 240 | + return 1; |
| 241 | + } |
| 242 | + if (initialize) { |
| 243 | + initializeDatabase(); |
| 244 | + } |
| 245 | + |
| 246 | + time_t start = getCurrentTime(); |
| 247 | + running = true; |
| 248 | + |
| 249 | + vector<thread> readers(cfg.nReaders); |
| 250 | + vector<thread> writers(cfg.nWriters); |
| 251 | + size_t nReads = 0; |
| 252 | + size_t nWrites = 0; |
| 253 | + |
| 254 | + for (int i = 0; i < cfg.nReaders; i++) { |
| 255 | + readers[i].start(i, reader); |
| 256 | + } |
| 257 | + for (int i = 0; i < cfg.nWriters; i++) { |
| 258 | + writers[i].start(i, writer); |
| 259 | + } |
| 260 | + |
| 261 | + for (int i = 0; i < cfg.nWriters; i++) { |
| 262 | + writers[i].wait(); |
| 263 | + nWrites += writers[i].proceeded; |
| 264 | + } |
| 265 | + |
| 266 | + running = false; |
| 267 | + |
| 268 | + for (int i = 0; i < cfg.nReaders; i++) { |
| 269 | + readers[i].wait(); |
| 270 | + nReads += readers[i].proceeded; |
| 271 | + } |
| 272 | + |
| 273 | + time_t elapsed = getCurrentTime() - start; |
| 274 | + printf("TPS(updates)=%f, TPS(selects)=%f\n", (double)(nWrites*USEC)/elapsed, (double)(nReads*USEC)/elapsed); |
| 275 | + return 0; |
| 276 | +} |
0 commit comments