Skip to content

Commit

Permalink
Added ScenarioWeightExample
Browse files Browse the repository at this point in the history
  • Loading branch information
AntyaDev committed Oct 16, 2024
1 parent 0925436 commit 4de03dc
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 37 deletions.
11 changes: 5 additions & 6 deletions examples/Demo/Demo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,15 @@
<PackageReference Include="Bogus" Version="34.0.2" />
<PackageReference Include="Dapper.Contrib" Version="2.0.78" />
<PackageReference Include="LiteDB" Version="5.0.15" />
<PackageReference Include="MathNet.Numerics" Version="5.0.0" />

<PackageReference Include="NBomber" Version="5.7.0" />
<PackageReference Include="NBomber" Version="5.8.0-beta.8" />
<PackageReference Include="NBomber.Data" Version="5.0.0" />
<PackageReference Include="NBomber.Http" Version="5.1.0" />
<PackageReference Include="NBomber.MQTT" Version="0.1.0" />
<PackageReference Include="NBomber.Sinks.Timescale" Version="0.5.1" />
<PackageReference Include="NBomber.Http" Version="5.2.0-beta.0" />
<PackageReference Include="NBomber.MQTT" Version="0.2.0-beta.0" />
<PackageReference Include="NBomber.Sinks.Timescale" Version="0.6.0-beta.5" />
<PackageReference Include="NBomber.WebBrowser" Version="0.1.0" />
<PackageReference Include="NBomber.WebSockets" Version="0.1.0" />
<PackageReference Include="NBomber.Sinks.InfluxDB" Version="5.0.2" />
<PackageReference Include="NBomber.Sinks.InfluxDB" Version="5.1.0-beta.0" />

<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="9.0.0" />
<PackageReference Include="Serilog.Sinks.Grafana.Loki" Version="8.3.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
using MathNet.Numerics.Distributions;
using NBomber.CSharp;
using NBomber.CSharp;

namespace Demo.Features.DynamicWorkload;

public class MultinomialDistributionExample
{
public void Run()
{
var ratios = new[] {0.7, 0.2, 0.1}; // 70%, 20%, 10%
// 70% for read, 20% for write, 10% for delete
var items = new [] { ("read", 70), ("write", 20), ("delete", 10) };

var scenario = Scenario.Create("multinomial_distribution", async context =>
{
int[] result = Multinomial.Sample(context.Random, ratios, 1);
if (result[0] == 1) // 70% for read
var randomItem = context.Random.Choice(items);
switch (randomItem)
{
await Step.Run("read", context, async () =>
{
await Task.Delay(100);
return Response.Ok();
});
}
else if (result[1] == 1) // 20% for write
{
await Step.Run("write", context, async () =>
{
await Task.Delay(100);
return Response.Ok();
});
}
else // 10% for delete
{
await Step.Run("delete", context, async () =>
{
await Task.Delay(100);
return Response.Ok();
});
case "read": // 70% for read
await Step.Run("read", context, async () =>
{
await Task.Delay(100);
return Response.Ok();
});
break;

case "write": // 20% for write
await Step.Run("write", context, async () =>
{
await Task.Delay(100);
return Response.Ok();
});
break;

case "delete": // 10% for delete
await Step.Run("delete", context, async () =>
{
await Task.Delay(100);
return Response.Ok();
});
break;
}

return Response.Ok();
Expand Down
37 changes: 37 additions & 0 deletions examples/Demo/Features/DynamicWorkload/ScenarioWeightExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using NBomber.CSharp;

namespace Demo.Features.DynamicWorkload;

public class ScenarioWeightExample
{
public void Run()
{
var readScenario = Scenario.Create("read_scenario", async context =>
{
await Task.Delay(100);
return Response.Ok();
})
.WithoutWarmUp()
.WithLoadSimulations(
// we use the same LoadSimulation settings
Simulation.Inject(rate: 10, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromSeconds(30))
)
.WithWeight(80); // sets 80%

var writeScenario = Scenario.Create("write_scenario", async context =>
{
await Task.Delay(100);
return Response.Ok();
})
.WithoutWarmUp()
.WithLoadSimulations(
// we use the same LoadSimulation settings
Simulation.Inject(rate: 10, interval: TimeSpan.FromSeconds(1), during: TimeSpan.FromSeconds(30))
)
.WithWeight(20); // sets 20%

NBomberRunner
.RegisterScenarios(readScenario, writeScenario)
.Run();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using MathNet.Numerics.Distributions;
using NBomber.CSharp;
using NBomber.CSharp;

namespace Demo.Features.DynamicWorkload;

Expand All @@ -9,7 +8,7 @@ public void Run()
{
var scenario = Scenario.Create("zipfian_distribution", async context =>
{
var stepNumber = Zipf.Sample(s: 1.3, n: 5, rnd: context.Random);
var stepNumber = context.Random.Zipf(n: 5, s: 1.3);
if (stepNumber == 1)
{
await Step.Run("step_1", context, async () =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public Task Init(IBaseContext context, IConfiguration infraConfig)
return Task.CompletedTask;
}

public Task Start() => Task.CompletedTask;
public Task Start(SessionStartInfo sessionInfo) => Task.CompletedTask;
public Task SaveRealtimeStats(ScenarioStats[] stats) => Task.CompletedTask;
public Task SaveFinalStats(NodeStats stats) => Task.CompletedTask;
public Task Stop() => Task.CompletedTask;
Expand All @@ -44,4 +44,4 @@ public void Run()
.WithReportingInterval(TimeSpan.FromSeconds(10))
.Run();
}
}
}
1 change: 1 addition & 0 deletions examples/Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
// new HttpWithTimeoutExample().Run();

// ---- Dynamic Workloads ----
// new ScenarioWeightExample().Run();
// new InstanceNumberDistributionExample().Run();
// new UniformDistributionExample().Run();
// new ZipfianDistributionExample().Run();
Expand Down

0 comments on commit 4de03dc

Please sign in to comment.