File: PerfStats.cs

package info (click to toggle)
onnxruntime 1.21.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 333,732 kB
  • sloc: cpp: 3,153,079; python: 179,219; ansic: 109,131; asm: 37,791; cs: 34,424; perl: 13,070; java: 11,047; javascript: 6,330; pascal: 4,126; sh: 3,277; xml: 598; objc: 281; makefile: 59
file content (71 lines) | stat: -rw-r--r-- 2,511 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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;
    }
}