Skip to content

Commit

Permalink
-Added benchmarks
Browse files Browse the repository at this point in the history
  - String equality
  - String hashing
  - Parser throughput
- Added IPV6 support to Host
  • Loading branch information
sebaFlame committed Jan 14, 2022
1 parent 9d6911b commit 5dd1d47
Show file tree
Hide file tree
Showing 10 changed files with 432 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
.vscode/
.vs/
.vimspector.json
raw.log

[Bb]in/
[Oo]bj/
Expand Down
45 changes: 45 additions & 0 deletions ThePlague.IRC.Parser.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StringComparer", "bench\Str
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ThePlague.IRC.Parser.Generator", "src\ThePlague.IRC.Parser.Generator\ThePlague.IRC.Parser.Generator.csproj", "{8ED31D9F-3C6E-4979-A971-2B688F89979A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IRCParserThroughput", "bench\IRCParserThroughput\IRCParserThroughput.csproj", "{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StringEquality", "bench\StringEquality\StringEquality.csproj", "{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StringHashing", "bench\StringHashing\StringHashing.csproj", "{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -120,6 +126,42 @@ Global
{8ED31D9F-3C6E-4979-A971-2B688F89979A}.Release|x64.Build.0 = Release|Any CPU
{8ED31D9F-3C6E-4979-A971-2B688F89979A}.Release|x86.ActiveCfg = Release|Any CPU
{8ED31D9F-3C6E-4979-A971-2B688F89979A}.Release|x86.Build.0 = Release|Any CPU
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}.Debug|x64.ActiveCfg = Debug|Any CPU
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}.Debug|x64.Build.0 = Debug|Any CPU
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}.Debug|x86.ActiveCfg = Debug|Any CPU
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}.Debug|x86.Build.0 = Debug|Any CPU
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}.Release|Any CPU.Build.0 = Release|Any CPU
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}.Release|x64.ActiveCfg = Release|Any CPU
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}.Release|x64.Build.0 = Release|Any CPU
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}.Release|x86.ActiveCfg = Release|Any CPU
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C}.Release|x86.Build.0 = Release|Any CPU
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}.Debug|x64.ActiveCfg = Debug|Any CPU
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}.Debug|x64.Build.0 = Debug|Any CPU
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}.Debug|x86.ActiveCfg = Debug|Any CPU
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}.Debug|x86.Build.0 = Debug|Any CPU
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}.Release|Any CPU.Build.0 = Release|Any CPU
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}.Release|x64.ActiveCfg = Release|Any CPU
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}.Release|x64.Build.0 = Release|Any CPU
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}.Release|x86.ActiveCfg = Release|Any CPU
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A}.Release|x86.Build.0 = Release|Any CPU
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}.Debug|x64.ActiveCfg = Debug|Any CPU
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}.Debug|x64.Build.0 = Debug|Any CPU
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}.Debug|x86.ActiveCfg = Debug|Any CPU
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}.Debug|x86.Build.0 = Debug|Any CPU
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}.Release|Any CPU.Build.0 = Release|Any CPU
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}.Release|x64.ActiveCfg = Release|Any CPU
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}.Release|x64.Build.0 = Release|Any CPU
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}.Release|x86.ActiveCfg = Release|Any CPU
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{2135173F-C9E5-4A63-A624-E58FFF652C15} = {26BB1C2C-28DD-416C-BB47-E79733D3D1E8}
Expand All @@ -129,5 +171,8 @@ Global
{70288853-C263-445C-94C5-28A0743CE06C} = {E49CF75C-8101-4669-B2E4-5BC9211C5922}
{585ED045-8720-4D8C-ABC7-68D5ACAC5725} = {57A7C693-DBC9-4D12-93AF-40AB17DE1F45}
{8ED31D9F-3C6E-4979-A971-2B688F89979A} = {26BB1C2C-28DD-416C-BB47-E79733D3D1E8}
{D5CFFC88-B9C6-4BFE-B0C7-0A691AFCA55C} = {57A7C693-DBC9-4D12-93AF-40AB17DE1F45}
{85F6AEA8-803E-47D0-859F-CDA8A9182E0A} = {57A7C693-DBC9-4D12-93AF-40AB17DE1F45}
{747AC2D8-B59D-4A6E-9956-014FB5BAD80E} = {57A7C693-DBC9-4D12-93AF-40AB17DE1F45}
EndGlobalSection
EndGlobal
19 changes: 19 additions & 0 deletions bench/IRCParserThroughput/IRCParserThroughput.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../../src/ThePlague.IRC.Parser/ThePlague.IRC.Parser.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.IO.Pipelines" Version="6.0.1" />
</ItemGroup>

<ItemGroup>
<Content Include="raw.log" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>
123 changes: 123 additions & 0 deletions bench/IRCParserThroughput/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System;
using System.Buffers;
using System.IO;
using System.IO.Pipelines;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;

using ThePlague.Model.Core.Text;
using ThePlague.IRC.Parser;
using ThePlague.IRC.Parser.Tokens;

namespace IRCParserThroughput
{
public class Program
{
private static string _Filename = "raw.log";

static async Task Main(string[] args)
{
#if DEBUG
Console.WriteLine($"Please attach a debugger to process {Environment.ProcessId}...");
while(!Debugger.IsAttached)
{
Thread.Sleep(100);
}
#endif

Stopwatch stopWatch = new Stopwatch();
using Stream stream = File.OpenRead(_Filename);
PipeReader reader = PipeReader.Create
(
stream,
new StreamPipeReaderOptions(null, 512, 128, true)
);

ValueTask<ReadResult> vt;
ReadOnlySequence<byte> sequence;
Token message;
long averageTime = 0, maxTime = 0, minTime = 0, time = 0, total = 0;
ReadResult result;
string min = string.Empty, max = string.Empty;

while(true)
{
if(!reader.TryRead(out result))
{
vt = reader.ReadAsync();

if(!vt.IsCompletedSuccessfully)
{
result = await vt;
}
else
{
result = vt.Result;
}
}

if(result.IsCompleted)
{
break;
}

sequence = result.Buffer;

stopWatch.Start();
if(IRCParser.TryParse
(
in sequence,
out message
))
{
stopWatch.Stop();

//Console.WriteLine(message.ToUtf8String().ToString());

time = stopWatch.ElapsedTicks;
total += time;

if(averageTime == 0)
{
averageTime = time;
}
else
{
averageTime += time;
averageTime /= 2;
}

if(maxTime == 0
|| time > maxTime)
{
maxTime = time;
max = message.ToUtf8String().ToString();
}

if(minTime == 0
|| time < minTime)
{
minTime = time;
min = message.ToUtf8String().ToString();
}

reader.AdvanceTo(message.Sequence.End);
}
else
{
reader.AdvanceTo(sequence.Start, sequence.End);
}

stopWatch.Reset();
}

reader.Complete();

Console.WriteLine($"Average: {averageTime * 1000000 / Stopwatch.Frequency}µs");
Console.WriteLine($"Max: {maxTime * 1000000 / Stopwatch.Frequency}µs ({max})");
Console.WriteLine($"Min: {minTime * 1000000 / Stopwatch.Frequency}µs ({min})");
Console.WriteLine($"Total: {total * 1000 / Stopwatch.Frequency}ms");
}
}
}
78 changes: 78 additions & 0 deletions bench/StringEquality/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;

using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;

using ThePlague.Model.Core.Text;

namespace StringEquality
{
public static class Program
{
public static void Main(string[] args)
=> BenchmarkRunner.Run<StringEqualityBenchmark>(args: args);
}

//[EventPipeProfiler(EventPipeProfile.CpuSampling)]
public class StringEqualityBenchmark
{
[Benchmark(Baseline = true)]
[ArgumentsSource(nameof(DataUtf16))]
public bool CoreFXEquality(string strA, string strB)
=> string.Equals(strA, strB, StringComparison.OrdinalIgnoreCase);

[Benchmark]
[ArgumentsSource(nameof(DataUtf8))]
public bool FirstDecodeToUtf16Equality(Utf8String strA, Utf8String strB)
{
string utf16A = (string)strA;
string utf16B = (string)strB;

return string.Equals
(
utf16A,
utf16B,
StringComparison.OrdinalIgnoreCase
);
}

[Benchmark]
[ArgumentsSource(nameof(DataUtf8))]
public bool OrdinalEquals(Utf8String strA, Utf8String strB)
{
IEqualityComparer<Utf8String> comparer
= BaseUtf8StringComparer.Ordinal;
return comparer.Equals(strA, strB);
}

[Benchmark]
[ArgumentsSource(nameof(DataUtf8))]
public bool OrdinalIgnoreCaseEquals(Utf8String strA, Utf8String strB)
{
IEqualityComparer<Utf8String> comparer =
BaseUtf8StringComparer.OrdinalIgnoreCase;
return comparer.Equals(strA, strB);
}

public static IEnumerable<object[]> DataUtf16()
{
yield return new object[] { "CaseFolding", "cASEfOLDING" };
yield return new object[] { "ЯяЯяЯяЯяЯяЯ", "яЯяЯяЯяЯяЯя" };
}

public static IEnumerable<object[]> DataUtf8()
{
yield return new object[]
{
new Utf8String("CaseFolding"),
new Utf8String("cASEfOLDING")
};
yield return new object[]
{
new Utf8String("ЯяЯяЯяЯяЯяЯ"),
new Utf8String("яЯяЯяЯяЯяЯя")
};
}
}
}
35 changes: 35 additions & 0 deletions bench/StringEquality/StringEquality.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<ItemGroup>
<ProjectReference Include="..\..\src\ThePlague.Model.Core.Text\ThePlague.Model.Core.Text.csproj" />
</ItemGroup>

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<LangVersion>Latest</LangVersion>
<TieredCompilation>true</TieredCompilation>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<IncludeBuildOutput>false</IncludeBuildOutput>
</PropertyGroup>

<PropertyGroup>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.1"/>
</ItemGroup>

<PropertyGroup>
<DefineConstants>$(DefineConstants);CORECLR</DefineConstants>
<IsWindows Condition="'$(OS)' == 'Windows_NT'">true</IsWindows>
</PropertyGroup>

<!-- Define non-windows, all configuration properties -->
<PropertyGroup Condition=" '$(IsWindows)' != 'true' ">
<DefineConstants>$(DefineConstants);UNIX</DefineConstants>
</PropertyGroup>
</Project>
Loading

0 comments on commit 5dd1d47

Please sign in to comment.