bench.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "strconv"
  6. "github.com/coreos/go-etcd/etcd"
  7. )
  8. func write(endpoint string, requests int, end chan int) {
  9. client := etcd.NewClient([]string{endpoint})
  10. for i := 0; i < requests; i++ {
  11. key := strconv.Itoa(i)
  12. _, err := client.Set(key, key, 0)
  13. if err != nil {
  14. println(err.Error())
  15. }
  16. }
  17. end <- 1
  18. }
  19. func watch(endpoint string, key string) {
  20. client := etcd.NewClient([]string{endpoint})
  21. receiver := make(chan *etcd.Response)
  22. go client.Watch(key, 0, true, receiver, nil)
  23. log.Printf("watching: %s", key)
  24. received := 0
  25. for {
  26. <-receiver
  27. received++
  28. }
  29. }
  30. func main() {
  31. endpoint := flag.String("endpoint", "http://127.0.0.1:4001/", "etcd HTTP endpoint")
  32. rWrites := flag.Int("write-requests", 50000, "number of writes")
  33. cWrites := flag.Int("concurrent-writes", 500, "number of concurrent writes")
  34. watches := flag.Int("watches", 500, "number of writes")
  35. flag.Parse()
  36. for i := 0; i < *watches; i++ {
  37. key := strconv.Itoa(i)
  38. go watch(*endpoint, key)
  39. }
  40. wChan := make(chan int, *cWrites)
  41. for i := 0; i < *cWrites; i++ {
  42. go write(*endpoint, (*rWrites / *cWrites), wChan)
  43. }
  44. for i := 0; i < *cWrites; i++ {
  45. <-wChan
  46. log.Printf("Completed %d writes", (*rWrites / *cWrites))
  47. }
  48. }