etcd_long_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. "net/http"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. // This test will kill the current leader and wait for the etcd cluster to elect a new leader for 200 times.
  13. // It will print out the election time and the average election time.
  14. func TestKillLeader(t *testing.T) {
  15. procAttr := new(os.ProcAttr)
  16. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  17. clusterSize := 5
  18. argGroup, etcds, err := createCluster(clusterSize, procAttr, false)
  19. if err != nil {
  20. t.Fatal("cannot create cluster")
  21. }
  22. defer destroyCluster(etcds)
  23. stop := make(chan bool)
  24. leaderChan := make(chan string, 1)
  25. all := make(chan bool, 1)
  26. time.Sleep(time.Second)
  27. go monitor(clusterSize, 1, leaderChan, all, stop)
  28. var totalTime time.Duration
  29. leader := "http://127.0.0.1:7001"
  30. for i := 0; i < clusterSize; i++ {
  31. fmt.Println("leader is ", leader)
  32. port, _ := strconv.Atoi(strings.Split(leader, ":")[2])
  33. num := port - 7001
  34. fmt.Println("kill server ", num)
  35. etcds[num].Kill()
  36. etcds[num].Release()
  37. start := time.Now()
  38. for {
  39. newLeader := <-leaderChan
  40. if newLeader != leader {
  41. leader = newLeader
  42. break
  43. }
  44. }
  45. take := time.Now().Sub(start)
  46. totalTime += take
  47. avgTime := totalTime / (time.Duration)(i+1)
  48. fmt.Println("Leader election time is ", take, "with election timeout", ElectionTimeout)
  49. fmt.Println("Leader election time average is", avgTime, "with election timeout", ElectionTimeout)
  50. etcds[num], err = os.StartProcess("etcd", argGroup[num], procAttr)
  51. }
  52. stop <- true
  53. }
  54. // TestKillRandom kills random machines in the cluster and
  55. // restart them after all other machines agree on the same leader
  56. func TestKillRandom(t *testing.T) {
  57. procAttr := new(os.ProcAttr)
  58. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  59. clusterSize := 9
  60. argGroup, etcds, err := createCluster(clusterSize, procAttr, false)
  61. if err != nil {
  62. t.Fatal("cannot create cluster")
  63. }
  64. defer destroyCluster(etcds)
  65. stop := make(chan bool)
  66. leaderChan := make(chan string, 1)
  67. all := make(chan bool, 1)
  68. time.Sleep(3 * time.Second)
  69. go monitor(clusterSize, 4, leaderChan, all, stop)
  70. toKill := make(map[int]bool)
  71. for i := 0; i < 200; i++ {
  72. fmt.Printf("TestKillRandom Round[%d/200]\n", i)
  73. j := 0
  74. for {
  75. r := rand.Int31n(9)
  76. if _, ok := toKill[int(r)]; !ok {
  77. j++
  78. toKill[int(r)] = true
  79. }
  80. if j > 3 {
  81. break
  82. }
  83. }
  84. for num, _ := range toKill {
  85. etcds[num].Kill()
  86. etcds[num].Release()
  87. }
  88. time.Sleep(ElectionTimeout)
  89. <-leaderChan
  90. for num, _ := range toKill {
  91. etcds[num], err = os.StartProcess("etcd", argGroup[num], procAttr)
  92. }
  93. toKill = make(map[int]bool)
  94. <-all
  95. }
  96. stop <- true
  97. }
  98. func templateBenchmarkEtcdDirectCall(b *testing.B, tls bool) {
  99. procAttr := new(os.ProcAttr)
  100. procAttr.Files = []*os.File{nil, os.Stdout, os.Stderr}
  101. clusterSize := 3
  102. _, etcds, _ := createCluster(clusterSize, procAttr, tls)
  103. defer destroyCluster(etcds)
  104. time.Sleep(time.Second)
  105. b.ResetTimer()
  106. for i := 0; i < b.N; i++ {
  107. resp, _ := http.Get("http://127.0.0.1:4001/test/speed")
  108. resp.Body.Close()
  109. }
  110. }
  111. func BenchmarkEtcdDirectCall(b *testing.B) {
  112. templateBenchmarkEtcdDirectCall(b, false)
  113. }
  114. func BenchmarkEtcdDirectCallTls(b *testing.B) {
  115. templateBenchmarkEtcdDirectCall(b, true)
  116. }