Skip to content

Feature/benchmark #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
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
4 changes: 2 additions & 2 deletions .github/workflows/nng.yml
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ jobs:
uses: actions/upload-artifact@v2
with:
path: |
nng.NETCore/runtimes/**/*
!nng.NETCore/runtimes/any/**/*
nng.NET/runtimes/**/*
!nng.NET/runtimes/any/**/*
if-no-files-found: error

105 changes: 105 additions & 0 deletions benchmarks/Benchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using BenchmarkDotNet;
using BenchmarkDotNet.Attributes;
using nng;
using nng.Native;
using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace benchmarks
{
public class Delegate
{
delegate int nng_aio_set_output_delegate(nng_aio aio, int index, IntPtr arg);
nng_aio_set_output_delegate nng_aio_set_output;

[GlobalSetup]
public void GlobalSetup()
{
var handle = DllImport.Load();
var ptr = NativeLibrary.GetExport(handle, "nng_aio_set_output");
nng_aio_set_output = Marshal.GetDelegateForFunctionPointer<nng_aio_set_output_delegate>(ptr);
}

[Benchmark]
public void CallDelegate()
{
var _ = nng_aio_set_output(nng_aio.Null, 9, IntPtr.Zero);
}
}

public unsafe class Pointer
{
delegate* unmanaged[Cdecl]<IntPtr, uint, IntPtr, int> nng_aio_set_output;

[GlobalSetup]
public void GlobalSetup()
{
var handle = DllImport.Load();

var ptr = NativeLibrary.GetExport(handle, "nng_aio_set_output");
nng_aio_set_output = (delegate* unmanaged[Cdecl]<IntPtr, uint, IntPtr, int>)ptr;
}

[Benchmark]
public void CallFunctionPointer()
{
var _ = nng_aio_set_output(IntPtr.Zero, 9, IntPtr.Zero);
}
}

public class DllImport
{
[GlobalSetup]
public void GlobalSetup()
{
//System.Console.WriteLine("GlobalSetup: " + System.IO.Directory.GetCurrentDirectory());
NativeLibrary.SetDllImportResolver(typeof(nng.Native.Basic.UnsafeNativeMethods).Assembly, DllImportResolver);
}

[Benchmark]
public void CallDllImport()
{
var _ = nng.Native.Aio.UnsafeNativeMethods.nng_aio_set_output(nng_aio.Null, 9, IntPtr.Zero);
}

[Benchmark(Baseline = true)]
public void CallManaged()
{
var _ = Managed(IntPtr.Zero, 9, IntPtr.Zero);
}

[MethodImpl(MethodImplOptions.NoInlining)]
static nint Managed(nint _unused0, int _unused1, nint _unused2) => 0;

static IntPtr DllImportResolver(string libraryName, System.Reflection.Assembly assembly, DllImportSearchPath? searchPath)
{
return Load();
}

public static IntPtr Load()
{
return NativeLibrary.Load("/Users/j_woltersdorf/projects/nng.NETCore/nng.NET/runtimes/osx-x64/native/libnng.dylib");
}
}

public class Interface
{
IAPIFactory<INngMsg> Factory { get; set; }

[GlobalSetup]
public void GlobalSetup()
{
var alc = new NngLoadContext("/Users/j_woltersdorf/projects/nng.NETCore/nng.NET/");
Factory = NngLoadContext.Init(alc);
}

[Benchmark]
public void CallInterfaceToDllImport()
{
Factory.CreateAlloc(0);
System.Threading.Thread.Sleep(1);
}
}
}
17 changes: 17 additions & 0 deletions benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Running;

namespace benchmarks
{
class Program
{
static void Main(string[] args)
{
System.Console.WriteLine(System.IO.Directory.GetCurrentDirectory());
var config = ManualConfig.Create(DefaultConfig.Instance)
.WithOptions(ConfigOptions.JoinSummary)
.AddExporter(BenchmarkDotNet.Exporters.Json.JsonExporter.Full);
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args, config);
}
}
}
22 changes: 22 additions & 0 deletions benchmarks/benchmarks.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>portable</DebugType>
<DebugSymbols>true</DebugSymbols>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Optimize>true</Optimize>
<Configuration>Release</Configuration>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.12.1" />
<PackageReference Include="BenchmarkDotNet.Diagnostics.Windows" Version="0.12.1" Condition="'$(OS)' == 'Windows_NT'" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\nng.NET\nng.NET.csproj" />
<ProjectReference Include="..\nng.NET.Shared\nng.NET.Shared.csproj" />
</ItemGroup>
</Project>
6 changes: 6 additions & 0 deletions clangsharp.rsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
--file
nng/include/nng/nng.h
--namespace
nng
--output
./interop
6 changes: 3 additions & 3 deletions dockerfiles/build_nng/Dockerfile
Original file line number Diff line number Diff line change
@@ -19,9 +19,9 @@ ARG SRC
ARG NNG_BRANCH=v1.4.0
WORKDIR ${SRC}

RUN git clone https://github.com/nanomsg/nng.git \
&& cd nng \
&& git checkout ${NNG_BRANCH}
RUN git clone \
-b ${NNG_BRANCH} --depth 1 --recursive --single-branch \
https://github.com/nanomsg/nng.git

# Build x64 Linux
FROM debian:buster AS linux-x64
4 changes: 2 additions & 2 deletions scripts/build_nng.ps1
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@
param([string]$nng_source = "../nng/",
[switch]$clean,
[string]$git_branch,
[string]$runtimes = "$PSScriptRoot/../nng.NETCore/runtimes"
[string]$runtimes = "$PSScriptRoot/../nng.NET/runtimes"
)

if (-not $IsLinux -and -not $(Test-Path $nng_source -PathType Container)){
@@ -33,7 +33,7 @@ try {
if ($IsLinux) {
docker build -t jeikabu/build-nng dockerfiles/build_nng
docker run --rm --privileged multiarch/qemu-user-static:register
docker run -i -t --rm -v "$PWD/nng.NETCore/runtimes:/runtimes" jeikabu/build-nng
docker run -i -t --rm -v "$PWD/nng.NET/runtimes:/runtimes" jeikabu/build-nng
}
else {
Write-Host "Placing nng $platforms binaries in $runtimes..."