/
HttpWithTimeoutExample.cs
43 lines (34 loc) · 1.39 KB
/
HttpWithTimeoutExample.cs
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
using NBomber.CSharp;
using NBomber.Http;
using NBomber.Http.CSharp;
using NBomber.Plugins.Network.Ping;
namespace Demo.HTTP;
public class HttpWithTimeoutExample
{
public void Run()
{
// Docs: https://nbomber.com/docs/nbomber/timeouts
using var httpClient = new HttpClient();
var scenario = Scenario.Create("http_scenario", async context =>
{
using var timeout = new CancellationTokenSource();
timeout.CancelAfter(50); // the operation will be canceled after 50 ms
var request =
Http.CreateRequest("GET", "https://nbomber.com")
.WithHeader("Content-Type", "application/json");
//.WithBody(new StringContent("{ some JSON }", Encoding.UTF8, "application/json"));
var clientArgs = HttpClientArgs.Create(timeout.Token);
var response = await Http.Send(httpClient, clientArgs, request);
return response;
})
.WithoutWarmUp()
.WithLoadSimulations(Simulation.Inject(rate: 5, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromSeconds(30)));
NBomberRunner
.RegisterScenarios(scenario)
.WithWorkerPlugins(
new PingPlugin(PingPluginConfig.CreateDefault("nbomber.com")),
new HttpMetricsPlugin(new [] { HttpVersion.Version1 })
)
.Run();
}
}