Skip to content

Commit 5dd2b7e

Browse files
authored
816 add example with dotnet script (#834)
* Add the HelloWorld script file * Add the HttpSendJson script file
1 parent 940f6a0 commit 5dd2b7e

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

examples/Demo/Demo.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133

134134
<ItemGroup>
135135
<Folder Include="DB\Redis\" />
136+
<Folder Include="Scripting\" />
136137
</ItemGroup>
137138

138139
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#r "nuget: NBomber, 6.0.2"
2+
3+
using NBomber.CSharp;
4+
5+
var scenario = Scenario.Create("hello_world_scenario", async context =>
6+
{
7+
// you can define and execute any logic here,
8+
// for example: send http request, SQL query etc
9+
// NBomber will measure how much time it takes to execute your logic
10+
await Task.Delay(500);
11+
12+
return Response.Ok();
13+
})
14+
.WithoutWarmUp()
15+
.WithLoadSimulations(
16+
Simulation.Inject(rate: 150, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromSeconds(30))
17+
);
18+
19+
NBomberRunner
20+
.RegisterScenarios(scenario)
21+
.Run();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#r "nuget: NBomber, 6.0.2"
2+
#r "nuget: NBomber.Http, 6.0.2"
3+
#r "nuget: System.Net.Http, 4.3.4"
4+
5+
using System.Text.Json;
6+
using NBomber.CSharp;
7+
using NBomber.Http;
8+
using NBomber.Http.CSharp;
9+
using NBomber.Plugins.Network.Ping;
10+
using System.Net.Http;
11+
12+
public class UserData
13+
{
14+
public int UserId { get; set; }
15+
public int Id { get; set; }
16+
public string Title { get; set; }
17+
public bool Completed { get; set; }
18+
}
19+
20+
var httpClient = new HttpClient();
21+
22+
var scenario = Scenario.Create("http_scenario", async context =>
23+
{
24+
// example of WithJsonBody
25+
var user = new UserData { UserId = 1, Title = "anton" };
26+
27+
var request1 =
28+
Http.CreateRequest("GET", "https://nbomber.com")
29+
.WithJsonBody(user);
30+
31+
var response1 = await Http.Send(httpClient, request1);
32+
33+
// example of typed Send<T> that deserialize JSON response
34+
var request2 =
35+
Http.CreateRequest("GET", "https://jsonplaceholder.typicode.com/todos/1")
36+
.WithHeader("Content-Type", "application/json");
37+
38+
var response2 = await Http.Send<UserData>(httpClient, request2);
39+
40+
var userData = response2.Payload.Value.Data;
41+
var title = userData.Title;
42+
var userId = userData.UserId;
43+
44+
return response2;
45+
})
46+
.WithoutWarmUp()
47+
.WithLoadSimulations(Simulation.Inject(rate: 5, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromSeconds(30)));
48+
49+
NBomberRunner
50+
.RegisterScenarios(scenario)
51+
.WithWorkerPlugins(
52+
new PingPlugin(PingPluginConfig.CreateDefault("nbomber.com")),
53+
new HttpMetricsPlugin(new[] { HttpVersion.Version1 })
54+
)
55+
.Run();

0 commit comments

Comments
 (0)