-
-
Save yyong37/dd0f28fbb26e78821146aeec488f4385 to your computer and use it in GitHub Desktop.
Sample Go App to mass insert 1M keys in redis using goroutines and unix socket
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 ( | |
"fmt" | |
"log" | |
"net" | |
"runtime" | |
"sync" | |
"time" | |
"github.com/garyburd/redigo/redis" | |
"github.com/twinj/uuid" | |
) | |
const numKeys = 1000000 | |
const routines = 16 | |
const portion = numKeys / routines | |
var uuids [numKeys]string | |
var wg sync.WaitGroup | |
var pool *redis.Pool | |
func prepareUUids() { | |
for i := 0; i < numKeys; i++ { | |
uuids[i] = uuid.NewV4().String() | |
} | |
} | |
func createPool() { | |
pool = &redis.Pool{ | |
MaxIdle: 16, | |
MaxActive: 16, | |
IdleTimeout: 60 * time.Second, | |
Dial: func () (redis.Conn, error) { | |
c, err := net.Dial("unix", "/tmp/redis.sock") | |
if err != nil { | |
log.Fatal(err) | |
return nil, err | |
} | |
return redis.NewConn(c, 10*time.Second, 10*time.Second), nil | |
}, | |
TestOnBorrow: func(c redis.Conn, t time.Time) error { | |
_, err := c.Do("PING") | |
return err | |
}, | |
} | |
} | |
func massImport() { | |
wg.Add(routines) | |
for i := 0; i < routines; i++ { | |
go importRoutine(i, pool.Get()) | |
} | |
wg.Wait() | |
} | |
func importRoutine(t int, client redis.Conn) { | |
defer client.Flush() | |
defer wg.Done() | |
for i := t * portion; i < (t + 1) * portion; i++ { | |
client.Send("SET", uuids[i], uuids[i]) | |
} | |
} | |
func closePool() { | |
pool.Close() | |
} | |
func main() { | |
runtime.GOMAXPROCS(16) | |
prepareUUids() | |
createPool() | |
start := time.Now() | |
massImport() | |
elapsed := time.Since(start) | |
fmt.Println("Took ", elapsed) | |
closePool() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment