-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
75 lines (60 loc) · 1.39 KB
/
main.go
File metadata and controls
75 lines (60 loc) · 1.39 KB
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
package main
import (
"context"
"fmt"
"log/slog"
"time"
_ "time/tzdata"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
)
const BATCH_SIZE = 1000
func main() {
loc, err := time.LoadLocation("UTC")
if err != nil {
panic(err)
}
time.Local = loc
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
DB: 0,
})
ctx := context.Background()
_, err = rdb.Ping(ctx).Result()
if err != nil {
slog.Error("redis ping failed", "error", err)
return
}
maxDuration := 1 * time.Second
startTime := time.Now()
count := 0
pipe := rdb.Pipeline()
for time.Since(startTime) < maxDuration {
impressionKey := fmt.Sprintf("impression:%s", uuid.New().String())
adID := "ad456"
imageURL := "https://example.com/image.jpg"
clickURL := "https://advertiser.com/click"
pipe.HSet(ctx, impressionKey,
"ad_id", adID,
"image_url", imageURL,
"click_url", clickURL)
count++
if count%BATCH_SIZE == 0 {
_, err := pipe.Exec(ctx)
if err != nil {
slog.Error("pipeline exec failed", "error", err)
return
}
pipe = rdb.Pipeline()
}
}
if count%BATCH_SIZE != 0 {
_, err := pipe.Exec(ctx)
if err != nil {
slog.Error("final pipeline exec failed", "error", err)
return
}
}
elapsed := time.Since(startTime)
fmt.Printf("Inserted %d impressions in %.2f seconds (%.2f inserts/second)\n", count, elapsed.Seconds(), float64(count)/elapsed.Seconds())
}