Last active
February 4, 2024 11:51
-
-
Save junoteam/7cddae18b2e145854a0479e9e3e3707e to your computer and use it in GitHub Desktop.
benchmark.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"testing" | |
"time" | |
"github.com/go-redis/redis/v8" | |
) | |
func setDragoFlyDB(iterations *testing.B) { | |
client := redis.NewClient(&redis.Options{ | |
Addr: "127.0.0.1:6376", | |
Password: "", | |
DB: 0, | |
PoolSize: 10, | |
}) | |
err := client.Ping(context.Background()).Err() | |
if err != nil { | |
log.Fatal(err) | |
} | |
iterations.N = 10000 // 10000 - number of iterations | |
ctx := context.Background() | |
for n := 0; n < iterations.N; n++ { | |
val := fmt.Sprintf("bench%d", n) | |
client.Set(ctx, | |
fmt.Sprintf("key%d", n), | |
val, time.Duration(time.Minute*1)) | |
} | |
} | |
func setRedisDB(iterations *testing.B) { | |
client := redis.NewClient(&redis.Options{ | |
Addr: "127.0.0.1:6379", | |
Password: "", | |
DB: 0, | |
PoolSize: 10, | |
}) | |
err := client.Ping(context.Background()).Err() | |
if err != nil { | |
log.Fatal(err) | |
} | |
iterations.N = 10000 // 10000 - number of iterations | |
ctx := context.Background() | |
for n := 0; n < iterations.N; n++ { | |
val := fmt.Sprintf("bench%d", n) | |
client.Set(ctx, | |
fmt.Sprintf("key%d", n), | |
val, time.Duration(time.Minute*1)) | |
} | |
} | |
func getDragoFlyDb(iterations *testing.B) { | |
client := redis.NewClient(&redis.Options{ | |
Addr: "127.0.0.1:6376", | |
Password: "", | |
DB: 0, | |
PoolSize: 10, | |
}) | |
err := client.Ping(context.Background()).Err() | |
if err != nil { | |
log.Fatal(err) | |
} | |
iterations.N = 10000 // 10000 - number of iterations | |
ctx := context.Background() | |
for n := 0; n < iterations.N; n++ { | |
_, err := client.Get(ctx, fmt.Sprintf("key%d", n)).Result() | |
if err == redis.Nil || err != nil { | |
log.Fatalf("dragonfly data miss %d - err: %v", n, err) | |
} | |
} | |
} | |
func getRedisDB(iterations *testing.B) { | |
client := redis.NewClient(&redis.Options{ | |
Addr: "127.0.0.1:6379", | |
Password: "", | |
DB: 0, | |
PoolSize: 10, | |
}) | |
err := client.Ping(context.Background()).Err() | |
if err != nil { | |
log.Fatal(err) | |
} | |
iterations.N = 10000 // 10000 - number of iterations | |
ctx := context.Background() | |
for n := 0; n < iterations.N; n++ { | |
_, err := client.Get(ctx, fmt.Sprintf("key%d", n)).Result() | |
if err == redis.Nil || err != nil { | |
log.Fatalf("redis data miss %d - err: %v", n, err) | |
} | |
} | |
} | |
func main() { | |
fmt.Println("TEST BENCHMARK DragonFly VS Redis") | |
fmt.Println("==================================") | |
fmt.Println("========Compare Set=========") | |
fmt.Println("DragonFly : ", testing.Benchmark(setDragoFlyDB)) | |
fmt.Println("Redis : ", testing.Benchmark(setRedisDB)) | |
fmt.Println() | |
fmt.Println("=======Compare Get=========") | |
fmt.Println("DragonFly : ", testing.Benchmark(getDragoFlyDb)) | |
fmt.Println("Redis : ", testing.Benchmark(getRedisDB)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment