Merge pull request #13438 from jtattermusch/csharp_more_microbenchmarks

Add more C# microbenchmarks
pull/13444/head
Jan Tattermusch 7 years ago committed by GitHub
commit 2bc15ca776
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 69
      src/csharp/Grpc.Microbenchmarks/CompletionRegistryBenchmark.cs
  2. 69
      src/csharp/Grpc.Microbenchmarks/GCStats.cs
  3. 4
      src/csharp/Grpc.Microbenchmarks/Grpc.Microbenchmarks.csproj
  4. 64
      src/csharp/Grpc.Microbenchmarks/PInvokeByteArrayBenchmark.cs
  5. 67
      src/csharp/Grpc.Microbenchmarks/Program.cs
  6. 3
      src/csharp/Grpc.Microbenchmarks/ThreadedBenchmark.cs

@ -0,0 +1,69 @@
#region Copyright notice and license
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System;
using System.Runtime.InteropServices;
using System.Threading;
using Grpc.Core;
using Grpc.Core.Internal;
using System.Collections.Generic;
using System.Diagnostics;
namespace Grpc.Microbenchmarks
{
public class CompletionRegistryBenchmark
{
GrpcEnvironment environment;
public void Init()
{
environment = GrpcEnvironment.AddRef();
}
public void Cleanup()
{
GrpcEnvironment.ReleaseAsync().Wait();
}
public void Run(int threadCount, int iterations)
{
Console.WriteLine(string.Format("CompletionRegistryBenchmark: threads={0}, iterations={1}", threadCount, iterations));
var threadedBenchmark = new ThreadedBenchmark(threadCount, () => ThreadBody(iterations));
threadedBenchmark.Run();
// TODO: parametrize by number of pending completions
}
private void ThreadBody(int iterations)
{
var completionRegistry = new CompletionRegistry(environment);
var ctx = BatchContextSafeHandle.Create();
var completionDelegate = new OpCompletionDelegate((success) => {});
var stopwatch = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
{
completionRegistry.Register(ctx.DangerousGetHandle(), completionDelegate);
var callback = completionRegistry.Extract(completionRegistry.LastRegisteredKey);
}
stopwatch.Stop();
Console.WriteLine("Elapsed millis: " + stopwatch.ElapsedMilliseconds);
ctx.Dispose();
}
}
}

@ -0,0 +1,69 @@
#region Copyright notice and license
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System;
using Grpc.Core;
using Grpc.Core.Internal;
namespace Grpc.Microbenchmarks
{
internal class GCStats
{
readonly object myLock = new object();
GCStatsSnapshot lastSnapshot;
public GCStats()
{
lastSnapshot = new GCStatsSnapshot(GC.CollectionCount(0), GC.CollectionCount(1), GC.CollectionCount(2));
}
public GCStatsSnapshot GetSnapshot(bool reset = false)
{
lock (myLock)
{
var newSnapshot = new GCStatsSnapshot(GC.CollectionCount(0) - lastSnapshot.Gen0,
GC.CollectionCount(1) - lastSnapshot.Gen1,
GC.CollectionCount(2) - lastSnapshot.Gen2);
if (reset)
{
lastSnapshot = newSnapshot;
}
return newSnapshot;
}
}
}
public class GCStatsSnapshot
{
public GCStatsSnapshot(int gen0, int gen1, int gen2)
{
this.Gen0 = gen0;
this.Gen1 = gen1;
this.Gen2 = gen2;
}
public int Gen0 { get; }
public int Gen1 { get; }
public int Gen2 { get; }
public override string ToString()
{
return string.Format("[GCCollectionCount: gen0 {0}, gen1 {1}, gen2 {2}]", Gen0, Gen1, Gen2);
}
}
}

@ -15,6 +15,10 @@
<ProjectReference Include="../Grpc.Core/Grpc.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.1.1-beta" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />

