onnxruntime/csharp/tools/MauiModelTester/PerfStats.cs
Scott McKay ad90352a68
Add MAUI test app that can be used to test model loading and performance (#16658)
### Description
<!-- Describe your changes. -->
MAUI test app with tooling to add model and generated or provided input
test data.

The app will load the model and validate the output. It can also run a
specified number of iterations to provide basic performance information.

<img width="401" alt="image"
src="https://github.com/microsoft/onnxruntime/assets/979079/daf3af13-fb22-4cbb-9159-486b483a7485">

### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->
Primarily to make it easier to test an arbitrary model on iOS. A MAUI
app allows testing on all platforms.

---------

Co-authored-by: Edward Chen <18449977+edgchen1@users.noreply.github.com>
2023-07-18 08:21:18 +10:00

71 lines
2.5 KiB
C#

namespace MauiModelTester
{
internal class PerfStats
{
internal PerfStats()
{
_runTimes = new List<double>();
}
internal TimeSpan LoadTime { get; set; }
internal TimeSpan WarmupTime { get; set; }
/// <summary>
/// Add TimeSpan for one call to Run.
/// </summary>
/// <param name="runTime">Elapsed time</param>
internal void AddRunTime(TimeSpan runTime)
{
_runTimes.Add(runTime.TotalMilliseconds);
}
internal void ClearRunTimes()
{
_runTimes.Clear();
}
internal List<string> GetRunStatsReport(bool outputRunTimes = false)
{
List<string> lines = new List<string>();
if (_runTimes.Count > 0)
{
// we want unsorted run times if we need to investigate any unexpected latency as that gives a clearer
// picture of when in the iterations the latency occurred.
List<string> runTimesOutput = null;
if (outputRunTimes)
{
runTimesOutput = new List<string>();
runTimesOutput.Add("\nRun times (ms):");
runTimesOutput.Add(string.Join(", ", _runTimes.Select(x => x.ToString("F2"))));
}
_runTimes.Sort();
var totalRunTime = _runTimes.Sum();
lines.Add($"Total time for {_runTimes.Count} iterations: {totalRunTime:F2} ms\n");
lines.Add($"Average run time: {totalRunTime / _runTimes.Count:F2} ms");
lines.Add($"Minimum run time: {_runTimes.Min():F2} ms");
lines.Add($"Maximum run time: {_runTimes.Max():F2} ms\n");
lines.Add($"50th Percentile run time: {_runTimes[(int)(_runTimes.Count * 0.5)]:F2} ms");
lines.Add($"90th Percentile run time: {_runTimes[(int)(_runTimes.Count * 0.9)]:F2} ms");
if (_runTimes.Count >= 100)
{
lines.Add($"95th Percentile run time: {_runTimes[(int)(_runTimes.Count * 0.95)]:F2} ms");
lines.Add($"99th Percentile run time: {_runTimes[(int)(_runTimes.Count * 0.99)]:F2} ms");
}
if (outputRunTimes)
{
lines.AddRange(runTimesOutput);
}
}
return lines;
}
private List<double> _runTimes;
}
}