Skip to content

Commit 2efc8d7

Browse files
committed
Add handling Scenario Finish event #829
1 parent 5dd2b7e commit 2efc8d7

21 files changed

+157
-149
lines changed

examples/Demo/AMQP/ClientPool/ClientPoolAmqpExample.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ public class CustomScenarioSettings
1717

1818
public class ClientPoolAmqpExample
1919
{
20+
// For this example, please spin up local RabbitMQ via docker-compose.yml located in the AMQP folder.
21+
2022
public void Run()
2123
{
2224
var clientPool = new ClientPool<AmqpClient>();
23-
var message = Array.Empty<byte>();
25+
byte[] message = [];
2426
var usePersistence = false;
2527

2628
var scenario = Scenario.Create("amqp_scenario", async ctx =>
@@ -38,14 +40,12 @@ public void Run()
3840

3941
var receive = await Step.Run("receive", ctx, async () =>
4042
{
41-
var response = await client.Receive().AsTask();
43+
var response = await client.Receive(ctx.ScenarioCancellationToken);
4244
return response;
4345
});
4446

4547
return Response.Ok();
4648
})
47-
.WithWarmUpDuration(TimeSpan.FromSeconds(3))
48-
.WithLoadSimulations(Simulation.KeepConstant(copies: 1, during: TimeSpan.FromSeconds(30)))
4949
.WithInit(async context =>
5050
{
5151
var config = context.CustomSettings.Get<CustomScenarioSettings>();
@@ -71,11 +71,13 @@ public void Run()
7171
}
7272
else
7373
throw new Exception("client can't connect to the AMQP broker");
74+
75+
await Task.Delay(10);
7476
}
7577
})
7678
.WithClean(ctx =>
7779
{
78-
clientPool.DisposeClients(client => client.Disconnect());
80+
clientPool.DisposeClients(client => client.Dispose());
7981
return Task.CompletedTask;
8082
});
8183

examples/Demo/AMQP/ClientPool/config.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"TestSuite": "example",
3-
"TestName": "custom_settings",
2+
"TestSuite": "AMQP",
3+
"TestName": "client_pool",
44

55
"TargetScenarios": [ "amqp_scenario" ],
66

@@ -12,12 +12,12 @@
1212
"WarmUpDuration": "00:00:03",
1313

1414
"LoadSimulationsSettings": [
15-
{ "KeepConstant": [ 100, "00:00:20" ] }
15+
{ "KeepConstant": [ 10, "00:00:30" ] }
1616
],
1717

1818
"CustomSettings": {
1919
"AmqpServerUrl": "localhost",
20-
"ClientCount": 100,
20+
"ClientCount": 10,
2121
"MsgSizeBytes": 200,
2222
"UsePersistence": false
2323
}

examples/Demo/AMQP/IndependentActors/AmqpPublishScenario.cs

+4-13
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,13 @@
77

88
namespace Demo.AMQP.IndependentActors;
99

10-
public class CustomPublishScenarioSettings
11-
{
12-
public string AmqpServerUrl { get; set; }
13-
public int MsgSizeBytes { get; set; }
14-
}
15-
1610
public class AmqpPublishScenario
1711
{
12+
// For this example, please spin up local RabbitMQ via docker-compose.yml located in the AMQP folder.
13+
1814
public ScenarioProps Create()
1915
{
20-
CustomPublishScenarioSettings config = null;
21-
byte[] payload = null;
16+
byte[] payload = [];
2217
AmqpClient amqpClient = null;
2318

2419
return Scenario.Create("publish_scenario", async ctx =>
@@ -40,13 +35,9 @@ public ScenarioProps Create()
4035

4136
return Response.Ok();
4237
})
43-
.WithoutWarmUp()
44-
.WithLoadSimulations(
45-
Simulation.Inject(100, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(30))
46-
)
4738
.WithInit(async ctx =>
4839
{
49-
config = ctx.CustomSettings.Get<CustomPublishScenarioSettings>();
40+
var config = ctx.GlobalCustomSettings.Get<AmqpCustomSettings>();
5041
payload = Data.GenerateRandomBytes(config.MsgSizeBytes);
5142

5243
var factory = new ConnectionFactory { HostName = config.AmqpServerUrl };

examples/Demo/AMQP/IndependentActors/IndependentActorsAmqpExample.cs

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
namespace Demo.AMQP.IndependentActors;
44

5+
public class AmqpCustomSettings
6+
{
7+
public string AmqpServerUrl { get; set; }
8+
public int MsgSizeBytes { get; set; }
9+
}
10+
511
public class IndependentActorsAmqpExample
612
{
713
public void Run()
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"TestSuite": "example",
3-
"TestName": "custom_settings",
2+
"TestSuite": "AMQP",
3+
"TestName": "independent_actors",
44

55
"TargetScenarios": [ "publish_scenario", "consume_scenario" ],
66

@@ -12,13 +12,8 @@
1212
"WarmUpDuration": "00:00:03",
1313

1414
"LoadSimulationsSettings": [
15-
{ "Inject": [ 100, "00:00:01", "00:00:30" ] }
16-
],
17-
18-
"CustomSettings": {
19-
"AmqpServerUrl": "localhost",
20-
"MsgSizeBytes": 200
21-
}
15+
{ "Inject": [ 10, "00:00:01", "00:00:30" ] }
16+
]
2217
},
2318
{
2419
"ScenarioName": "consume_scenario",
@@ -27,12 +22,13 @@
2722

2823
"LoadSimulationsSettings": [
2924
{ "KeepConstant": [ 1, "00:00:30" ] }
30-
],
31-
32-
"CustomSettings": {
33-
"AmqpServerUrl": "localhost"
34-
}
25+
]
3526
}
36-
]
27+
],
28+
29+
"GlobalCustomSettings": {
30+
"AmqpServerUrl": "localhost",
31+
"MsgSizeBytes": 200
32+
}
3733
}
3834
}

