Skip to content

Instantly share code, notes, and snippets.

@ReubenBond ReubenBond/Program.cs Secret
Last active Nov 10, 2018

Embed
What would you like to do?
Ping pong test
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Orleans;
using Orleans.Configuration;
using Orleans.Hosting;
using Orleans.Logging;
using Orleans.Runtime.Configuration;
namespace Repro
{
class Program
{
static void Main(string[] args)
{
var extraOffset = int.Parse(args[0]) * 1000;
var basePort = (Debugger.IsAttached ? 11000 : 10000) + extraOffset;
var siloPorts = new[] {basePort + 1, basePort + 2};
var gatewayPorts = new[] {basePort + 101, basePort + 102};
var siloAddress = IPAddress.Loopback;
ISiloHost CreateSilo(int id) =>
new SiloHostBuilder()
.UseDevelopmentClustering(options => options.PrimarySiloEndpoint = new IPEndPoint(siloAddress, siloPorts[0]))
.UseInMemoryReminderService()
.ConfigureEndpoints(siloAddress, siloPorts[id], gatewayPorts[id])
.Configure<ClusterOptions>(options => options.ClusterId = "helloworldcluster")
.ConfigureApplicationParts(appParts => appParts.AddApplicationPart(typeof(Program).Assembly))
.Configure<StatisticsOptions>(o => o.CollectionLevel = StatisticsLevel.Verbose)
.ConfigureLogging(builder =>
{
builder.SetMinimumLevel(LogLevel.Information);
builder.AddFilter("Orleans.Runtime.SafeTimer", LogLevel.Information);
builder.AddFilter("Orleans.Runtime.ReminderService.LocalReminderService", LogLevel.Information);
builder.AddFilter("Orleans.Runtime.GrainDirectory.LocalGrainDirectory", LogLevel.Information);
builder.AddFilter("Orleans.Runtime.DeploymentLoadPublisher", LogLevel.Warning);
builder.AddFilter("Orleans.Runtime.SiloControl", LogLevel.Warning);
builder.AddFilter("Orleans.LifecycleSubject", LogLevel.Warning);
builder.AddFilter("Orleans.Runtime.GrainTimer", LogLevel.Warning);
builder.AddFilter("Orleans.Runtime.Management.ManagementGrain", LogLevel.Warning);
builder.AddConsole();
builder.AddFile($"hostlog-{DateTime.Now.ToString("s").Replace(":", ".")}");
})
.Build();
var silos = new[] {CreateSilo(0), CreateSilo(1)};
silos[0].StartAsync().Wait();
Task.Delay(5000);
silos[1].StartAsync().Wait();
Task.Delay(5000);
var client =
new ClientBuilder()
.UseStaticClustering(new IPEndPoint(siloAddress, gatewayPorts[0]), new IPEndPoint(siloAddress, gatewayPorts[1]))
.Configure<ClusterOptions>(options => options.ClusterId = "helloworldcluster")
.ConfigureApplicationParts(appParts => appParts.AddApplicationPart(typeof(Program).Assembly))
.Configure<StatisticsOptions>(o => o.CollectionLevel = StatisticsLevel.Verbose)
.ConfigureLogging(builder =>
{
builder.SetMinimumLevel(LogLevel.Information);
builder.AddFilter("Orleans.Runtime.SafeTimer", LogLevel.Information);
builder.AddFilter("Orleans.Runtime.ReminderService.LocalReminderService", LogLevel.Information);
builder.AddFilter("Orleans.Runtime.GrainDirectory.LocalGrainDirectory", LogLevel.Information);
builder.AddConsole();
builder.AddFile($"clientlog-{DateTime.Now.ToString("s").Replace(":", ".")}");
})
.Build();
client.Connect().Wait();
var cts = new CancellationTokenSource();
var grains = new List<ITestGrain>(2);
grains.Add(client.GetGrain<ITestGrain>(0));
var firstId = grains[0].GetRuntimeId().Result;
var i = 1;
while (grains.Count < 2)
{
var newG = client.GetGrain<ITestGrain>(i++);
if (!string.Equals(firstId, newG.GetRuntimeId().Result))
{
grains.Add(newG);
}
}
var log = client.ServiceProvider.GetRequiredService<ILoggerFactory>().CreateLogger("Test");
Task.Run(async () =>
{
var sw = Stopwatch.StartNew();
var calls = 0;
var timeLimit = TimeSpan.FromSeconds(5);
var callLimit = 200;
while (!cts.IsCancellationRequested)
{
try
{
await grains[0].PingOther(grains[1]);
++calls;
var elapsed = sw.Elapsed;
if (elapsed > timeLimit || calls > callLimit)
{
var millis = elapsed.TotalMilliseconds;
var avgMs = millis / calls;
log.LogInformation("{Calls} calls in {ElapsedMillis} ms = {Average} ms/call", calls, millis, avgMs);
calls = 0;
await Task.Delay(500);
sw.Restart();
}
}
catch (Exception ex)
{
Console.WriteLine($"Got exception: {ex}");
}
}
});
Console.WriteLine("Press key to exit...");
Console.ReadLine();
cts.Cancel();
silos[0].StopAsync().Wait();
silos[1].StopAsync().Wait();
}
public interface ITestGrain : IGrainWithIntegerKey
{
Task PingOther(ITestGrain other);
Task Ping();
Task<string> GetRuntimeId();
}
public class TestGrain : Grain, ITestGrain
{
public Task PingOther(ITestGrain other) => other.Ping();
public Task Ping() => Task.CompletedTask;
public Task<string> GetRuntimeId() => Task.FromResult(this.RuntimeIdentity);
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>netcoreapp2.0;net462</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.0.1" />
<PackageReference Include="Microsoft.Orleans.Core" Version="2.0.0" />
<PackageReference Include="Microsoft.Orleans.OrleansCodeGenerator.Build" Version="2.0.0" />
<PackageReference Include="Microsoft.Orleans.OrleansRuntime" Version="2.0.0" />
</ItemGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.