Skip to content

SignalR: Benchmark server logging #9264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/SignalR/perf/benchmarkapps/BenchmarkServer/Hubs/EchoHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ namespace BenchmarkServer.Hubs
{
public class EchoHub : Hub
{
private EchoHubConnectionCounter _counter;

public EchoHub(EchoHubConnectionCounter counter)
{
_counter = counter;
}

public async Task Broadcast(int duration)
{
var sent = 0;
Expand All @@ -30,6 +37,17 @@ public async Task Broadcast(int duration)
Console.WriteLine("Broadcast exited: Sent {0} messages", sent);
}

public override Task OnConnectedAsync()
{
_counter?.Connected();
return Task.CompletedTask;
}

public override Task OnDisconnectedAsync(Exception exception) {
_counter?.Disconnected();
return Task.CompletedTask;
}

public DateTime Echo(DateTime time)
{
return time;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;

public class EchoHubConnectionCounter
{
private int _connectionCount;
private int _peakConnectionCount;

private readonly object _lock = new object();

private DateTime _start = DateTime.Now;

public string Status
{
get
{
lock (_lock)
{
return $"{_connectionCount} current, {_peakConnectionCount} peak.";
}
}
}

private void LogConnections(string label)
{
int connectionCount;
lock (_lock)
{
connectionCount = _connectionCount;
}
if (connectionCount < 100 || connectionCount % 100 == 0)
{
var timeSinceServerStart = DateTime.Now.Subtract(_start).ToString(@"hh\:mm\:ss");
Console.WriteLine($"[{timeSinceServerStart}] {label}: {Status}");
}
}

public void Connected()
{
lock (_lock)
{
_connectionCount++;
_peakConnectionCount = Math.Max(_connectionCount, _peakConnectionCount);
}
LogConnections("Connected");
}

public void Disconnected() {
lock (_lock) {
_connectionCount--;
}
LogConnections("Disconnected");
}
}
2 changes: 2 additions & 0 deletions src/SignalR/perf/benchmarkapps/BenchmarkServer/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public void ConfigureServices(IServiceCollection services)
{
signalrBuilder.AddStackExchangeRedis(redisConnectionString);
}

services.AddSingleton<EchoHubConnectionCounter>();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
Expand Down