@ -0,0 +1,64 @@
#region Copyright notice and license
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System;
using System.Runtime.InteropServices;
using System.Threading;
using Grpc.Core;
using Grpc.Core.Internal;
using System.Collections.Generic;
using System.Diagnostics;
namespace Grpc.Microbenchmarks
{
public class PInvokeByteArrayBenchmark
{
static readonly NativeMethods Native = NativeMethods.Get();
public void Init()
{
}
public void Cleanup()
{
}
public void Run(int threadCount, int iterations, int payloadSize)
{
Console.WriteLine(string.Format("PInvokeByteArrayBenchmark: threads={0}, iterations={1}, payloadSize={2}", threadCount, iterations, payloadSize));
var threadedBenchmark = new ThreadedBenchmark(threadCount, () => ThreadBody(iterations, payloadSize));
threadedBenchmark.Run();
}
private void ThreadBody(int iterations, int payloadSize)
{
var payload = new byte[payloadSize];
var stopwatch = Stopwatch.StartNew();
for (int i = 0; i < iterations; i++)
{
var gcHandle = GCHandle.Alloc(payload, GCHandleType.Pinned);
var payloadPtr = gcHandle.AddrOfPinnedObject();
Native.grpcsharp_test_nop(payloadPtr);
gcHandle.Free();
}
stopwatch.Stop();
Console.WriteLine("Elapsed millis: " + stopwatch.ElapsedMilliseconds);
}
}
}

@ -20,14 +20,81 @@ using System;
using Grpc.Core;
using Grpc.Core.Internal;
using Grpc.Core.Logging;
using CommandLine;
using CommandLine.Text;
namespace Grpc.Microbenchmarks
{
class Program
{
public enum MicrobenchmarkType
{
CompletionRegistry,
PInvokeByteArray,
SendMessage
}
private class BenchmarkOptions
{
[Option("benchmark", Required = true, HelpText = "Benchmark to run")]
public MicrobenchmarkType Benchmark { get; set; }
}
public static void Main(string[] args)
{
GrpcEnvironment.SetLogger(new ConsoleLogger());
var parserResult = Parser.Default.ParseArguments<BenchmarkOptions>(args)
.WithNotParsed(errors => {
Console.WriteLine("Supported benchmarks:");
foreach (var enumValue in Enum.GetValues(typeof(MicrobenchmarkType)))
{
Console.WriteLine(" " + enumValue);
}
Environment.Exit(1);
})
.WithParsed(options =>
{
switch (options.Benchmark)
{
case MicrobenchmarkType.CompletionRegistry:
RunCompletionRegistryBenchmark();
break;
case MicrobenchmarkType.PInvokeByteArray:
RunPInvokeByteArrayBenchmark();
break;
case MicrobenchmarkType.SendMessage:
RunSendMessageBenchmark();
break;
default:
throw new ArgumentException("Unsupported benchmark.");
}
});
}
static void RunCompletionRegistryBenchmark()
{
var benchmark = new CompletionRegistryBenchmark();
benchmark.Init();
foreach (int threadCount in new int[] {1, 1, 2, 4, 8, 12})
{
benchmark.Run(threadCount, 4 * 1000 * 1000);
}
benchmark.Cleanup();
}
static void RunPInvokeByteArrayBenchmark()
{
var benchmark = new PInvokeByteArrayBenchmark();
benchmark.Init();
foreach (int threadCount in new int[] {1, 1, 2, 4, 8, 12})
{
benchmark.Run(threadCount, 4 * 1000 * 1000, 0);
}
benchmark.Cleanup();
}
static void RunSendMessageBenchmark()
{
var benchmark = new SendMessageBenchmark();
benchmark.Init();
foreach (int threadCount in new int[] {1, 1, 2, 4, 8, 12})

@ -46,6 +46,7 @@ namespace Grpc.Microbenchmarks
public void Run()
{
Console.WriteLine("Running threads.");
var gcStats = new GCStats();
var threads = new List<Thread>();
for (int i = 0; i < runners.Count; i++)
{
@ -58,7 +59,7 @@ namespace Grpc.Microbenchmarks
{
thread.Join();
}
Console.WriteLine("All threads finished.");
Console.WriteLine("All threads finished (GC Stats Delta: " + gcStats.GetSnapshot() + ")");
}
}
}

Loading…
Cancel
Save