-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Crankier server #12406
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
Merged
Merged
Crankier server #12406
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4b86a01
move server into Crankier
staff0rd c3f50f6
added ConnectionCounter
staff0rd 9dc0356
allow logging
staff0rd 1ff616b
SendPayload
staff0rd ff6a5e2
copyright header
staff0rd 3964e2d
default configuration
staff0rd 0a55f48
brackets
staff0rd fe74071
whitespace
staff0rd caeda96
remove extra options
staff0rd 5d16e1b
thread safety
staff0rd 1038cec
Merge branch 'crankier-server' of github.com:staff0rd/aspnetcore into…
staff0rd e56d1f5
pr comments
staff0rd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/SignalR/perf/benchmarkapps/Crankier/Commands/ServerCommand.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http.Connections; | ||
using Microsoft.Extensions.CommandLineUtils; | ||
using static Microsoft.AspNetCore.SignalR.Crankier.Commands.CommandLineUtilities; | ||
using System.Diagnostics; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.AspNetCore.SignalR.Crankier.Server; | ||
|
||
namespace Microsoft.AspNetCore.SignalR.Crankier.Commands | ||
{ | ||
internal class ServerCommand | ||
{ | ||
public static void Register(CommandLineApplication app) | ||
{ | ||
app.Command("server", cmd => | ||
{ | ||
var logLevelOption = cmd.Option("--log <LOG_LEVEL>", "The LogLevel to use.", CommandOptionType.SingleValue); | ||
|
||
cmd.OnExecute(() => | ||
{ | ||
LogLevel logLevel = Defaults.LogLevel; | ||
|
||
if (logLevelOption.HasValue() && !Enum.TryParse(logLevelOption.Value(), out logLevel)) | ||
{ | ||
return InvalidArg(logLevelOption); | ||
} | ||
return Execute(logLevel); | ||
}); | ||
}); | ||
} | ||
|
||
private static int Execute(LogLevel logLevel) | ||
{ | ||
Console.WriteLine($"Process ID: {Process.GetCurrentProcess().Id}"); | ||
|
||
var config = new ConfigurationBuilder() | ||
.AddEnvironmentVariables(prefix: "ASPNETCORE_") | ||
.Build(); | ||
|
||
var host = new WebHostBuilder() | ||
.UseConfiguration(config) | ||
.ConfigureLogging(loggerFactory => | ||
{ | ||
loggerFactory.AddConsole().SetMinimumLevel(logLevel); | ||
}) | ||
.UseKestrel() | ||
.UseStartup<Startup>(); | ||
|
||
host.Build().Run(); | ||
|
||
return 0; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
src/SignalR/perf/benchmarkapps/Crankier/Server/ConnectionCounter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
|
||
namespace Microsoft.AspNetCore.SignalR.Crankier.Server | ||
{ | ||
public class ConnectionCounter | ||
{ | ||
private int _totalConnectedCount; | ||
private int _peakConnectedCount; | ||
private int _totalDisconnectedCount; | ||
private int _receivedCount; | ||
|
||
private readonly object _lock = new object(); | ||
|
||
public ConnectionSummary Summary | ||
{ | ||
get | ||
{ | ||
lock (_lock) | ||
{ | ||
return new ConnectionSummary | ||
{ | ||
CurrentConnections = _totalConnectedCount - _totalDisconnectedCount, | ||
PeakConnections = _peakConnectedCount, | ||
TotalConnected = _totalConnectedCount, | ||
TotalDisconnected = _totalDisconnectedCount, | ||
ReceivedCount = _receivedCount | ||
}; | ||
} | ||
} | ||
} | ||
|
||
public void Receive(string payload) | ||
{ | ||
lock (_lock) | ||
{ | ||
_receivedCount += payload.Length; | ||
} | ||
} | ||
|
||
public void Connected() | ||
{ | ||
lock (_lock) | ||
{ | ||
_totalConnectedCount++; | ||
_peakConnectedCount = Math.Max(_totalConnectedCount - _totalDisconnectedCount, _peakConnectedCount); | ||
} | ||
} | ||
|
||
public void Disconnected() | ||
{ | ||
lock (_lock) | ||
{ | ||
_totalDisconnectedCount++; | ||
} | ||
} | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
src/SignalR/perf/benchmarkapps/Crankier/Server/ConnectionCounterHostedService.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Diagnostics; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace Microsoft.AspNetCore.SignalR.Crankier.Server | ||
{ | ||
public class ConnectionCounterHostedService : IHostedService, IDisposable | ||
{ | ||
private Stopwatch _timeSinceFirstConnection; | ||
private readonly ConnectionCounter _counter; | ||
private ConnectionSummary _lastSummary; | ||
private Timer _timer; | ||
private int _executingDoWork; | ||
|
||
public ConnectionCounterHostedService(ConnectionCounter counter) | ||
{ | ||
_counter = counter; | ||
_timeSinceFirstConnection = new Stopwatch(); | ||
} | ||
|
||
public Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
_timer = new Timer(DoWork, null, TimeSpan.Zero, TimeSpan.FromSeconds(1)); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private void DoWork(object state) | ||
staff0rd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (Interlocked.Exchange(ref _executingDoWork, 1) == 0) | ||
{ | ||
var summary = _counter.Summary; | ||
|
||
if (summary.PeakConnections > 0) | ||
{ | ||
if (_timeSinceFirstConnection.ElapsedTicks == 0) | ||
{ | ||
_timeSinceFirstConnection.Start(); | ||
} | ||
|
||
var elapsed = _timeSinceFirstConnection.Elapsed; | ||
|
||
if (_lastSummary != null) | ||
{ | ||
Console.WriteLine(@"[{0:hh\:mm\:ss}] Current: {1}, peak: {2}, connected: {3}, disconnected: {4}, rate: {5}/s", | ||
elapsed, | ||
summary.CurrentConnections, | ||
summary.PeakConnections, | ||
summary.TotalConnected - _lastSummary.TotalConnected, | ||
summary.TotalDisconnected - _lastSummary.TotalDisconnected, | ||
summary.CurrentConnections - _lastSummary.CurrentConnections | ||
); | ||
} | ||
|
||
_lastSummary = summary; | ||
} | ||
|
||
Interlocked.Exchange(ref _executingDoWork, 0); | ||
} | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
_timer?.Change(Timeout.Infinite, 0); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_timer?.Dispose(); | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/SignalR/perf/benchmarkapps/Crankier/Server/ConnectionSummary.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.SignalR.Crankier.Server | ||
{ | ||
public class ConnectionSummary | ||
{ | ||
public int TotalConnected { get; set; } | ||
|
||
public int TotalDisconnected { get; set; } | ||
|
||
public int PeakConnections { get; set; } | ||
|
||
public int CurrentConnections { get; set; } | ||
|
||
public int ReceivedCount { get; set; } | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.SignalR; | ||
|
||
namespace Microsoft.AspNetCore.SignalR.Crankier.Server | ||
{ | ||
public class EchoHub : Hub | ||
{ | ||
private ConnectionCounter _counter; | ||
|
||
public EchoHub(ConnectionCounter counter) | ||
{ | ||
_counter = counter; | ||
} | ||
|
||
public async Task Broadcast(int duration) | ||
{ | ||
var sent = 0; | ||
try | ||
{ | ||
var t = new CancellationTokenSource(); | ||
t.CancelAfter(TimeSpan.FromSeconds(duration)); | ||
while (!t.IsCancellationRequested && !Context.ConnectionAborted.IsCancellationRequested) | ||
{ | ||
await Clients.All.SendAsync("send", DateTime.UtcNow); | ||
sent++; | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
} | ||
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; | ||
} | ||
|
||
public Task EchoAll(DateTime time) | ||
{ | ||
return Clients.All.SendAsync("send", time); | ||
} | ||
|
||
public void SendPayload(string payload) | ||
{ | ||
_counter?.Receive(payload); | ||
} | ||
|
||
public DateTime GetCurrentTime() | ||
{ | ||
return DateTime.UtcNow; | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Microsoft.AspNetCore.SignalR.Crankier.Server | ||
{ | ||
public class Startup | ||
{ | ||
private readonly IConfiguration _config; | ||
public Startup(IConfiguration configuration) | ||
{ | ||
_config = configuration; | ||
} | ||
|
||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
var signalrBuilder = services.AddSignalR() | ||
.AddMessagePackProtocol(); | ||
|
||
services.AddSingleton<ConnectionCounter>(); | ||
|
||
services.AddHostedService<ConnectionCounterHostedService>(); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) | ||
{ | ||
app.UseRouting(); | ||
|
||
app.UseEndpoints(endpoints => | ||
{ | ||
endpoints.MapHub<EchoHub>("/echo"); | ||
}); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.