Files
beacon-server/db/observation_counts_test.go
MrAlders0n bbc964fd2f perf(observers): count unrolled hours in memory for the directory
The directory re-read every raw observation of the hours not yet
rolled up: on prod about 293k rows for the open hour, a heap page each
(new rows land wherever cleanup freed space), 7 of the 8 s an
all-regions request took. The ingesting process now also counts each
stored observation per minute, observer and IATA, and the directory
adds those up for hours wholly counted, reading raw rows only for the
last partial minute. Partial or cleanup-thinned hours and processes
that don't ingest read raw rows as before; results are identical (an
integration test compares both paths).
2026-10-27 15:18:13 -04:00

54 lines
2.1 KiB
Go

// Copyright 2026 Beacon Contributors
// SPDX-License-Identifier: AGPL-3.0-or-later
package db
import (
"context"
"errors"
"testing"
"time"
sqlc "github.com/MeshCore-Beacon/beacon-server/db/sqlc"
mockdb "github.com/MeshCore-Beacon/beacon-server/db/sqlc/mock"
"github.com/MeshCore-Beacon/beacon-server/internal/ingest"
"github.com/MeshCore-Beacon/beacon-server/internal/obscount"
"github.com/google/uuid"
"go.uber.org/mock/gomock"
)
// Only a row that was really stored is counted: a duplicate hearing writes nothing.
func TestInsertObservationCountsStoredRows(t *testing.T) {
ctx := context.Background()
start := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
clock := start
counts := obscount.New(func() time.Time { return clock })
mock := mockdb.NewMockQuerier(gomock.NewController(t))
s := &Store{q: mock}
WithObservationCounts(counts)(s)
observer := uuid.MustParse("00000000-0000-0000-0000-000000000001")
at := start.Add(90 * time.Minute)
o := ingest.InsertObservationParams{PacketHash: []byte{1}, ObserverID: observer, IATA: "YVR", HeardAt: at}
mock.EXPECT().InsertObservation(gomock.Any(), gomock.Any()).Return(sqlc.InsertObservationRow{Inserted: true, ObservationCount: 1}, nil)
mock.EXPECT().InsertObservation(gomock.Any(), gomock.Any()).Return(sqlc.InsertObservationRow{Inserted: false, ObservationCount: 1}, nil)
mock.EXPECT().InsertObservation(gomock.Any(), gomock.Any()).Return(sqlc.InsertObservationRow{}, errors.New("boom"))
mock.EXPECT().InsertObservationUnsited(gomock.Any(), gomock.Any()).Return(sqlc.InsertObservationUnsitedRow{Inserted: true, ObservationCount: 2}, nil)
for range 3 {
if _, _, err := s.InsertObservation(ctx, o); err != nil {
t.Fatal(err)
}
}
clock = start.Add(3 * time.Hour)
snap := counts.Snapshot(clock)
var total int64
for i := range snap.Counts {
if snap.Observers[i] != observer || snap.IATAs[i] != "YVR" || !snap.Hours[i].Equal(start.Add(time.Hour)) {
t.Fatalf("unexpected count row %v %v %v", snap.Observers[i], snap.IATAs[i], snap.Hours[i])
}
total += snap.Counts[i]
}
if total != 2 {
t.Fatalf("counted %d rows, want the 2 that were stored", total)
}
}