-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
90 lines (76 loc) · 1.58 KB
/
main.go
File metadata and controls
90 lines (76 loc) · 1.58 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"fmt"
"log"
"os"
"runtime/pprof"
"strings"
redis "github.com/go-redis/redis/v8"
)
const (
enableProfiler = false
profilerFileName = "cpu_profile"
)
func main() {
if enableProfiler {
f, err := os.Create(profilerFileName)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:9001",
DB: 0,
Password: "",
})
mode := os.Getenv("MODE")
fmt.Println(mode)
if mode == "POPULATEREDIS" {
insertRandomKeysRoutine(rdb, 5000)
} else {
t := scanRedis(context.Background(), rdb)
t.Trim(.2)
//t.TrimLargestKeys(5)
t.Print(10, t.mem)
}
}
func scanRedis(ctx context.Context, rClient *redis.Client) *trie {
t := NewTrie()
cursor := uint64(0)
var keys []string
var err error
totalKeys := 0
for {
keys, cursor, err = rClient.Scan(ctx, cursor, "*", 10000).Result()
totalKeys += len(keys)
fmt.Printf("\rScanning keys. Total memory so far:%s", ByteCountSI(t.mem))
if err != nil {
fmt.Println(err.Error())
return t
}
addToTrie(ctx, rClient, t, keys)
if cursor == 0 {
fmt.Println()
return t
}
}
}
func addToTrie(ctx context.Context, rClient *redis.Client, t *trie, keys []string) {
pipe := rClient.Pipeline()
m := map[string]*redis.IntCmd{}
for _, key := range keys {
m[key] = pipe.MemoryUsage(ctx, key)
}
_, err := pipe.Exec(ctx)
if err != nil {
fmt.Println(err.Error())
}
for key, memUsage := range m {
prefixes := strings.Split(key, ":")
mem, _ := memUsage.Result()
t.Add(prefixes, mem)
}
}