Files
MeterVault/tests/Integration.Tests/Performance/PerfDatabase.cs
T
Florian Schmidt 8940ef25c3
ci / build-test (push) Successful in 2m31s
Analysis: one selected period, one set of numbers, on every page
The dashboards told several stories at once. Overview asked for full
calendar years, meter detail for a fixed 12-month window that was really
13, Trends for 24 months with an Apply button, and the energy pages for
60. Each page derived "today" from UTC, so the first hours of a local day
belonged to yesterday. A missing tariff, a month nobody measured and a
genuine zero all rendered as 0. And a virtual meter -- the one thing the
spreadsheet leans on hardest -- was excluded from analysis outright:
MeterPeriodService returned null for it and the page offered a flow
diagram instead.

docs/DASHBOARD_ANALYSIS_CHANGE_BRIEF.md is the work order. Every choice it
left open is settled in docs/ANALYSIS_IMPLEMENTATION_NOTE.md as D-01..D-58
plus amendments A-01..A-30; code, tests and release notes cite those ids.

The analysis layer

Core/Analysis holds the pure rules: period presets resolved once in the
instance zone into a local date range and a half-open UTC range, bucket
plans, calendar-unit comparisons, coverage runs with a resolution class,
normalized quantities and units, the totals policy, the virtual formula
parser/validator/evaluator, and the cost calculator. "Now" comes from
TimeProvider; services never read the clock.

Normalization now writes, in the same transaction as consumption and by
diff, per-meter rollups by local day and month plus coverage runs and a
rollup state (AnalysisDataWriter). AnalysisReader answers a request from
those tables -- month rollups for month and year buckets, day rollups
otherwise, at most two partial edge days from consumption -- and
CostReader prices the result month by month. Pages, /api/v1 and the CSV
export read nothing else. The unused continuous aggregates are dropped.

The reader's statement count per request is constant whether it covers one
meter or a thousand. On a synthetic 1,000-meter, ten-year instance the
brief's target request (100 meters, ten years, monthly) takes 374 ms
against a two-second target, and the Overview went from 48,244 SQL
statements per load to 205.

Missing is not zero

Every bucket carries a status -- available, partial, missing, unresolved,
invalid, pending -- derived from coverage, never from the amount, with
provenance and a reason code beside it. A true zero is a number and a bar
on the baseline; an unknown bucket is a gap that says why; a month whose
data only exists monthly says so instead of inventing daily detail; a
scope with no tariff says "not priced" instead of 0. Rows whose interval
closes after now are reported separately rather than counted.

Virtual meters are analysis subjects

A virtual meter stores a canonical definition -- expression over m<id>
references, result kind, unit and cost rule -- validated on save and on
read for syntax, unknown or self references, loops and unit/kind rules.
It is evaluated on read from its sources' rollups over their joint
coverage: a missing source makes the bucket missing, an observed zero is
a valid input, a non-finite result is invalid with its dependency path,
and the page lists each source's contribution. Topology links are
topology only and never rewrite a saved calculation; expression-less
meters from older installs are converted once at startup. The editor has
Sum, Difference and Advanced modes with a live preview.

Totals and the bill

Per energy type the totals policy separates use, grid import, export,
generation and runtime, marks breakdown meters as breakdowns and virtual
meters as views, and never adds across units. The bill follows it: grid
import where there is one, separately priced subsections at their own
price, feed-in only on export meters, standing charges once per scope per
local day, manual costs once on their start day, categories as
non-overlapping covers whose composition reconciles to the bill. The
seeded demo's yearly totals now match the spreadsheet.

Pages and navigation

The period lives in the URL and every page reads the same contract, so a
link, a reload and the browser's Back button keep it. Shared components
carry it: page header with breadcrumbs, period toolbar, theme-aware chart
with an accessible table beside it, metric cards, comparison and
availability states, attention items that each link to the one action
that fixes them. Meter detail leads with an Analysis tab and resolves its
tabs by key; the energy page has Overview, History, Flow and Meters; the
old cost-only Trends page is a general Analysis page over portfolio, type,
category, meter or a meter comparison. Records tabs are paged server-side
instead of showing the latest 200. Everything is English and German,
light and dark, down to 360px.

