-
Notifications
You must be signed in to change notification settings - Fork 2
/
performance_test.go
106 lines (91 loc) · 2.4 KB
/
performance_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
//go:build performance
// +build performance
package fireboltgosdk
import (
"database/sql"
"fmt"
"log"
"os"
"strconv"
"sync"
"testing"
)
var pool *sql.DB
var threadCount int
var selectLineItemQuery = "select * from lineitem ORDER BY l_orderkey LIMIT 1000;"
var select1Query = "select 1;"
func TestMain(m *testing.M) {
username := os.Getenv("USER_NAME")
password := os.Getenv("PASSWORD")
databaseName := os.Getenv("DATABASE_NAME")
threadCount, _ = strconv.Atoi(os.Getenv("TEST_THREAD_COUNT"))
dsn := fmt.Sprintf("firebolt://%s:%s@%s", username, password, databaseName)
var err error
pool, err = sql.Open("firebolt", dsn)
if err != nil {
log.Fatal("error during opening a connector", err)
}
code := m.Run()
err = pool.Close()
if err != nil {
return
}
os.Exit(code)
}
func BenchmarkSelectLineItemWithThreads(b *testing.B) {
benchmarkSelectWithThreads(b, selectLineItemQuery)
}
func BenchmarkSelectLineItemWithoutThreads(b *testing.B) {
benchmarkSelectWithoutThreads(b, selectLineItemQuery)
}
func BenchmarkSelect1WithThreads(b *testing.B) {
benchmarkSelectWithThreads(b, select1Query)
}
func BenchmarkSelect1WithoutThreads(b *testing.B) {
benchmarkSelectWithoutThreads(b, select1Query)
}
func benchmarkSelectWithThreads(b *testing.B, query string) {
var loops = b.N
var wg sync.WaitGroup
wg.Add(threadCount)
for j := 0; j < threadCount; j++ {
go func(i int) {
executeQuery(loops, query, b)
defer wg.Done()
}(j)
}
wg.Wait()
}
func benchmarkSelectWithoutThreads(b *testing.B, query string) {
executeQuery(b.N, query, b)
}
func executeQuery(loops int, query string, b *testing.B) {
var columns []string
for i := 0; i < loops; i++ {
rows, err := pool.Query(query)
if err != nil {
b.Errorf("error during select query %v", err)
}
// because the function is used for different queries, we only know the number of columns at runtime.
columns, err = rows.Columns()
if err != nil {
b.Errorf("error while getting columns %v", err)
}
columnCount := len(columns)
values := make([]interface{}, columnCount)
valuePointers := make([]interface{}, columnCount)
for i := range columns {
valuePointers[i] = &values[i]
}
// iterating over the resulting rows
for rows.Next() {
if err := rows.Scan(valuePointers...); err != nil {
b.Errorf("error during scan: %v", err)
}
}
err = rows.Close()
if err != nil {
b.Errorf("error while closing the row %v", err)
}
}
}