-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperftest.go
182 lines (154 loc) · 5.13 KB
/
perftest.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"context"
"fmt"
"os"
"reflect"
"time"
"github.com/google/uuid"
"github.com/oklog/ulid/v2"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsoncodec"
"go.mongodb.org/mongo-driver/bson/bsonrw"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/mongo"
mongooptions "go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readpref"
)
func main() {
coll, cleanup := mustConnect()
defer cleanup()
tester := Tester{
Coll: coll,
}
start := time.Now()
results, err := tester.Run()
if err != nil {
panic(err)
}
testDuration := time.Now().Sub(start)
printer := new(TablePrinter)
printer.Print(results)
fmt.Printf("\nTotal execution time: %s\n", testDuration.Round(time.Millisecond).String())
}
func mustConnect() (*mongo.Collection, func()) {
const timeout = 1 * time.Second
ctx := context.Background()
uri := os.Getenv("MONGO_URI")
registry := bson.NewRegistryBuilder().
RegisterTypeEncoder(ulidType, bsoncodec.ValueEncoderFunc(ULIDEncodeValue)).
RegisterTypeDecoder(ulidType, bsoncodec.ValueDecoderFunc(ULIDDecodeValue)).
RegisterTypeEncoder(uuidType, bsoncodec.ValueEncoderFunc(UUIDEncodeValue)).
RegisterTypeDecoder(uuidType, bsoncodec.ValueDecoderFunc(UUIDDecodeValue)).
Build()
connCtx, connCancel := context.WithTimeout(ctx, timeout)
defer connCancel()
connOptions := mongooptions.Client().ApplyURI(uri).SetRegistry(registry)
client, err := mongo.Connect(connCtx, connOptions)
if err != nil {
panic(fmt.Errorf("connection failed: %w", err))
}
pingCtx, pingCancel := context.WithTimeout(ctx, timeout)
defer pingCancel()
if err = client.Ping(pingCtx, readpref.Primary()); err != nil {
panic(fmt.Errorf("connection failed: %w", err))
}
cleanup := func() {
if err = client.Disconnect(ctx); err != nil {
panic(err)
}
}
const dbName = "perftest"
const collectionName = "perftest"
db := client.Database(dbName)
collection := db.Collection(collectionName)
return collection, cleanup
}
var uuidType = reflect.TypeOf(uuid.UUID{})
var ulidType = reflect.TypeOf(ulid.ULID{})
func ULIDEncodeValue(_ bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
if !val.IsValid() || val.Type() != ulidType {
return bsoncodec.ValueEncoderError{Name: "ULIDEncodeValue", Types: []reflect.Type{ulidType}, Received: val}
}
b, ok := val.Interface().(ulid.ULID)
if !ok {
return fmt.Errorf("failed to convert interface of type %s to %s",
reflect.TypeOf(val.Interface()).String(), reflect.TypeOf(b))
}
if err := vw.WriteBinaryWithSubtype(b[:], bsontype.BinaryUUID); err != nil {
return fmt.Errorf("failed to write binary: %w", err)
}
return nil
}
func ULIDDecodeValue(_ bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != ulidType {
return bsoncodec.ValueDecoderError{Name: "ULIDDecodeValue", Types: []reflect.Type{ulidType}, Received: val}
}
var data []byte
var subtype byte
var err error
//nolint:exhaustive // the rest of types are covered by the `default` branch
switch vrType := vr.Type(); vrType {
case bsontype.Binary:
data, subtype, err = vr.ReadBinary()
if subtype != bsontype.BinaryUUID {
return fmt.Errorf("unsupported binary subtype %v for ULID", subtype)
}
case bsontype.Null:
err = vr.ReadNull()
case bsontype.Undefined:
err = vr.ReadUndefined()
default:
return fmt.Errorf("cannot decode %v into a ULID", vrType)
}
if err != nil {
return fmt.Errorf("failed to read ULID value: %w", err)
}
val.Set(reflect.ValueOf(ulid.ULID(data)))
return nil
}
func UUIDEncodeValue(_ bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
if !val.IsValid() || val.Type() != uuidType {
return bsoncodec.ValueEncoderError{Name: "UUIDEncodeValue", Types: []reflect.Type{uuidType}, Received: val}
}
b, ok := val.Interface().(uuid.UUID)
if !ok {
return fmt.Errorf("failed to convert interface of type %s to %s",
reflect.TypeOf(val.Interface()).String(), reflect.TypeOf(b))
}
if err := vw.WriteBinaryWithSubtype(b[:], bsontype.BinaryUUID); err != nil {
return fmt.Errorf("failed to write binary: %w", err)
}
return nil
}
func UUIDDecodeValue(_ bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {
if !val.CanSet() || val.Type() != uuidType {
return bsoncodec.ValueDecoderError{Name: "UUIDDecodeValue", Types: []reflect.Type{uuidType}, Received: val}
}
var data []byte
var subtype byte
var err error
//nolint:exhaustive // the rest of types are covered by the `default` branch
switch vrType := vr.Type(); vrType {
case bsontype.Binary:
data, subtype, err = vr.ReadBinary()
if subtype != bsontype.BinaryUUID {
return fmt.Errorf("unsupported binary subtype %v for UUID", subtype)
}
case bsontype.Null:
err = vr.ReadNull()
case bsontype.Undefined:
err = vr.ReadUndefined()
default:
return fmt.Errorf("cannot decode %v into a UUID", vrType)
}
if err != nil {
return fmt.Errorf("failed to read UUID value: %w", err)
}
uuidBytes, err := uuid.FromBytes(data)
if err != nil {
return fmt.Errorf("failed to read UUID from bytes: %w", err)
}
val.Set(reflect.ValueOf(uuidBytes))
return nil
}