| 1 |
package memmetrics |
| 2 |
|
| 3 |
import ( |
| 4 |
"runtime" |
| 5 |
"sync" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/assert" |
| 10 |
"github.com/stretchr/testify/require" |
| 11 |
"github.com/vulcand/oxy/v2/internal/holsterv4/clock" |
| 12 |
"github.com/vulcand/oxy/v2/testutils" |
| 13 |
) |
| 14 |
|
| 15 |
func TestDefaults(t *testing.T) { |
| 16 |
done := testutils.FreezeTime() |
| 17 |
defer done() |
| 18 |
|
| 19 |
rr, err := NewRTMetrics() |
| 20 |
require.NoError(t, err) |
| 21 |
require.NotNil(t, rr) |
| 22 |
|
| 23 |
rr.Record(200, clock.Second) |
| 24 |
rr.Record(502, 2*clock.Second) |
| 25 |
rr.Record(200, clock.Second) |
| 26 |
rr.Record(200, clock.Second) |
| 27 |
|
| 28 |
assert.EqualValues(t, 1, rr.NetworkErrorCount()) |
| 29 |
assert.EqualValues(t, 4, rr.TotalCount()) |
| 30 |
assert.Equal(t, map[int]int64{502: 1, 200: 3}, rr.StatusCodesCounts()) |
| 31 |
assert.Equal(t, float64(1)/float64(4), rr.NetworkErrorRatio()) |
| 32 |
assert.Equal(t, 1.0/3.0, rr.ResponseCodeRatio(500, 503, 200, 300)) |
| 33 |
|
| 34 |
h, err := rr.LatencyHistogram() |
| 35 |
require.NoError(t, err) |
| 36 |
assert.Equal(t, 2, int(h.LatencyAtQuantile(100)/clock.Second)) |
| 37 |
|
| 38 |
rr.Reset() |
| 39 |
assert.EqualValues(t, 0, rr.NetworkErrorCount()) |
| 40 |
assert.EqualValues(t, 0, rr.TotalCount()) |
| 41 |
assert.Equal(t, map[int]int64{}, rr.StatusCodesCounts()) |
| 42 |
assert.Equal(t, float64(0), rr.NetworkErrorRatio()) |
| 43 |
assert.Equal(t, float64(0), rr.ResponseCodeRatio(500, 503, 200, 300)) |
| 44 |
|
| 45 |
h, err = rr.LatencyHistogram() |
| 46 |
require.NoError(t, err) |
| 47 |
assert.Equal(t, time.Duration(0), h.LatencyAtQuantile(100)) |
| 48 |
} |
| 49 |
|
| 50 |
func TestAppend(t *testing.T) { |
| 51 |
done := testutils.FreezeTime() |
| 52 |
defer done() |
| 53 |
|
| 54 |
rr, err := NewRTMetrics() |
| 55 |
require.NoError(t, err) |
| 56 |
require.NotNil(t, rr) |
| 57 |
|
| 58 |
rr.Record(200, clock.Second) |
| 59 |
rr.Record(502, 2*clock.Second) |
| 60 |
rr.Record(200, clock.Second) |
| 61 |
rr.Record(200, clock.Second) |
| 62 |
|
| 63 |
rr2, err := NewRTMetrics() |
| 64 |
require.NoError(t, err) |
| 65 |
require.NotNil(t, rr2) |
| 66 |
|
| 67 |
rr2.Record(200, 3*clock.Second) |
| 68 |
rr2.Record(501, 3*clock.Second) |
| 69 |
rr2.Record(200, 3*clock.Second) |
| 70 |
rr2.Record(200, 3*clock.Second) |
| 71 |
|
| 72 |
require.NoError(t, rr2.Append(rr)) |
| 73 |
assert.Equal(t, map[int]int64{501: 1, 502: 1, 200: 6}, rr2.StatusCodesCounts()) |
| 74 |
assert.EqualValues(t, 1, rr2.NetworkErrorCount()) |
| 75 |
|
| 76 |
h, err := rr2.LatencyHistogram() |
| 77 |
require.NoError(t, err) |
| 78 |
assert.EqualValues(t, 3, h.LatencyAtQuantile(100)/clock.Second) |
| 79 |
} |
| 80 |
|
| 81 |
func TestConcurrentRecords(t *testing.T) { |
| 82 |
// This test asserts a race condition which requires parallelism |
| 83 |
runtime.GOMAXPROCS(100) |
| 84 |
|
| 85 |
rr, err := NewRTMetrics() |
| 86 |
require.NoError(t, err) |
| 87 |
|
| 88 |
for code := 0; code < 100; code++ { |
| 89 |
for numRecords := 0; numRecords < 10; numRecords++ { |
| 90 |
go func(statusCode int) { |
| 91 |
_ = rr.recordStatusCode(statusCode) |
| 92 |
}(code) |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func TestRTMetricExportReturnsNewCopy(t *testing.T) { |
| 98 |
a := RTMetrics{ |
| 99 |
statusCodes: map[int]*RollingCounter{}, |
| 100 |
statusCodesLock: sync.RWMutex{}, |
| 101 |
histogram: &RollingHDRHistogram{}, |
| 102 |
histogramLock: sync.RWMutex{}, |
| 103 |
} |
| 104 |
|
| 105 |
var err error |
| 106 |
a.total, err = NewCounter(1, clock.Second) |
| 107 |
require.NoError(t, err) |
| 108 |
|
| 109 |
a.netErrors, err = NewCounter(1, clock.Second) |
| 110 |
require.NoError(t, err) |
| 111 |
|
| 112 |
a.newCounter = func() (*RollingCounter, error) { |
| 113 |
return NewCounter(counterBuckets, counterResolution) |
| 114 |
} |
| 115 |
a.newHist = func() (*RollingHDRHistogram, error) { |
| 116 |
return NewRollingHDRHistogram(histMin, histMax, histSignificantFigures, histPeriod, histBuckets) |
| 117 |
} |
| 118 |
|
| 119 |
b := a.Export() |
| 120 |
a.total = nil |
| 121 |
a.netErrors = nil |
| 122 |
a.statusCodes = nil |
| 123 |
a.histogram = nil |
| 124 |
a.newCounter = nil |
| 125 |
a.newHist = nil |
| 126 |
|
| 127 |
assert.NotNil(t, b.total) |
| 128 |
assert.NotNil(t, b.netErrors) |
| 129 |
assert.NotNil(t, b.statusCodes) |
| 130 |
assert.NotNil(t, b.histogram) |
| 131 |
assert.NotNil(t, b.newCounter) |
| 132 |
assert.NotNil(t, b.newHist) |
| 133 |
|
| 134 |
// a and b should have different locks |
| 135 |
locksSucceed := make(chan bool) |
| 136 |
go func() { |
| 137 |
a.statusCodesLock.Lock() |
| 138 |
b.statusCodesLock.Lock() |
| 139 |
a.histogramLock.Lock() |
| 140 |
b.histogramLock.Lock() |
| 141 |
locksSucceed <- true |
| 142 |
}() |
| 143 |
|
| 144 |
for { |
| 145 |
select { |
| 146 |
case <-locksSucceed: |
| 147 |
return |
| 148 |
case <-clock.After(10 * clock.Second): |
| 149 |
t.FailNow() |
| 150 |
} |
| 151 |
} |
| 152 |
} |