storj/satellite/metabase/rangedloop/rangedlooptest/sleepobserver.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

46 lines
1.2 KiB
Go

// Copyright (C) 2022 Storj Labs, Inc.
// See LICENSE for copying information.
package rangedlooptest
import (
"context"
"time"
"storj.io/storj/satellite/metabase/rangedloop"
"storj.io/storj/satellite/metabase/segmentloop"
)
// SleepObserver is a subscriber to the segment loop which sleeps for every batch.
type SleepObserver struct {
Duration time.Duration
}
// Start is the callback for segment loop start.
func (c *SleepObserver) Start(ctx context.Context, time time.Time) error {
return nil
}
// Fork splits the observer to process a segment range.
func (c *SleepObserver) Fork(ctx context.Context) (rangedloop.Partial, error) {
return c, nil
}
// Join is a noop.
func (c *SleepObserver) Join(ctx context.Context, partial rangedloop.Partial) error {
// Range done
return nil
}
// Finish is the callback for segment loop end.
func (c *SleepObserver) Finish(ctx context.Context) error {
return nil
}
// Process sleeps for every batch of segments to simulate execution time.
func (c *SleepObserver) Process(ctx context.Context, segments []segmentloop.Segment) error {
sleepTime := time.Duration(c.Duration.Nanoseconds() * int64(len(segments)))
time.Sleep(sleepTime)
return nil
}