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 |
pull/19515/head
mgravell 6 years ago
parent 52de8a0a17
commit f5091b2622
  1. 1
      src/csharp/Grpc.Microbenchmarks/Grpc.Microbenchmarks.csproj
  2. 50
      src/csharp/Grpc.Microbenchmarks/Utf8Decode.cs

@ -6,6 +6,7 @@
<TargetFrameworks>net461;netcoreapp2.1</TargetFrameworks>
<OutputType>Exe</OutputType>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>

@ -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<int, byte[]> Payloads = new Dictionary<int, byte[]> {
{ 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);
}
}
}
}
}
Loading…
Cancel
Save