1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-30 21:20:04 +02:00
opentelemetry-go/sdk/trace/evictedqueue_test.go
ttoad 30e82e01b6
trace: Use non-generic to replace newEvictedQueue in trace.start to reduce memory usage. (#5497)
benchstat:
```
goos: darwin
goarch: arm64
pkg: go.opentelemetry.io/otel/sdk/trace
              │     old     │                 new                 │
              │   sec/op    │   sec/op     vs base                │
TraceStart-10   950.6n ± 1%   641.0n ± 0%  -32.57% (p=0.000 n=10)

              │     old     │                new                 │
              │    B/op     │    B/op     vs base                │
TraceStart-10   1040.0 ± 0%   704.0 ± 0%  -32.31% (p=0.000 n=10)

              │    old     │                new                 │
              │ allocs/op  │ allocs/op   vs base                │
TraceStart-10   20.00 ± 0%   14.00 ± 0%  -30.00% (p=0.000 n=10)
```

---------

Co-authored-by: Damien Mathieu <damien.mathieu@elastic.co>
Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
2024-06-17 07:39:03 -07:00

68 lines
1.8 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package trace
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func init() {
}
func TestAdd(t *testing.T) {
q := newEvictedQueueLink(3)
q.add(Link{})
q.add(Link{})
if wantLen, gotLen := 2, len(q.queue); wantLen != gotLen {
t.Errorf("got queue length %d want %d", gotLen, wantLen)
}
}
func TestCopy(t *testing.T) {
q := newEvictedQueueEvent(3)
q.add(Event{Name: "value1"})
cp := q.copy()
q.add(Event{Name: "value2"})
assert.Equal(t, []Event{{Name: "value1"}}, cp, "queue update modified copy")
cp[0] = Event{Name: "value0"}
assert.Equal(t, Event{Name: "value1"}, q.queue[0], "copy update modified queue")
}
func TestDropCount(t *testing.T) {
q := newEvictedQueueEvent(3)
var called bool
q.logDropped = func() { called = true }
q.add(Event{Name: "value1"})
assert.False(t, called, `"value1" logged as dropped`)
q.add(Event{Name: "value2"})
assert.False(t, called, `"value2" logged as dropped`)
q.add(Event{Name: "value3"})
assert.False(t, called, `"value3" logged as dropped`)
q.add(Event{Name: "value1"})
assert.True(t, called, `"value2" not logged as dropped`)
q.add(Event{Name: "value4"})
if wantLen, gotLen := 3, len(q.queue); wantLen != gotLen {
t.Errorf("got queue length %d want %d", gotLen, wantLen)
}
if wantDropCount, gotDropCount := 2, q.droppedCount; wantDropCount != gotDropCount {
t.Errorf("got drop count %d want %d", gotDropCount, wantDropCount)
}
wantArr := []Event{{Name: "value3"}, {Name: "value1"}, {Name: "value4"}}
gotArr := q.copy()
if wantLen, gotLen := len(wantArr), len(gotArr); gotLen != wantLen {
t.Errorf("got array len %d want %d", gotLen, wantLen)
}
if !reflect.DeepEqual(gotArr, wantArr) {
t.Errorf("got array = %#v; want %#v", gotArr, wantArr)
}
}