-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
87 lines (68 loc) · 1.89 KB
/
main_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
package main
import (
"context"
"fmt"
"os"
"testing"
"github.com/redis/go-redis/v9"
"github.com/valkey-io/valkey-go"
)
var redisCli *redis.Client
var valkeyCli valkey.Client
func TestMain(m *testing.M) {
// 共通のセットアップ
ctx := context.Background()
redisCli = NewRedisClient(ctx)
valkeyCli = NewValkeyClient()
// テスト実行
code := m.Run()
// 共通のクリーンアップ
redisCli.Close()
valkeyCli.Close()
// 終了コードを返す
os.Exit(code)
}
func BenchmarkRedisSetValue(b *testing.B) {
ctx := context.Background()
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := fmt.Sprintf("%s-%d", "bench_key1", i)
if err := redisCli.Set(ctx, key, "bench_value", 0).Err(); err != nil {
b.Fatalf("Failed to set value: %v", err)
}
}
}
func BenchmarkRedisGetValue(b *testing.B) {
ctx := context.Background()
if err := redisCli.Set(ctx, "bench_key1", "bench_value", 0).Err(); err != nil {
b.Fatalf("Failed to set value: %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := redisCli.Get(ctx, "bench_key1").Result(); err != nil {
b.Fatalf("Failed to get value: %v", err)
}
}
}
func BenchmarkValkeySetValue(b *testing.B) {
ctx := context.Background()
b.ResetTimer()
for i := 0; i < b.N; i++ {
key := fmt.Sprintf("%s-%d", "bench_key2", i)
if err := valkeyCli.Do(ctx, valkeyCli.B().Set().Key(key).Value("bench_value").Build()).Error(); err != nil {
b.Fatalf("Failed to set value: %v", err)
}
}
}
func BenchmarkValkeyGetValue(b *testing.B) {
ctx := context.Background()
if err := valkeyCli.Do(ctx, valkeyCli.B().Set().Key("bench_key2").Value("bench_value").Build()).Error(); err != nil {
b.Fatalf("Failed to set value: %v", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
if _, err := valkeyCli.Do(ctx, valkeyCli.B().Get().Key("bench_key2").Build()).ToString(); err != nil {
b.Fatalf("Failed to get value: %v", err)
}
}
}