examples/Demo/AMQP/IndependentActors/AmqpConsumeScenario.cs

+2-8
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,10 @@
66

77
namespace Demo.AMQP.IndependentActors;
88

9-
public class CustomConsumeScenarioSettings
10-
{
11-
public string AmqpServerUrl { get; set; }
12-
}
13-
149
public class AmqpConsumeScenario
1510
{
1611
public ScenarioProps Create()
1712
{
18-
CustomConsumeScenarioSettings config = null;
1913
AmqpClient amqpClient = null;
2014

2115
return Scenario.Create("consume_scenario", async ctx =>
@@ -26,15 +20,15 @@ public ScenarioProps Create()
2620
var timestampMs = (long)message.Payload.Value.BasicProperties.Headers["timestamp"];
2721
var latency = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() - timestampMs;
2822

29-
return Response.Ok(customLatencyMs: latency);
23+
return Response.Ok(customLatencyMs: latency, sizeBytes: message.SizeBytes);
3024
})
3125
.WithoutWarmUp()
3226
.WithLoadSimulations(
3327
Simulation.KeepConstant(1, TimeSpan.FromSeconds(30))
3428
)
3529
.WithInit(async ctx =>
3630
{
37-
config = ctx.CustomSettings.Get<CustomConsumeScenarioSettings>();
31+
var config = ctx.GlobalCustomSettings.Get<AmqpCustomSettings>();
3832

3933
var factory = new ConnectionFactory { HostName = config.AmqpServerUrl };
4034
var connection = await factory.CreateConnectionAsync();

examples/Demo/AMQP/PingPongAmqpTest.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,30 @@ namespace Demo.AMQP;
77

88
public class PingPongAmqpTest
99
{
10+
// For this example, please spin up local RabbitMQ via docker-compose.yml located in the AMQP folder.
11+
1012
public void Run()
1113
{
1214
var payload = Data.GenerateRandomBytes(200);
1315
var factory = new ConnectionFactory { HostName = "localhost" };
1416

1517
var scenario = Scenario.Create("ping_pong_amqp_scenario", async ctx =>
1618
{
19+
AmqpClient amqpClient = null;
20+
1721
var connect = await Step.Run("connect", ctx, async () =>
1822
{
1923
var connection = await factory.CreateConnectionAsync();
2024
var channel = await connection.CreateChannelAsync();
2125

22-
var amqpClient = new AmqpClient(channel);
23-
ctx.Data["amqpClient"] = amqpClient;
26+
amqpClient = new AmqpClient(channel);
2427

2528
var scenarioInstanceId = ctx.ScenarioInfo.InstanceId;
2629

2730
return await amqpClient.Connect(exchange: "myExchange", exchangeType: ExchangeType.Direct, queue: scenarioInstanceId,
2831
routingKey: scenarioInstanceId);
2932
});
3033

31-
using var amqpClient = (AmqpClient)ctx.Data["amqpClient"];
32-
3334
var subscribe = await Step.Run("subscribe", ctx, async () =>
3435
{
3536
var queueName = ctx.ScenarioInfo.InstanceId;

examples/Demo/Demo.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,15 @@
8383
<PackageReference Include="Grpc.Net.Client" Version="2.67.0" />
8484
<PackageReference Include="LiteDB" Version="5.0.15" />
8585

86-
<PackageReference Include="NBomber" Version="6.0.2" />
86+
<PackageReference Include="NBomber" Version="6.1.0-beta.1" />
8787

88-
<PackageReference Include="NBomber.AMQP" Version="0.1.2" />
88+
<PackageReference Include="NBomber.AMQP" Version="0.1.3-beta.0" />
8989
<PackageReference Include="NBomber.Data" Version="6.0.0" />
9090
<PackageReference Include="NBomber.Http" Version="6.0.2" />
91-
<PackageReference Include="NBomber.MQTT" Version="0.4.0" />
91+
<PackageReference Include="NBomber.MQTT" Version="0.4.1-beta.0" />
9292
<PackageReference Include="NBomber.Sinks.Timescale" Version="0.7.0" />
9393
<PackageReference Include="NBomber.WebBrowser" Version="0.2.0" />
94-
<PackageReference Include="NBomber.WebSockets" Version="0.1.0" />
94+
<PackageReference Include="NBomber.WebSockets" Version="0.1.1-beta.0" />
9595
<PackageReference Include="NBomber.Sinks.InfluxDB" Version="6.0.0" />
9696

9797
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="9.0.0" />

examples/Demo/MQTT/ClientPool/ClientPoolMqttExample.cs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.Extensions.Configuration;
1+
using Microsoft.Extensions.Configuration;
22
using MQTTnet;
33
using NBomber;
44
using NBomber.CSharp;
@@ -16,10 +16,12 @@ public class CustomScenarioSettings
1616

1717
public class ClientPoolMqttExample
1818
{
19+
// For this example, please spin up local MQTT broker via docker-compose.yml located in the MQTT folder.
20+
1921
public void Run()
2022
{
2123
var clientPool = new ClientPool<MqttClient>();
22-
var message = Data.GenerateRandomBytes(200);
24+
byte[] payload = [];
2325

2426
var scenario = Scenario.Create("mqtt_scenario", async ctx =>
2527
{
@@ -30,23 +32,24 @@ public void Run()
3032
var topic = $"/clients/{ctx.ScenarioInfo.InstanceId}";
3133
var msg = new MqttApplicationMessageBuilder()
3234
.WithTopic(topic)
33-
.WithPayload(message)
35+
.WithPayload(payload)
3436
.Build();
3537

3638
return await mqttClient.Publish(msg);
3739
});
3840

3941
var receive = await Step.Run("receive", ctx, async () =>
40-
await mqttClient.Receive(ctx.ScenarioCancellationToken));
42+
{
43+
var response = await mqttClient.Receive(ctx.ScenarioCancellationToken);
44+
return response;
45+
});
4146

4247
return Response.Ok();
4348
})
44-
.WithWarmUpDuration(TimeSpan.FromSeconds(3))
45-
.WithLoadSimulations(Simulation.KeepConstant(copies: 1, during: TimeSpan.FromSeconds(30)))
4649
.WithInit(async context =>
4750
{
4851
var config = context.CustomSettings.Get<CustomScenarioSettings>();
49-
message = Data.GenerateRandomBytes(config.MsgSizeBytes);
52+
payload = Data.GenerateRandomBytes(config.MsgSizeBytes);
5053

5154
for (var i = 0; i < config.ClientCount; i++)
5255
{
@@ -67,11 +70,13 @@ public void Run()
6770
}
6871
else
6972
throw new Exception("client can't connect to the MQTT broker");
73+
74+
await Task.Delay(10);
7075
}
7176
})
7277
.WithClean(ctx =>
7378
{
74-
clientPool.DisposeClients(async client => await client.Disconnect());
79+
clientPool.DisposeClients(client => client.Dispose());
7580
return Task.CompletedTask;
7681
});
7782

examples/Demo/MQTT/ClientPool/config.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"TestSuite": "example",
3-
"TestName": "custom_settings",
2+
"TestSuite": "MQTT",
3+
"TestName": "client_pool",
44

55
"TargetScenarios": ["mqtt_scenario"],
66

examples/Demo/MQTT/IndependentActors/IndependentActorsMqttExample.cs

+8
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
namespace Demo.MQTT.IndependentActors;
44

5+
public class MqttCustomSettings
6+
{
7+
public string MqttServerUrl { get; set; }
8+
public int MsgSizeBytes { get; set; }
9+
}
10+
511
public class IndependentActorsMqttExample
612
{
13+
// For this example, please spin up local MQTT broker via docker-compose.yml located in the MQTT folder.
14+
715
public void Run()
816
{
917
NBomberRunner.RegisterScenarios(

0 commit comments

Comments
 (0)