Skip to content

Commit 57b2344

Browse files
author
rhamlett_microsoft
committed
Fixed NAN issue in memory reporting.
Fixed Active Simulations not clear when sim completes
1 parent 4970cf6 commit 57b2344

3 files changed

Lines changed: 86 additions & 3 deletions

File tree

src/PerfProblemSimulator/Services/MetricsBroadcastService.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace PerfProblemSimulator.Services;
2929
public class MetricsBroadcastService : IHostedService
3030
{
3131
private readonly IMetricsCollector _metricsCollector;
32+
private readonly ISimulationTracker _simulationTracker;
3233
private readonly IHubContext<MetricsHub, IMetricsClient> _hubContext;
3334
private readonly ILogger<MetricsBroadcastService> _logger;
3435

@@ -37,10 +38,12 @@ public class MetricsBroadcastService : IHostedService
3738
/// </summary>
3839
public MetricsBroadcastService(
3940
IMetricsCollector metricsCollector,
41+
ISimulationTracker simulationTracker,
4042
IHubContext<MetricsHub, IMetricsClient> hubContext,
4143
ILogger<MetricsBroadcastService> logger)
4244
{
4345
_metricsCollector = metricsCollector ?? throw new ArgumentNullException(nameof(metricsCollector));
46+
_simulationTracker = simulationTracker ?? throw new ArgumentNullException(nameof(simulationTracker));
4447
_hubContext = hubContext ?? throw new ArgumentNullException(nameof(hubContext));
4548
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
4649
}
@@ -49,6 +52,8 @@ public MetricsBroadcastService(
4952
public Task StartAsync(CancellationToken cancellationToken)
5053
{
5154
_metricsCollector.MetricsCollected += OnMetricsCollected;
55+
_simulationTracker.SimulationStarted += OnSimulationStarted;
56+
_simulationTracker.SimulationCompleted += OnSimulationCompleted;
5257
_metricsCollector.Start();
5358

5459
_logger.LogInformation("Metrics broadcast service started");
@@ -59,6 +64,8 @@ public Task StartAsync(CancellationToken cancellationToken)
5964
public Task StopAsync(CancellationToken cancellationToken)
6065
{
6166
_metricsCollector.MetricsCollected -= OnMetricsCollected;
67+
_simulationTracker.SimulationStarted -= OnSimulationStarted;
68+
_simulationTracker.SimulationCompleted -= OnSimulationCompleted;
6269
_metricsCollector.Stop();
6370

6471
_logger.LogInformation("Metrics broadcast service stopped");
@@ -76,4 +83,30 @@ private async void OnMetricsCollected(object? sender, MetricsSnapshot snapshot)
7683
_logger.LogError(ex, "Error broadcasting metrics to clients");
7784
}
7885
}
86+
87+
private async void OnSimulationStarted(object? sender, SimulationEventArgs e)
88+
{
89+
try
90+
{
91+
await _hubContext.Clients.All.SimulationStarted(e.Type.ToString(), e.SimulationId);
92+
_logger.LogDebug("Broadcast SimulationStarted: {Type} {Id}", e.Type, e.SimulationId);
93+
}
94+
catch (Exception ex)
95+
{
96+
_logger.LogError(ex, "Error broadcasting simulation started event");
97+
}
98+
}
99+
100+
private async void OnSimulationCompleted(object? sender, SimulationEventArgs e)
101+
{
102+
try
103+
{
104+
await _hubContext.Clients.All.SimulationCompleted(e.Type.ToString(), e.SimulationId);
105+
_logger.LogDebug("Broadcast SimulationCompleted: {Type} {Id}", e.Type, e.SimulationId);
106+
}
107+
catch (Exception ex)
108+
{
109+
_logger.LogError(ex, "Error broadcasting simulation completed event");
110+
}
111+
}
79112
}

src/PerfProblemSimulator/Services/SimulationTracker.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ namespace PerfProblemSimulator.Services;
88
/// </summary>
99
public interface ISimulationTracker
1010
{
11+
/// <summary>
12+
/// Event fired when a simulation is registered.
13+
/// </summary>
14+
event EventHandler<SimulationEventArgs>? SimulationStarted;
15+
16+
/// <summary>
17+
/// Event fired when a simulation is unregistered (completed or cancelled).
18+
/// </summary>
19+
event EventHandler<SimulationEventArgs>? SimulationCompleted;
20+
1121
/// <summary>
1222
/// Registers a new active simulation.
1323
/// </summary>
@@ -61,6 +71,31 @@ void RegisterSimulation(
6171
bool TryGetSimulation(Guid simulationId, out ActiveSimulationInfo? info);
6272
}
6373

74+
/// <summary>
75+
/// Event arguments for simulation lifecycle events.
76+
/// </summary>
77+
public class SimulationEventArgs : EventArgs
78+
{
79+
/// <summary>
80+
/// Gets the simulation ID.
81+
/// </summary>
82+
public Guid SimulationId { get; }
83+
84+
/// <summary>
85+
/// Gets the simulation type.
86+
/// </summary>
87+
public SimulationType Type { get; }
88+
89+
/// <summary>
90+
/// Initializes a new instance of the <see cref="SimulationEventArgs"/> class.
91+
/// </summary>
92+
public SimulationEventArgs(Guid simulationId, SimulationType type)
93+
{
94+
SimulationId = simulationId;
95+
Type = type;
96+
}
97+
}
98+
6499
/// <summary>
65100
/// Information about an active simulation.
66101
/// </summary>
@@ -112,6 +147,12 @@ public class SimulationTracker : ISimulationTracker
112147
private readonly ConcurrentDictionary<Guid, TrackedSimulation> _simulations = new();
113148
private readonly ILogger<SimulationTracker> _logger;
114149

150+
/// <inheritdoc />
151+
public event EventHandler<SimulationEventArgs>? SimulationStarted;
152+
153+
/// <inheritdoc />
154+
public event EventHandler<SimulationEventArgs>? SimulationCompleted;
155+
115156
/// <summary>
116157
/// Internal tracking record that includes the cancellation source.
117158
/// </summary>
@@ -155,6 +196,9 @@ public void RegisterSimulation(
155196
type,
156197
simulationId,
157198
parameters);
199+
200+
// Fire the SimulationStarted event
201+
SimulationStarted?.Invoke(this, new SimulationEventArgs(simulationId, type));
158202
}
159203
else
160204
{
@@ -174,6 +218,10 @@ public bool UnregisterSimulation(Guid simulationId)
174218
tracked.Info.Type,
175219
simulationId,
176220
DateTimeOffset.UtcNow - tracked.Info.StartedAt);
221+
222+
// Fire the SimulationCompleted event
223+
SimulationCompleted?.Invoke(this, new SimulationEventArgs(simulationId, tracked.Info.Type));
224+
177225
return true;
178226
}
179227

