add utf-8 encode benchmark

pull/19515/head
mgravell 5 years ago
parent f5091b2622
commit dbef6c9c70
  1. 6
      src/csharp/Grpc.Core.Tests/Properties/AssemblyInfo.cs
  2. 1
      src/csharp/Grpc.Microbenchmarks/Grpc.Microbenchmarks.csproj
  3. 72
      src/csharp/Grpc.Microbenchmarks/Utf8Encode.cs

@ -27,3 +27,9 @@ using System.Runtime.CompilerServices;
[assembly: AssemblyCopyright("Google Inc. All rights reserved.")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: InternalsVisibleTo("Grpc.Microbenchmarks,PublicKey=" +
"00240000048000009400000006020000002400005253413100040000010001002f5797a92c6fcde81bd4098f43" +
"0442bb8e12768722de0b0cb1b15e955b32a11352740ee59f2c94c48edc8e177d1052536b8ac651bce11ce5da3a" +
"27fc95aff3dc604a6971417453f9483c7b5e836756d5b271bf8f2403fe186e31956148c03d804487cf642f8cc0" +
"71394ee9672dfe5b55ea0f95dfd5a7f77d22c962ccf51320d3")]

@ -11,6 +11,7 @@
<ItemGroup>
<ProjectReference Include="../Grpc.Core/Grpc.Core.csproj" />
<ProjectReference Include="../Grpc.Core.Tests/Grpc.Core.Tests.csproj" />
</ItemGroup>
<ItemGroup>

@ -0,0 +1,72 @@
using System;
using System.Collections.Generic;
using System.Text;
using BenchmarkDotNet.Attributes;
using Grpc.Core;
using Grpc.Core.Internal;
using Grpc.Core.Internal.Tests;
namespace Grpc.Microbenchmarks
{
[ClrJob, CoreJob] // test .NET Core and .NET Framework
[MemoryDiagnoser] // allocations
public class Utf8Encode : ISendStatusFromServerCompletionCallback
{
static readonly NativeMethods Native = NativeMethods.Get();
[Params(0, 1, 4, 128, 1024)]
public int PayloadSize { get; set; }
static readonly Dictionary<int, string> Payloads = new Dictionary<int, string> {
{ 0, Invent(0) },
{ 1, Invent(1) },
{ 4, Invent(4) },
{ 128, Invent(128) },
{ 1024, Invent(1024) },
};
static string 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 new string(chars);
}
[GlobalSetup]
public void Setup()
{
Native.grpcsharp_test_override_method("grpcsharp_call_start_batch", "nop");
metadata = MetadataArraySafeHandle.Create(Metadata.Empty);
call = new FakeNativeCall();
}
public void Cleanup()
{
metadata.Dispose();
metadata = null;
call.Dispose();
call = null;
}
private INativeCall call;
private MetadataArraySafeHandle metadata;
const int Iterations = 1000;
[Benchmark(OperationsPerInvoke = Iterations)]
public unsafe void Run()
{
string payload = Payloads[PayloadSize];
var status = new Status(StatusCode.OK, payload);
for (int i = 0; i < Iterations; i++)
{
call.StartSendStatusFromServer(this, status,
metadata, false, null, WriteFlags.NoCompress);
}
}
void ISendStatusFromServerCompletionCallback.OnSendStatusFromServerCompletion(bool success) { }
}
}
Loading…
Cancel
Save