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.
 
 
 

93 regels
2.2 KiB

  1. // Copyright 2018, OpenCensus 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. //
  15. package view
  16. import (
  17. "context"
  18. "fmt"
  19. "testing"
  20. "time"
  21. "go.opencensus.io/stats"
  22. "go.opencensus.io/tag"
  23. )
  24. var (
  25. m = stats.Float64("m", "", "")
  26. k1, _ = tag.NewKey("k1")
  27. k2, _ = tag.NewKey("k2")
  28. k3, _ = tag.NewKey("k3")
  29. k4, _ = tag.NewKey("k4")
  30. k5, _ = tag.NewKey("k5")
  31. k6, _ = tag.NewKey("k6")
  32. k7, _ = tag.NewKey("k7")
  33. k8, _ = tag.NewKey("k8")
  34. view = &View{
  35. Measure: m,
  36. Aggregation: Distribution(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
  37. TagKeys: []tag.Key{k1, k2},
  38. }
  39. )
  40. // BenchmarkRecordReqCommand benchmarks calling the internal recording machinery
  41. // directly.
  42. func BenchmarkRecordReqCommand(b *testing.B) {
  43. w := newWorker()
  44. register := &registerViewReq{views: []*View{view}, err: make(chan error, 1)}
  45. register.handleCommand(w)
  46. if err := <-register.err; err != nil {
  47. b.Fatal(err)
  48. }
  49. const tagCount = 10
  50. ctxs := make([]context.Context, 0, tagCount)
  51. for i := 0; i < tagCount; i++ {
  52. ctx, _ := tag.New(context.Background(),
  53. tag.Upsert(k1, fmt.Sprintf("v%d", i)),
  54. tag.Upsert(k2, fmt.Sprintf("v%d", i)),
  55. tag.Upsert(k3, fmt.Sprintf("v%d", i)),
  56. tag.Upsert(k4, fmt.Sprintf("v%d", i)),
  57. tag.Upsert(k5, fmt.Sprintf("v%d", i)),
  58. tag.Upsert(k6, fmt.Sprintf("v%d", i)),
  59. tag.Upsert(k7, fmt.Sprintf("v%d", i)),
  60. tag.Upsert(k8, fmt.Sprintf("v%d", i)),
  61. )
  62. ctxs = append(ctxs, ctx)
  63. }
  64. b.ReportAllocs()
  65. b.ResetTimer()
  66. for i := 0; i < b.N; i++ {
  67. record := &recordReq{
  68. ms: []stats.Measurement{
  69. m.M(1),
  70. m.M(1),
  71. m.M(1),
  72. m.M(1),
  73. m.M(1),
  74. m.M(1),
  75. m.M(1),
  76. m.M(1),
  77. },
  78. tm: tag.FromContext(ctxs[i%len(ctxs)]),
  79. t: time.Now(),
  80. }
  81. record.handleCommand(w)
  82. }
  83. }