| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package telemetry |
| 5 |
|
| 6 |
import ( |
| 7 |
"bytes" |
| 8 |
"encoding/json" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
var ( |
| 14 |
traceID = [16]byte{0x1} |
| 15 |
|
| 16 |
spanID1 = [8]byte{0x1} |
| 17 |
spanID2 = [8]byte{0x2} |
| 18 |
|
| 19 |
now = time.Now() |
| 20 |
nowPlus1 = now.Add(1 * time.Second) |
| 21 |
|
| 22 |
spanA = &Span{ |
| 23 |
TraceID: traceID, |
| 24 |
SpanID: spanID2, |
| 25 |
ParentSpanID: spanID1, |
| 26 |
Flags: 1, |
| 27 |
Name: "span-a", |
| 28 |
StartTime: now, |
| 29 |
EndTime: nowPlus1, |
| 30 |
Status: &Status{ |
| 31 |
Message: "test status", |
| 32 |
Code: StatusCodeOK, |
| 33 |
}, |
| 34 |
} |
| 35 |
|
| 36 |
spanB = &Span{} |
| 37 |
|
| 38 |
scopeSpans = &ScopeSpans{ |
| 39 |
Scope: &Scope{ |
| 40 |
Name: "TestTracer", |
| 41 |
Version: "v0.1.0", |
| 42 |
}, |
| 43 |
SchemaURL: "http://go.opentelemetry.io/test", |
| 44 |
Spans: []*Span{spanA, spanB}, |
| 45 |
} |
| 46 |
) |
| 47 |
|
| 48 |
func BenchmarkJSONMarshalUnmarshal(b *testing.B) { |
| 49 |
var out ScopeSpans |
| 50 |
|
| 51 |
b.ReportAllocs() |
| 52 |
b.ResetTimer() |
| 53 |
for n := 0; n < b.N; n++ { |
| 54 |
var inBuf bytes.Buffer |
| 55 |
enc := json.NewEncoder(&inBuf) |
| 56 |
err := enc.Encode(scopeSpans) |
| 57 |
if err != nil { |
| 58 |
b.Fatal(err) |
| 59 |
} |
| 60 |
|
| 61 |
payload := inBuf.Bytes() |
| 62 |
|
| 63 |
dec := json.NewDecoder(bytes.NewReader(payload)) |
| 64 |
err = dec.Decode(&out) |
| 65 |
if err != nil { |
| 66 |
b.Fatal(err) |
| 67 |
} |
| 68 |
} |
| 69 |
_ = out |
| 70 |
} |