Skip to content

Commit

Permalink
added ManualCluster Example
Browse files Browse the repository at this point in the history
  • Loading branch information
AntyaDev committed Oct 23, 2023
1 parent c0c85b6 commit 83023e3
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 22 deletions.
36 changes: 23 additions & 13 deletions examples/Demo/Cluster/AutoCluster/AutoClusterExample.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NBomber.CSharp;
using NBomber.Contracts;
using NBomber.CSharp;
using NBomber.Http;
using NBomber.Http.CSharp;
using NBomber.Plugins.Network.Ping;
Expand All @@ -8,31 +9,40 @@ namespace Demo.Cluster.AutoCluster;
public class AutoClusterExample
{
public void Run()
{
var scenario = BuildScenario();
StartNode(scenario);
}

private void StartNode(ScenarioProps scenario)
{
NBomberRunner
.RegisterScenarios(scenario)
.WithWorkerPlugins(
new PingPlugin(PingPluginConfig.CreateDefault("nbomber.com")),
new HttpMetricsPlugin(new [] { HttpVersion.Version1 })
)
.LoadConfig("Cluster/AutoCluster/autocluster-config.json") // you can use: --config=Cluster/ManualCluster/manual-cluster-config.json
.EnableLocalDevCluster(true) // you can use: --cluster-local-dev=true
.Run(); // more info about available CLI args: https://nbomber.com/docs/getting-started/cli/
}

private ScenarioProps BuildScenario()
{
using var httpClient = new HttpClient();

var scenario = Scenario.Create("http_scenario", async context =>
return Scenario.Create("http_scenario", async context =>
{
var request =
Http.CreateRequest("GET", "https://nbomber.com")
.WithHeader("Content-Type", "application/json");
// .WithBody(new StringContent("{ some JSON }", Encoding.UTF8, "application/json"));
// .WithBody(new StringContent("{ some JSON }", Encoding.UTF8, "application/json"));

var response = await Http.Send(httpClient, request);

return response;
})
.WithoutWarmUp()
.WithLoadSimulations(Simulation.Inject(rate: 10, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromSeconds(30)));

NBomberRunner
.RegisterScenarios(scenario)
.WithWorkerPlugins(
new PingPlugin(PingPluginConfig.CreateDefault("nbomber.com")),
new HttpMetricsPlugin(new [] { HttpVersion.Version1 })
)
.LoadConfig("Cluster/AutoCluster/autocluster-config.json")
.EnableLocalDevCluster(true)
.Run();
}
}
2 changes: 0 additions & 2 deletions examples/Demo/Cluster/AutoCluster/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ In order to run that demo you should spin-up NATS message broker via docker-comp
docker compose up
```

After this you need start 2 instances of NBomber.

Useful links:
- [Cluster overview](https://nbomber.com/docs/cluster/overview)
- [How to run cluster](https://nbomber.com/docs/cluster/run-cluster)
Expand Down
2 changes: 1 addition & 1 deletion examples/Demo/Cluster/AutoCluster/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
services:

nats:
image: "nats:2.9.6"
image: "nats:2.9.9"
command: --js
ports:
- "8222:8222"
Expand Down
49 changes: 49 additions & 0 deletions examples/Demo/Cluster/ManualCluster/ManualClusterExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using NBomber.Contracts;
using NBomber.Contracts.Stats;
using NBomber.CSharp;
using NBomber.Http;
using NBomber.Http.CSharp;
using NBomber.Plugins.Network.Ping;

namespace Demo.Cluster.ManualCluster;

public class ManualClusterExample
{
public void Run(string[] args)
{
var scenario = BuildScenario();
StartNode(scenario, args);
}

private void StartNode(ScenarioProps scenario, string[] args)
{
NBomberRunner
.RegisterScenarios(scenario)
.WithWorkerPlugins(
new PingPlugin(PingPluginConfig.CreateDefault("nbomber.com")),
new HttpMetricsPlugin(new [] { HttpVersion.Version1 })
)
.LoadConfig("Cluster/ManualCluster/manual-cluster-config.json") // you can use: --config=Cluster/ManualCluster/manual-cluster-config.json
.EnableLocalDevCluster(true) // you can use: --cluster-local-dev=true
.Run(args); // more info about available CLI args: https://nbomber.com/docs/getting-started/cli/
}

private ScenarioProps BuildScenario()
{
using var httpClient = new HttpClient();

return Scenario.Create("http_scenario", async context =>
{
var request =
Http.CreateRequest("GET", "https://nbomber.com")
.WithHeader("Content-Type", "application/json");
// .WithBody(new StringContent("{ some JSON }", Encoding.UTF8, "application/json"));

var response = await Http.Send(httpClient, request);

return response;
})
.WithoutWarmUp()
.WithLoadSimulations(Simulation.Inject(rate: 10, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromSeconds(30)));
}
}
9 changes: 9 additions & 0 deletions examples/Demo/Cluster/ManualCluster/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "3.4"
services:

nats:
image: "nats:2.9.9"
command: --js
ports:
- "8222:8222"
- "4222:4222"
23 changes: 23 additions & 0 deletions examples/Demo/Cluster/ManualCluster/manual-cluster-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"TestSuite": "my test suite",
"TestName": "my test",

"ClusterSettings": {

"ManualCluster": {
"ClusterId": "test_cluster",
"NATSServerURL": "nats://localhost",

"Coordinator": {
"TargetScenarios": ["http_scenario"]
},

"Agent": {
"AgentGroups": [{"AgentGroup": "1", "TargetScenarios": ["http_scenario"]}],
"AgentsCount": 1
}

}

}
}
7 changes: 5 additions & 2 deletions examples/Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
<None Update="Cluster\AutoCluster\autocluster-config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Features\RealtimeReporting\InfluxDB\influx_v2\infra-config.json">
<None Update="Cluster\ManualCluster\manual-cluster-config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Features\RealtimeReporting\InfluxDB\influx_v2\infra-config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

<ItemGroup>
Expand All @@ -34,7 +37,7 @@
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Dapper.Contrib" Version="2.0.78" />
<PackageReference Include="LiteDB" Version="5.0.15" />
<PackageReference Include="NBomber" Version="5.2.2" />
<PackageReference Include="NBomber" Version="5.3.0" />
<PackageReference Include="NBomber.Data" Version="5.0.0" />
<PackageReference Include="NBomber.Http" Version="5.0.0" />
<PackageReference Include="NBomber.Sinks.InfluxDB" Version="5.0.1" />
Expand Down
6 changes: 2 additions & 4 deletions examples/Demo/HelloWorld/HelloWorldExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ public void Run()
// you can define and execute any logic here,
// for example: send http request, SQL query etc
// NBomber will measure how much time it takes to execute your logic
await Task.Delay(1000);
await Task.Delay(500);

return Response.Ok();
})
.WithoutWarmUp()
.WithLoadSimulations(
Simulation.RampingInject(rate: 150, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromMinutes(1)), // rump-up to rate 150
Simulation.Inject(rate: 150, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromSeconds(30)), // keep injecting with rate 150
Simulation.RampingInject(rate: 0, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromMinutes(1)) // rump-down to rate 0
Simulation.Inject(rate: 1, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromSeconds(30)) // keep injecting with rate 150
);

NBomberRunner
Expand Down
15 changes: 15 additions & 0 deletions examples/Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Demo.Cluster.AutoCluster;
using Demo.Cluster.ManualCluster;
using Demo.DB.LiteDB;
using Demo.DB.Redis;
using Demo.DB.SQLiteDB;
Expand Down Expand Up @@ -78,5 +79,19 @@
// ---------------------
// ----- Cluster -------
// ---------------------

// ----- Auto Cluster -------
// in order to run this example you should start 2 instances of NBomber,
// for this, you need to run this NBomber application twice

// new AutoClusterExample().Run();
// new AutoClusterExample().Run();

// ----- Manual Cluster -------
// in order to run this example you should start 2 instances of NBomber,
// - first instance will act as Coordinator
// - second instance will act as Agent

// new ManualClusterExample().Run(args: new [] { "--cluster-node-type=coordinator" });
// new ManualClusterExample().Run(args: new [] { "--cluster-node-type=agent", "--cluster-agent-group=1" });

0 comments on commit 83023e3

Please sign in to comment.