print_benchmark.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2014 The Cockroach 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
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. //
  16. // Author: Tyler Neely (t@jujit.su)
  17. package loghisto
  18. import (
  19. "fmt"
  20. "os"
  21. "runtime"
  22. "text/tabwriter"
  23. "time"
  24. )
  25. // PrintBenchmark will run the provided function at the specified
  26. // concurrency, time the operation, and once per second write the
  27. // following information to standard out:
  28. //
  29. // 2014-08-09 17:44:57 -0400 EDT
  30. // raft_AppendLogEntries_count: 16488
  31. // raft_AppendLogEntries_max: 3.982478339757623e+07
  32. // raft_AppendLogEntries_99.99: 3.864778314316012e+07
  33. // raft_AppendLogEntries_99.9: 3.4366224772310276e+06
  34. // raft_AppendLogEntries_99: 2.0228126576114902e+06
  35. // raft_AppendLogEntries_50: 469769.7083161708
  36. // raft_AppendLogEntries_min: 129313.15075081984
  37. // raft_AppendLogEntries_sum: 9.975892639594093e+09
  38. // raft_AppendLogEntries_avg: 605039.5827022133
  39. // raft_AppendLogEntries_agg_avg: 618937
  40. // raft_AppendLogEntries_agg_count: 121095
  41. // raft_AppendLogEntries_agg_sum: 7.4950269894e+10
  42. // sys.Alloc: 997328
  43. // sys.NumGC: 1115
  44. // sys.PauseTotalNs: 2.94946542e+08
  45. // sys.NumGoroutine: 26
  46. func PrintBenchmark(name string, concurrency uint, op func()) {
  47. runtime.GOMAXPROCS(runtime.NumCPU())
  48. var ms = NewMetricSystem(time.Second, true)
  49. mc := make(chan *ProcessedMetricSet, 1)
  50. ms.SubscribeToProcessedMetrics(mc)
  51. ms.Start()
  52. defer ms.Stop()
  53. go receiver(name, mc)
  54. for i := uint(0); i < concurrency; i++ {
  55. go func() {
  56. for {
  57. timer := ms.StartTimer(name)
  58. op()
  59. timer.Stop()
  60. }
  61. }()
  62. }
  63. <-make(chan struct{})
  64. }
  65. func receiver(name string, mc chan *ProcessedMetricSet) {
  66. interesting := []string{
  67. fmt.Sprintf("%s_count", name),
  68. fmt.Sprintf("%s_max", name),
  69. fmt.Sprintf("%s_99.99", name),
  70. fmt.Sprintf("%s_99.9", name),
  71. fmt.Sprintf("%s_99", name),
  72. fmt.Sprintf("%s_95", name),
  73. fmt.Sprintf("%s_90", name),
  74. fmt.Sprintf("%s_75", name),
  75. fmt.Sprintf("%s_50", name),
  76. fmt.Sprintf("%s_min", name),
  77. fmt.Sprintf("%s_sum", name),
  78. fmt.Sprintf("%s_avg", name),
  79. fmt.Sprintf("%s_agg_avg", name),
  80. fmt.Sprintf("%s_agg_count", name),
  81. fmt.Sprintf("%s_agg_sum", name),
  82. "sys.Alloc",
  83. "sys.NumGC",
  84. "sys.PauseTotalNs",
  85. "sys.NumGoroutine",
  86. }
  87. w := new(tabwriter.Writer)
  88. w.Init(os.Stdout, 0, 8, 0, '\t', 0)
  89. for m := range mc {
  90. fmt.Fprintln(w, m.Time)
  91. for _, e := range interesting {
  92. fmt.Fprintln(w, fmt.Sprintf("%s:\t", e), m.Metrics[e])
  93. }
  94. fmt.Fprintln(w)
  95. w.Flush()
  96. }
  97. }