func addKey(key string, new_value string, i *int32) { spec := redis.DefaultSpec() client, e := redis.NewSynchClientWithSpec(spec) if e != nil { log.Println("failed to create the client", e) return } for { atomic.AddInt32(i, 1) if *i > MAXKEYS { done <- true } else { if *i%100000 == 0 { fmt.Printf("Inserted %d \n", *i) } } key := key + strconv.Itoa(int(*i)) value, e := client.Get(key) if e != nil { log.Println("error on Get", e) return } if value == nil { value2set := []byte(new_value) client.Set(key, value2set) runtime.Gosched() } else { //fmt.Println("Already found " + string(key)) } //time.Sleep(time.Millisecond * 10) } }
func delKey(key string, i *int32) { spec := redis.DefaultSpec() client, e := redis.NewSynchClientWithSpec(spec) if e != nil { log.Println("failed to create the client", e) return } for { if *i > MAXKEYS/2 { s := strconv.Itoa(int(*i)) key := key + s value, e := client.Get(key) if e != nil { log.Println("error on Get", e) return } if value != nil { client.Del(key) fmt.Println("Deleted " + key) atomic.AddInt32(i, -1) runtime.Gosched() } } time.Sleep(time.Millisecond * 100) } }
// Reflect over the methods defined in redis.Client // and send back as []string (tolowercase) // TOOD get rid of redundant code in switch // (REVU: needs minor update to core code) func getDefinedMethods(ctype clientType) (map[string]string, *error_) { var mmap = map[string]string{} spec := redis.DefaultSpec().Db(13).Password("go-redis") var e redis.Error var client interface{} switch ctype { case sync: client, e = redis.NewSynchClientWithSpec(spec) case async: client, e = redis.NewAsynchClientWithSpec(spec) } if e != nil { log.Println("ignoring - ", e) } if client == nil { return mmap, &error_{"client is nil", nil} } else { defer client.Quit() } tc := reflect.TypeOf(client) nm := tc.NumMethod() for i := 0; i < nm; i++ { m := tc.Method(i) mname := strings.ToLower(m.Name) mmap[mname] = mname } return mmap, nil }
func makeConcurrentClients(workers int) (clients []redis.Client, err error) { clients = make([]redis.Client, workers) for i := 0; i < workers; i++ { spec := redis.DefaultSpec().Db(13).Password("go-redis") client, e := redis.NewSynchClientWithSpec(spec) if e != nil { log.Println("Error creating client for worker: ", e) return nil, e } clients[i] = client } return }
func doOne(cnt int) error { var delta time.Duration spec := redis.DefaultSpec().Db(13).Password("go-redis") fmt.Printf("\n\n=== Bench synchclient ================ 1 Client -- %d opts --- \n", cnt) fmt.Println() client, e := redis.NewSynchClientWithSpec(spec) if e != nil { return onError("on NewSynchClient call: ", e) } if client == nil { return failedTest("NewSynchClient returned nil!") } // defer client.Quit() // will be deprecated soon defer client.Quit() client.Flushdb() delta = doPing(client, cnt) report("PING", delta, cnt) delta = doIncr(client, cnt) report("INCR", delta, cnt) delta = doSet(client, cnt) report("SET", delta, cnt) delta = doGet(client, cnt) report("GET", delta, cnt) delta = doSadd(client, cnt) report("SADD", delta, cnt) delta = doLpush(client, cnt) report("LPUSH", delta, cnt) delta = doRpush(client, cnt) report("RPUSH", delta, cnt) delta = doLpop(client, cnt) report("LPOP", delta, cnt) delta = doRpop(client, cnt) report("RPOP", delta, cnt) return nil }
func main() { // Parse command-line flags; needed to let flags used by Go-Redis be parsed. flag.Parse() // create the client. Here we are using a synchronous client. // Using the default ConnectionSpec, we are specifying the client to connect // to db 13 (e.g. SELECT 13), and a password of go-redis (e.g. AUTH go-redis) spec := redis.DefaultSpec().Password("go-redis") channel := "example/pubsub/channel" // publish using sync client syncPublish(spec, channel) // publish using async client asyncPublish(spec, channel) }
func main() { // Parse command-line flags; needed to let flags used by Go-Redis be parsed. flag.Parse() // create the client. Here we are using a synchronous client. // Using the default ConnectionSpec, we are specifying the client to connect // to db 13 (e.g. SELECT 13), and a password of go-redis (e.g. AUTH go-redis) spec := redis.DefaultSpec().Db(13).Password("go-redis") client, e := redis.NewSynchClientWithSpec(spec) if e != nil { log.Println("failed to create the client", e) return } key := "examples/hello/user.name" value, e := client.Get(key) if e != nil { log.Println("error on Get", e) return } if value == nil { fmt.Printf("\nHello, don't believe we've met before!\nYour name? ") reader := bufio.NewReader(os.Stdin) user, _ := reader.ReadString(byte('\n')) if len(user) > 1 { user = user[0 : len(user)-1] value = []byte(user) client.Set(key, value) } else { fmt.Printf("vafanculo!\n") return } } fmt.Printf("Hey, ciao %s!\n", fmt.Sprintf("%s", value)) }
func addSingleKey(key string, new_value string) { spec := redis.DefaultSpec() client, e := redis.NewSynchClientWithSpec(spec) if e != nil { log.Println("failed to create the client", e) return } value, e := client.Get(key) if e != nil { log.Println("error on Get", e) return } if value == nil { value2set := []byte(new_value) client.Set(key, value2set) runtime.Gosched() } else { fmt.Println("Already found " + string(key)) } //time.Sleep(time.Millisecond * 10) }
// Use a single redis.AsyncClient with specified number // of workers to bench concurrent load on the async client func benchTask(taskspec taskSpec, iterations int, workers int, printReport bool) (delta time.Duration, err error) { // channel to signal completion signal := make(chan int, workers) // spec and initialize an AsyncClient // will flush your db13 as noted in README .. spec := redis.DefaultSpec().Db(13).Password("go-redis") client, e := redis.NewAsynchClientWithSpec(spec) if e != nil { log.Println("Error creating client for worker: ", e) return -1, e } defer client.Quit() // panics setup(client) t0 := time.Now() for i := 0; i < workers; i++ { id := fmt.Sprintf("%d", i) go taskspec.task(id, signal, client, iterations) } // wait for completion for i := 0; i < workers; i++ { <-signal } delta = time.Now().Sub(t0) if printReport { report(taskspec.name, workers, delta, iterations*workers) } return }
// Test ConnectionSpec uses redis db 13 and assumes AUTH password go-redis func getTestConnSpec() *redis.ConnectionSpec { spec := redis.DefaultSpec() spec.Password("go-redis").Db(13) return spec }