Skip to content
This repository has been archived by the owner on Jan 24, 2023. It is now read-only.

Commit

Permalink
Add NBench specs (#47)
Browse files Browse the repository at this point in the history
* migrated to NBench 1.2.1 to help resolve #4

* enabled NBench in FAKE build

* added Span serialization spec
  • Loading branch information
Aaronontheweb committed Jul 13, 2018
1 parent 21ff3a6 commit 88b0ece
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ _site/

# MSTest test Results
[Tt]est[Rr]esult*/
[Pp]erf[Rr]esult*/
[Bb]uild[Ll]og.*

# NUNIT
Expand Down
52 changes: 20 additions & 32 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,38 +114,26 @@ Target "RunTests" (fun _ ->
)

Target "NBench" <| fun _ ->
let nbenchTestPath = findToolInSubPath "NBench.Runner.exe" (toolsDir @@ "NBench.Runner*")
printfn "Using NBench.Runner: %s" nbenchTestPath

let nbenchTestAssemblies = !! "./src/**/*Tests.Performance.dll" // doesn't support .NET Core at the moment

let runNBench assembly =
let includes = getBuildParam "include"
let excludes = getBuildParam "exclude"
let teamcityStr = (getBuildParam "teamcity")
let enableTeamCity =
match teamcityStr with
| null -> false
| "" -> false
| _ -> bool.Parse teamcityStr

let args = StringBuilder()
|> append assembly
|> append (sprintf "output-directory=\"%s\"" outputPerfTests)
|> append (sprintf "concurrent=\"%b\"" true)
|> append (sprintf "trace=\"%b\"" true)
|> append (sprintf "teamcity=\"%b\"" enableTeamCity)
|> appendIfNotNullOrEmpty includes "include="
|> appendIfNotNullOrEmpty excludes "include="
|> toText

let result = ExecProcess(fun info ->
info.FileName <- nbenchTestPath
info.WorkingDirectory <- (Path.GetDirectoryName (FullName nbenchTestPath))
info.Arguments <- args) (System.TimeSpan.FromMinutes 45.0) (* Reasonably long-running task. *)
if result <> 0 then failwithf "NBench.Runner failed. %s %s" nbenchTestPath args
let projects =
match (isWindows) with
| true -> !! "./src/**/*.Tests.Performance.csproj"
| _ -> !! "./src/**/*.Tests.Performance.csproj" // if you need to filter specs for Linux vs. Windows, do it here


let runSingleProject project =
let arguments =
match (hasTeamCity) with
| true -> (sprintf "nbench --nobuild --teamcity --concurrent true --trace true --output %s" (outputPerfTests))
| false -> (sprintf "nbench --nobuild --concurrent true --trace true --output %s" (outputPerfTests))

let result = ExecProcess(fun info ->
info.FileName <- "dotnet"
info.WorkingDirectory <- (Directory.GetParent project).FullName
info.Arguments <- arguments) (TimeSpan.FromMinutes 30.0)

ResultHandling.failBuildIfXUnitReportedError TestRunnerErrorLevel.DontFailBuild result

nbenchTestAssemblies |> Seq.iter runNBench
projects |> Seq.iter runSingleProject


//--------------------------------------------------------------------------------
Expand Down Expand Up @@ -259,7 +247,7 @@ Target "Nuget" DoNothing
// all
"BuildRelease" ==> "All"
"RunTests" ==> "All"
//"NBench" ==> "All"
"NBench" ==> "All"
"Nuget" ==> "All"

RunTargetOrDefault "Help"
17 changes: 1 addition & 16 deletions build.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ Param(
)

$FakeVersion = "4.61.2"
$NBenchVersion = "1.0.1"
$DotNetChannel = "LTS";
$DotNetVersion = "2.0.0";
$DotNetInstallerUri = "https://raw.githubusercontent.com/dotnet/cli/v$DotNetVersion/scripts/obtain/dotnet-install.ps1";
$DotNetInstallerUri = "https://raw.githubusercontent.com/dotnet/cli/v2.0.0/scripts/obtain/dotnet-install.ps1";
$NugetVersion = "4.1.0";
$NugetUrl = "https://dist.nuget.org/win-x86-commandline/v$NugetVersion/nuget.exe"
$ProtobufVersion = "3.2.0"
Expand Down Expand Up @@ -115,20 +114,6 @@ if (!(Test-Path $FakeExePath)) {
}
}

###########################################################################
# INSTALL NBench Runner
###########################################################################

