@@ -18,14 +18,14 @@ const (
18
18
19
19
20
20
var cfg1 = pgx.ConnConfig {
21
- Host : "127.0.0.1 " ,
22
- Port : 5432 ,
21
+ Host : "astro9 " ,
22
+ Port : 15432 ,
23
23
Database : "postgres" ,
24
24
}
25
25
26
26
var cfg2 = pgx.ConnConfig {
27
- Host : "127.0.0.1 " ,
28
- Port : 5433 ,
27
+ Host : "astro9 " ,
28
+ Port : 15433 ,
29
29
Database : "postgres" ,
30
30
}
31
31
@@ -60,10 +60,12 @@ func prepare_db() {
60
60
snapshot = execQuery (conn1 , "select dtm_extend($1)" , gtid )
61
61
snapshot = execQuery (conn2 , "select dtm_access($1, $2)" , snapshot , gtid )
62
62
63
- for i := 0 ; i < N_ACCOUNTS ; i ++ {
64
- exec (conn1 , "insert into t values($1, $2)" , i , INIT_AMOUNT )
65
- exec (conn2 , "insert into t values($1, $2)" , i , INIT_AMOUNT )
66
- }
63
+ //for i := 0; i < N_ACCOUNTS; i++ {
64
+ // exec(conn1, "insert into t values($1, $2)", i, INIT_AMOUNT)
65
+ // exec(conn2, "insert into t values($1, $2)", i, INIT_AMOUNT)
66
+ //}
67
+ exec (conn1 , "insert into t (select generate_series(0,$1-1), $2)" ,N_ACCOUNTS ,0 )
68
+ exec (conn2 , "insert into t (select generate_series(0,$1-1), $2)" ,N_ACCOUNTS ,0 )
67
69
68
70
exec (conn1 , "prepare transaction '" + gtid + "'" )
69
71
exec (conn2 , "prepare transaction '" + gtid + "'" )
@@ -104,60 +106,38 @@ func transfer(id int, wg *sync.WaitGroup) {
104
106
defer conn2 .Close ()
105
107
106
108
for i := 0 ; i < N_ITERATIONS ; i ++ {
107
- var dst * pgx.Conn
108
- var src * pgx.Conn
109
- choice := rand .Intn (3 )- 1
109
+
110
110
gtid := strconv .Itoa (id ) + "." + strconv .Itoa (i )
111
- if choice < 0 {
112
- src = conn1
113
- dst = conn1
114
- exec (conn1 , "begin transaction" )
115
- } else if choice > 0 {
116
- src = conn2
117
- dst = conn2
118
- exec (conn2 , "begin transaction" )
119
- } else {
120
- src = conn1
121
- dst = conn2
122
- exec (conn1 , "begin transaction" )
123
- exec (conn2 , "begin transaction" )
124
- snapshot = execQuery (conn1 , "select dtm_extend($1)" , gtid )
125
- snapshot = execQuery (conn2 , "select dtm_access($1, $2)" , snapshot , gtid )
126
- }
127
- //amount := 2*rand.Intn(2) - 1
128
- amount := 1
129
- account1 := rand .Intn (N_ACCOUNTS )
130
- account2 := rand .Intn (N_ACCOUNTS )
131
-
132
- if account1 > account2 {
133
- tmp := account1
134
- account1 = account2
135
- account2 = tmp
136
- }
111
+ amount := 2 * rand .Intn (2 ) - 1
112
+ account1 := 2 * id + 1
113
+ account2 := 2 * id + 2
137
114
138
- exec (src , "update t set v = v - $1 where u=$2" , amount , account1 )
139
- exec (dst , "update t set v = v + $1 where u=$2" , amount , account2 )
115
+ exec (conn1 , "begin transaction" )
116
+ exec (conn2 , "begin transaction" )
117
+ snapshot = execQuery (conn1 , "select dtm_extend($1)" , gtid )
118
+ snapshot = execQuery (conn2 , "select dtm_access($1, $2)" , snapshot , gtid )
140
119
141
- if (src != dst ) {
142
- exec (conn1 , "prepare transaction '" + gtid + "'" )
143
- exec (conn2 , "prepare transaction '" + gtid + "'" )
144
120
145
- exec (conn1 , "select dtm_begin_prepare($1) " , gtid )
146
- exec (conn2 , "select dtm_begin_prepare($1) " , gtid )
121
+ exec (conn1 , "update t set v = v - $1 where u=$2 " , amount , account1 )
122
+ exec (conn2 , "update t set v = v + $1 where u=$2 " , amount , account2 )
147
123
148
- csn = execQuery (conn1 , "select dtm_prepare($1, 0)" , gtid )
149
- csn = execQuery (conn2 , "select dtm_prepare($1, $2)" , gtid , csn )
124
+ exec (conn1 , "prepare transaction '" + gtid + "'" )
125
+ exec (conn2 , "prepare transaction '" + gtid + "'" )
150
126
151
- exec (conn1 , "select dtm_end_prepare ($1, $2 )" , gtid , csn )
152
- exec (conn2 , "select dtm_end_prepare ($1, $2 )" , gtid , csn )
127
+ exec (conn1 , "select dtm_begin_prepare ($1)" , gtid )
128
+ exec (conn2 , "select dtm_begin_prepare ($1)" , gtid )
153
129
154
- exec (conn1 , "commit prepared '" + gtid + "'" )
155
- exec (conn2 , "commit prepared '" + gtid + "'" )
156
- nGlobalTrans ++
157
- } else {
158
- exec (dst , "commit" )
159
- }
160
- }
130
+ csn = execQuery (conn1 , "select dtm_prepare($1, 0)" , gtid )
131
+ csn = execQuery (conn2 , "select dtm_prepare($1, $2)" , gtid , csn )
132
+
133
+ exec (conn1 , "select dtm_end_prepare($1, $2)" , gtid , csn )
134
+ exec (conn2 , "select dtm_end_prepare($1, $2)" , gtid , csn )
135
+
136
+ exec (conn1 , "commit prepared '" + gtid + "'" )
137
+ exec (conn2 , "commit prepared '" + gtid + "'" )
138
+ nGlobalTrans ++
139
+
140
+ }
161
141
162
142
fmt .Printf ("Test completed, performed %d global transactions\n " , nGlobalTrans )
163
143
wg .Done ()
@@ -215,7 +195,8 @@ func main() {
215
195
running = false
216
196
inspectWg .Wait ()
217
197
218
- fmt .Printf ("Elapsed time %f sec" , time .Since (start ).Seconds ())
198
+ fmt .Printf ("Elapsed time %f sec\n " , time .Since (start ).Seconds ())
199
+ fmt .Printf ("TPS = %f\n " , float64 (TRANSFER_CONNECTIONS * N_ITERATIONS )/ time .Since (start ).Seconds ())
219
200
}
220
201
221
202
func exec (conn * pgx.Conn , stmt string , arguments ... interface {}) {
@@ -238,4 +219,3 @@ func checkErr(err error) {
238
219
}
239
220
}
240
221
241
-
0 commit comments