From f5091b2622d57fb34fc30fec1f609fe7b8d74800 Mon Sep 17 00:00:00 2001 From: mgravell Date: Mon, 1 Jul 2019 16:16:00 +0100 Subject: [PATCH] add UTF8-decode benchmark | Method | Job | Runtime | PayloadSize | Mean | Error | StdDev | Gen 0 | Gen 1 | Gen 2 | Allocated | |------- |----- |-------- |------------ |-------------:|-----------:|-----------:|-------:|------:|------:|----------:| | Run | Clr | Clr | 0 | 1.736 ns | 0.0101 ns | 0.0094 ns | - | - | - | - | | Run | Core | Core | 0 | 1.306 ns | 0.0108 ns | 0.0095 ns | - | - | - | - | | Run | Clr | Clr | 1 | 35.384 ns | 0.2282 ns | 0.2135 ns | 0.0101 | - | - | 64 B | | Run | Core | Core | 1 | 32.388 ns | 0.3333 ns | 0.2955 ns | 0.0101 | - | - | 64 B | | Run | Clr | Clr | 4 | 57.736 ns | 0.3889 ns | 0.3448 ns | 0.0114 | - | - | 72 B | | Run | Core | Core | 4 | 52.878 ns | 0.2802 ns | 0.2621 ns | 0.0114 | - | - | 72 B | | Run | Clr | Clr | 128 | 554.819 ns | 4.4341 ns | 4.1477 ns | 0.0830 | - | - | 530 B | | Run | Core | Core | 128 | 336.356 ns | 1.6148 ns | 1.4315 ns | 0.0835 | - | - | 528 B | | Run | Clr | Clr | 1024 | 4,050.850 ns | 28.9245 ns | 25.6408 ns | 0.6016 | - | - | 3820 B | | Run | Core | Core | 1024 | 2,272.534 ns | 33.8963 ns | 31.7066 ns | 0.6016 | - | - | 3808 B | --- .../Grpc.Microbenchmarks.csproj | 1 + src/csharp/Grpc.Microbenchmarks/Utf8Decode.cs | 50 +++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 src/csharp/Grpc.Microbenchmarks/Utf8Decode.cs diff --git a/src/csharp/Grpc.Microbenchmarks/Grpc.Microbenchmarks.csproj b/src/csharp/Grpc.Microbenchmarks/Grpc.Microbenchmarks.csproj index 899e41ce532..f775e4c85fb 100644 --- a/src/csharp/Grpc.Microbenchmarks/Grpc.Microbenchmarks.csproj +++ b/src/csharp/Grpc.Microbenchmarks/Grpc.Microbenchmarks.csproj @@ -6,6 +6,7 @@ net461;netcoreapp2.1 Exe true + true diff --git a/src/csharp/Grpc.Microbenchmarks/Utf8Decode.cs b/src/csharp/Grpc.Microbenchmarks/Utf8Decode.cs new file mode 100644 index 00000000000..1c3f4d261ee --- /dev/null +++ b/src/csharp/Grpc.Microbenchmarks/Utf8Decode.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Text; +using BenchmarkDotNet.Attributes; +using Grpc.Core.Internal; + +namespace Grpc.Microbenchmarks +{ + [ClrJob, CoreJob] // test .NET Core and .NET Framework + [MemoryDiagnoser] // allocations + public class Utf8Decode + { + [Params(0, 1, 4, 128, 1024)] + public int PayloadSize { get; set; } + + static readonly Dictionary Payloads = new Dictionary { + { 0, Invent(0) }, + { 1, Invent(1) }, + { 4, Invent(4) }, + { 128, Invent(128) }, + { 1024, Invent(1024) }, + }; + + static byte[] Invent(int length) + { + var rand = new Random(Seed: length); + var chars = new char[length]; + for(int i = 0; i < chars.Length; i++) + { + chars[i] = (char)rand.Next(32, 300); + } + return Encoding.UTF8.GetBytes(chars); + } + + const int Iterations = 1000; + [Benchmark(OperationsPerInvoke = Iterations)] + public unsafe void Run() + { + byte[] payload = Payloads[PayloadSize]; + fixed (byte* ptr = payload) + { + var iPtr = new IntPtr(ptr); + for (int i = 0; i < Iterations; i++) + { + MarshalUtils.PtrToStringUTF8(iPtr, payload.Length); + } + } + } + } +}