Created
July 23, 2015 08:25
-
-
Save agrison/e46916b9b67a6dbe2bb3 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() | |
} |
On my MacBook Pro in 2015 it took like 3 sec, so roughly 329K updates / sec.
On my current machine (Ryzen 3900X, 64GB memory) on Windows via WSL2 it
takes 1.22 sec
…On Mon, Jan 4, 2021 at 7:43 AM MohammadReza ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
hello, how much time does it take for you with this code?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/e46916b9b67a6dbe2bb3#gistcomment-3581286>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAF4FQOAHODAQBNS5A4SPVTSYFPSVANCNFSM4VSSPE7A>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On my MacBook Pro in 2015 it took like 3 sec, so roughly 329K updates / sec.
On my current machine (Ryzen 3900X, 64GB memory) on Windows via WSL2 it takes 1.22 sec