Created
July 23, 2015 07:42
-
-
Save agrison/82e8db5ec30dda4f9d49 to your computer and use it in GitHub Desktop.
Sample Go app to mass import 1M keys in redis
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" | |
"time" | |
"github.com/garyburd/redigo/redis" | |
"github.com/twinj/uuid" | |
) | |
const numKeys = 1000000 | |
var uuids [numKeys]string | |
var client redis.Conn | |
func prepareUUids() { | |
for i := 0; i < numKeys; i++ { | |
uuids[i] = uuid.NewV4().String() | |
} | |
} | |
func createClient() { | |
c, err := net.Dial("unix", "/tmp/redis.sock") | |
if err != nil { | |
log.Fatal(err) | |
return | |
} | |
client = redis.NewConn(c, 10*time.Second, 10*time.Second) | |
} | |
func massImport() { | |
for i := 0; i < numKeys; i++ { | |
client.Send("SET", uuids[i], uuids[i]) | |
} | |
client.Flush() | |
} | |
func closeClient() { | |
client.Close() | |
} | |
func main() { | |
runtime.GOMAXPROCS(8) | |
prepareUUids() | |
createClient() | |
start := time.Now() | |
massImport() | |
elapsed := time.Since(start) | |
fmt.Println("Took ", elapsed) | |
closeClient() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment