| 1 |
// Copyright (c) 2024 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package dbutil_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"math/rand" |
| 12 |
"strings" |
| 13 |
"testing" |
| 14 |
"time" |
| 15 |
|
| 16 |
"github.com/stretchr/testify/assert" |
| 17 |
|
| 18 |
"go.mau.fi/util/dbutil" |
| 19 |
"go.mau.fi/util/random" |
| 20 |
) |
| 21 |
|
| 22 |
type AbstractMassInsertable[T dbutil.Array] struct { |
| 23 |
Data T |
| 24 |
} |
| 25 |
|
| 26 |
func (a AbstractMassInsertable[T]) GetMassInsertValues() T { |
| 27 |
return a.Data |
| 28 |
} |
| 29 |
|
| 30 |
type OneParamMassInsertable = AbstractMassInsertable[[1]any] |
| 31 |
|
| 32 |
func TestNewMassInsertBuilder_InvalidParams(t *testing.T) { |
| 33 |
assert.PanicsWithError(t, "invalid insert query: placeholders not found", func() { |
| 34 |
dbutil.NewMassInsertBuilder[OneParamMassInsertable, [1]any]("", "") |
| 35 |
}) |
| 36 |
assert.PanicsWithError(t, "invalid placeholder template: static placeholder $1 not found", func() { |
| 37 |
dbutil.NewMassInsertBuilder[OneParamMassInsertable, [1]any]("INSERT INTO foo VALUES ($1, $2)", "") |
| 38 |
}) |
| 39 |
assert.PanicsWithError(t, "invalid placeholder template: non-static placeholder $2 found", func() { |
| 40 |
dbutil.NewMassInsertBuilder[OneParamMassInsertable, [1]any]("INSERT INTO foo VALUES ($1, $2)", "($1, $2)") |
| 41 |
}) |
| 42 |
assert.PanicsWithError(t, "invalid placeholder template: extra string found", func() { |
| 43 |
dbutil.NewMassInsertBuilder[OneParamMassInsertable, [1]any]("INSERT INTO foo VALUES ($1, $2)", "($1)") |
| 44 |
}) |
| 45 |
} |
| 46 |
|
| 47 |
func TestMassInsertBuilder_Build(t *testing.T) { |
| 48 |
builder := dbutil.NewMassInsertBuilder[OneParamMassInsertable, [1]any]("INSERT INTO foo VALUES ($1, $2)", "($1, $%d)") |
| 49 |
query, values := builder.Build([1]any{"hi"}, []OneParamMassInsertable{{[1]any{"hmm"}}, {[1]any{"meow"}}, {[1]any{"third"}}}) |
| 50 |
assert.Equal(t, "INSERT INTO foo VALUES ($1, $2), ($1, $3), ($1, $4)", query) |
| 51 |
assert.Equal(t, []any{"hi", "hmm", "meow", "third"}, values) |
| 52 |
} |
| 53 |
|
| 54 |
func TestMassInsertBuilder_Build_MultiValue(t *testing.T) { |
| 55 |
ts := time.Now().UnixMilli() |
| 56 |
builder := dbutil.NewMassInsertBuilder[AbstractMassInsertable[[5]any], [3]any]("INSERT INTO foo VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", "($1, $2, $%d, $%d, $3, $%d, $%d, $%d)") |
| 57 |
query, values := builder.Build([3]any{"first", "second", 3}, []AbstractMassInsertable[[5]any]{ |
| 58 |
{[5]any{"foo1", 123, true, "meow", ts}}, |
| 59 |
{[5]any{"foo2", 666, false, "meow", ts + 1}}, |
| 60 |
{[5]any{"foo3", 999, true, "no meow", ts + 2}}, |
| 61 |
{[5]any{"foo4", 0, true, "meow!", 0}}, |
| 62 |
}) |
| 63 |
assert.Equal(t, "INSERT INTO foo VALUES ($1, $2, $4, $5, $3, $6, $7, $8), ($1, $2, $9, $10, $3, $11, $12, $13), ($1, $2, $14, $15, $3, $16, $17, $18), ($1, $2, $19, $20, $3, $21, $22, $23)", query) |
| 64 |
assert.Equal(t, []any{"first", "second", 3, "foo1", 123, true, "meow", ts, "foo2", 666, false, "meow", ts + 1, "foo3", 999, true, "no meow", ts + 2, "foo4", 0, true, "meow!", 0}, values) |
| 65 |
} |
| 66 |
|
| 67 |
func TestMassInsertBuilder_Build_CompareWithManual(t *testing.T) { |
| 68 |
builder := dbutil.NewMassInsertBuilder[AbstractMassInsertable[[5]any], [3]any]("INSERT INTO foo VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", "($1, $2, $%d, $%d, $3, $%d, $%d, $%d)") |
| 69 |
data := makeBenchmarkData[[5]any](100) |
| 70 |
manualQuery, manualParams := buildMassInsertManual(data) |
| 71 |
query, params := builder.Build([3]any{"first", "second", 3}, data) |
| 72 |
assert.Equal(t, manualQuery, query) |
| 73 |
assert.Equal(t, manualParams, params) |
| 74 |
} |
| 75 |
|
| 76 |
func makeBenchmarkData[T dbutil.Array](n int) []AbstractMassInsertable[T] { |
| 77 |
outArr := make([]AbstractMassInsertable[T], n) |
| 78 |
dataLen := len(outArr[0].Data) |
| 79 |
for i := 0; i < dataLen; i++ { |
| 80 |
var val any |
| 81 |
switch rand.Intn(4) { |
| 82 |
case 0: |
| 83 |
val = rand.Intn(1000) |
| 84 |
case 1: |
| 85 |
val = rand.Intn(2) == 0 |
| 86 |
case 2: |
| 87 |
val = time.Now().UnixMilli() |
| 88 |
case 3: |
| 89 |
val = random.String(16) |
| 90 |
} |
| 91 |
for j := 0; j < len(outArr); j++ { |
| 92 |
outArr[j].Data[i] = val |
| 93 |
} |
| 94 |
} |
| 95 |
return outArr |
| 96 |
} |
| 97 |
|
| 98 |
func BenchmarkMassInsertBuilder_Build5x100(b *testing.B) { |
| 99 |
builder := dbutil.NewMassInsertBuilder[AbstractMassInsertable[[5]any], [3]any]("INSERT INTO foo VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", "($1, $2, $%d, $%d, $3, $%d, $%d, $%d)") |
| 100 |
data := makeBenchmarkData[[5]any](100) |
| 101 |
for i := 0; i < b.N; i++ { |
| 102 |
builder.Build([3]any{"first", "second", 3}, data) |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
func buildMassInsertManual(data []AbstractMassInsertable[[5]any]) (string, []any) { |
| 107 |
const queryTemplate = `INSERT INTO foo VALUES %s` |
| 108 |
const placeholderTemplate = "($1, $2, $%d, $%d, $3, $%d, $%d, $%d)" |
| 109 |
placeholders := make([]string, len(data)) |
| 110 |
params := make([]any, 3+len(data)*5) |
| 111 |
params[0] = "first" |
| 112 |
params[1] = "second" |
| 113 |
params[2] = 3 |
| 114 |
for j, item := range data { |
| 115 |
baseIndex := j*5 + 3 |
| 116 |
params[baseIndex] = item.Data[0] |
| 117 |
params[baseIndex+1] = item.Data[1] |
| 118 |
params[baseIndex+2] = item.Data[2] |
| 119 |
params[baseIndex+3] = item.Data[3] |
| 120 |
params[baseIndex+4] = item.Data[4] |
| 121 |
placeholders[j] = fmt.Sprintf(placeholderTemplate, baseIndex+1, baseIndex+2, baseIndex+3, baseIndex+4, baseIndex+5) |
| 122 |
} |
| 123 |
query := fmt.Sprintf(queryTemplate, strings.Join(placeholders, ", ")) |
| 124 |
return query, params |
| 125 |
} |
| 126 |
|
| 127 |
func BenchmarkMassInsertBuilder_Build5x100_Manual(b *testing.B) { |
| 128 |
data := makeBenchmarkData[[5]any](100) |
| 129 |
for i := 0; i < b.N; i++ { |
| 130 |
buildMassInsertManual(data) |
| 131 |
} |
| 132 |
} |