Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Created May 25, 2020 14:15
Show Gist options
  • Save chgeuer/281787adffe4d6da2b421aaaaed33b91 to your computer and use it in GitHub Desktop.
Save chgeuer/281787adffe4d6da2b421aaaaed33b91 to your computer and use it in GitHub Desktop.
xunit_nbomber.cs
namespace XUnitLoadTestProject
{
using Microsoft.Extensions.Configuration;
using Microsoft.FSharp.Core;
using NBomber.Contracts;
using NBomber.CSharp;
using Serilog;
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
public class UnitTest1
{
private readonly ITestOutputHelper output;
public UnitTest1(ITestOutputHelper output)
{
this.output = output;
}
static async Task<bool> IsProperResponse(HttpResponseMessage responseMessage)
{
var response200 = responseMessage.StatusCode == HttpStatusCode.OK;
var s = await responseMessage.Content.ReadAsStringAsync();
var correctContent = s.Contains("Hello World!");
return response200 && correctContent;
}
[Fact]
public void Test1()
{
var warmUpPhase = TimeSpan.FromSeconds(1);
var testDuration = TimeSpan.FromSeconds(5);
var concurrentCopies = 305;
var requiredMinimumRps = 2_000;
var client = new HttpClient();
var step = Step.Create("httpGet", async context =>
{
var responseMessage = await client.GetAsync(
requestUri: "http://localhost:8080/",
cancellationToken: context.CancellationToken);
var success = await IsProperResponse(responseMessage);
return success ? Response.Ok() : Response.Fail();
});
var performanceAssertion = Assertion.ForStep(stepName: "httpGet",
assertion: statistics => statistics.RPS >= requiredMinimumRps);
var scenarioHttp = ScenarioBuilder.CreateScenario("test gitter", new[] { step })
.WithConcurrentCopies(concurrentCopies: concurrentCopies)
.WithWarmUpDuration(warmUpPhase)
.WithDuration(testDuration)
.WithAssertions(performanceAssertion)
;
var sink = new MySink((s) => {
output.WriteLine(s);
return Task.CompletedTask;
});
NBomberRunner
.RegisterScenarios(scenarioHttp)
.WithReportingSinks(
new IReportingSink[] { sink },
sendStatsInterval: TimeSpan.FromSeconds(1))
.RunTest();
Assert.Single(sink.Statistics);
var s = sink.Statistics[0];
output.WriteLine($"RPS: {s.RPS}, OkCount: {s.OkCount}, FailCount: {s.FailCount},");
}
}
public class MySink : IReportingSink
{
private readonly Func<string, Task> output;
public MySink(Func<string, Task> output) { this.output = output; }
void IReportingSink.Init(ILogger logger, FSharpOption<IConfiguration> infraConfig) { }
Task IReportingSink.StartTest(TestInfo testInfo) => Task.CompletedTask;
async Task IReportingSink.SaveRealtimeStats(TestInfo testInfo, Statistics[] stats)
{
var s = stats[0];
await output($"RPS: {s.RPS}, OkCount: {s.OkCount}, FailCount: {s.FailCount},");
}
Task IReportingSink.SaveFinalStats(TestInfo testInfo, Statistics[] stats, ReportFile[] reportFiles)
{
this.Statistics = stats;
this.ReportFiles = reportFiles;
return Task.CompletedTask;
}
Task IReportingSink.FinishTest(TestInfo testInfo) => Task.CompletedTask;
public Statistics[] Statistics { get; private set; }
public ReportFile[] ReportFiles { get; private set; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment