Skip to content

Commit 506ee52

Browse files
authored
[Perf] Print CPU and Memory metrics (Azure#24302)
- Format percentages as numbers to ensure consistent formatting across cultures
1 parent a806249 commit 506ee52

File tree

1 file changed

+36
-4
lines changed

1 file changed

+36
-4
lines changed

common/Perf/Azure.Test.Perf/PerfProgram.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace Azure.Test.Perf
1919
{
2020
public static class PerfProgram
2121
{
22+
private const int BYTES_PER_MEGABYTE = 1024 * 1024;
23+
2224
private static int[] _completedOperations;
2325
private static TimeSpan[] _lastCompletionTimes;
2426
private static List<TimeSpan>[] _latencies;
@@ -330,20 +332,46 @@ private static async Task RunTestsAsync(IPerfTest[] tests, PerfOptions options,
330332
using var testCts = new CancellationTokenSource(duration);
331333
var cancellationToken = testCts.Token;
332334

335+
var cpuStopwatch = Stopwatch.StartNew();
336+
TimeSpan lastCpuElapsed = default;
337+
var startCpuTime = Process.GetCurrentProcess().TotalProcessorTime;
338+
var lastCpuTime = Process.GetCurrentProcess().TotalProcessorTime;
339+
333340
var lastCompleted = 0;
334341

335342
using var progressStatusCts = new CancellationTokenSource();
336343
var progressStatusThread = PerfStressUtilities.PrintStatus(
337344
$"=== {title} ===" + Environment.NewLine +
338-
"Current\t\tTotal\t\tAverage",
345+
$"{"Current",11} {"Total",15} {"Average",14} {"CPU",7} {"WorkingSet",10} {"PrivateMemory",13}",
339346
() =>
340347
{
341348
var totalCompleted = CompletedOperations;
342349
var currentCompleted = totalCompleted - lastCompleted;
343350
var averageCompleted = OperationsPerSecond;
344351

345352
lastCompleted = totalCompleted;
346-
return $"{currentCompleted}\t\t{totalCompleted}\t\t{averageCompleted:F2}";
353+
354+
var process = Process.GetCurrentProcess();
355+
356+
var cpuElapsed = cpuStopwatch.Elapsed;
357+
var cpuTime = process.TotalProcessorTime;
358+
var currentCpuElapsed = (cpuElapsed - lastCpuElapsed).TotalMilliseconds;
359+
var currentCpuTime = (cpuTime - lastCpuTime).TotalMilliseconds;
360+
var cpuPercentage = (currentCpuTime / currentCpuElapsed) / Environment.ProcessorCount;
361+
lastCpuElapsed = cpuElapsed;
362+
lastCpuTime = cpuTime;
363+
364+
var privateMemoryMB = ((double)process.PrivateMemorySize64) / (BYTES_PER_MEGABYTE);
365+
var workingSetMB = ((double)process.WorkingSet64) / (BYTES_PER_MEGABYTE);
366+
367+
// Max Widths
368+
// Current: NNN,NNN,NNN (11)
369+
// Total: NNN,NNN,NNN,NNN (15)
370+
// Average: NNN,NNN,NNN.NN (14)
371+
// CPU: NNN.NN% (7)
372+
// Memory: NNN,NNN.NN (10)
373+
return $"{currentCompleted,11:N0} {totalCompleted,15:N0} {averageCompleted,14:N2} {cpuPercentage * 100,6:N2}% " +
374+
$"{workingSetMB,10:N2}M {privateMemoryMB,13:N2}M";
347375
},
348376
newLine: true,
349377
progressStatusCts.Token,
@@ -400,8 +428,12 @@ private static async Task RunTestsAsync(IPerfTest[] tests, PerfOptions options,
400428
var secondsPerOperation = 1 / operationsPerSecond;
401429
var weightedAverageSeconds = totalOperations / operationsPerSecond;
402430

431+
var cpuElapsed = cpuStopwatch.Elapsed.TotalMilliseconds;
432+
var cpuTime = (Process.GetCurrentProcess().TotalProcessorTime - startCpuTime).TotalMilliseconds;
433+
var cpuPercentage = (cpuTime / cpuElapsed) / Environment.ProcessorCount;
434+
403435
Console.WriteLine($"Completed {totalOperations:N0} operations in a weighted-average of {weightedAverageSeconds:N2}s " +
404-
$"({operationsPerSecond:N2} ops/s, {secondsPerOperation:N3} s/op)");
436+
$"({operationsPerSecond:N2} ops/s, {secondsPerOperation:N3} s/op, {cpuPercentage * 100:N2}% CPU)");
405437
Console.WriteLine();
406438

407439
if (latency)
@@ -448,7 +480,7 @@ private static void PrintLatencies(string header, List<TimeSpan>[] latencies)
448480
var percentiles = new double[] { 0.5, 0.75, 0.9, 0.99, 0.999, 0.9999, 0.99999, 1.0 };
449481
foreach (var percentile in percentiles)
450482
{
451-
Console.WriteLine($"{percentile,8:P3}\t{sortedLatencies[(int)(sortedLatencies.Length * percentile) - 1].TotalMilliseconds:N2}ms");
483+
Console.WriteLine($"{percentile * 100,7:N3}% {sortedLatencies[(int)(sortedLatencies.Length * percentile) - 1].TotalMilliseconds,8:N2}ms");
452484
}
453485
Console.WriteLine();
454486
}

0 commit comments

Comments
 (0)