@@ -18,14 +18,14 @@ const (
1818
1919
2020var cfg1 = pgx.ConnConfig {
21- Host : "127.0.0.1 " ,
22- Port : 5432 ,
21+ Host : "astro9 " ,
22+ Port : 15432 ,
2323 Database : "postgres" ,
2424 }
2525
2626var cfg2 = pgx.ConnConfig {
27- Host : "127.0.0.1 " ,
28- Port : 5433 ,
27+ Host : "astro9 " ,
28+ Port : 15433 ,
2929 Database : "postgres" ,
3030 }
3131
@@ -60,10 +60,12 @@ func prepare_db() {
6060 snapshot = execQuery (conn1 , "select dtm_extend($1)" , gtid )
6161 snapshot = execQuery (conn2 , "select dtm_access($1, $2)" , snapshot , gtid )
6262
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 )
6769
6870 exec (conn1 , "prepare transaction '" + gtid + "'" )
6971 exec (conn2 , "prepare transaction '" + gtid + "'" )
@@ -104,60 +106,38 @@ func transfer(id int, wg *sync.WaitGroup) {
104106 defer conn2 .Close ()
105107
106108 for i := 0 ; i < N_ITERATIONS ; i ++ {
107- var dst * pgx.Conn
108- var src * pgx.Conn
109- choice := rand .Intn (3 )- 1
109+
110110 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
137114
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 )
140119
141- if (src != dst ) {
142- exec (conn1 , "prepare transaction '" + gtid + "'" )
143- exec (conn2 , "prepare transaction '" + gtid + "'" )
144120
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 )
147123
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 + "'" )
150126
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 )
153129
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+ }
161141
162142 fmt .Printf ("Test completed, performed %d global transactions\n " , nGlobalTrans )
163143 wg .Done ()
@@ -215,7 +195,8 @@ func main() {
215195 running = false
216196 inspectWg .Wait ()
217197
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 ())
219200}
220201
221202func exec (conn * pgx.Conn , stmt string , arguments ... interface {}) {
@@ -238,4 +219,3 @@ func checkErr(err error) {
238219 }
239220}
240221
241-
0 commit comments