src/PerfProblemSimulator/wwwroot/js/dashboard.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,9 @@ async function allocateMemory() {
360360

361361
if (response.ok) {
362362
const result = await response.json();
363-
addActiveSimulation(result.blockId, 'memory', `Memory ${sizeMb}MB`);
364-
logEvent('success', `Memory allocated: ${result.blockId} (${result.actualSizeBytes / 1024 / 1024} MB)`);
363+
const actualSizeMb = result.actualParameters?.sizeMegabytes ?? sizeMb;
364+
addActiveSimulation(result.simulationId, 'memory', `Memory ${actualSizeMb}MB`);
365+
logEvent('success', `Memory allocated: ${result.simulationId} (${actualSizeMb} MB)`);
365366
} else {
366367
const error = await response.json();
367368
logEvent('error', `Failed: ${error.detail || 'Unknown error'}`);
@@ -387,7 +388,8 @@ async function releaseMemory() {
387388
}
388389
});
389390
updateActiveSimulationsUI();
390-
logEvent('success', `Released ${result.blocksReleased} blocks (${result.bytesReleased / 1024 / 1024} MB)`);
391+
const releasedMb = result.releasedMegabytes ?? (result.releasedBytes / 1024 / 1024);
392+
logEvent('success', `Released ${result.releasedBlockCount ?? 0} blocks (${releasedMb.toFixed(1)} MB)`);
391393
} else {
392394
const error = await response.json();
393395
logEvent('error', `Failed: ${error.detail || 'Unknown error'}`);

0 commit comments

Comments
 (0)