lease.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "time"
  17. v3 "github.com/coreos/etcd/clientv3"
  18. "github.com/spf13/cobra"
  19. "golang.org/x/net/context"
  20. "gopkg.in/cheggaaa/pb.v1"
  21. )
  22. var leaseKeepaliveCmd = &cobra.Command{
  23. Use: "lease-keepalive",
  24. Short: "Benchmark lease keepalive",
  25. Run: leaseKeepaliveFunc,
  26. }
  27. var (
  28. leaseKeepaliveTotal int
  29. )
  30. func init() {
  31. RootCmd.AddCommand(leaseKeepaliveCmd)
  32. leaseKeepaliveCmd.Flags().IntVar(&leaseKeepaliveTotal, "total", 10000, "Total number of lease keepalive requests")
  33. }
  34. func leaseKeepaliveFunc(cmd *cobra.Command, args []string) {
  35. results = make(chan result)
  36. requests := make(chan struct{})
  37. bar = pb.New(leaseKeepaliveTotal)
  38. clients := mustCreateClients(totalClients, totalConns)
  39. bar.Format("Bom !")
  40. bar.Start()
  41. for i := range clients {
  42. wg.Add(1)
  43. go doLeaseKeepalive(context.Background(), clients[i].Lease, requests)
  44. }
  45. pdoneC := printReport(results)
  46. for i := 0; i < leaseKeepaliveTotal; i++ {
  47. requests <- struct{}{}
  48. }
  49. close(requests)
  50. wg.Wait()
  51. bar.Finish()
  52. close(results)
  53. <-pdoneC
  54. }
  55. func doLeaseKeepalive(ctx context.Context, client v3.Lease, requests <-chan struct{}) {
  56. defer wg.Done()
  57. resp, err := client.Grant(ctx, 100)
  58. if err != nil {
  59. panic(err)
  60. }
  61. for _ = range requests {
  62. st := time.Now()
  63. _, err := client.KeepAliveOnce(ctx, resp.ID)
  64. var errStr string
  65. if err != nil {
  66. errStr = err.Error()
  67. }
  68. results <- result{errStr: errStr, duration: time.Since(st), happened: time.Now()}
  69. bar.Increment()
  70. }
  71. }