Skip to content

Instantly share code, notes, and snippets.

@x4m
Last active July 19, 2021 07:01
Show Gist options
  • Save x4m/8b6025995eedf29bf588727375014dfc to your computer and use it in GitHub Desktop.
Save x4m/8b6025995eedf29bf588727375014dfc to your computer and use it in GitHub Desktop.
stress vxid->xid->2px
package main
import (
"fmt"
"github.com/jackc/pgx"
"math/rand"
"time"
)
var timeToExit = false
func main() {
go doInserts(0)
go doInserts(100000)
go doInserts(200000)
time.Sleep(time.Second * 10)
timeToExit = true
time.Sleep(time.Second)
}
func doInserts(start int) {
cs := "host=127.0.0.1 port=5432 user=x4mmm database=postgres"
ci, err := pgx.ParseConnectionString(cs)
panic_err(err)
conn, err := pgx.Connect(ci)
panic_err(err)
for i := start; ; i++ {
if timeToExit {
return
}
is := fmt.Sprintf("%d", i)
_, err = conn.Exec("begin;")
time.Sleep(time.Millisecond * 50)
panic_err(err)
_, err = conn.Exec(fmt.Sprintf( //"begin;"+
"insert into t1 values(%d);"+ // select pg_sleep(0.1);"+
" PREPARE TRANSACTION 'x"+is+"'; ",
rand.Int31()))
panic_err(err)
_, err := conn.Exec(" COMMIT PREPARED 'x" + is + "';")
panic_err(err)
}
}
func panic_err(err error) {
if err != nil {
panic(err)
}
}
package main
import (
"fmt"
"github.com/jackc/pgx"
"math/rand"
"time"
)
var timeToExit = false
func main() {
go doInserts(0)
go doInserts(100000)
go doInserts(200000)
time.Sleep(time.Second * 10)
timeToExit = true
time.Sleep(time.Second)
}
func doInserts(start int) {
cs := "host=127.0.0.1 port=5432 user=x4mmm database=postgres"
ci, err := pgx.ParseConnectionString(cs)
panic_err(err)
conn, err := pgx.Connect(ci)
panic_err(err)
for i := start; ; i++ {
if timeToExit {
return
}
is := fmt.Sprintf("%d", i)
_, err = conn.Exec(fmt.Sprintf("begin; select pg_sleep(0.1);"+
"insert into t1 values(%d); select pg_sleep(0.01);"+
" PREPARE TRANSACTION 'x"+is+"'; ",
rand.Int31()))
panic_err(err)
_, err := conn.Exec(" COMMIT PREPARED 'x" + is + "';")
panic_err(err)
}
}
func panic_err(err error) {
if err != nil {
panic(err)
}
}
package main
import (
"fmt"
"github.com/jackc/pgx"
"math/rand"
"time"
)
var timeToExit = false
func main() {
go doInserts(0)
go doInserts(100000)
go doInserts(200000)
time.Sleep(time.Second * 10)
timeToExit = true
time.Sleep(time.Second)
}
func doInserts(start int) {
cs := "host=127.0.0.1 port=5432 user=x4mmm database=postgres"
ci, err := pgx.ParseConnectionString(cs)
panic_err(err)
conn, err := pgx.Connect(ci)
panic_err(err)
for i := start; ; i++ {
if timeToExit {
return
}
is := fmt.Sprintf("%d", i)
_, err = conn.Exec(fmt.Sprintf("begin;"+
"insert into t1 values(%d); select pg_sleep(0.1);"+
" PREPARE TRANSACTION 'x"+is+"'; ",
rand.Int31()))
panic_err(err)
_, err := conn.Exec(" COMMIT PREPARED 'x" + is + "';")
panic_err(err)
}
}
func panic_err(err error) {
if err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment