forked from proullon/ramsql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_test.go
136 lines (114 loc) · 2.92 KB
/
bench_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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main_test
import (
"crypto/rand"
"database/sql"
"encoding/hex"
"fmt"
"os"
"testing"
_ "github.com/lib/pq"
_ "github.com/proullon/ramsql/driver"
)
func benchmarkInsert(b *testing.B, driver string, nbRows int) {
u := os.Getenv("PG_USER")
pwd := os.Getenv("PG_PASSWORD")
ip := os.Getenv("PG_IP")
port := os.Getenv("PG_PORT")
dsn := fmt.Sprintf("postgres://%s:%s@%s:%s/postgres?sslmode=disable", u, pwd, ip, port)
db, err := sql.Open(driver, dsn)
if err != nil {
b.Fatalf("sql.Open: %s", err)
}
db.Exec(`DROP TABLE account`)
_, err = db.Exec(`CREATE TABLE account (id BIGSERIAL PRIMARY KEY, email TEXT)`)
if err != nil {
b.Fatalf("sql.Exec: %s", err)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
query := `INSERT INTO account (email) VALUES ($1)`
for i := 0; i < nbRows; i++ {
size := 32
bs := make([]byte, size)
_, err := rand.Read(bs)
if err != nil {
b.Fatalf("rand failed: %s", err)
}
str := hex.EncodeToString(bs)
token := []byte(str)[0:size]
_, err = db.Exec(query, string(token)+"@foobar.com")
if err != nil {
b.Fatalf("cannot insert rows: %s", err)
}
}
}
_, err = db.Exec(`DROP TABLE account`)
if err != nil {
b.Fatalf("sql.Exec: %s", err)
}
}
func benchmarkSelect(b *testing.B, driver string, n int) {
u := os.Getenv("PG_USER")
pwd := os.Getenv("PG_PASSWORD")
ip := os.Getenv("PG_IP")
port := os.Getenv("PG_PORT")
dsn := fmt.Sprintf("postgres://%s:%s@%s:%s/postgres?sslmode=disable", u, pwd, ip, port)
db, err := sql.Open(driver, dsn)
if err != nil {
b.Fatalf("sql.Open: %s", err)
}
db.Exec(`DROP TABLE account`)
_, err = db.Exec(`CREATE TABLE account (id BIGSERIAL PRIMARY KEY, email TEXT)`)
if err != nil {
b.Fatalf("sql.Exec: %s", err)
}
query := `INSERT INTO account (email) VALUES ($1)`
for i := 0; i < n; i++ {
size := 32
bs := make([]byte, size)
_, err := rand.Read(bs)
if err != nil {
b.Fatalf("rand failed: %s", err)
}
str := hex.EncodeToString(bs)
token := []byte(str)[0:size]
_, err = db.Exec(query, string(token)+"@foobar.com")
if err != nil {
b.Fatalf("cannot insert rows: %s", err)
}
}
var id int64
var email string
b.ResetTimer()
for n := 0; n < b.N; n++ {
query := `SELECT account.id, account.email FROM account WHERE id > $1 AND id < $2`
rows, err := db.Query(query, 20, 50)
if err != nil {
b.Fatalf("cannot query rows: %s", err)
}
for rows.Next() {
err = rows.Scan(&id, &email)
if err != nil {
b.Fatalf("cannot scan rows: %s", err)
}
}
}
_ = id
_ = email
_, err = db.Exec(`DROP TABLE account`)
if err != nil {
b.Fatalf("sql.Exec: %s", err)
}
}
func BenchmarkRamSQLSelect(b *testing.B) {
benchmarkSelect(b, "ramsql", 100)
}
func BenchmarkPostgresSelect(b *testing.B) {
benchmarkSelect(b, "postgres", 100)
}
func BenchmarkRamSQLInsert10(b *testing.B) {
benchmarkInsert(b, "ramsql", 10)
}
func BenchmarkPostgresInsert10(b *testing.B) {
benchmarkInsert(b, "postgres", 10)
}