aboutsummaryrefslogtreecommitdiff
path: root/benchmark/benchmark_test.go
blob: e0822f20c6a1837e3d7205d2b01d60bf4be494d0 (plain)
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
137
// SPDX-FileCopyrightText: 2026 Stefan Majewsky <majewsky@gmx.net>
// SPDX-License-Identifier: Apache-2.0

package main_test

import (
	"crypto/sha256"
	"database/sql"
	"fmt"
	"strconv"
	"testing"

	"github.com/go-gorp/gorp/v3"
	_ "github.com/mattn/go-sqlite3"
	"go.xyrillian.de/oblast"
)

func BenchmarkSelect(b *testing.B) {
	const totalRecordCount = 1000

	db, err := sql.Open("sqlite3", "file:foobar?mode=memory&cache=shared")
	if err != nil {
		b.Fatal(err)
	}

	// fill in some random-looking, but deterministic data
	_, err = db.Exec(`CREATE TABLE entries (id INTEGER, message TEXT)`)
	if err != nil {
		b.Fatal(err)
	}
	stmt, err := db.Prepare(`INSERT INTO entries (id, message) VALUES (?, ?)`)
	if err != nil {
		b.Fatal(err)
	}
	for idx := range totalRecordCount {
		buf := sha256.Sum256([]byte(strconv.Itoa(idx)))
		_, err = stmt.Exec(idx, fmt.Sprintf("sha256:%x", buf[:]))
		if err != nil {
			b.Fatal(err)
		}
	}
	err = stmt.Close()
	if err != nil {
		b.Fatal(err)
	}

	// test with different sizes of resultsets (N=1 is an OLTP-like workload,
	// then the larger N lean more towards the OLAP side of things)
	for _, selectedRecordCount := range []int{1, 10, 100, 1000} {
		b.Run("N="+strconv.Itoa(selectedRecordCount), func(b *testing.B) {
			// prepare the functions that will be benched
			type record struct {
				ID      int    `db:"id"`
				Message string `db:"message"`
			}
			store, err := oblast.NewStore[record](oblast.SqliteDialect())
			if err != nil {
				b.Fatal(err)
			}
			gdb := gorp.DbMap{Db: db, Dialect: gorp.SqliteDialect{}}
			query := `SELECT * FROM entries WHERE id < ` + strconv.Itoa(selectedRecordCount) //nolint:gosec

			selectWithOblast := func(b *testing.B) {
				records, err := store.Select(db, query)
				if err != nil {
					b.Error(err)
				}
				if len(records) != selectedRecordCount {
					b.Errorf("expected %d, but got %d records", selectedRecordCount, len(records))
				}
			}

			selectWithGorp := func(b *testing.B) {
				var records []record
				_, err := gdb.Select(&records, query)
				if err != nil {
					b.Error(err)
				}
				if len(records) != selectedRecordCount {
					b.Errorf("expected %d, but got %d records", selectedRecordCount, len(records))
				}
			}

			selectWithSqlite := func(b *testing.B) {
				var count int
				rows, err := db.Query(query)
				if err != nil {
					b.Error(err)
				}
				var (
					id      int64
					message string
				)
				for rows.Next() {
					err := rows.Scan(&id, &message)
					if err != nil {
						b.Error(err)
					}
					if id != 20000 && message != "" { // always true; ensures that values are not optimized away
						count++
					}
				}
				err = rows.Close()
				if err != nil {
					b.Error(err)
				}
				if count != selectedRecordCount {
					b.Errorf("expected %d, but got %d records", selectedRecordCount, count)
				}
			}

			// run once to prewarm caches
			selectWithOblast(b)
			selectWithGorp(b)
			if b.Failed() {
				b.FailNow()
			}

			// run actual benchmark
			b.Run("via Gorp", func(b *testing.B) {
				for range b.N {
					selectWithGorp(b)
				}
			})
			b.Run("via Oblast", func(b *testing.B) {
				for range b.N {
					selectWithOblast(b)
				}
			})
			b.Run("just SQLite", func(b *testing.B) {
				for range b.N {
					selectWithSqlite(b)
				}
			})
		})
	}
}