forked from canonical/go-dqlite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench.go
130 lines (103 loc) · 2.92 KB
/
bench.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
package main
import (
"context"
"database/sql"
"fmt"
"net"
"testing"
"time"
"github.com/CanonicalLtd/go-dqlite"
"github.com/CanonicalLtd/raft-test"
"github.com/hashicorp/raft"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// Return a new bench command.
func newBench() *cobra.Command {
bench := &cobra.Command{
Use: "bench [address]",
Short: "Bench all raft logs after the given index (included).",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
address := args[0]
role := args[1]
if role == "server" {
return runServer(address)
}
return runClient(address)
},
}
return bench
}
func runServer(address string) error {
registry := dqlite.NewRegistry("0")
fsm := dqlite.NewFSM(registry)
t := &testing.T{}
r, cleanup := rafttest.Server(t, fsm, rafttest.Transport(func(i int) raft.Transport {
_, transport := raft.NewInmemTransport(raft.ServerAddress(address))
return transport
}))
defer cleanup()
listener, err := net.Listen("tcp", address)
if err != nil {
return errors.Wrap(err, "failed to listen")
}
server, err := dqlite.NewServer(r, registry, listener)
if err != nil {
return errors.Wrap(err, "failed to create server")
}
time.Sleep(time.Minute)
return server.Close()
}
func runClient(address string) error {
store, err := dqlite.DefaultServerStore(":memory:")
if err != nil {
return errors.Wrap(err, "failed to create server store")
}
if err := store.Set(context.Background(), []dqlite.ServerInfo{{Address: address}}); err != nil {
return errors.Wrap(err, "failed to set server address")
}
driver, err := dqlite.NewDriver(store)
if err != nil {
return errors.Wrap(err, "failed to create dqlite driver")
}
sql.Register("dqlite", driver)
ctx, cancel := context.WithTimeout(context.Background(), 250*time.Millisecond)
defer cancel()
db, err := sql.Open("dqlite", "test.db")
if err != nil {
return errors.Wrap(err, "failed to open database")
}
defer db.Close()
tx, err := db.Begin()
if err != nil {
return errors.Wrap(err, "failed to begin transaction")
}
start := time.Now()
if _, err := tx.ExecContext(ctx, "CREATE TABLE test (n INT, t TEXT)"); err != nil {
return errors.Wrapf(err, "failed to create test table")
}
for i := 0; i < 100; i++ {
if _, err := tx.ExecContext(ctx, "INSERT INTO test(n,t) VALUES(?, ?)", int64(i), "hello"); err != nil {
return errors.Wrapf(err, "failed to insert test value")
}
}
rows, err := tx.QueryContext(ctx, "SELECT n FROM test")
if err != nil {
return errors.Wrapf(err, "failed to query test table")
}
for rows.Next() {
var n int64
if err := rows.Scan(&n); err != nil {
return errors.Wrap(err, "failed to scan row")
}
}
if err := rows.Err(); err != nil {
return errors.Wrap(err, "result set failure")
}
if err := tx.Commit(); err != nil {
return errors.Wrap(err, "failed to commit transaction")
}
fmt.Printf("time %s\n", time.Since(start))
return nil
}