File: benchmark_test.go

package info (click to toggle)
golang-github-cowsql-go-cowsql 1.22.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 716 kB
  • sloc: sh: 373; makefile: 5
file content (125 lines) | stat: -rw-r--r-- 2,786 bytes parent folder | download | duplicates (2)
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
package benchmark_test

import (
	"context"
	"database/sql"
	"io/ioutil"
	"os"
	"testing"
	"time"

	"github.com/cowsql/go-cowsql/app"
	"github.com/cowsql/go-cowsql/benchmark"
	"github.com/stretchr/testify/require"
)

const (
	addr1 = "127.0.0.1:9011"
	addr2 = "127.0.0.1:9012"
	addr3 = "127.0.0.1:9013"
)

func bmSetup(t *testing.T, addr string, join []string) (string, *app.App, *sql.DB, func()) {
	t.Helper()

	dir, err := ioutil.TempDir("", "cowsql-app-test-")
	require.NoError(t, err)

	app, err := app.New(dir, app.WithAddress(addr), app.WithCluster(join))
	require.NoError(t, err)

	readyCtx, cancel := context.WithTimeout(context.Background(), time.Duration(3)*time.Second)
	err = app.Ready(readyCtx)
	require.NoError(t, err)

	db, err := app.Open(context.Background(), "benchmark")
	require.NoError(t, err)

	cleanups := func() {
		os.RemoveAll(dir)
		cancel()
	}
	return dir, app, db, cleanups
}

func bmRun(t *testing.T, bm *benchmark.Benchmark, app *app.App, db *sql.DB) {
	defer db.Close()
	defer app.Close()
	ch := make(chan os.Signal)

	err := bm.Run(ch)
	require.NoError(t, err)
}

// Create a Benchmark with default values.
func TestNew_Default(t *testing.T) {
	dir, app, db, cleanup := bmSetup(t, addr1, nil)
	defer cleanup()

	bm, err := benchmark.New(
		app,
		db,
		dir,
		benchmark.WithCluster([]string{addr1}),
		benchmark.WithDuration(1))
	require.NoError(t, err)

	bmRun(t, bm, app, db)
}

// Create a Benchmark with a kvReadWriteWorkload.
func TestNew_KvReadWrite(t *testing.T) {
	dir, app, db, cleanup := bmSetup(t, addr1, nil)
	defer cleanup()

	bm, err := benchmark.New(
		app,
		db,
		dir,
		benchmark.WithCluster([]string{addr1}),
		benchmark.WithDuration(1),
		benchmark.WithWorkload("KvReadWrite"))
	require.NoError(t, err)

	bmRun(t, bm, app, db)
}

// Create a clustered Benchmark.
func TestNew_ClusteredKvReadWrite(t *testing.T) {
	dir, app, db, cleanup := bmSetup(t, addr1, nil)
	_, _, _, cleanup2 := bmSetup(t, addr2, []string{addr1})
	_, _, _, cleanup3 := bmSetup(t, addr3, []string{addr1})
	defer cleanup()
	defer cleanup2()
	defer cleanup3()

	bm, err := benchmark.New(
		app,
		db,
		dir,
		benchmark.WithCluster([]string{addr1, addr2, addr3}),
		benchmark.WithDuration(2))
	require.NoError(t, err)

	bmRun(t, bm, app, db)
}

// Create a clustered Benchmark that times out waiting for the cluster to form.
func TestNew_ClusteredTimeout(t *testing.T) {
	dir, app, db, cleanup := bmSetup(t, addr1, nil)
	defer cleanup()
	defer db.Close()
	defer app.Close()

	bm, err := benchmark.New(
		app,
		db,
		dir,
		benchmark.WithCluster([]string{addr1, addr2}),
		benchmark.WithClusterTimeout(2))
	require.NoError(t, err)

	ch := make(chan os.Signal)
	err = bm.Run(ch)
	require.Errorf(t, err, "Timed out waiting for cluster: context deadline exceeded")
}