Skip to content

Instantly share code, notes, and snippets.

@vbatts
Last active January 8, 2016 15:30
Show Gist options
  • Save vbatts/90d20b8003b059849250 to your computer and use it in GitHub Desktop.
Save vbatts/90d20b8003b059849250 to your computer and use it in GitHub Desktop.
etcd hammer
package main
import (
"crypto/rand"
"flag"
"fmt"
"log"
"os"
"time"
"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
"github.com/coreos/etcd/client"
)
var (
flNum = flag.Int("n", 3, "number of concurrent requests to make")
)
func main() {
flag.Parse()
e := fmt.Sprintf("http://%s:%s",
os.Getenv("ETCD_PORT_2379_TCP_ADDR"),
os.Getenv("ETCD_PORT_2379_TCP_PORT"))
if e == "http://:" {
e = "http://127.0.0.1:2379"
}
log.Printf("using %q", e)
cfg := client.Config{
Endpoints: []string{e},
Transport: client.DefaultTransport,
// set timeout per request to fail fast when the target endpoint is unavailable
HeaderTimeoutPerRequest: time.Second,
}
c, err := client.New(cfg)
if err != nil {
log.Fatal(err)
}
kapi := client.NewKeysAPI(c)
var sum int64
quit := make(chan bool)
for i := 0; i < *flNum; i++ {
go func() {
for {
key, err := newKey()
if err != nil {
log.Println(err)
quit <- true
return
}
// set "/foo" key with "bar" value
log.Printf("Setting %q key with 'bar' value", key)
_, err = kapi.Set(context.Background(), key, "bar", nil)
if err != nil {
log.Println(err)
quit <- true
return
}
// get "/foo" key's value
log.Printf("Getting %q key value", key)
_, err = kapi.Get(context.Background(), key, nil)
if err != nil {
log.Println(err)
quit <- true
return
}
// update "/foo" key's value
log.Printf("Updating %q key with 'baz' value", key)
_, err = kapi.Update(context.Background(), key, "baz")
if err != nil {
log.Println(err)
quit <- true
return
}
// update "/foo" key's value
log.Printf("Deleting %q key", key)
_, err = kapi.Delete(context.Background(), key, nil)
if err != nil {
log.Println(err)
quit <- true
return
}
sum += 1
}
}()
}
select {
case <-quit:
log.Printf("iterated %d times", sum)
}
}
func newKey() (string, error) {
b := make([]byte, 10)
if _, err := rand.Read(b); err != nil {
return "", err
}
return fmt.Sprintf("/foo-%x", b), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment