bench.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "strconv"
  6. "time"
  7. "github.com/coreos/etcd/third_party/github.com/coreos/go-etcd/etcd"
  8. )
  9. var (
  10. // output debug to log
  11. verbose *bool
  12. )
  13. func write(endpoint string, requests int, end chan int) {
  14. client := etcd.NewClient([]string{endpoint})
  15. for i := 0; i < requests; i++ {
  16. key := strconv.Itoa(i)
  17. _, err := client.Set(key, key, 0)
  18. if err != nil {
  19. println(err.Error())
  20. }
  21. }
  22. end <- 1
  23. }
  24. func watch(endpoint string, key string) {
  25. client := etcd.NewClient([]string{endpoint})
  26. receiver := make(chan *etcd.Response)
  27. go client.Watch(key, 0, true, receiver, nil)
  28. if *verbose {
  29. log.Printf("watching: %s", key)
  30. }
  31. received := 0
  32. for {
  33. <-receiver
  34. received++
  35. }
  36. }
  37. func main() {
  38. endpoint := flag.String("endpoint", "http://127.0.0.1:4001", "etcd HTTP endpoint")
  39. rWrites := flag.Int("write-requests", 50000, "number of writes")
  40. cWrites := flag.Int("concurrent-writes", 500, "number of concurrent writes")
  41. watches := flag.Int("watches", 500, "number of concurrent watches")
  42. verbose = flag.Bool("verbose", false, "output debug info")
  43. flag.Parse()
  44. log.Printf("Benchmarking %v", *endpoint)
  45. log.Printf("%v writes with %v concurrent writers and %v watches", *rWrites, *cWrites, *watches)
  46. t := time.Now()
  47. for i := 0; i < *watches; i++ {
  48. key := strconv.Itoa(i)
  49. go watch(*endpoint, key)
  50. }
  51. wChan := make(chan int, *cWrites)
  52. for i := 0; i < *cWrites; i++ {
  53. go write(*endpoint, (*rWrites / *cWrites), wChan)
  54. }
  55. for i := 0; i < *cWrites; i++ {
  56. <-wChan
  57. if *verbose {
  58. log.Printf("Completed %d writes", (*rWrites / *cWrites))
  59. }
  60. }
  61. log.Printf("Took %v", time.Now().Sub(t))
  62. }