-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathAspNetTest.cs
More file actions
89 lines (81 loc) · 3.91 KB
/
AspNetTest.cs
File metadata and controls
89 lines (81 loc) · 3.91 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using StackExchange.Profiling.Storage;
using System;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace StackExchange.Profiling.Tests
{
public abstract class AspNetTest : BaseTest
{
/// <summary>
/// URL that <see cref="GetRequest"/> and <see cref="GetProfiler"/> will hit.
/// </summary>
public const string DefaultRequestUrl = "http://localhost/Test.aspx";
protected AspNetTest(ITestOutputHelper output) : base(output)
{
ThreadPool.SetMinThreads(50, 50);
// Instance per class, so multiple tests swapping the provider don't cause issues here
// It's not a threading issue of the profiler, but rather tests swapping providers
MiniProfiler.Configure(Options = new MiniProfilerOptions()
{
StopwatchProvider = () => new UnitTestStopwatch(),
Storage = new MemoryCacheStorage(TimeSpan.FromDays(1))
});
}
/// <summary>
/// Returns a simulated HTTP request to <paramref name="url"/>.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="startAndStopProfiler">The start And Stop Profiler.</param>
/// <returns>the request</returns>
public IDisposable GetRequest(string url = DefaultRequestUrl, bool startAndStopProfiler = true)
{
var result = new Subtext.TestLibrary.HttpSimulator();
result.SimulateRequest(new Uri(url));
if (startAndStopProfiler)
{
var mp = Options.StartProfiler();
result.OnBeforeDispose += () => mp?.Stop();
}
return result;
}
/// <summary>
/// Returns a profiler for <paramref name="url"/>. Only child steps will take any time,
/// e.g. when <paramref name="childDepth"/> is 0, the resulting <see cref="MiniProfiler.DurationMilliseconds"/> will be zero.
/// </summary>
/// <param name="url">The URL of the request.</param>
/// <param name="childDepth">number of levels of child steps underneath result's <see cref="MiniProfiler.Root"/>.</param>
/// <param name="stepMs">Amount of time each step will "do work for" in each step.</param>
/// <returns>The generated <see cref="MiniProfiler"/>.</returns>
public MiniProfiler GetProfiler(string url = DefaultRequestUrl, int childDepth = 0, int stepMs = StepTimeMilliseconds)
{
using (GetRequest(url, startAndStopProfiler: false))
{
var result = Options.StartProfiler(url);
Assert.NotNull(result);
AddRecursiveChildren(result, childDepth, stepMs);
result.Stop();
return result;
}
}
/// <summary>
/// Returns a profiler for <paramref name="url"/>. Only child steps will take any time,
/// e.g. when <paramref name="childDepth"/> is 0, the resulting <see cref="MiniProfiler.DurationMilliseconds"/> will be zero.
/// </summary>
/// <param name="url">The URL of the request.</param>
/// <param name="childDepth">number of levels of child steps underneath result's <see cref="MiniProfiler.Root"/></param>
/// <param name="stepMs">Amount of time each step will "do work for" in each step</param>
/// <returns>The generated <see cref="MiniProfiler"/>.</returns>
public async Task<MiniProfiler> GetProfilerAsync(string url = DefaultRequestUrl, int childDepth = 0, int stepMs = StepTimeMilliseconds)
{
using (GetRequest(url, startAndStopProfiler: false))
{
var result = Options.StartProfiler();
Assert.NotNull(result);
AddRecursiveChildren(result, childDepth, stepMs);
await result.StopAsync().ConfigureAwait(false);
return result;
}
}
}
}