storj/satellite/metrics/observer_test.go
Erik van Velzen 37b4981cc0
satellite/metabase/rangedloop: measure observer duration (#5350)
Track duration of all segment loop observers. Factor out functions to
reduce size.

Still need to send the measurements out via monkit.

Part of https://github.com/storj/storj/issues/5223

Change-Id: Iae0260e250f8ea33affed95c6592a1f42df384eb
2022-12-21 21:58:08 +01:00

101 lines
2.7 KiB
Go

// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package metrics
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"storj.io/common/testcontext"
"storj.io/common/uuid"
"storj.io/storj/satellite/metabase"
"storj.io/storj/satellite/metabase/rangedloop"
"storj.io/storj/satellite/metabase/rangedloop/rangedlooptest"
"storj.io/storj/satellite/metabase/segmentloop"
)
var (
inline1 = []segmentloop.Segment{
{StreamID: uuid.UUID{1}, EncryptedSize: 10},
}
remote2 = []segmentloop.Segment{
{StreamID: uuid.UUID{2}, EncryptedSize: 16, Pieces: metabase.Pieces{{}}},
{StreamID: uuid.UUID{2}, EncryptedSize: 10},
}
remote3 = []segmentloop.Segment{
{StreamID: uuid.UUID{3}, EncryptedSize: 16, Pieces: metabase.Pieces{{}}},
{StreamID: uuid.UUID{3}, EncryptedSize: 16, Pieces: metabase.Pieces{{}}},
{StreamID: uuid.UUID{3}, EncryptedSize: 16, Pieces: metabase.Pieces{{}}},
{StreamID: uuid.UUID{3}, EncryptedSize: 10},
}
)
func TestObserver(t *testing.T) {
ctx := testcontext.New(t)
loop := func(tb testing.TB, obs *Observer, streams ...[]segmentloop.Segment) Metrics {
service := rangedloop.NewService(
zap.NewNop(),
rangedloop.Config{BatchSize: 2, Parallelism: 2},
&rangedlooptest.RangeSplitter{Segments: combineSegments(streams...)},
[]rangedloop.Observer{obs})
_, err := service.RunOnce(ctx)
require.NoError(tb, err)
return obs.TestingMetrics()
}
t.Run("stats aggregation", func(t *testing.T) {
obs := NewObserver()
metrics := loop(t, obs, inline1, remote2, remote3)
require.Equal(t, Metrics{
InlineObjects: 1,
RemoteObjects: 2,
TotalInlineSegments: 3,
TotalRemoteSegments: 4,
TotalInlineBytes: 30,
TotalRemoteBytes: 64,
}, metrics)
})
t.Run("stats reset by start", func(t *testing.T) {
obs := NewObserver()
_ = loop(t, obs, inline1)
// Any metrics gathered during the first loop should be dropped.
metrics := loop(t, obs, remote3)
require.Equal(t, Metrics{
InlineObjects: 0,
RemoteObjects: 1,
TotalInlineSegments: 1,
TotalRemoteSegments: 3,
TotalInlineBytes: 10,
TotalRemoteBytes: 48,
}, metrics)
})
t.Run("join fails gracefully on bad partial type", func(t *testing.T) {
type wrongPartial struct{ rangedloop.Partial }
obs := NewObserver()
err := obs.Start(ctx, time.Time{})
require.NoError(t, err)
err = obs.Join(ctx, wrongPartial{})
require.EqualError(t, err, "metrics: expected *metrics.observerFork but got metrics.wrongPartial")
})
}
func combineSegments(ss ...[]segmentloop.Segment) []segmentloop.Segment {
var combined []segmentloop.Segment
for _, s := range ss {
combined = append(combined, s...)
}
return combined
}