Skip to content

Commit

Permalink
Added Thresholds examples
Browse files Browse the repository at this point in the history
  • Loading branch information
AntyaDev committed Oct 15, 2024
1 parent 1ae9ef4 commit 0925436
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 4 deletions.
31 changes: 31 additions & 0 deletions examples/xUnitExample/Configs/nbomber-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"GlobalSettings": {
"ScenariosSettings": [
{
"ScenarioName": "http_scenario",

"LoadSimulationsSettings": [
{ "Inject": [1, "00:00:01", "00:00:30"] }
],

"ThresholdSettings": [

{ "OkRequest": "RPS >= 30" },
{ "OkRequest": "Percent > 90" },

{ "FailRequest": "Percent < 10" },

{ "OkLatency": "max < 100" },
{ "OkLatency": "p75 < 80" },

{ "OkDataTransfer": "p75 < 80" },

{ "StatusCode": ["500", "Percent < 5"] },
{ "StatusCode": ["400", "Percent < 10"], "AbortWhenErrorCount": 5 },
{ "StatusCode": ["200", "Percent >= 50"], "AbortWhenErrorCount": 5, "StartCheckAfter": "00:00:10" }

]
}
]
}
}
15 changes: 13 additions & 2 deletions examples/xUnitExample/LoadTestExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void SimpleHttpExample()
{
var request =
Http.CreateRequest("GET", "https://nbomber.com")
.WithHeader("Accept", "text/html")
.WithHeader("Content-Type", "application/json")
.WithBody(new StringContent("{ some JSON }"));

var response = await Http.Send(httpClient, request);
Expand All @@ -31,7 +31,7 @@ public void SimpleHttpExample()
{
var request =
Http.CreateRequest("GET", "https://nbomber.com")
.WithHeader("Accept", "text/html")
.WithHeader("Content-Type", "application/json")
.WithBody(new StringContent("{ some JSON }"));

var response = await Http.Send(httpClient, request);
Expand All @@ -48,7 +48,16 @@ public void SimpleHttpExample()
.RegisterScenarios(scenario)
.Run();

// StatsHelper API extensions provides: Get, Find, Exist methods to work with ScenarioStats, StepStats, StatusCodes, etc.

// gets scenario stats:
// it throws exception if "http_scenario" is not found
var scnStats = result.ScenarioStats.Get("http_scenario");

// finds scenario stats:
// it returns null if "http_scenario" is not found
scnStats = result.ScenarioStats.Find("http_scenario");

var step1Stats = scnStats.StepStats.Get("step_1");

var isStep2Exist = scnStats.StepStats.Exists("step_2");
Expand Down Expand Up @@ -81,6 +90,8 @@ public void SimpleHttpExample()
step1Stats.Fail.StatusCodes.Exists("503")
&& step1Stats.Fail.StatusCodes.Get("503").Percent < 5
);
// or you can use .Find() which may return null
Assert.True(step1Stats.Fail.StatusCodes.Find("503")?.Percent < 5);

Assert.True(step2Stats.Ok.DataTransfer.MinBytes > Bytes.FromKb(1));
Assert.True(step2Stats.Ok.DataTransfer.MaxBytes > Bytes.FromKb(1));
Expand Down
63 changes: 63 additions & 0 deletions examples/xUnitExample/Thresholds.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using NBomber.Contracts;
using NBomber.CSharp;
using NBomber.Http.CSharp;
using Xunit;

namespace xUnitExample;

public class Thresholds
{
[Fact]
public void Runtime_Thresholds_Example()
{
using var httpClient = new HttpClient();

var scenario = Scenario.Create("http_scenario", async context =>
{
var step1 = await Step.Run("step_1", context, async () =>
{
var request =
Http.CreateRequest("GET", "https://nbomber.com")
.WithHeader("Content-Type", "application/json");

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

return response;
});

return Response.Ok();
})
.WithoutWarmUp()
.WithLoadSimulations(Simulation.Inject(rate: 1, interval: TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(30)))
.WithThresholds(
// Scenario's threshold that checks: error rate < 10%
Threshold.Create(scenarioStats => scenarioStats.Fail.Request.Percent < 10),

// Step's threshold that checks: error rate < 10%
Threshold.Create("step_1", stepStats => stepStats.Fail.Request.Percent < 10),

// Scenario's threshold that checks if any response contains status code 404
Threshold.Create(
scenarioStats => scenarioStats.Fail.StatusCodes.Exists("404"),

abortWhenErrorCount: 5, // Threshold checks are executed based on the ReportingInterval (by default each 5 sec),
// so when the threshold ErrorCount = 5, the load test will be aborted.
// By default, 'abortWhenErrorCount' is null meaning Thresholds errors will not abort the test execution.


startCheckAfter: TimeSpan.FromSeconds(10) // Threshold check will be delayed on 10 sec
),

Threshold.Create(scenarioStats => scenarioStats.Ok.StatusCodes.Find("200")?.Percent >= 80)
);

var result = NBomberRunner
.RegisterScenarios(scenario)
.Run();

// Here, we attempt to find a failed threshold, and if one is found (i.e., it is not null), we throw an exception.
var failedThreshold = result.Thresholds.FirstOrDefault(x => x.IsFailed);

Assert.True(failedThreshold == null);
}
}
44 changes: 44 additions & 0 deletions examples/xUnitExample/ThresholdsFromConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using NBomber.CSharp;
using NBomber.Http.CSharp;
using Xunit;

namespace xUnitExample;

public class ThresholdsFromConfig
{
[Fact]
public void Runtime_Thresholds_Example()
{
using var httpClient = new HttpClient();

var scenario = Scenario.Create("http_scenario", async context =>
{
var step1 = await Step.Run("step_1", context, async () =>
{
var request =
Http.CreateRequest("GET", "https://nbomber.com")
.WithHeader("Content-Type", "application/json");

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

return response;
});

return Response.Ok();
})
.WithoutWarmUp()
.WithLoadSimulations(
Simulation.Inject(rate: 1, interval: TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(30))
);

var result = NBomberRunner
.RegisterScenarios(scenario)
.LoadConfig("Configs/nbomber-config.json")
.Run();

// Here, we attempt to find a failed threshold, and if one is found (i.e., it is not null), we throw an exception.
var failedThreshold = result.Thresholds.FirstOrDefault(x => x.IsFailed);

Assert.True(failedThreshold != null);
}
}
10 changes: 8 additions & 2 deletions examples/xUnitExample/xUnitExample.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<PackageReference Include="NBomber" Version="5.6.0-beta.13" />
<PackageReference Include="NBomber.Http" Version="5.1.0-beta.3" />
<PackageReference Include="NBomber" Version="5.8.0-beta.7" />
<PackageReference Include="NBomber.Http" Version="5.2.0-beta.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -21,4 +21,10 @@
</PackageReference>
</ItemGroup>

<ItemGroup>
<None Update="Configs\nbomber-config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>

0 comments on commit 0925436

Please sign in to comment.