Skip to content

Commit 4beee82

Browse files
committed
debugging CPU sim id
1 parent 0934ee5 commit 4beee82

2 files changed

Lines changed: 23 additions & 9 deletions

File tree

src/PerfProblemSimulator/Services/CpuStressService.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,10 @@ public Task<SimulationResult> TriggerCpuStressAsync(int durationSeconds, Cancell
160160
// STEP 3: Track SimulationStarted event BEFORE starting background work
161161
// ==========================================================================
162162
// IMPORTANT: We track the start event here, in the synchronous HTTP request flow,
163-
// BEFORE launching the CPU-intensive background task. This ensures the telemetry
164-
// is transmitted before CPU saturation prevents I/O threads from running.
165-
_simulationContext.TrackSimulationStarted(simulationId, SimulationType.Cpu.ToString());
163+
// BEFORE launching the CPU-intensive background task. We use waitForTransmission: true
164+
// to ensure the telemetry is actually transmitted over the network before CPU saturation
165+
// prevents I/O threads from running.
166+
_simulationContext.TrackSimulationStarted(simulationId, SimulationType.Cpu.ToString(), waitForTransmission: true);
166167

167168
// ==========================================================================
168169
// STEP 4: Start the CPU stress in the background

src/PerfProblemSimulator/Services/SimulationContext.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public interface ISimulationContext
3636
/// </summary>
3737
/// <param name="simulationId">The simulation ID.</param>
3838
/// <param name="simulationType">The type of simulation.</param>
39-
void TrackSimulationStarted(Guid simulationId, string simulationType);
39+
/// <param name="waitForTransmission">If true, blocks until telemetry transmission completes. Use for CPU-intensive simulations.</param>
40+
void TrackSimulationStarted(Guid simulationId, string simulationType, bool waitForTransmission = false);
4041

4142
/// <summary>
4243
/// Tracks a SimulationEnded event in Application Insights.
@@ -101,9 +102,9 @@ public IDisposable SetContext(Guid simulationId, string simulationType, bool tra
101102
}
102103

103104
/// <inheritdoc />
104-
public void TrackSimulationStarted(Guid simulationId, string simulationType)
105+
public void TrackSimulationStarted(Guid simulationId, string simulationType, bool waitForTransmission = false)
105106
{
106-
TrackSimulationEvent("SimulationStarted", simulationId, simulationType);
107+
TrackSimulationEvent("SimulationStarted", simulationId, simulationType, waitForTransmission);
107108
}
108109

109110
/// <inheritdoc />
@@ -115,7 +116,11 @@ public void TrackSimulationEnded(Guid simulationId, string simulationType)
115116
/// <summary>
116117
/// Tracks a simulation event in Application Insights.
117118
/// </summary>
118-
internal void TrackSimulationEvent(string eventName, Guid simulationId, string simulationType)
119+
/// <param name="eventName">The event name (SimulationStarted/SimulationEnded).</param>
120+
/// <param name="simulationId">The simulation ID.</param>
121+
/// <param name="simulationType">The simulation type.</param>
122+
/// <param name="waitForTransmission">If true, blocks until transmission completes (use for CPU-intensive simulations).</param>
123+
internal void TrackSimulationEvent(string eventName, Guid simulationId, string simulationType, bool waitForTransmission = false)
119124
{
120125
_logger.LogInformation(
121126
"Tracking App Insights event: {EventName} for simulation {SimulationId} ({SimulationType})",
@@ -128,7 +133,7 @@ internal void TrackSimulationEvent(string eventName, Guid simulationId, string s
128133

129134
if (telemetryClient == null)
130135
{
131-
_logger.LogDebug("TelemetryClient not available (App Insights not configured), skipping event tracking");
136+
_logger.LogWarning("TelemetryClient not available (App Insights not configured), skipping event tracking");
132137
return;
133138
}
134139

@@ -140,9 +145,17 @@ internal void TrackSimulationEvent(string eventName, Guid simulationId, string s
140145

141146
telemetryClient.TrackEvent(eventName, properties);
142147

143-
// Flush to ensure event is sent immediately (important for short-lived simulations)
148+
// Flush to ensure event is sent
144149
telemetryClient.Flush();
145150

151+
// For CPU-intensive operations, wait to ensure transmission completes
152+
// before background threads saturate all cores
153+
if (waitForTransmission)
154+
{
155+
_logger.LogDebug("Waiting for telemetry transmission to complete...");
156+
Thread.Sleep(1000); // Give network I/O time to complete
157+
}
158+
146159
_logger.LogDebug("Successfully tracked and flushed event {EventName}", eventName);
147160
}
148161
catch (Exception ex)

0 commit comments

Comments
 (0)