|
4 | 4 |
|
5 | 5 | #include "libdtm.h"
|
6 | 6 |
|
7 |
| -#define NODES 1 |
| 7 | +#define unless(x) if (!(x)) |
8 | 8 |
|
9 |
| -#ifdef WIN32 |
10 |
| -#include <windows.h> |
11 |
| -static float li2f(LARGE_INTEGER x) { |
12 |
| - float result = ((float)x.HighPart) * 4.294967296E9 + (float)((x).LowPart); |
13 |
| - return result; |
14 |
| -} |
| 9 | +#define NODES 1 |
15 | 10 |
|
16 |
| -static float now_s() { |
17 |
| - LARGE_INTEGER freq, count; |
18 |
| - QueryPerformanceFrequency(&freq); |
19 |
| - QueryPerformanceCounter(&count); |
20 |
| - return li2f(count) / li2f(freq); |
21 |
| -} |
22 |
| -#else |
23 |
| -static float now_s() { |
24 |
| - // current time in seconds |
25 |
| - struct timespec t; |
26 |
| - if (clock_gettime(CLOCK_MONOTONIC, &t) == 0) { |
27 |
| - return t.tv_sec + t.tv_nsec * 1e-9; |
28 |
| - } else { |
29 |
| - printf("Error while clock_gettime()\n"); |
30 |
| - exit(0); |
31 |
| - } |
32 |
| -} |
33 |
| -#endif |
34 | 11 |
|
35 |
| -static void start_transaction(DTMConn conn, TransactionId base) { |
| 12 | +int main(int argc, char **argv) { |
36 | 13 | GlobalTransactionId gtid;
|
| 14 | + TransactionId base = 42; |
| 15 | + int transactions = 10000; |
| 16 | + int i; |
37 | 17 | gtid.nNodes = NODES;
|
38 | 18 | gtid.xids = malloc(sizeof(TransactionId) * gtid.nNodes);
|
39 | 19 | gtid.nodes = malloc(sizeof(NodeId) * gtid.nNodes);
|
40 | 20 |
|
41 |
| - int n; |
42 |
| - for (n = 0; n < gtid.nNodes; n++) { |
43 |
| - gtid.xids[n] = base + n; |
44 |
| - gtid.nodes[n] = n; |
45 |
| - } |
46 |
| - |
47 |
| - if (!DtmGlobalStartTransaction(conn, >id)) { |
48 |
| - fprintf(stdout, "global transaction not started\n"); |
49 |
| - exit(EXIT_FAILURE); |
| 21 | + DTMConn conn = DtmConnect("localhost", 5431); |
| 22 | + if (!conn) { |
| 23 | + exit(1); |
50 | 24 | }
|
51 |
| - //fprintf(stdout, "global transaction started\n"); |
52 | 25 |
|
53 |
| - free(gtid.xids); |
54 |
| - free(gtid.nodes); |
55 |
| -} |
| 26 | + for (i = 0; i < transactions; i++) { |
56 | 27 |
|
57 |
| -static void commit_transaction(DTMConn conn, TransactionId base) { |
58 |
| - int n; |
59 |
| - for (n = 0; n < NODES; n++) { |
60 |
| - if (!DtmGlobalSetTransStatus(conn, n, base + n, TRANSACTION_STATUS_COMMITTED)) { |
61 |
| - fprintf(stdout, "global transaction not committed\n"); |
62 |
| - exit(EXIT_FAILURE); |
| 28 | + int n; |
| 29 | + for (n = 0; n < gtid.nNodes; n++) { |
| 30 | + gtid.xids[n] = base + n; |
| 31 | + gtid.nodes[n] = n; |
63 | 32 | }
|
64 |
| - } |
65 |
| - //fprintf(stdout, "global transaction committed\n"); |
66 |
| -} |
67 | 33 |
|
68 |
| -static void abort_transaction(DTMConn conn, TransactionId base) { |
69 |
| - if (!DtmGlobalSetTransStatus(conn, 0, base + 0, TRANSACTION_STATUS_ABORTED)) { |
70 |
| - fprintf(stdout, "global transaction not aborted\n"); |
71 |
| - exit(EXIT_FAILURE); |
72 |
| - } |
73 |
| - //fprintf(stdout, "global transaction aborted\n"); |
74 |
| -} |
75 |
| - |
76 |
| -static void show_snapshots(DTMConn conn, TransactionId base) { |
77 |
| - int i, n; |
78 |
| - for (n = 0; n < NODES; n++) { |
79 |
| - Snapshot s = malloc(sizeof(SnapshotData)); |
80 |
| - s->xip = NULL; |
81 |
| - |
82 |
| - if (!DtmGlobalGetSnapshot(conn, n, base + n, s)) { |
83 |
| - fprintf(stdout, "failed to get a snapshot[%d]\n", n); |
| 34 | + if (!DtmGlobalStartTransaction(conn, >id)) { |
| 35 | + fprintf(stdout, "global transaction not started\n"); |
84 | 36 | exit(EXIT_FAILURE);
|
85 | 37 | }
|
86 |
| - //fprintf(stdout, "snapshot[%d, %#x]: xmin = %#x, xmax = %#x, active =", n, base + n, s->xmin, s->xmax); |
87 |
| - //for (i = 0; i < s->xcnt; i++) { |
88 |
| - // fprintf(stdout, " %#x", s->xip[i]); |
89 |
| - //} |
90 |
| - //fprintf(stdout, "\n"); |
91 |
| - |
92 |
| - free(s->xip); |
93 |
| - free(s); |
94 |
| - } |
95 |
| -} |
96 | 38 |
|
97 |
| -static void show_status(DTMConn conn, TransactionId base) { |
98 |
| - int n; |
99 |
| - for (n = 0; n < NODES; n++) { |
100 |
| - XidStatus s = DtmGlobalGetTransStatus(conn, n, base + n); |
101 |
| - if (s == -1) { |
102 |
| - fprintf(stdout, "failed to get transaction status [%d, %#x]\n", n, base + n); |
| 39 | + if (!DtmGlobalSetTransStatus(conn, 0, base + 0, TRANSACTION_STATUS_COMMITTED)) { |
| 40 | + fprintf(stdout, "global transaction not committed\n"); |
103 | 41 | exit(EXIT_FAILURE);
|
104 | 42 | }
|
105 |
| - //fprintf(stdout, "status[%d, %#x]: ", n, base + n); |
106 |
| - //switch (s) { |
107 |
| - // case TRANSACTION_STATUS_COMMITTED: |
108 |
| - // fprintf(stdout, "committed\n"); |
109 |
| - // break; |
110 |
| - // case TRANSACTION_STATUS_ABORTED: |
111 |
| - // fprintf(stdout, "aborted\n"); |
112 |
| - // break; |
113 |
| - // case TRANSACTION_STATUS_IN_PROGRESS: |
114 |
| - // fprintf(stdout, "in progress\n"); |
115 |
| - // break; |
116 |
| - // default: |
117 |
| - // fprintf(stdout, "(error)\n"); |
118 |
| - // break; |
119 |
| - //} |
120 |
| - } |
121 |
| -} |
122 | 43 |
|
123 |
| -int main(int argc, char **argv) { |
124 |
| - DTMConn conn = DtmConnect("localhost", 5431); |
125 |
| - if (!conn) { |
126 |
| - exit(1); |
127 |
| - } |
128 |
| - |
129 |
| - TransactionId base = 42; |
| 44 | + unless (i%10) { |
| 45 | + printf("Commited %u txs.\n", i+1); |
| 46 | + } |
130 | 47 |
|
131 |
| - int transactions = atoi(argv[1]); |
132 |
| - int i; |
133 |
| - float started = now_s(); |
134 |
| - for (i = 0; i < transactions; i++) { |
135 |
| - printf("-------- %d, base = %d -------- %0.6f sec \n", i, base, now_s() - started); |
136 |
| - start_transaction(conn, base); |
137 |
| - //show_snapshots(conn, base); |
138 |
| - //show_status(conn, base); |
139 |
| - commit_transaction(conn, base); |
140 |
| - base += NODES; |
| 48 | + base++; |
141 | 49 | }
|
142 |
| - float elapsed = now_s() - started; |
143 | 50 |
|
144 |
| - printf( |
145 |
| - "%d transactions in %0.2f sec -> %0.2f tps\n", |
146 |
| - transactions, |
147 |
| - elapsed, |
148 |
| - transactions / elapsed |
149 |
| - ); |
150 | 51 |
|
151 | 52 | DtmDisconnect(conn);
|
152 | 53 | return EXIT_SUCCESS;
|
|
0 commit comments