Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
add load test file
  • Loading branch information
Anaig committed Jul 29, 2020
commit 5c98e7b6d14c617dc9630fc03ba859d1cc8320d8
14 changes: 11 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ WORKDIR /app
COPY Src/*.sln .
COPY Src/Contoso/*.csproj ./Contoso/
COPY Src/Contoso.UnitTests/*.csproj ./Contoso.UnitTests/
COPY Src/Contoso.LoadTests/*.csproj ./Contoso.LoadTests/
RUN dotnet restore

# copy everything else and build app
Expand All @@ -20,15 +21,22 @@ WORKDIR /app/Contoso
RUN dotnet build -c Release /p:VersionPrefix=${VersionPrefix} /p:TreatWarningsAsErrors=true -warnaserror


FROM build AS testrunner
FROM build AS unittestrunner
WORKDIR /app/Contoso.UnitTests
ENTRYPOINT ["dotnet", "test", "--logger:trx", "--collect:XPlat Code Coverage"]


FROM build AS test
FROM build AS unittest
WORKDIR /app/Contoso.UnitTests
RUN dotnet test --collect:"XPlat Code Coverage"

FROM build AS loadtestrunner
WORKDIR /app/Contoso.LoadTests
ENTRYPOINT ["dotnet", "test", "--logger:trx", "--results-directory", "/loadTestResults"]

FROM build AS loadtest
WORKDIR /app/Contoso.LoadTests
RUN dotnet test

FROM build AS publish
WORKDIR /app/Contoso
RUN dotnet publish -c Release -o /out
Expand Down
16 changes: 16 additions & 0 deletions Src/Contoso.LoadTests/Contoso.LoadTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

</Project>
71 changes: 71 additions & 0 deletions Src/Contoso.LoadTests/LoadTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using NBomber.Contracts;
using NBomber.CSharp;
using RestSharp;
using System;
using System.Net.Http;
using System.Runtime.Versioning;
using Xunit;
using Xunit.Abstractions;

namespace Contoso.LoadTests
{
public class LoadTest
{

static HttpClient client = new HttpClient();
static string _stepName = "CheckIfOK";
private readonly Uri _path;
private readonly int _concurrency;
private readonly int _duration;
private readonly int _warmuUp;
private readonly ITestOutputHelper _output;

public LoadTest(ITestOutputHelper output)
{
static Uri getUriFromEnvironment(string name)
{
var env = Environment.GetEnvironmentVariable(name);
if (string.IsNullOrWhiteSpace(env))
{
throw new NotSupportedException($"Missing environment variable {name}");
}
return new Uri(env);
};

_path = getUriFromEnvironment("SERVICE_URL");
_concurrency = 10;
_duration = 10;
_warmuUp = 5;
}

///<summary>
/// Executes a basic load test using NBomber
///</summary>
[Fact]
public void RunLoadTest()
{
// Load test settings
var warmUpPhase = TimeSpan.FromSeconds(_warmuUp); // execution time of warm-up before start bombing
var testDuration = TimeSpan.FromSeconds(_duration); // execution time of Scenario
var concurrentCopies = _concurrency; // specify how many copies of current Scenario to run in parallel

// Assertions parameters
var acceptedFailNumber = 0;
var minRPS = 5;

// Execution step for the load scenario
var step = Step.Create(_stepName, async context =>
{
var response = await client.GetAsync(_path + "/sample/sumNumbersUpTo?value=100");
var success = response.IsSuccessStatusCode;
return success ? Response.Ok() : Response.Fail();
});

// List of assertions to be respected by the scenario for each step
var successAssertion = Assertion.ForStep(stepName: _stepName,
assertion: statistics => statistics.FailCount == acceptedFailNumber);
var rpsAssertion = Assertion.ForStep(stepName: _stepName,
assertion: statistics => statistics.RPS >= minRPS);
}
}
}
12 changes: 12 additions & 0 deletions Src/Contoso.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Contoso.UnitTests", "Contoso.UnitTests\Contoso.UnitTests.csproj", "{B219CDAD-7641-4ECB-8B96-EB47E25DE5AC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{0AAA7F28-054C-45C6-83A2-8FB787A7DF50}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Contoso.LoadTests", "Contoso.LoadTests\Contoso.LoadTests.csproj", "{55737DD5-84C3-4CFB-AC6A-14FB8E14FA37}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -22,10 +26,18 @@ Global
{B219CDAD-7641-4ECB-8B96-EB47E25DE5AC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B219CDAD-7641-4ECB-8B96-EB47E25DE5AC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B219CDAD-7641-4ECB-8B96-EB47E25DE5AC}.Release|Any CPU.Build.0 = Release|Any CPU
{55737DD5-84C3-4CFB-AC6A-14FB8E14FA37}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{55737DD5-84C3-4CFB-AC6A-14FB8E14FA37}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55737DD5-84C3-4CFB-AC6A-14FB8E14FA37}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55737DD5-84C3-4CFB-AC6A-14FB8E14FA37}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{B219CDAD-7641-4ECB-8B96-EB47E25DE5AC} = {0AAA7F28-054C-45C6-83A2-8FB787A7DF50}
{55737DD5-84C3-4CFB-AC6A-14FB8E14FA37} = {0AAA7F28-054C-45C6-83A2-8FB787A7DF50}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E55E11EA-AB0F-4280-943D-F6F1EC519DFB}
EndGlobalSection
Expand Down