Skip to content

Commit

Permalink
added StreamCodeReader benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
luithefirst committed May 13, 2024
1 parent 4c2f1a1 commit edc96a6
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"paket": {
"version": "7.2.1",
"version": "8.0.3",
"commands": [
"paket"
]
Expand Down
5 changes: 2 additions & 3 deletions src/Aardvark.Base.IO/StreamCodeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ namespace Aardvark.Base.Coder
{
public partial class StreamCodeReader : BinaryReader
{
private const int c_bufferSize = 262144;
// private byte[] m_buffer = new byte[c_bufferSize];
private byte[] m_guidBuffer = new byte[16]; // own buffer since creator requires 16-byte length

#region Constructors
Expand Down Expand Up @@ -101,6 +99,7 @@ public long ReadArray(byte[] array, long index, long count)
return (long)Read(array, (int)index, (int)count);
}

#if !NET6_0_OR_GREATER
[StructLayout(LayoutKind.Explicit)]
struct ByteArrayUnion
{
Expand All @@ -119,6 +118,7 @@ private static IntPtr GetTypeIdUncached<T>() where T : struct
gcHandle.Free();
return typeId;
}
#endif

public long ReadArray<T>(T[] array, long index, long count)
where T : struct
Expand Down Expand Up @@ -307,7 +307,6 @@ public int ReadList<T>(List<T> buffer, int index, int count)
public override void Close()
{
base.Close();
// m_buffer = null;
m_guidBuffer = null;
}

Expand Down
8 changes: 7 additions & 1 deletion src/Aardvark.Base.IO/StreamCodeWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ namespace Aardvark.Base.Coder
{
public partial class StreamCodeWriter : BinaryWriter
{
#if !NET6_0_OR_GREATER
private const int c_bufferSize = 262144;
private byte[] m_buffer = new byte[c_bufferSize];
#endif

#region Constructors

Expand Down Expand Up @@ -106,6 +108,7 @@ public void Write(CameraIntrinsics c)

#region Write Arrays and Lists

#if !NET6_0_OR_GREATER
[StructLayout(LayoutKind.Explicit)]
struct ByteArrayUnion
{
Expand All @@ -114,6 +117,7 @@ struct ByteArrayUnion
[FieldOffset(0)]
public Array structs;
}
#endif

public void WriteArray(byte[] array, long index, long count)
{
Expand Down Expand Up @@ -259,9 +263,11 @@ public void WriteList<T>(List<T> buffer, int index, int count)
public override void Close()
{
base.Close();
#if! NET6_0_OR_GREATER
m_buffer = null;
#endif
}

#endregion
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<None Include="**\*_template.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Aardvark.Base.IO\Aardvark.Base.IO.csproj" />
<ProjectReference Include="..\..\Aardvark.Base.Telemetry\Aardvark.Base.Telemetry.csproj" />
<ProjectReference Include="..\..\Aardvark.Base.Tensors\Aardvark.Base.Tensors.fsproj" />
<ProjectReference Include="..\..\Aardvark.Base\Aardvark.Base.csproj" />
Expand Down
5 changes: 3 additions & 2 deletions src/Tests/Aardvark.Base.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;

namespace Aardvark.Base.Benchmarks
{
Expand All @@ -9,7 +8,9 @@ public class Program
public static void Main(string[] args)
{
var cfg = ManualConfig.Create(DefaultConfig.Instance).WithOptions(ConfigOptions.DisableOptimizationsValidator);
BenchmarkSwitcher.FromAssembly(typeof(IntegerPowerFloat).Assembly).Run(args, cfg);
//BenchmarkSwitcher.FromAssembly(typeof(IntegerPowerFloat).Assembly).Run(args, cfg);

BenchmarkRunner.Run<StreamWriterReader>(cfg);

//BenchmarkRunner.Run<V4fLength>();
//BenchmarkRunner.Run<V3fLeng
Expand Down
74 changes: 74 additions & 0 deletions src/Tests/Aardvark.Base.Benchmarks/StreamWriterReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using Aardvark.Base.Coder;
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Aardvark.Base.Benchmarks
{
//BenchmarkDotNet v0.13.9+228a464e8be6c580ad9408e98f18813f6407fb5a, Windows 10 (10.0.19045.4291/22H2/2022Update)
//Intel Core i7-8700K CPU 3.70GHz(Coffee Lake), 1 CPU, 12 logical and 6 physical cores
//.NET SDK 6.0.421
// [Host] : .NET 6.0.29 (6.0.2924.17105), X64 RyuJIT AVX2

//Job = InProcess Toolchain=InProcessEmitToolchain

//| Method | Size | Mean | Error | StdDev | Allocated |
//|----------- |----- |----------:|---------:|---------:|----------:|
//| WriteArray | 10 | 39.21 ns | 0.062 ns | 0.048 ns | - |
//| ReadArray | 10 | 12.73 ns | 0.098 ns | 0.092 ns | - |
//| WriteArray | 100 | 54.25 ns | 0.259 ns | 0.229 ns | - |
//| ReadArray | 100 | 17.63 ns | 0.129 ns | 0.114 ns | - |
//| WriteArray | 1000 | 139.65 ns | 0.589 ns | 0.522 ns | - |
//| ReadArray | 1000 | 48.70 ns | 0.275 ns | 0.258 ns | - |

// OLD:
//| Method | Size | Mean | Error | StdDev | Allocated |
//|----------- |----- |------------:|----------:|----------:|----------:|
//| WriteArray | 10 | 61.18 ns | 0.357 ns | 0.334 ns | - |
//| ReadArray | 10 | 34.82 ns | 0.189 ns | 0.158 ns | - |
//| WriteArray | 100 | 316.98 ns | 0.696 ns | 0.617 ns | - |
//| ReadArray | 100 | 39.43 ns | 0.132 ns | 0.110 ns | - |
//| WriteArray | 1000 | 2,883.07 ns | 24.201 ns | 20.209 ns | - |
//| ReadArray | 1000 | 74.27 ns | 0.193 ns | 0.171 ns | - |

[InProcess]
[PlainExporter, MemoryDiagnoser]
public class StreamWriterReader
{
float[] _data;
StreamCodeWriter _writer;
StreamCodeReader _reader;

[Params(10, 100, 1000)]
public int Size;

[GlobalSetup]
public void GlobalSetup()
{
_data = new float[Size].SetByIndex(i => i);
_writer = new StreamCodeWriter(new MemoryStream(_data.Length * 4));

var tmpWriter = new StreamCodeWriter(new MemoryStream(_data.Length * 4));
tmpWriter.WriteArray(_data, 0, _data.Length);
_reader = new StreamCodeReader(tmpWriter.BaseStream);
}

[Benchmark]
public void WriteArray()
{
_writer.BaseStream.Position = 0;
_writer.WriteArray(_data, 0, _data.Length);
}

[Benchmark]
public void ReadArray()
{
_reader.BaseStream.Position = 0;
_reader.ReadArray(_data, 0, _data.Length);
}
}
}

0 comments on commit edc96a6

Please sign in to comment.