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
29 changes: 29 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,11 @@ namespace BenchmarkServer.Hubs
{
public class EchoHub : Hub
{
private static int _connectionCount = 0;
private static int _peakConnectionCount = 0;

static DateTime _serverStart = DateTime.Now;

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

public static string Status => $"{_connectionCount} current, {_peakConnectionCount} peak.";

public void LogConnections(string label) {
var connectionCount = Interlocked.Read(ref _connectionCount);
if (connectionCount < 100 || connectionCount % 100 == 0)
{
var timeSinceServerStart = DateTime.Now.Subtract(_serverStart).ToString(@"hh\:mm\:ss");
Console.WriteLine($"[{timeSinceServerStart}] {label}: {Status}");
}
}

public override Task OnConnectedAsync() {
var newConnectionCount = Interlocked.Increment(ref _connectionCount);
_peakConnectionCount = Math.Max(_connectionCount, _peakConnectionCount);
LogConnections("Connected");
return Task.CompletedTask;
}

public override Task OnDisconnectedAsync(Exception exception) {
Interlocked.Decrement(ref _connectionCount);
LogConnections("Disconnected");
return Task.CompletedTask;
}

public DateTime Echo(DateTime time)
{
return time;
Expand Down