2020-03-25 23:47:17 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
2019-10-29 22:27:22 +02:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// This test is too large for the race detector. This SDK uses no locks
|
|
|
|
// that the race detector would help with, anyway.
|
|
|
|
// +build !race
|
|
|
|
|
2020-03-21 02:19:48 +02:00
|
|
|
package metric
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
"math/rand"
|
|
|
|
"runtime"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
"go.opentelemetry.io/otel"
|
2020-08-18 05:25:03 +02:00
|
|
|
"go.opentelemetry.io/otel/label"
|
2020-11-11 17:24:12 +02:00
|
|
|
"go.opentelemetry.io/otel/metric/number"
|
2019-11-05 23:08:55 +02:00
|
|
|
export "go.opentelemetry.io/otel/sdk/export/metric"
|
2020-06-10 07:53:30 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/export/metric/aggregation"
|
2020-08-10 22:35:15 +02:00
|
|
|
"go.opentelemetry.io/otel/sdk/metric/processor/processortest"
|
2019-10-29 22:27:22 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
concurrencyPerCPU = 100
|
|
|
|
reclaimPeriod = time.Millisecond * 100
|
2020-03-27 23:06:48 +02:00
|
|
|
testRun = 5 * time.Second
|
2019-10-29 22:27:22 +02:00
|
|
|
epsilon = 1e-10
|
|
|
|
)
|
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
var Must = otel.Must
|
2020-03-21 02:19:48 +02:00
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
type (
|
|
|
|
testFixture struct {
|
2020-01-06 20:08:40 +02:00
|
|
|
// stop has to be aligned for 64-bit atomic operations.
|
2019-10-29 22:27:22 +02:00
|
|
|
stop int64
|
|
|
|
expected sync.Map
|
|
|
|
received sync.Map // Note: doesn't require synchronization
|
|
|
|
wg sync.WaitGroup
|
|
|
|
impl testImpl
|
|
|
|
T *testing.T
|
|
|
|
|
2020-06-23 19:51:15 +02:00
|
|
|
export.AggregatorSelector
|
2020-06-13 09:55:01 +02:00
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
lock sync.Mutex
|
|
|
|
lused map[string]bool
|
|
|
|
|
|
|
|
dupCheck map[testKey]int
|
|
|
|
totalDups int64
|
|
|
|
}
|
|
|
|
|
|
|
|
testKey struct {
|
|
|
|
labels string
|
2020-10-17 18:48:21 +02:00
|
|
|
descriptor *otel.Descriptor
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
testImpl struct {
|
2020-10-17 18:48:21 +02:00
|
|
|
newInstrument func(meter otel.Meter, name string) SyncImpler
|
2020-11-11 17:24:12 +02:00
|
|
|
getUpdateValue func() number.Number
|
|
|
|
operate func(interface{}, context.Context, number.Number, []label.KeyValue)
|
2019-10-29 22:27:22 +02:00
|
|
|
newStore func() interface{}
|
|
|
|
|
|
|
|
// storeCollect and storeExpect are the same for
|
2020-03-11 01:00:37 +02:00
|
|
|
// counters, different for lastValues, to ensure we are
|
2019-10-29 22:27:22 +02:00
|
|
|
// testing the timestamps correctly.
|
2020-11-11 17:24:12 +02:00
|
|
|
storeCollect func(store interface{}, value number.Number, ts time.Time)
|
|
|
|
storeExpect func(store interface{}, value number.Number)
|
|
|
|
readStore func(store interface{}) number.Number
|
|
|
|
equalValues func(a, b number.Number) bool
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-19 21:02:46 +02:00
|
|
|
SyncImpler interface {
|
2020-10-17 18:48:21 +02:00
|
|
|
SyncImpl() otel.SyncImpl
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
// lastValueState supports merging lastValue values, for the case
|
2019-10-29 22:27:22 +02:00
|
|
|
// where a race condition causes duplicate records. We always
|
|
|
|
// take the later timestamp.
|
2020-03-11 01:00:37 +02:00
|
|
|
lastValueState struct {
|
2020-01-06 20:08:40 +02:00
|
|
|
// raw has to be aligned for 64-bit atomic operations.
|
2020-11-11 17:24:12 +02:00
|
|
|
raw number.Number
|
2019-10-29 22:27:22 +02:00
|
|
|
ts time.Time
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func concurrency() int {
|
|
|
|
return concurrencyPerCPU * runtime.NumCPU()
|
|
|
|
}
|
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
func canonicalizeLabels(ls []label.KeyValue) string {
|
2019-10-29 22:27:22 +02:00
|
|
|
copy := append(ls[0:0:0], ls...)
|
|
|
|
sort.SliceStable(copy, func(i, j int) bool {
|
|
|
|
return copy[i].Key < copy[j].Key
|
|
|
|
})
|
|
|
|
var b strings.Builder
|
|
|
|
for _, kv := range copy {
|
|
|
|
b.WriteString(string(kv.Key))
|
|
|
|
b.WriteString("=")
|
|
|
|
b.WriteString(kv.Value.Emit())
|
|
|
|
b.WriteString("$")
|
|
|
|
}
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPeriod() time.Duration {
|
|
|
|
dur := math.Max(
|
|
|
|
float64(reclaimPeriod)/10,
|
|
|
|
float64(reclaimPeriod)*(1+0.1*rand.NormFloat64()),
|
|
|
|
)
|
|
|
|
return time.Duration(dur)
|
|
|
|
}
|
|
|
|
|
2020-08-18 05:25:03 +02:00
|
|
|
func (f *testFixture) someLabels() []label.KeyValue {
|
2019-10-29 22:27:22 +02:00
|
|
|
n := 1 + rand.Intn(3)
|
2020-08-18 05:25:03 +02:00
|
|
|
l := make([]label.KeyValue, n)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
oused := map[string]bool{}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
var k string
|
|
|
|
for {
|
|
|
|
k = fmt.Sprint("k", rand.Intn(1000000000))
|
|
|
|
if !oused[k] {
|
|
|
|
oused[k] = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-08-18 05:25:03 +02:00
|
|
|
l[i] = label.String(k, fmt.Sprint("v", rand.Intn(1000000000)))
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
lc := canonicalizeLabels(l)
|
|
|
|
f.lock.Lock()
|
|
|
|
avail := !f.lused[lc]
|
|
|
|
if avail {
|
|
|
|
f.lused[lc] = true
|
|
|
|
f.lock.Unlock()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
f.lock.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-17 18:48:21 +02:00
|
|
|
func (f *testFixture) startWorker(impl *Accumulator, meter otel.Meter, wg *sync.WaitGroup, i int) {
|
2019-10-29 22:27:22 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
name := fmt.Sprint("test_", i)
|
2020-03-19 21:02:46 +02:00
|
|
|
instrument := f.impl.newInstrument(meter, name)
|
2020-10-17 18:48:21 +02:00
|
|
|
var descriptor *otel.Descriptor
|
2020-03-21 02:19:48 +02:00
|
|
|
if ii, ok := instrument.SyncImpl().(*syncInstrument); ok {
|
|
|
|
descriptor = &ii.descriptor
|
|
|
|
}
|
2019-10-29 22:27:22 +02:00
|
|
|
kvs := f.someLabels()
|
|
|
|
clabs := canonicalizeLabels(kvs)
|
|
|
|
dur := getPeriod()
|
|
|
|
key := testKey{
|
|
|
|
labels: clabs,
|
|
|
|
descriptor: descriptor,
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
sleep := time.Duration(rand.ExpFloat64() * float64(dur))
|
|
|
|
time.Sleep(sleep)
|
|
|
|
value := f.impl.getUpdateValue()
|
2020-03-27 23:06:48 +02:00
|
|
|
f.impl.operate(instrument, ctx, value, kvs)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
actual, _ := f.expected.LoadOrStore(key, f.impl.newStore())
|
|
|
|
|
|
|
|
f.impl.storeExpect(actual, value)
|
|
|
|
|
|
|
|
if atomic.LoadInt64(&f.stop) != 0 {
|
|
|
|
wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *testFixture) assertTest(numCollect int) {
|
2020-03-27 23:06:48 +02:00
|
|
|
var allErrs []func()
|
2019-10-29 22:27:22 +02:00
|
|
|
csize := 0
|
|
|
|
f.received.Range(func(key, gstore interface{}) bool {
|
|
|
|
csize++
|
|
|
|
gvalue := f.impl.readStore(gstore)
|
|
|
|
|
|
|
|
estore, loaded := f.expected.Load(key)
|
|
|
|
if !loaded {
|
2020-03-27 23:06:48 +02:00
|
|
|
allErrs = append(allErrs, func() {
|
|
|
|
f.T.Error("Could not locate expected key: ", key)
|
|
|
|
})
|
|
|
|
return true
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
evalue := f.impl.readStore(estore)
|
|
|
|
|
|
|
|
if !f.impl.equalValues(evalue, gvalue) {
|
2020-03-27 23:06:48 +02:00
|
|
|
allErrs = append(allErrs, func() {
|
|
|
|
f.T.Error("Expected value mismatch: ",
|
|
|
|
evalue, "!=", gvalue, " for ", key)
|
|
|
|
})
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
rsize := 0
|
|
|
|
f.expected.Range(func(key, value interface{}) bool {
|
|
|
|
rsize++
|
|
|
|
if _, loaded := f.received.Load(key); !loaded {
|
2020-03-27 23:06:48 +02:00
|
|
|
allErrs = append(allErrs, func() {
|
|
|
|
f.T.Error("Did not receive expected key: ", key)
|
|
|
|
})
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if rsize != csize {
|
|
|
|
f.T.Error("Did not receive the correct set of metrics: Received != Expected", rsize, csize)
|
|
|
|
}
|
|
|
|
|
2020-03-27 23:06:48 +02:00
|
|
|
for _, anErr := range allErrs {
|
|
|
|
anErr()
|
|
|
|
}
|
|
|
|
|
2019-10-29 22:27:22 +02:00
|
|
|
// Note: It's useful to know the test triggers this condition,
|
|
|
|
// but we can't assert it. Infrequently no duplicates are
|
|
|
|
// found, and we can't really force a race to happen.
|
|
|
|
//
|
|
|
|
// fmt.Printf("Test duplicate records seen: %.1f%%\n",
|
|
|
|
// float64(100*f.totalDups/int64(numCollect*concurrency())))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *testFixture) preCollect() {
|
2019-11-15 23:01:20 +02:00
|
|
|
// Collect calls Process in a single-threaded context. No need
|
2019-10-29 22:27:22 +02:00
|
|
|
// to lock this struct.
|
|
|
|
f.dupCheck = map[testKey]int{}
|
|
|
|
}
|
|
|
|
|
2019-11-15 23:01:20 +02:00
|
|
|
func (*testFixture) CheckpointSet() export.CheckpointSet {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-18 19:16:33 +02:00
|
|
|
func (f *testFixture) Process(accumulation export.Accumulation) error {
|
|
|
|
labels := accumulation.Labels().ToSlice()
|
2019-10-29 22:27:22 +02:00
|
|
|
key := testKey{
|
Replace `Ordered` with an iterator in `export.Labels`. (#567)
* Do not expose a slice of labels in export.Record
This is really an inconvenient implementation detail leak - we may
want to store labels in a different way. Replace it with an iterator -
it does not force us to use slice of key values as a storage in the
long run.
* Add Len to LabelIterator
It may come in handy in several situations, where we don't have access
to export.Labels object, but only to the label iterator.
* Use reflect value label iterator for the fixed labels
* add reset operation to iterator
Makes my life easier when writing a benchmark. Might also be an
alternative to cloning the iterator.
* Add benchmarks for iterators
* Add import comment
* Add clone operation to label iterator
* Move iterator tests to a separate package
* Add tests for cloning iterators
* Pass label iterator to export labels
* Use non-addressable array reflect values
By not using the value created by `reflect.New()`, but rather by
`reflect.ValueOf()`, we get a non-addressable array in the value,
which does not infer an allocation cost when getting an element from
the array.
* Drop zero iterator
This can be substituted by a reflect value iterator that goes over a
value with a zero-sized array.
* Add a simple iterator that implements label iterator
In the long run this will completely replace the LabelIterator
interface.
* Replace reflect value iterator with simple iterator
* Pass label storage to new export labels, not label iterator
* Drop label iterator interface, rename storage iterator to label iterator
* Drop clone operation from iterator
It's a leftover from interface times and now it's pointless - the
iterator is a simple struct, so cloning it is a simple copy.
* Drop Reset from label iterator
The sole existence of Reset was actually for benchmarking convenience.
Now we can just copy the iterator cheaply, so a need for Reset is no
more.
* Drop noop iterator tests
* Move back iterator tests to export package
* Eagerly get the reflect value of ordered labels
So we won't get into problems when several goroutines want to iterate
the same labels at the same time. Not sure if this would be a big
deal, since every goroutine would compute the same reflect.Value, but
concurrent write to the same memory is bad anyway. And it doesn't cost
us any extra allocations anyway.
* Replace NewSliceLabelIterator() with a method of LabelSlice
* Add some documentation
* Documentation fixes
2020-03-20 00:01:34 +02:00
|
|
|
labels: canonicalizeLabels(labels),
|
2020-06-18 19:16:33 +02:00
|
|
|
descriptor: accumulation.Descriptor(),
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
if f.dupCheck[key] == 0 {
|
|
|
|
f.dupCheck[key]++
|
|
|
|
} else {
|
|
|
|
f.totalDups++
|
|
|
|
}
|
|
|
|
|
|
|
|
actual, _ := f.received.LoadOrStore(key, f.impl.newStore())
|
|
|
|
|
2020-06-18 19:16:33 +02:00
|
|
|
agg := accumulation.Aggregator()
|
2020-10-13 16:55:31 +02:00
|
|
|
switch accumulation.Descriptor().InstrumentKind() {
|
2020-10-17 18:48:21 +02:00
|
|
|
case otel.CounterInstrumentKind:
|
2020-06-10 07:53:30 +02:00
|
|
|
sum, err := agg.(aggregation.Sum).Sum()
|
2019-11-15 23:01:20 +02:00
|
|
|
if err != nil {
|
|
|
|
f.T.Fatal("Sum error: ", err)
|
|
|
|
}
|
|
|
|
f.impl.storeCollect(actual, sum, time.Time{})
|
2020-10-17 18:48:21 +02:00
|
|
|
case otel.ValueRecorderInstrumentKind:
|
2020-06-10 07:53:30 +02:00
|
|
|
lv, ts, err := agg.(aggregation.LastValue).LastValue()
|
|
|
|
if err != nil && err != aggregation.ErrNoData {
|
2019-11-15 23:01:20 +02:00
|
|
|
f.T.Fatal("Last value error: ", err)
|
|
|
|
}
|
|
|
|
f.impl.storeCollect(actual, lv, ts)
|
2019-10-29 22:27:22 +02:00
|
|
|
default:
|
|
|
|
panic("Not used in this test")
|
|
|
|
}
|
2019-11-15 23:01:20 +02:00
|
|
|
return nil
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func stressTest(t *testing.T, impl testImpl) {
|
|
|
|
ctx := context.Background()
|
|
|
|
t.Parallel()
|
|
|
|
fixture := &testFixture{
|
2020-06-23 19:51:15 +02:00
|
|
|
T: t,
|
|
|
|
impl: impl,
|
|
|
|
lused: map[string]bool{},
|
2020-08-10 22:35:15 +02:00
|
|
|
AggregatorSelector: processortest.AggregatorSelector(),
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
cc := concurrency()
|
2020-10-31 20:16:55 +02:00
|
|
|
|
|
|
|
sdk := NewAccumulator(fixture, nil)
|
2020-10-17 18:48:21 +02:00
|
|
|
meter := otel.WrapMeterImpl(sdk, "stress_test")
|
2019-10-29 22:27:22 +02:00
|
|
|
fixture.wg.Add(cc + 1)
|
|
|
|
|
|
|
|
for i := 0; i < cc; i++ {
|
2020-03-19 21:02:46 +02:00
|
|
|
go fixture.startWorker(sdk, meter, &fixture.wg, i)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
numCollect := 0
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
time.Sleep(reclaimPeriod)
|
|
|
|
fixture.preCollect()
|
|
|
|
sdk.Collect(ctx)
|
|
|
|
numCollect++
|
|
|
|
if atomic.LoadInt64(&fixture.stop) != 0 {
|
|
|
|
fixture.wg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(testRun)
|
|
|
|
atomic.StoreInt64(&fixture.stop, 1)
|
|
|
|
fixture.wg.Wait()
|
|
|
|
fixture.preCollect()
|
|
|
|
sdk.Collect(ctx)
|
|
|
|
numCollect++
|
|
|
|
|
|
|
|
fixture.assertTest(numCollect)
|
|
|
|
}
|
|
|
|
|
2020-11-11 17:24:12 +02:00
|
|
|
func int64sEqual(a, b number.Number) bool {
|
2019-10-29 22:27:22 +02:00
|
|
|
return a.AsInt64() == b.AsInt64()
|
|
|
|
}
|
|
|
|
|
2020-11-11 17:24:12 +02:00
|
|
|
func float64sEqual(a, b number.Number) bool {
|
2019-10-29 22:27:22 +02:00
|
|
|
diff := math.Abs(a.AsFloat64() - b.AsFloat64())
|
|
|
|
return diff < math.Abs(a.AsFloat64())*epsilon
|
|
|
|
}
|
|
|
|
|
|
|
|
// Counters
|
|
|
|
|
2020-03-12 05:21:34 +02:00
|
|
|
func intCounterTestImpl() testImpl {
|
2019-10-29 22:27:22 +02:00
|
|
|
return testImpl{
|
2020-10-17 18:48:21 +02:00
|
|
|
newInstrument: func(meter otel.Meter, name string) SyncImpler {
|
2020-06-13 09:55:01 +02:00
|
|
|
return Must(meter).NewInt64Counter(name + ".sum")
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
getUpdateValue: func() number.Number {
|
2019-10-29 22:27:22 +02:00
|
|
|
for {
|
2020-03-12 05:21:34 +02:00
|
|
|
x := int64(rand.Intn(100))
|
2019-10-29 22:27:22 +02:00
|
|
|
if x != 0 {
|
2020-11-11 17:24:12 +02:00
|
|
|
return number.NewInt64Number(x)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
operate: func(inst interface{}, ctx context.Context, value number.Number, labels []label.KeyValue) {
|
2020-10-17 18:48:21 +02:00
|
|
|
counter := inst.(otel.Int64Counter)
|
2020-03-27 23:06:48 +02:00
|
|
|
counter.Add(ctx, value.AsInt64(), labels...)
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
|
|
|
newStore: func() interface{} {
|
2020-11-11 17:24:12 +02:00
|
|
|
n := number.NewInt64Number(0)
|
2019-10-29 22:27:22 +02:00
|
|
|
return &n
|
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
storeCollect: func(store interface{}, value number.Number, _ time.Time) {
|
|
|
|
store.(*number.Number).AddInt64Atomic(value.AsInt64())
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
storeExpect: func(store interface{}, value number.Number) {
|
|
|
|
store.(*number.Number).AddInt64Atomic(value.AsInt64())
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
readStore: func(store interface{}) number.Number {
|
|
|
|
return store.(*number.Number).AsNumberAtomic()
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
|
|
|
equalValues: int64sEqual,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 05:21:34 +02:00
|
|
|
func TestStressInt64Counter(t *testing.T) {
|
|
|
|
stressTest(t, intCounterTestImpl())
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-12 05:21:34 +02:00
|
|
|
func floatCounterTestImpl() testImpl {
|
2019-10-29 22:27:22 +02:00
|
|
|
return testImpl{
|
2020-10-17 18:48:21 +02:00
|
|
|
newInstrument: func(meter otel.Meter, name string) SyncImpler {
|
2020-06-13 09:55:01 +02:00
|
|
|
return Must(meter).NewFloat64Counter(name + ".sum")
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
getUpdateValue: func() number.Number {
|
2019-10-29 22:27:22 +02:00
|
|
|
for {
|
2020-03-12 05:21:34 +02:00
|
|
|
x := rand.Float64()
|
2019-10-29 22:27:22 +02:00
|
|
|
if x != 0 {
|
2020-11-11 17:24:12 +02:00
|
|
|
return number.NewFloat64Number(x)
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
operate: func(inst interface{}, ctx context.Context, value number.Number, labels []label.KeyValue) {
|
2020-10-17 18:48:21 +02:00
|
|
|
counter := inst.(otel.Float64Counter)
|
2020-03-27 23:06:48 +02:00
|
|
|
counter.Add(ctx, value.AsFloat64(), labels...)
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
|
|
|
newStore: func() interface{} {
|
2020-11-11 17:24:12 +02:00
|
|
|
n := number.NewFloat64Number(0.0)
|
2019-10-29 22:27:22 +02:00
|
|
|
return &n
|
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
storeCollect: func(store interface{}, value number.Number, _ time.Time) {
|
|
|
|
store.(*number.Number).AddFloat64Atomic(value.AsFloat64())
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
storeExpect: func(store interface{}, value number.Number) {
|
|
|
|
store.(*number.Number).AddFloat64Atomic(value.AsFloat64())
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
readStore: func(store interface{}) number.Number {
|
|
|
|
return store.(*number.Number).AsNumberAtomic()
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
|
|
|
equalValues: float64sEqual,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-12 05:21:34 +02:00
|
|
|
func TestStressFloat64Counter(t *testing.T) {
|
|
|
|
stressTest(t, floatCounterTestImpl())
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
// LastValue
|
2019-10-29 22:27:22 +02:00
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
func intLastValueTestImpl() testImpl {
|
2019-10-29 22:27:22 +02:00
|
|
|
return testImpl{
|
2020-10-17 18:48:21 +02:00
|
|
|
newInstrument: func(meter otel.Meter, name string) SyncImpler {
|
2020-05-16 07:11:12 +02:00
|
|
|
return Must(meter).NewInt64ValueRecorder(name + ".lastvalue")
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
getUpdateValue: func() number.Number {
|
2020-03-11 01:00:37 +02:00
|
|
|
r1 := rand.Int63()
|
2020-11-11 17:24:12 +02:00
|
|
|
return number.NewInt64Number(rand.Int63() - r1)
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
operate: func(inst interface{}, ctx context.Context, value number.Number, labels []label.KeyValue) {
|
2020-10-17 18:48:21 +02:00
|
|
|
valuerecorder := inst.(otel.Int64ValueRecorder)
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder.Record(ctx, value.AsInt64(), labels...)
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
|
|
|
newStore: func() interface{} {
|
2020-03-11 01:00:37 +02:00
|
|
|
return &lastValueState{
|
2020-11-11 17:24:12 +02:00
|
|
|
raw: number.NewInt64Number(0),
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
storeCollect: func(store interface{}, value number.Number, ts time.Time) {
|
2020-03-11 01:00:37 +02:00
|
|
|
gs := store.(*lastValueState)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
if !ts.Before(gs.ts) {
|
|
|
|
gs.ts = ts
|
|
|
|
gs.raw.SetInt64Atomic(value.AsInt64())
|
|
|
|
}
|
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
storeExpect: func(store interface{}, value number.Number) {
|
2020-03-11 01:00:37 +02:00
|
|
|
gs := store.(*lastValueState)
|
2019-10-29 22:27:22 +02:00
|
|
|
gs.raw.SetInt64Atomic(value.AsInt64())
|
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
readStore: func(store interface{}) number.Number {
|
2020-03-11 01:00:37 +02:00
|
|
|
gs := store.(*lastValueState)
|
2019-10-29 22:27:22 +02:00
|
|
|
return gs.raw.AsNumberAtomic()
|
|
|
|
},
|
|
|
|
equalValues: int64sEqual,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
func TestStressInt64LastValue(t *testing.T) {
|
|
|
|
stressTest(t, intLastValueTestImpl())
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
func floatLastValueTestImpl() testImpl {
|
2019-10-29 22:27:22 +02:00
|
|
|
return testImpl{
|
2020-10-17 18:48:21 +02:00
|
|
|
newInstrument: func(meter otel.Meter, name string) SyncImpler {
|
2020-05-16 07:11:12 +02:00
|
|
|
return Must(meter).NewFloat64ValueRecorder(name + ".lastvalue")
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
getUpdateValue: func() number.Number {
|
|
|
|
return number.NewFloat64Number((-0.5 + rand.Float64()) * 100000)
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
operate: func(inst interface{}, ctx context.Context, value number.Number, labels []label.KeyValue) {
|
2020-10-17 18:48:21 +02:00
|
|
|
valuerecorder := inst.(otel.Float64ValueRecorder)
|
2020-05-16 07:11:12 +02:00
|
|
|
valuerecorder.Record(ctx, value.AsFloat64(), labels...)
|
2019-10-29 22:27:22 +02:00
|
|
|
},
|
|
|
|
newStore: func() interface{} {
|
2020-03-11 01:00:37 +02:00
|
|
|
return &lastValueState{
|
2020-11-11 17:24:12 +02:00
|
|
|
raw: number.NewFloat64Number(0),
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
storeCollect: func(store interface{}, value number.Number, ts time.Time) {
|
2020-03-11 01:00:37 +02:00
|
|
|
gs := store.(*lastValueState)
|
2019-10-29 22:27:22 +02:00
|
|
|
|
|
|
|
if !ts.Before(gs.ts) {
|
|
|
|
gs.ts = ts
|
|
|
|
gs.raw.SetFloat64Atomic(value.AsFloat64())
|
|
|
|
}
|
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
storeExpect: func(store interface{}, value number.Number) {
|
2020-03-11 01:00:37 +02:00
|
|
|
gs := store.(*lastValueState)
|
2019-10-29 22:27:22 +02:00
|
|
|
gs.raw.SetFloat64Atomic(value.AsFloat64())
|
|
|
|
},
|
2020-11-11 17:24:12 +02:00
|
|
|
readStore: func(store interface{}) number.Number {
|
2020-03-11 01:00:37 +02:00
|
|
|
gs := store.(*lastValueState)
|
2019-10-29 22:27:22 +02:00
|
|
|
return gs.raw.AsNumberAtomic()
|
|
|
|
},
|
|
|
|
equalValues: float64sEqual,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-11 01:00:37 +02:00
|
|
|
func TestStressFloat64LastValue(t *testing.T) {
|
|
|
|
stressTest(t, floatLastValueTestImpl())
|
2019-10-29 22:27:22 +02:00
|
|
|
}
|