|
| 1 | +// Copyright The OpenTelemetry 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 metric // import "go.opentelemetry.io/otel/sdk/metric" |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "go.opentelemetry.io/otel/attribute" |
| 22 | + "go.opentelemetry.io/otel/metric/instrument/syncint64" |
| 23 | + "go.opentelemetry.io/otel/sdk/metric/view" |
| 24 | +) |
| 25 | + |
| 26 | +func benchCounter(b *testing.B, views ...view.View) (context.Context, Reader, syncint64.Counter) { |
| 27 | + ctx := context.Background() |
| 28 | + rdr := NewManualReader() |
| 29 | + provider := NewMeterProvider(WithReader(rdr), WithView(views...)) |
| 30 | + cntr, _ := provider.Meter("test").SyncInt64().Counter("hello") |
| 31 | + b.ResetTimer() |
| 32 | + b.ReportAllocs() |
| 33 | + return ctx, rdr, cntr |
| 34 | +} |
| 35 | + |
| 36 | +func BenchmarkCounterAddNoAttrs(b *testing.B) { |
| 37 | + ctx, _, cntr := benchCounter(b) |
| 38 | + |
| 39 | + for i := 0; i < b.N; i++ { |
| 40 | + cntr.Add(ctx, 1) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func BenchmarkCounterAddOneAttr(b *testing.B) { |
| 45 | + ctx, _, cntr := benchCounter(b) |
| 46 | + |
| 47 | + for i := 0; i < b.N; i++ { |
| 48 | + cntr.Add(ctx, 1, attribute.String("K", "V")) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func BenchmarkCounterAddOneInvalidAttr(b *testing.B) { |
| 53 | + ctx, _, cntr := benchCounter(b) |
| 54 | + |
| 55 | + for i := 0; i < b.N; i++ { |
| 56 | + cntr.Add(ctx, 1, attribute.String("", "V"), attribute.String("K", "V")) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func BenchmarkCounterAddSingleUseAttrs(b *testing.B) { |
| 61 | + ctx, _, cntr := benchCounter(b) |
| 62 | + |
| 63 | + for i := 0; i < b.N; i++ { |
| 64 | + cntr.Add(ctx, 1, attribute.Int("K", i)) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func BenchmarkCounterAddSingleUseInvalidAttrs(b *testing.B) { |
| 69 | + ctx, _, cntr := benchCounter(b) |
| 70 | + |
| 71 | + for i := 0; i < b.N; i++ { |
| 72 | + cntr.Add(ctx, 1, attribute.Int("", i), attribute.Int("K", i)) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func BenchmarkCounterAddSingleUseFilteredAttrs(b *testing.B) { |
| 77 | + vw, _ := view.New(view.WithFilterAttributes(attribute.Key("K"))) |
| 78 | + |
| 79 | + ctx, _, cntr := benchCounter(b, vw) |
| 80 | + |
| 81 | + for i := 0; i < b.N; i++ { |
| 82 | + cntr.Add(ctx, 1, attribute.Int("L", i), attribute.Int("K", i)) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func BenchmarkCounterCollectOneAttr(b *testing.B) { |
| 87 | + ctx, rdr, cntr := benchCounter(b) |
| 88 | + |
| 89 | + for i := 0; i < b.N; i++ { |
| 90 | + cntr.Add(ctx, 1, attribute.Int("K", 1)) |
| 91 | + |
| 92 | + _, _ = rdr.Collect(ctx) |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func BenchmarkCounterCollectTenAttrs(b *testing.B) { |
| 97 | + ctx, rdr, cntr := benchCounter(b) |
| 98 | + |
| 99 | + for i := 0; i < b.N; i++ { |
| 100 | + for j := 0; j < 10; j++ { |
| 101 | + cntr.Add(ctx, 1, attribute.Int("K", j)) |
| 102 | + } |
| 103 | + _, _ = rdr.Collect(ctx) |
| 104 | + } |
| 105 | +} |
0 commit comments