Skip to content

Commit 377a3c9

Browse files
committed
Add handling Scenario Finish event #829
1 parent 5dd2b7e commit 377a3c9

21 files changed

+163
-162
lines changed

examples/Demo/AMQP/ClientPool/ClientPoolAmqpExample.cs

Lines changed: 7 additions & 5 deletions
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

Lines changed: 5 additions & 5 deletions
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

@@ -9,16 +9,16 @@
99
{
1010
"ScenarioName": "amqp_scenario",
1111

12-
"WarmUpDuration": "00:00:03",
12+
"WarmUpDuration": "00:00:05",
1313

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

1818
"CustomSettings": {
1919
"AmqpServerUrl": "localhost",
2020
"ClientCount": 100,
21-
"MsgSizeBytes": 200,
21+
"MsgSizeBytes": 100,
2222
"UsePersistence": false
2323
}
2424
}

examples/Demo/AMQP/IndependentActors/AmqpPublishScenario.cs

Lines changed: 4 additions & 13 deletions
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

Lines changed: 6 additions & 0 deletions
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()
Lines changed: 13 additions & 17 deletions
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

@@ -9,30 +9,26 @@
99
{
1010
"ScenarioName": "publish_scenario",
1111

12-
"WarmUpDuration": "00:00:03",
12+
"WarmUpDuration": "00:00:05",
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",
2520

26-
"WarmUpDuration": "00:00:03",
21+
"WarmUpDuration": "00:00:05",
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": 100
32+
}
3733
}
3834
}

examples/Demo/AMQP/IndependentActors/AmqpConsumeScenario.cs

Lines changed: 2 additions & 8 deletions
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

Lines changed: 5 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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.2" />
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.2" />
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.3" />
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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using Microsoft.Extensions.Configuration;
1+
using Microsoft.Extensions.Configuration;
22
using MQTTnet;
3+
using MQTTnet.Protocol;
34
using NBomber;
45
using NBomber.CSharp;
56
using NBomber.Data;
@@ -16,10 +17,12 @@ public class CustomScenarioSettings
1617

1718
public class ClientPoolMqttExample
1819
{
20+
// For this example, please spin up local MQTT broker via docker-compose.yml located in the MQTT folder.
21+
1922
public void Run()
2023
{
2124
var clientPool = new ClientPool<MqttClient>();
22-
var message = Data.GenerateRandomBytes(200);
25+
byte[] payload = [];
2326

2427
var scenario = Scenario.Create("mqtt_scenario", async ctx =>
2528
{
@@ -30,30 +33,32 @@ public void Run()
3033
var topic = $"/clients/{ctx.ScenarioInfo.InstanceId}";
3134
var msg = new MqttApplicationMessageBuilder()
3235
.WithTopic(topic)
33-
.WithPayload(message)
36+
.WithPayload(payload)
37+
.WithQualityOfServiceLevel(MqttQualityOfServiceLevel.AtMostOnce)
3438
.Build();
3539

3640
return await mqttClient.Publish(msg);
3741
});
3842

3943
var receive = await Step.Run("receive", ctx, async () =>
40-
await mqttClient.Receive(ctx.ScenarioCancellationToken));
44+
{
45+
var response = await mqttClient.Receive(ctx.ScenarioCancellationToken);
46+
return response;
47+
});
4148

4249
return Response.Ok();
4350
})
44-
.WithWarmUpDuration(TimeSpan.FromSeconds(3))
45-
.WithLoadSimulations(Simulation.KeepConstant(copies: 1, during: TimeSpan.FromSeconds(30)))
4651
.WithInit(async context =>
4752
{
4853
var config = context.CustomSettings.Get<CustomScenarioSettings>();
49-
message = Data.GenerateRandomBytes(config.MsgSizeBytes);
54+
payload = Data.GenerateRandomBytes(config.MsgSizeBytes);
5055

5156
for (var i = 0; i < config.ClientCount; i++)
5257
{
5358
var topic = $"/clients/mqtt_scenario_{i}";
5459
var clientId = $"mqtt_client_{i}";
5560
var options = new MqttClientOptionsBuilder()
56-
.WithWebSocketServer(options => { options.WithUri(config.MqttServerUrl); })
61+
.WithTcpServer(config.MqttServerUrl)
5762
.WithClientId(clientId)
5863
.Build();
5964

@@ -62,16 +67,18 @@ public void Run()
6267

6368
if (!connectResult.IsError)
6469
{
65-
await mqttClient.Subscribe(topic);
70+
await mqttClient.Subscribe(topic, MqttQualityOfServiceLevel.AtMostOnce);
6671
clientPool.AddClient(mqttClient);
6772
}
6873
else
6974
throw new Exception("client can't connect to the MQTT broker");
75+
76+
await Task.Delay(10);
7077
}
7178
})
7279
.WithClean(ctx =>
7380
{
74-
clientPool.DisposeClients(async client => await client.Disconnect());
81+
clientPool.DisposeClients(client => client.Dispose());
7582
return Task.CompletedTask;
7683
});
7784

examples/Demo/MQTT/ClientPool/config.json

Lines changed: 6 additions & 6 deletions
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

@@ -9,16 +9,16 @@
99
{
1010
"ScenarioName": "mqtt_scenario",
1111

12-
"WarmUpDuration": "00:00:03",
12+
"WarmUpDuration": "00:00:05",
1313

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

1818
"CustomSettings": {
19-
"MqttServerUrl": "ws://localhost:8083/mqtt",
19+
"MqttServerUrl": "localhost",
2020
"ClientCount": 100,
21-
"MsgSizeBytes": 200
21+
"MsgSizeBytes": 100
2222
}
2323
}
2424
]

examples/Demo/MQTT/IndependentActors/IndependentActorsMqttExample.cs

Lines changed: 8 additions & 0 deletions
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)