Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

102 linhas
2.5 KiB

  1. // Copyright 2017 Google LLC
  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. // Busybench is a tool that runs a benchmark with the profiler enabled.
  15. package main
  16. import (
  17. "bytes"
  18. "compress/gzip"
  19. "flag"
  20. "log"
  21. "math/rand"
  22. "sync"
  23. "time"
  24. "cloud.google.com/go/profiler"
  25. )
  26. var (
  27. service = flag.String("service", "", "service name")
  28. mutexProfiling = flag.Bool("mutex_profiling", false, "enable mutex profiling")
  29. duration = flag.Int("duration", 150, "duration of the benchmark in seconds")
  30. apiAddr = flag.String("api_address", "", "API address of the profiler (e.g. 'cloudprofiler.googleapis.com:443')")
  31. projectID = flag.String("project_id", "", "cloud project ID")
  32. )
  33. // busywork continuously generates 1MiB of random data and compresses it
  34. // throwing away the result.
  35. func busywork(mu *sync.Mutex) {
  36. ticker := time.NewTicker(time.Duration(*duration) * time.Second)
  37. defer ticker.Stop()
  38. for {
  39. select {
  40. case <-ticker.C:
  41. return
  42. default:
  43. mu.Lock()
  44. busyworkOnce()
  45. mu.Unlock()
  46. }
  47. }
  48. }
  49. func busyworkOnce() {
  50. data := make([]byte, 1024*1024)
  51. rand.Read(data)
  52. var b bytes.Buffer
  53. gz := gzip.NewWriter(&b)
  54. if _, err := gz.Write(data); err != nil {
  55. log.Printf("Failed to write to gzip stream: %v", err)
  56. return
  57. }
  58. if err := gz.Flush(); err != nil {
  59. log.Printf("Failed to flush to gzip stream: %v", err)
  60. return
  61. }
  62. if err := gz.Close(); err != nil {
  63. log.Printf("Failed to close gzip stream: %v", err)
  64. }
  65. // Throw away the result.
  66. }
  67. func main() {
  68. flag.Parse()
  69. defer log.Printf("busybench finished profiling.")
  70. if *service == "" {
  71. log.Print("Service name must be configured using --service flag.")
  72. return
  73. }
  74. if err := profiler.Start(profiler.Config{Service: *service,
  75. MutexProfiling: *mutexProfiling,
  76. DebugLogging: true,
  77. APIAddr: *apiAddr,
  78. ProjectID: *projectID}); err != nil {
  79. log.Printf("Failed to start the profiler: %v", err)
  80. return
  81. }
  82. mu := new(sync.Mutex)
  83. var wg sync.WaitGroup
  84. wg.Add(5)
  85. for i := 0; i < 5; i++ {
  86. go func() {
  87. defer wg.Done()
  88. busywork(mu)
  89. }()
  90. }
  91. wg.Wait()
  92. }