lease.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package cmd
  15. import (
  16. "fmt"
  17. "time"
  18. v3 "github.com/coreos/etcd/clientv3"
  19. "github.com/coreos/etcd/pkg/report"
  20. "github.com/spf13/cobra"
  21. "golang.org/x/net/context"
  22. "gopkg.in/cheggaaa/pb.v1"
  23. )
  24. var leaseKeepaliveCmd = &cobra.Command{
  25. Use: "lease-keepalive",
  26. Short: "Benchmark lease keepalive",
  27. Run: leaseKeepaliveFunc,
  28. }
  29. var (
  30. leaseKeepaliveTotal int
  31. )
  32. func init() {
  33. RootCmd.AddCommand(leaseKeepaliveCmd)
  34. leaseKeepaliveCmd.Flags().IntVar(&leaseKeepaliveTotal, "total", 10000, "Total number of lease keepalive requests")
  35. }
  36. func leaseKeepaliveFunc(cmd *cobra.Command, args []string) {
  37. requests := make(chan struct{})
  38. clients := mustCreateClients(totalClients, totalConns)
  39. bar = pb.New(leaseKeepaliveTotal)
  40. bar.Format("Bom !")
  41. bar.Start()
  42. r := newReport()
  43. for i := range clients {
  44. wg.Add(1)
  45. go func(c v3.Lease) {
  46. defer wg.Done()
  47. resp, err := c.Grant(context.Background(), 100)
  48. if err != nil {
  49. panic(err)
  50. }
  51. for _ = range requests {
  52. st := time.Now()
  53. _, err := c.KeepAliveOnce(context.TODO(), resp.ID)
  54. r.Results() <- report.Result{Err: err, Start: st, End: time.Now()}
  55. bar.Increment()
  56. }
  57. }(clients[i])
  58. }
  59. wg.Add(1)
  60. go func() {
  61. defer wg.Done()
  62. for i := 0; i < leaseKeepaliveTotal; i++ {
  63. requests <- struct{}{}
  64. }
  65. close(requests)
  66. }()
  67. rc := r.Run()
  68. wg.Wait()
  69. close(r.Results())
  70. bar.Finish()
  71. fmt.Printf("%s", <-rc)
  72. }