benchmark_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. package trace
  15. import (
  16. "context"
  17. "testing"
  18. )
  19. func BenchmarkStartEndSpan(b *testing.B) {
  20. traceBenchmark(b, func(b *testing.B) {
  21. ctx := context.Background()
  22. b.ResetTimer()
  23. for i := 0; i < b.N; i++ {
  24. _, span := StartSpan(ctx, "/foo")
  25. span.End()
  26. }
  27. })
  28. }
  29. func BenchmarkSpanWithAnnotations_4(b *testing.B) {
  30. traceBenchmark(b, func(b *testing.B) {
  31. ctx := context.Background()
  32. b.ResetTimer()
  33. for i := 0; i < b.N; i++ {
  34. _, span := StartSpan(ctx, "/foo")
  35. span.AddAttributes(
  36. BoolAttribute("key1", false),
  37. StringAttribute("key2", "hello"),
  38. Int64Attribute("key3", 123),
  39. Float64Attribute("key4", 123.456),
  40. )
  41. span.End()
  42. }
  43. })
  44. }
  45. func BenchmarkSpanWithAnnotations_8(b *testing.B) {
  46. traceBenchmark(b, func(b *testing.B) {
  47. ctx := context.Background()
  48. b.ResetTimer()
  49. for i := 0; i < b.N; i++ {
  50. _, span := StartSpan(ctx, "/foo")
  51. span.AddAttributes(
  52. BoolAttribute("key1", false),
  53. BoolAttribute("key2", true),
  54. StringAttribute("key3", "hello"),
  55. StringAttribute("key4", "hello"),
  56. Int64Attribute("key5", 123),
  57. Int64Attribute("key6", 456),
  58. Float64Attribute("key7", 123.456),
  59. Float64Attribute("key8", 456.789),
  60. )
  61. span.End()
  62. }
  63. })
  64. }
  65. func BenchmarkTraceID_DotString(b *testing.B) {
  66. traceBenchmark(b, func(b *testing.B) {
  67. t := TraceID{0x0D, 0x0E, 0x0A, 0x0D, 0x0B, 0x0E, 0x0E, 0x0F, 0x0F, 0x0E, 0x0E, 0x0B, 0x0D, 0x0A, 0x0E, 0x0D}
  68. want := "0d0e0a0d0b0e0e0f0f0e0e0b0d0a0e0d"
  69. for i := 0; i < b.N; i++ {
  70. if got := t.String(); got != want {
  71. b.Fatalf("got = %q want = %q", got, want)
  72. }
  73. }
  74. })
  75. }
  76. func BenchmarkSpanID_DotString(b *testing.B) {
  77. traceBenchmark(b, func(b *testing.B) {
  78. s := SpanID{0x0D, 0x0E, 0x0A, 0x0D, 0x0B, 0x0E, 0x0E, 0x0F}
  79. want := "0d0e0a0d0b0e0e0f"
  80. for i := 0; i < b.N; i++ {
  81. if got := s.String(); got != want {
  82. b.Fatalf("got = %q want = %q", got, want)
  83. }
  84. }
  85. })
  86. }
  87. func traceBenchmark(b *testing.B, fn func(*testing.B)) {
  88. b.Run("AlwaysSample", func(b *testing.B) {
  89. b.ReportAllocs()
  90. ApplyConfig(Config{DefaultSampler: AlwaysSample()})
  91. fn(b)
  92. })
  93. b.Run("NeverSample", func(b *testing.B) {
  94. b.ReportAllocs()
  95. ApplyConfig(Config{DefaultSampler: NeverSample()})
  96. fn(b)
  97. })
  98. }