You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

156 lines
3.7 KiB

  1. /*
  2. Copyright 2016 Google LLC
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. Scantest does scan-related load testing against Cloud Bigtable. The logic here
  15. mimics a similar test written using the Java client.
  16. */
  17. package main
  18. import (
  19. "bytes"
  20. "context"
  21. "flag"
  22. "fmt"
  23. "log"
  24. "math/rand"
  25. "os"
  26. "sync"
  27. "sync/atomic"
  28. "text/tabwriter"
  29. "time"
  30. "cloud.google.com/go/bigtable"
  31. "cloud.google.com/go/bigtable/internal/cbtconfig"
  32. "cloud.google.com/go/bigtable/internal/stat"
  33. )
  34. var (
  35. runFor = flag.Duration("run_for", 5*time.Second, "how long to run the load test for")
  36. numScans = flag.Int("concurrent_scans", 1, "number of concurrent scans")
  37. rowLimit = flag.Int("row_limit", 10000, "max number of records per scan")
  38. config *cbtconfig.Config
  39. client *bigtable.Client
  40. )
  41. func main() {
  42. flag.Usage = func() {
  43. fmt.Printf("Usage: scantest [options] <table_name>\n\n")
  44. flag.PrintDefaults()
  45. }
  46. var err error
  47. config, err = cbtconfig.Load()
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. config.RegisterFlags()
  52. flag.Parse()
  53. if err := config.CheckFlags(cbtconfig.ProjectAndInstanceRequired); err != nil {
  54. log.Fatal(err)
  55. }
  56. if config.Creds != "" {
  57. os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", config.Creds)
  58. }
  59. if flag.NArg() != 1 {
  60. flag.Usage()
  61. os.Exit(1)
  62. }
  63. table := flag.Arg(0)
  64. log.Printf("Dialing connections...")
  65. client, err = bigtable.NewClient(context.Background(), config.Project, config.Instance)
  66. if err != nil {
  67. log.Fatalf("Making bigtable.Client: %v", err)
  68. }
  69. defer client.Close()
  70. log.Printf("Starting scan test... (run for %v)", *runFor)
  71. tbl := client.Open(table)
  72. sem := make(chan int, *numScans) // limit the number of requests happening at once
  73. var scans stats
  74. stopTime := time.Now().Add(*runFor)
  75. var wg sync.WaitGroup
  76. for time.Now().Before(stopTime) {
  77. sem <- 1
  78. wg.Add(1)
  79. go func() {
  80. defer wg.Done()
  81. defer func() { <-sem }()
  82. ok := true
  83. opStart := time.Now()
  84. defer func() {
  85. scans.Record(ok, time.Since(opStart))
  86. }()
  87. // Start at a random row key
  88. key := fmt.Sprintf("user%d", rand.Int63())
  89. limit := bigtable.LimitRows(int64(*rowLimit))
  90. noop := func(bigtable.Row) bool { return true }
  91. if err := tbl.ReadRows(context.Background(), bigtable.NewRange(key, ""), noop, limit); err != nil {
  92. log.Printf("Error during scan: %v", err)
  93. ok = false
  94. }
  95. }()
  96. }
  97. wg.Wait()
  98. agg := stat.NewAggregate("scans", scans.ds, scans.tries-scans.ok)
  99. log.Printf("Scans (%d ok / %d tries):\nscan times:\n%v\nthroughput (rows/second):\n%v",
  100. scans.ok, scans.tries, agg, throughputString(agg))
  101. }
  102. func throughputString(agg *stat.Aggregate) string {
  103. var buf bytes.Buffer
  104. tw := tabwriter.NewWriter(&buf, 0, 0, 1, ' ', 0) // one-space padding
  105. rowLimitF := float64(*rowLimit)
  106. fmt.Fprintf(
  107. tw,
  108. "min:\t%.2f\nmedian:\t%.2f\nmax:\t%.2f\n",
  109. rowLimitF/agg.Max.Seconds(),
  110. rowLimitF/agg.Median.Seconds(),
  111. rowLimitF/agg.Min.Seconds())
  112. tw.Flush()
  113. return buf.String()
  114. }
  115. var allStats int64 // atomic
  116. type stats struct {
  117. mu sync.Mutex
  118. tries, ok int
  119. ds []time.Duration
  120. }
  121. func (s *stats) Record(ok bool, d time.Duration) {
  122. s.mu.Lock()
  123. s.tries++
  124. if ok {
  125. s.ok++
  126. }
  127. s.ds = append(s.ds, d)
  128. s.mu.Unlock()
  129. if n := atomic.AddInt64(&allStats, 1); n%1000 == 0 {
  130. log.Printf("Progress: done %d ops", n)
  131. }
  132. }