Some figures change on purpose; docs/RELEASE_NOTES.md lists each one and
what the first start after the update does (it rebuilds all analysis data
before the web server listens). docs/SDD.md and CLAUDE.md describe the
system as it now is.

Tests: 1,733 Core and 746 integration, all green, plus an opt-in
performance suite with a synthetic 1,000-meter generator.
2026-09-20 10:29:13 +02:00

282 lines
13 KiB
C#

using System.Diagnostics;
using System.Globalization;
using System.Text.Json;
using MeterVault.Core.Normalization;
using MeterVault.Infrastructure.Normalization;
using MeterVault.Infrastructure.Options;
using MeterVault.Infrastructure.Persistence;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Npgsql;
using Testcontainers.PostgreSql;
using Xunit.Abstractions;
namespace MeterVault.Integration.Tests.Performance;
/// <summary>How the measured database was prepared: loaded now or reused, and what each step took.</summary>
internal sealed record PreparedDatabase(
DatasetManifest Manifest,
bool Loaded,
RebuildStats? Rebuild,
double? CompressSeconds,
int CompressedChunks);
/// <summary>The startup rebuild (NormalizationUpgrade) over the whole dataset.</summary>
internal sealed record RebuildStats(double Seconds, int Meters, int Rebuilt, bool MeasuredInThisRun);
/// <summary>
/// The database of one performance run: a fresh TimescaleDB container (the image the fixture pins), or an existing
/// database from <c>METERVAULT_PERF_DB</c>. Prepares it the way a real instance gets there — migrations, the default
/// seed, the raw dataset, the startup rebuild (<see cref="NormalizationUpgrade"/>), and the compression policy's work
/// on raw chunks older than 30 days — and hands out contexts for the readers.
/// </summary>
internal sealed class PerfDatabase : IDbContextFactory<MeterVaultDbContext>, IAsyncDisposable
{
/// <summary>The pinned image of <see cref="TimescaleFixture"/>.</summary>
public const string Image = "timescale/timescaledb:2.17.2-pg16";
/// <summary>The frozen "now" the measured dataset ends at: 19 September 2026, 14:37 Berlin, as the cost tests use.</summary>
public static readonly DateTimeOffset FrozenNow = new DateTimeOffset(2026, 9, 19, 14, 37, 0, TimeSpan.FromHours(2)).ToUniversalTime();
private const string RebuildKey = "perf_rebuild";
private readonly PostgreSqlContainer? _container;
private PerfDatabase(PostgreSqlContainer? container, string connectionString)
{
_container = container;
// Long statements are allowed: a 1,000-meter read on a busy machine must be measured, not time out.
ConnectionString = new NpgsqlConnectionStringBuilder(connectionString) { CommandTimeout = 600, IncludeErrorDetail = true }.ConnectionString;
}
public string ConnectionString { get; }
/// <summary>Where the database came from, for the report.</summary>
public string Origin => _container is null ? "existing database (METERVAULT_PERF_DB)" : $"fresh container ({Image})";
public static async Task<PerfDatabase> OpenAsync(PerfSettings settings, PerfLog log)
{
if (settings.Database is { } external)
{
log.Write("Using the database from METERVAULT_PERF_DB");
return new PerfDatabase(null, external);
}
log.Write($"Starting a {Image} container…");
var container = new PostgreSqlBuilder(Image)
.WithDatabase("metervault")
.WithUsername("metervault")
.WithPassword("metervault")
.Build();
await container.StartAsync();
return new PerfDatabase(container, container.GetConnectionString());
}
public MeterVaultDbContext CreateDbContext()
{
var options = new DbContextOptionsBuilder<MeterVaultDbContext>()
.UseNpgsql(ConnectionString, npgsql => npgsql.MigrationsAssembly(typeof(MeterVaultDbContext).Assembly.FullName))
.UseSnakeCaseNamingConvention()
.Options;
return new MeterVaultDbContext(options);
}
/// <summary>
/// Migrates and seeds, loads the dataset unless the database already holds it, rebuilds what is not built yet, and
/// compresses raw chunks older than 30 days (what the compression policy does on a running instance).
/// </summary>
public async Task<PreparedDatabase> PrepareAsync(PerfSettings settings, PerfLog log)
{
await using (var db = CreateDbContext())
{
db.Database.SetCommandTimeout(TimeSpan.FromMinutes(10));
await db.Database.MigrateAsync();
await DatabaseSeeder.SeedAsync(db);
// As in TimescaleFixture: the scheduled compression job must not race the load and rebuild. Compression
// is applied explicitly below, once, the way the policy would have left the chunks.
await db.Database.ExecuteSqlRawAsync(
"SELECT alter_job(job_id, scheduled => false) FROM timescaledb_information.jobs " +
"WHERE proc_name IN ('policy_compression', 'policy_columnstore');");
}
await using var connection = new NpgsqlConnection(ConnectionString);
await connection.OpenAsync();
var manifest = await SyntheticDataset.ReadMarkerAsync(connection);
var loaded = false;
if (manifest is null)
{
var zone = TimeZoneInfo.FindSystemTimeZoneById("Europe/Berlin");
log.Write(string.Create(CultureInfo.InvariantCulture, $"Loading the synthetic dataset (scale {settings.Scale}, now {FrozenNow:O})…"));
manifest = await SyntheticDataset.LoadAsync(ConnectionString, FrozenNow, zone, settings.Scale, log.Write);
loaded = true;
}
else
{
log.Write(string.Create(CultureInfo.InvariantCulture, $"Reusing the loaded dataset ({manifest.MeterCount} meters, now {manifest.Now:O})"));
}
var rebuild = await RebuildAsync(connection, manifest, log);
var (compressSeconds, compressed) = await CompressAsync(connection, manifest.Now, log);
if (loaded || rebuild?.MeasuredInThisRun == true || compressed > 0)
{
log.Write("VACUUM ANALYZE…");
await ExecuteAsync(connection, "VACUUM ANALYZE");
}
return new PreparedDatabase(manifest, loaded, rebuild, compressSeconds, compressed);
}
/// <summary>
/// Runs the startup rebuild when stored consumption is missing or outdated — timed, with a frozen clock at the
/// dataset's "now" — and remembers its duration in the database, so a later reuse can still report it.
/// </summary>
private async Task<RebuildStats?> RebuildAsync(NpgsqlConnection connection, DatasetManifest manifest, PerfLog log)
{
var meters = Convert.ToInt32(await ScalarAsync(connection, "SELECT count(*) FROM meter"), CultureInfo.InvariantCulture);
var built = Convert.ToInt32(
await ScalarAsync(connection, $"SELECT count(*) FROM meter_rollup_state WHERE revision >= {NormalizationUpgrade.CurrentRevision}"),
CultureInfo.InvariantCulture);
var revision = await ScalarAsync(connection, $"SELECT value::text FROM app_setting WHERE key = '{NormalizationUpgrade.SettingKey}'");
if (built >= meters && revision is not null)
{
return await ScalarAsync(connection, $"SELECT value::text FROM app_setting WHERE key = '{RebuildKey}'") is string json
? JsonSerializer.Deserialize<RebuildStats>(json)! with { MeasuredInThisRun = false }
: null;
}
log.Write(string.Create(CultureInfo.InvariantCulture, $"Rebuilding {meters} meters through NormalizationUpgrade…"));
var watch = Stopwatch.StartNew();
int rebuilt;
await using (var db = CreateDbContext())
{
var normalization = Normalization(db, manifest);
var upgrade = new NormalizationUpgrade(db, normalization, new PerfLogger<NormalizationUpgrade>(log));
rebuilt = await upgrade.RunAsync();
}
watch.Stop();
var stats = new RebuildStats(watch.Elapsed.TotalSeconds, meters, rebuilt, MeasuredInThisRun: true);
log.Write(string.Create(CultureInfo.InvariantCulture, $"Rebuilt {rebuilt} of {meters} meters in {stats.Seconds:F1} s"));
await using var command = new NpgsqlCommand(
"INSERT INTO app_setting (key, value) VALUES (@key, @value) ON CONFLICT (key) DO UPDATE SET value = excluded.value", connection);
command.Parameters.AddWithValue("key", RebuildKey);
command.Parameters.Add(new NpgsqlParameter("value", NpgsqlTypes.NpgsqlDbType.Jsonb) { Value = JsonSerializer.Serialize(stats) });
await command.ExecuteNonQueryAsync();
return stats;
}
/// <summary>A normalization service as the app builds it, in the dataset's zone, on a clock frozen at its "now".</summary>
public static NormalizationService Normalization(MeterVaultDbContext db, DatasetManifest manifest) =>
new(db, NormalizationEngine.CreateDefault(),
Microsoft.Extensions.Options.Options.Create(new MeterVaultOptions { TimeZone = manifest.Zone }),
new FixedTimeProvider(manifest.Now));
/// <summary>Compresses the raw chunks the 30-day policy would have compressed by the dataset's "now".</summary>
private static async Task<(double? Seconds, int Chunks)> CompressAsync(NpgsqlConnection connection, DateTimeOffset now, PerfLog log)
{
await using var pending = new NpgsqlCommand(
"""
SELECT count(*) FROM timescaledb_information.chunks
WHERE hypertable_name = 'reading' AND NOT is_compressed AND range_end <= @horizon
""", connection);
pending.Parameters.AddWithValue("horizon", now.AddDays(-30).UtcDateTime);
var count = Convert.ToInt32(await pending.ExecuteScalarAsync(), CultureInfo.InvariantCulture);
if (count == 0)
{
return (null, 0);
}
log.Write(string.Create(CultureInfo.InvariantCulture, $"Compressing {count} raw chunks older than 30 days…"));
var watch = Stopwatch.StartNew();
await using var compress = new NpgsqlCommand(
"SELECT count(compress_chunk(c, if_not_compressed => true)) FROM show_chunks('reading', older_than => @horizon) c", connection)
{
CommandTimeout = 0,
};
compress.Parameters.AddWithValue("horizon", now.AddDays(-30).UtcDateTime);
await compress.ExecuteScalarAsync();
return (watch.Elapsed.TotalSeconds, count);
}
public static async Task<object?> ScalarAsync(NpgsqlConnection connection, string sql)
{
await using var command = new NpgsqlCommand(sql, connection) { CommandTimeout = 0 };
var value = await command.ExecuteScalarAsync();
return value is DBNull ? null : value;
}
public static async Task ExecuteAsync(NpgsqlConnection connection, string sql)
{
await using var command = new NpgsqlCommand(sql, connection) { CommandTimeout = 0 };
await command.ExecuteNonQueryAsync();
}
public async ValueTask DisposeAsync()
{
if (_container is not null)
{
await _container.DisposeAsync();
}
}
}
/// <summary>Timestamped progress to <c>perf-&lt;label&gt;.log</c> in the output directory and to the test output.</summary>
internal sealed class PerfLog : IDisposable
{
private readonly StreamWriter _file;
private readonly ITestOutputHelper? _output;
private readonly Stopwatch _clock = Stopwatch.StartNew();
public PerfLog(PerfSettings settings, string name, ITestOutputHelper? output)
{
Directory.CreateDirectory(settings.OutputDirectory);
Path = System.IO.Path.Combine(settings.OutputDirectory, $"{name}-{settings.Label}.log");
_file = new StreamWriter(Path, append: false) { AutoFlush = true };
_output = output;
}
public string Path { get; }
public void Write(string message)
{
var line = string.Create(CultureInfo.InvariantCulture, $"[{DateTimeOffset.Now:HH:mm:ss} +{_clock.Elapsed.TotalSeconds,7:F1}s] {message}");
lock (_file)
{
_file.WriteLine(line);
}
try
{
_output?.WriteLine(line);
}
catch (InvalidOperationException)
{
// The test has finished; the file still has it.
}
}
public void Dispose() => _file.Dispose();
}
/// <summary>Routes the upgrade's progress ("Rebuilt 100 of 1000 meter(s)") into the perf log.</summary>
internal sealed class PerfLogger<T>(PerfLog log) : ILogger<T>
{
public IDisposable? BeginScope<TState>(TState state)
where TState : notnull => null;
public bool IsEnabled(LogLevel logLevel) => logLevel >= LogLevel.Information;
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (IsEnabled(logLevel))
{
log.Write($"{typeof(T).Name}: {formatter(state, exception)}{(exception is null ? string.Empty : " " + exception.Message)}");
}
}
}