Skip to content

Commit

Permalink
Change bench seed to use new leaderboard flows (#62)
Browse files Browse the repository at this point in the history
Co-authored-by: Arthur Nogueira Gonçalves <arthur.nogueira@wildlifestudios.com>
  • Loading branch information
arthur29 and Arthur Nogueira Gonçalves authored May 14, 2021
1 parent 11b9fd5 commit 90d0652
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions bench/seed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,20 @@
package main

import (
"context"
"flag"
"fmt"
"time"

"github.com/go-redis/redis"

"github.com/gosuri/uiprogress"
"github.com/gosuri/uiprogress/util/strutil"
"github.com/topfreegames/podium/leaderboard/database/redis"
"github.com/topfreegames/podium/testing"
)

var currentStage int
var stages map[int]string

func setScore(cli *redis.Client, leaderboard, member string, score int) {
_, err := cli.ZAdd(leaderboard, redis.Z{float64(score), member}).Result()
if err != nil {
panic(err)
}
}

func createTestData(cli *redis.Client, leaderboardCount, membersPerLeaderboard int, progress func() bool) error {
for i := 0; i < leaderboardCount; i++ {
for j := 0; j < membersPerLeaderboard; j++ {
setScore(cli, fmt.Sprintf("leaderboard-%d", i), fmt.Sprintf("member-%d", j), i*j)
progress()
}
}

return nil
}

var leaderboardCount = flag.Int("leaderboards", 3, "number of leaderboards to create")
var membersPerLeaderboard = flag.Int("mpl", 5000000, "number of members per leaderboard")

Expand All @@ -49,6 +32,11 @@ func main() {

totalOps := *leaderboardCount * *membersPerLeaderboard

config, err := testing.GetDefaultConfig("../../default.yaml")
if err != nil {
panic(err)
}

uiprogress.Start() // start rendering
bar := uiprogress.AddBar(totalOps - 1) // Add a new bar
bar.AppendCompleted()
Expand All @@ -63,11 +51,30 @@ func main() {
return strutil.Resize(text, uint(len(text)))
})

client := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "",
DB: 0,
client := redis.NewStandaloneClient(redis.StandaloneOptions{
Host: config.GetString("redis.host"),
Port: config.GetInt("redis.port"),
Password: config.GetString("redis.password"),
DB: config.GetInt("redis.db"),
})

createTestData(client, *leaderboardCount, *membersPerLeaderboard, bar.Incr)
}

func createTestData(cli redis.Redis, leaderboardCount, membersPerLeaderboard int, progress func() bool) error {
for i := 0; i < leaderboardCount; i++ {
for j := 0; j < membersPerLeaderboard; j++ {
setScore(cli, fmt.Sprintf("leaderboard-%d", i), fmt.Sprintf("member-%d", j), i*j)
progress()
}
}

return nil
}

func setScore(cli redis.Redis, leaderboard, member string, score int) {
err := cli.ZAdd(context.Background(), leaderboard, &redis.Member{Score: float64(score), Member: member})
if err != nil {
panic(err)
}
}

0 comments on commit 90d0652

Please sign in to comment.