Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 22565d7

Browse files
committed
Add PerfUtils for measuring actions within a specified time
1 parent 856e449 commit 22565d7

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed

src/ServiceStack.Text/PerfUtils.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using System;
2+
using System.Diagnostics;
3+
4+
namespace ServiceStack.Text
5+
{
6+
public class PerfUtils
7+
{
8+
/// <summary>
9+
/// Runs an action for a minimum of runForMs
10+
/// </summary>
11+
/// <param name="fn">What to run</param>
12+
/// <param name="runForMs">Minimum ms to run for</param>
13+
/// <returns>time elapsed in micro seconds</returns>
14+
public static double MeasureFor(Action fn, int runForMs)
15+
{
16+
int iter = 0;
17+
var watch = new Stopwatch();
18+
watch.Start();
19+
long elapsed = 0;
20+
while (elapsed < runForMs)
21+
{
22+
fn();
23+
elapsed = watch.ElapsedMilliseconds;
24+
iter++;
25+
}
26+
27+
return 1000.0 * elapsed / iter;
28+
}
29+
30+
/// <summary>
31+
/// Returns average microseconds an action takes when run for the specified runForMs
32+
/// </summary>
33+
/// <param name="fn">What to run</param>
34+
/// <param name="times">How many times to run for each iteration</param>
35+
/// <param name="runForMs">Minimum ms to run for</param>
36+
/// <param name="setup"></param>
37+
/// <param name="warmup"></param>
38+
/// <param name="teardown"></param>
39+
/// <returns></returns>
40+
public static double Measure(Action fn,
41+
int times = 1,
42+
int runForMs = 2000,
43+
Action setup = null,
44+
Action warmup = null,
45+
Action teardown = null)
46+
{
47+
if (setup != null)
48+
setup();
49+
50+
// Warmup for at least 100ms. Discard result.
51+
if (warmup == null)
52+
warmup = fn;
53+
54+
MeasureFor(() => warmup(), 100);
55+
56+
// Run the benchmark for at least 2000ms.
57+
double result = MeasureFor(() =>
58+
{
59+
for (var i = 0; i < times; i++)
60+
fn();
61+
}, runForMs);
62+
63+
if (teardown != null)
64+
teardown();
65+
66+
return result;
67+
}
68+
}
69+
}

src/ServiceStack.Text/ServiceStack.Text.PCL.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@
195195
<Compile Include="PclExport.Net40.cs" />
196196
<Compile Include="PathUtils.cs" />
197197
<Compile Include="PclExport.cs" />
198+
<Compile Include="PerfUtils.cs" />
198199
<Compile Include="Properties\AssemblyInfo.cs" />
199200
<Compile Include="QueryStringSerializer.cs">
200201
<SubType>Code</SubType>

src/ServiceStack.Text/ServiceStack.Text.SL5.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@
218218
<Compile Include="PclExport.Net40.cs" />
219219
<Compile Include="PathUtils.cs" />
220220
<Compile Include="PclExport.cs" />
221+
<Compile Include="PerfUtils.cs" />
221222
<Compile Include="Properties\AssemblyInfo.cs" />
222223
<Compile Include="QueryStringSerializer.cs">
223224
<SubType>Code</SubType>

src/ServiceStack.Text/ServiceStack.Text.Signed.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@
250250
<Compile Include="PclExport.Net40.cs" />
251251
<Compile Include="PathUtils.cs" />
252252
<Compile Include="PclExport.cs" />
253+
<Compile Include="PerfUtils.cs" />
253254
<Compile Include="Properties\AssemblyInfo.cs" />
254255
<Compile Include="QueryStringSerializer.cs">
255256
<SubType>Code</SubType>

src/ServiceStack.Text/ServiceStack.Text.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
<Compile Include="PclExport.Net40.cs" />
245245
<Compile Include="PathUtils.cs" />
246246
<Compile Include="PclExport.cs" />
247+
<Compile Include="PerfUtils.cs" />
247248
<Compile Include="Properties\AssemblyInfo.cs" />
248249
<Compile Include="QueryStringSerializer.cs">
249250
<SubType>Code</SubType>

0 commit comments

Comments
 (0)