generated from maragudk/template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
postgres_test.go
73 lines (55 loc) · 1.44 KB
/
postgres_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//go:build postgres
package sqlite_test
import (
"database/sql"
_ "embed"
"fmt"
"math/rand"
"os/exec"
"testing"
"time"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/maragudk/sqlite-benchmark"
)
func BenchmarkDB_Postgres_ReadPostAndMaybeWriteComment(b *testing.B) {
for _, commentRate := range []float64{0.01, 0.1, 1} {
b.Run(fmt.Sprintf("comment rate %v", commentRate), func(b *testing.B) {
db := setupPostgres(b, false)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_, _, err := db.ReadPost(1)
noErr(b, err)
if rand.Float64() < commentRate {
err = db.WriteComment(1, "Love it!", "Great post. :D")
noErr(b, err)
}
}
})
})
}
}
//go:embed postgres.sql
var postgresSchema string
func setupPostgres(tb testing.TB, withMutex bool) *sqlite.DB {
tb.Helper()
db, err := sql.Open("pgx", "postgresql://test:123@localhost:5432/benchmark?sslmode=disable")
noErr(tb, err)
cmd := exec.Command("docker", "compose", "up", "-d")
if output, err := cmd.CombinedOutput(); err != nil {
tb.Fatal(string(output))
}
time.Sleep(3 * time.Second)
_, err = db.Exec(postgresSchema)
noErr(tb, err)
tb.Cleanup(func() {
cmd := exec.Command("docker", "compose", "down")
if output, err := cmd.CombinedOutput(); err != nil {
tb.Fatal(string(output))
}
})
newDB := sqlite.NewDB(db, withMutex)
err = newDB.WritePost("First post!", loremIpsum)
noErr(tb, err)
return newDB
}