Skip to content

Instantly share code, notes, and snippets.

@tirkarthi
Created August 27, 2018 11:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tirkarthi/6ce3a681b723b3bd20434fb27ca71b74 to your computer and use it in GitHub Desktop.
Save tirkarthi/6ce3a681b723b3bd20434fb27ca71b74 to your computer and use it in GitHub Desktop.
A sample program using redis streams and goroutines
package main
import (
"fmt"
"github.com/go-redis/redis"
"sync"
"time"
)
func main() {
var wg sync.WaitGroup
var redisdb = redis.NewClient(&redis.Options{
Addr: "localhost:6379",
Password: "", // no password set
DB: 0,
})
var data = &redis.XAddArgs{
Stream: "chennai",
ID: "*",
Values: map[string]interface{}{"temperature": 21, "humidity": 11},
}
var batch = 5
wg.Add(batch)
start := time.Now()
for i := 0; i < batch; i++ {
go func() {
for i := 0; i < 86400; i++ {
redisdb.XAdd(data).Result()
}
defer wg.Done()
}()
}
wg.Wait()
elapsed := time.Since(start)
var total = 86400 * batch
fmt.Print(total, " inserts took ", elapsed, " at the rate of ", float64(total)/elapsed.Seconds(), " per second")
}
// go build redis_run.go && time ./redis_run
// 432000 inserts took 15.839241009s at the rate of 27274.03413803311 per second.
// redis_run 6.87s user 10.51s system 109% cpu 15.853 total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment