Skip to content

Commit

Permalink
Added benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
gfoidl committed Nov 9, 2019
1 parent 9764591 commit e5be9ce
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 9 deletions.
70 changes: 70 additions & 0 deletions perf/gfoidl.Base64.Benchmarks/ReadOnlySequenceBenchmark.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using System;
using System.Buffers;
using System.IO.Pipelines;
using BenchmarkDotNet.Attributes;

namespace gfoidl.Base64.Benchmarks
{
public class ReadOnlySequenceBase64Benchmark : ReadOnlySequenceBaseBenchmark
{
public ReadOnlySequenceBase64Benchmark() : base(Base64.Default) { }
}
//-------------------------------------------------------------------------
public class ReadOnlySequenceBase64UrlBenchmark : ReadOnlySequenceBaseBenchmark
{
public ReadOnlySequenceBase64UrlBenchmark() : base(Base64.Url) { }
}
//-------------------------------------------------------------------------
//[Config(typeof(HardwareIntrinsicsCustomConfig))]
public abstract class ReadOnlySequenceBaseBenchmark
{
private const int ByteArraySize = 10_000_000;
private readonly ReadOnlySequence<byte> _dataSingleSegment;
private readonly ReadOnlySequence<byte> _dataMultiSegment;
private readonly ReadOnlySequence<byte> _guid;
private readonly ArrayBufferWriter<byte> _arrayBufferWriter = new ArrayBufferWriter<byte>((ByteArraySize + 2) / 3 * 4);
protected readonly Base64 _encoder;
//---------------------------------------------------------------------
protected ReadOnlySequenceBaseBenchmark(Base64 encoder)
{
_encoder = encoder ?? throw new ArgumentNullException(nameof(encoder));

var rnd = new Random(42);
var data = new byte[ByteArraySize];
rnd.NextBytes(data);
_dataSingleSegment = new ReadOnlySequence<byte>(data);

var pipe = new Pipe();
pipe.Writer.Write(data);
pipe.Writer.Complete();
_dataMultiSegment = pipe.Reader.ReadAsync().GetAwaiter().GetResult().Buffer;

byte[] guid = Guid.NewGuid().ToByteArray();
_guid = new ReadOnlySequence<byte>(guid);
}
//---------------------------------------------------------------------
[IterationCleanup]
public void IterationCleanup() => _arrayBufferWriter.Clear();
//---------------------------------------------------------------------
[Benchmark]
public void EncodeSingleSegment()
{
_encoder.Encode(_dataSingleSegment, _arrayBufferWriter, out long _, out long _);
}
//---------------------------------------------------------------------
[Benchmark]
public void EncodeMultiSegment()
{
_encoder.Encode(_dataMultiSegment, _arrayBufferWriter, out long _, out long _);
}
//---------------------------------------------------------------------
[Benchmark(OperationsPerInvoke = 10_000)]
public void EncodeGuid()
{
for (int i = 0; i < 10_000; ++i)
{
_encoder.Encode(_guid, _arrayBufferWriter, out long _, out long _);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="LocalsInit.Fody" Version="1.1.0" />
<PackageReference Include="System.IO.Pipelines" Version="4.6.0" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 11 additions & 9 deletions run-benchmarks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
set -e
#------------------------------------------------------------------------------
if [[ "$1" == "--install-sdk" ]]; then
echo "installing SDK $SDK_VERSION"
echo ""
echo "installing SDK $SDK_VERSION"
echo ""

curl -o dotnet-install.sh https://dot.net/v1/dotnet-install.sh
mkdir dotnet
chmod u+x ./dotnet-install.sh
./dotnet-install.sh --install-dir $(pwd)/dotnet -v $SDK_VERSION
rm $(pwd)/dotnet-install.sh
export PATH="$(pwd)/dotnet:$PATH"
curl -o dotnet-install.sh https://dot.net/v1/dotnet-install.sh
mkdir dotnet
chmod u+x ./dotnet-install.sh
./dotnet-install.sh --install-dir $(pwd)/dotnet -v $SDK_VERSION
rm $(pwd)/dotnet-install.sh
export PATH="$(pwd)/dotnet:$PATH"

echo ""
echo ""
fi

echo 'installed sdks:'
Expand All @@ -30,3 +30,5 @@ dotnet gfoidl.Base64.Benchmarks.dll -f *DecodeStringBenchmark*
dotnet gfoidl.Base64.Benchmarks.dll -f *DecodeUtf8Benchmark*
dotnet gfoidl.Base64.Benchmarks.dll -f *EncodeStringBenchmark*
dotnet gfoidl.Base64.Benchmarks.dll -f *EncodeUtf8Benchmark*
dotnet gfoidl.Base64.Benchmarks.dll -f *ReadOnlySequenceBase64Benchmark*
dotnet gfoidl.Base64.Benchmarks.dll -f *ReadOnlySequenceBase64UrlBenchmark*

0 comments on commit e5be9ce

Please sign in to comment.