# Make sure NBench Runner has been installed.
$NBenchDllPath = Join-Path $ToolPath "NBench.Runner/lib/net45/NBench.Runner.exe"
if (!(Test-Path $NBenchDllPath)) {
Write-Host "Installing NBench..."
Invoke-Expression "&`"$NugetPath`" install NBench.Runner -ExcludeVersion -Version $NBenchVersion -OutputDirectory `"$ToolPath`"" | Out-Null;
if ($LASTEXITCODE -ne 0) {
Throw "An error occured while restoring NBench.Runner from NuGet."
}
}

###########################################################################
# Docfx
###########################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@


<PropertyGroup>
<TargetFramework>net461</TargetFramework>
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="$(TestSdkVersion)" />
<PackageReference Include="NBench" Version="1.0.4" />
<PackageReference Include="NBench" Version="1.2.1" />
<DotNetCliToolReference Include="dotnet-nbench" Version="1.2.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
// -----------------------------------------------------------------------
// <copyright file="UnitTest1.cs" company="Petabridge, LLC">
// <copyright file="SpanBuilderSpecs.cs" company="Petabridge, LLC">
// Copyright (C) 2018 - 2018 Petabridge, LLC <https://petabridge.com>
// </copyright>
// -----------------------------------------------------------------------

using NBench;
using Petabridge.Tracing.Zipkin.Tracers;

namespace Petabridge.Tracing.Zipkin.Tests.Performance
{
public class UnitTest1
public class SpanBuilderSpecs
{
public const string CounterName = "Operations";
public const string CounterName = "CompletedSpans";

public const int SpanCount = 100000;

private readonly MockZipkinTracer _mockTracer = new MockZipkinTracer();
private Counter _opsCounter;

[PerfSetup]
Expand All @@ -19,13 +24,19 @@ public void Setup(BenchmarkContext context)
_opsCounter = context.GetCounter(CounterName);
}

[PerfBenchmark(NumberOfIterations = 5, RunMode = RunMode.Throughput, RunTimeMilliseconds = 1000)]
[PerfBenchmark(NumberOfIterations = 5, RunMode = RunMode.Iterations, RunTimeMilliseconds = 1000)]
[CounterMeasurement(CounterName)]
[GcMeasurement(GcMetric.TotalCollections, GcGeneration.AllGc)]
[MemoryMeasurement(MemoryMetric.TotalBytesAllocated)]
public void TestMethod1()
public void CreateSpans()
{
_opsCounter.Increment();
for (var i = 0; i < SpanCount; i++)
using (_mockTracer.BuildSpan("test1").WithTag("foo", "bar").StartActive())
{

}

_opsCounter.Increment(SpanCount);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NBench;
using Petabridge.Tracing.Zipkin.Reporting;
using Petabridge.Tracing.Zipkin.Tracers;

namespace Petabridge.Tracing.Zipkin.Tests.Performance
{
public class SpanSerializerSpecs
{
public const string CounterName = "SerializedSpans";

public const int SpanCount = 100000;

private readonly MockZipkinTracer _mockTracer = new MockZipkinTracer(new Endpoint("actorsystem", "127.0.0.1", 8008));
private readonly ISpanSerializer _serializer = new JsonSpanSerializer();
private Span _testSpan;
private Counter _opsCounter;

[PerfSetup]
public void Setup(BenchmarkContext context)
{
_opsCounter = context.GetCounter(CounterName);

var startTime = new DateTimeOffset(new DateTime(2018, 1, 12, 13, 12, 14));
var endTime = startTime.AddMilliseconds(10);

// create a reasonably complex span
var span = new Span(_mockTracer, "op1",
new SpanContext(new TraceId(7776525154056436086, 6707114971141086261), -7118946577185884628, null,
true),
startTime, SpanKind.CLIENT)
.SetRemoteEndpoint(new Endpoint("actorsystem", "127.0.0.1", 8009)).SetTag("foo1", "bar")
.SetTag("numberOfPets", 2)
.SetTag("timeInChair", "long").Log(startTime.AddMilliseconds(1), "foo");
_testSpan = (Span) span;
_testSpan.Finish(endTime);
}

[PerfBenchmark(NumberOfIterations = 5, RunMode = RunMode.Iterations, RunTimeMilliseconds = 1000)]
[CounterMeasurement(CounterName)]
[GcMeasurement(GcMetric.TotalCollections, GcGeneration.AllGc)]
[MemoryMeasurement(MemoryMetric.TotalBytesAllocated)]
public void SerializeSpans()
{
for (var i = 0; i < SpanCount; i++)
using(var mem = new MemoryStream())
_serializer.Serialize(mem, _testSpan);

_opsCounter.Increment(SpanCount);
}
}
}

0 comments on commit 88b0ece

Please sign in to comment.