perf(go): deduplicate unchanged mileage samples

This commit is contained in:
lingniu
2026-07-03 08:39:48 +08:00
parent c1640b4332
commit 8f5afd20a6
2 changed files with 80 additions and 3 deletions

View File

@@ -149,6 +149,59 @@ func TestWriterEnsuresSchemaAndUpsertsDailyMileage(t *testing.T) {
}
}
func TestWriterSkipsConsecutiveDuplicateMileageSamples(t *testing.T) {
exec := &recordingExec{}
writer := NewWriter(exec, time.FixedZone("Asia/Shanghai", 8*3600))
event := envelope.FrameEnvelope{
Protocol: envelope.ProtocolJT808,
VIN: "LNBVIN00000000001",
EventTimeMS: time.Date(2026, 7, 1, 9, 0, 0, 0, time.FixedZone("Asia/Shanghai", 8*3600)).UnixMilli(),
Fields: map[string]any{
envelope.FieldTotalMileageKM: 10241.2,
},
}
if err := writer.Append(context.Background(), event); err != nil {
t.Fatalf("first Append() error = %v", err)
}
event.ReceivedAtMS += 1000
if err := writer.Append(context.Background(), event); err != nil {
t.Fatalf("duplicate Append() error = %v", err)
}
if len(exec.calls) != 1 {
t.Fatalf("exec calls = %d, want 1", len(exec.calls))
}
}
func TestWriterKeepsOnlyLatestDateInMileageCache(t *testing.T) {
exec := &recordingExec{}
writer := NewWriter(exec, time.FixedZone("Asia/Shanghai", 8*3600))
event := envelope.FrameEnvelope{
Protocol: envelope.ProtocolJT808,
VIN: "LNBVIN00000000001",
Fields: map[string]any{
envelope.FieldTotalMileageKM: 10241.2,
},
}
event.EventTimeMS = time.Date(2026, 7, 1, 23, 59, 0, 0, time.FixedZone("Asia/Shanghai", 8*3600)).UnixMilli()
if err := writer.Append(context.Background(), event); err != nil {
t.Fatalf("first Append() error = %v", err)
}
event.EventTimeMS = time.Date(2026, 7, 2, 0, 1, 0, 0, time.FixedZone("Asia/Shanghai", 8*3600)).UnixMilli()
if err := writer.Append(context.Background(), event); err != nil {
t.Fatalf("next-day Append() error = %v", err)
}
if len(exec.calls) != 2 {
t.Fatalf("exec calls = %d, want 2", len(exec.calls))
}
if len(writer.lastTotalMileage) != 1 {
t.Fatalf("cache entries = %d, want 1", len(writer.lastTotalMileage))
}
}
type execCall struct {
query string
args []any