-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (118 loc) · 3.5 KB
/
main.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
package main
// Import packages
import (
"context"
"fmt"
"math/rand"
"os"
"strconv"
"time"
"github.com/joho/godotenv"
"github.com/redis/go-redis/v9"
)
// define context for use in redis
var ctx = context.Background()
// Generate Random string with a custom length
func GenerateRandomString(n int) string {
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
result := make([]byte, n)
rand.Seed(time.Now().UnixNano())
for i := range result {
result[i] = letters[rand.Intn(len(letters))]
}
return string(result)
}
// Function to insert a map of key values into the redis
func RedisSetKey(redis_connection *redis.Client, keyValues map[string]string) {
fmt.Println("Start insert keys")
err := redis_connection.MSet(ctx, keyValues).Err()
if err != nil {
panic(err)
}
fmt.Println("All keys insert successfully")
}
// Function for Scan keys with a specific key name
func RedisScanKeys(redis_connection *redis.Client, keyName string) {
startTime := time.Now()
var cursor uint64
var keys []string
var err error
pattern := fmt.Sprintf("*" + keyName + "*")
for {
keys, cursor, err = redis_connection.Scan(ctx, cursor, pattern, 10).Result()
if err != nil {
fmt.Println("Error while scanning:", err)
return
}
for _, key := range keys {
fmt.Println("Found key:", key)
}
if cursor == 0 {
break
}
}
fmt.Println(time.Since(startTime).Milliseconds())
}
// Function for Get specific key from redis
func RedisGetKeys(redis_connection *redis.Client, keyName string) {
startTime := time.Now()
key, err := redis_connection.Get(ctx, keyName).Result()
if err != nil {
panic(err)
}
fmt.Println("Found key:", key)
fmt.Println(time.Since(startTime).Milliseconds())
}
func main() {
// Load .env file and get variables
if err := godotenv.Load(); err != nil {
panic("Failed to load environment variables")
}
key_count, _ := strconv.Atoi(os.Getenv("KEY_COUNT"))
key_char_size, _ := strconv.Atoi(os.Getenv("KEY_CHAR_SIZE"))
value_char_size, _ := strconv.Atoi(os.Getenv("VALUE_CHAR_SIZE"))
run_count, _ := strconv.Atoi(os.Getenv("RUN_COUNT"))
redis_host := os.Getenv("REDIS_HOST")
redis_port := os.Getenv("REDIS_PORT")
// Create redis connection address
redis_connection := fmt.Sprintf(redis_host + ":" + redis_port)
// loop for run application for several times
for i := 1; i <= run_count; i++ {
fmt.Printf("************** Run %d ************\n", i)
// Create connection for start insert keys
rdb := redis.NewClient(&redis.Options{
Addr: redis_connection,
Password: "",
DB: 0,
})
fmt.Println("Connected to redis")
// FlushAll keys from redis
rdb.FlushAll(ctx)
var testKey string
// Generate a map of random key values for insert into redis
randomMap := make(map[string]string, key_count)
for i := 0; i < key_count; i++ {
key := GenerateRandomString(key_char_size)
value := GenerateRandomString(value_char_size)
randomMap[key] = value
testKey = key
}
// Call Redis Set Function and Close redis client connection
RedisSetKey(rdb, randomMap)
rdb.Close()
// Create another connection for scan and get keys
rdb2 := redis.NewClient(&redis.Options{
Addr: redis_connection,
Password: "",
DB: 0,
})
// Call Redis Scan Function to find a key
fmt.Println("*************** Scan Method **************")
RedisScanKeys(rdb2, testKey)
// Call Redis Get Function to find a key
fmt.Println("*************** Get Method ***************")
RedisGetKeys(rdb2, testKey)
// Close Connection
rdb2.Close()
}
}