Using SignalR performance counters in an Azure Web Role
By Luke Latham
SignalR performance counters are used to monitor your app’s performance in an Azure Web Role. The counters are captured by Microsoft Azure Diagnostics. You install SignalR performance counters on Azure with signalr.exe, the same tool used for standalone or on-premises apps. Since Azure roles are transient, you configure an app to install and register SignalR performance counters upon startup.
Prerequisites
- Visual Studio 2015
- Microsoft Azure SDK for Visual Studio 2015 (VS2015) Note: Restart your machine after installing the SDK.
- Microsoft Azure subscription: To sign up for a free Azure trial account, see Azure Free Trial.
Creating an Azure Web Role application that exposes SignalR performance counters
Open Visual Studio 2015.
In Visual Studio 2015, select File > New > Project.
In the Templates pane of the New Project window under the Visual C# node, select the Cloud node and select the Azure Cloud Service template. Name the app SignalRPerfCounters and select OK.
In the New Microsoft Azure Cloud Service dialog, select ASP.NET Web Role and select the > button to add the role to the project. Select OK.
In the New ASP.NET Web Application - WebRole1 dialog, select the MVC template, and then select OK.
In Solution Explorer, open the diagnostics.wadcfgx file under WebRole1.
Replace the contents of the file with the following configuration and save the file:
[!code-xmlMain]
1: <?xml version="1.0" encoding="utf-8"?>
2: <DiagnosticsConfiguration xmlns="http://schemas.microsoft.com/ServiceHosting/2010/10/DiagnosticsConfiguration">
3: <PublicConfig>
4: <WadCfg>
5: <DiagnosticMonitorConfiguration overallQuotaInMB="4096">
6: <DiagnosticInfrastructureLogs scheduledTransferLogLevelFilter="Error" />
7: <Logs scheduledTransferPeriod="PT1M" scheduledTransferLogLevelFilter="Error" />
8: <Directories scheduledTransferPeriod="PT1M">
9: <IISLogs containerName ="wad-iis-logfiles" />
10: <FailedRequestLogs containerName ="wad-failedrequestlogs" />
11: </Directories>
12: <WindowsEventLog scheduledTransferPeriod="PT1M">
13: <DataSource name="Application!*[System[(Level=1 or Level=2 or Level=3)]]" />
14: <DataSource name="Windows Azure!*[System[(Level=1 or Level=2 or Level=3 or Level=4)]]" />
15: </WindowsEventLog>
16: <CrashDumps containerName="wad-crashdumps" dumpType="Mini">
17: <CrashDumpConfiguration processName="WaIISHost.exe" />
18: <CrashDumpConfiguration processName="WaWorkerHost.exe" />
19: <CrashDumpConfiguration processName="w3wp.exe" />
20: </CrashDumps>
21: <PerformanceCounters scheduledTransferPeriod="PT1M">
22: <PerformanceCounterConfiguration counterSpecifier="\Memory\Available MBytes" sampleRate="PT3M" />
23: <PerformanceCounterConfiguration counterSpecifier="\Web Service(_Total)\ISAPI Extension Requests/sec" sampleRate="PT3M" />
24: <PerformanceCounterConfiguration counterSpecifier="\Web Service(_Total)\Bytes Total/Sec" sampleRate="PT3M" />
25: <PerformanceCounterConfiguration counterSpecifier="\ASP.NET Applications(__Total__)\Requests/Sec" sampleRate="PT3M" />
26: <PerformanceCounterConfiguration counterSpecifier="\ASP.NET Applications(__Total__)\Errors Total/Sec" sampleRate="PT3M" />
27: <PerformanceCounterConfiguration counterSpecifier="\ASP.NET\Requests Queued" sampleRate="PT3M" />
28: <PerformanceCounterConfiguration counterSpecifier="\ASP.NET\Requests Rejected" sampleRate="PT3M" />
29: <PerformanceCounterConfiguration counterSpecifier="\Processor(_Total)\% Processor Time" sampleRate="PT3M" />
30: <PerformanceCounterConfiguration counterSpecifier="\.NET CLR Memory(w3wp)\% Time in GC" sampleRate="PT10S" />
31: <PerformanceCounterConfiguration counterSpecifier="\.NET CLR Exceptions(w3wp)\# of Exceps Thrown / sec" sampleRate="PT10S" />
32: <PerformanceCounterConfiguration counterSpecifier="\.NET CLR LocksAndThreads(w3wp)\# of current logical Threads" sampleRate="PT10S" />
33: <PerformanceCounterConfiguration counterSpecifier="\.NET CLR LocksAndThreads(w3wp)\# of current physical Threads" sampleRate="PT10S" />
34: <PerformanceCounterConfiguration counterSpecifier="\.NET CLR LocksAndThreads(w3wp)\Current Queue Length" sampleRate="PT10S" />
35: <PerformanceCounterConfiguration counterSpecifier="\.NET CLR LocksAndThreads(w3wp)\Contention Rate / sec" sampleRate="PT10S" />
36: <PerformanceCounterConfiguration counterSpecifier="\.NET CLR Memory(w3wp)\# Bytes in all Heaps" sampleRate="PT10S" />
37: <PerformanceCounterConfiguration counterSpecifier="\.NET CLR Memory(w3wp)\# GC Handles" sampleRate="PT10S" />
38: <PerformanceCounterConfiguration counterSpecifier="\.NET CLR Memory(w3wp)\# of Pinned Objects" sampleRate="PT10S" />
39:
40: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Connections Connected" sampleRate="PT10S" />
41: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Connections Reconnected" sampleRate="PT10S" />
42: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Connections Disconnected" sampleRate="PT10S" />
43: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Connections Current" sampleRate="PT10S" />
44: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Connection Messages Received Total" sampleRate="PT10S" />
45: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Connection Messages Sent Total" sampleRate="PT10S" />
46: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Connection Messages Received/Sec" sampleRate="PT10S" />
47: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Connection Messages Sent/Sec" sampleRate="PT10S" />
48: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Message Bus Messages Received Total" sampleRate="PT10S" />
49: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Message Bus Messages Received/Sec" sampleRate="PT10S" />
50: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Scaleout Message Bus Messages Received/Sec" sampleRate="PT10S" />
51: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Message Bus Messages Published Total" sampleRate="PT10S" />
52: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Message Bus Messages Published/Sec" sampleRate="PT10S" />
53: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Message Bus Subscribers Current" sampleRate="PT10S" />
54: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Message Bus Subscribers Total" sampleRate="PT10S" />
55: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Message Bus Subscribers/Sec" sampleRate="PT10S" />
56: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Message Bus Allocated Workers" sampleRate="PT10S" />
57: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Message Bus Busy Workers" sampleRate="PT10S" />
58: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Message Bus Topics Current" sampleRate="PT10S" />
59: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Errors: All Total" sampleRate="PT10S" />
60: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Errors: All/Sec" sampleRate="PT10S" />
61: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Errors: Hub Resolution Total" sampleRate="PT10S" />
62: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Errors: Hub Resolution/Sec" sampleRate="PT10S" />
63: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Errors: Hub Invocation Total" sampleRate="PT10S" />
64: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Errors: Hub Invocation/Sec" sampleRate="PT10S" />
65: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Errors: Tranport Total" sampleRate="PT10S" />
66: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Errors: Transport/Sec" sampleRate="PT10S" />
67: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Scaleout Streams Total" sampleRate="PT10S" />
68: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Scaleout Streams Open" sampleRate="PT10S" />
69: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Scaleout Streams Buffering" sampleRate="PT10S" />
70: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Scaleout Errors Total" sampleRate="PT10S" />
71: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Scaleout Errors/Sec" sampleRate="PT10S" />
72: <PerformanceCounterConfiguration counterSpecifier="\SignalR(*)\Scaleout Send Queue Length" sampleRate="PT10S" />
73: </PerformanceCounters>
74: </DiagnosticMonitorConfiguration>
75: </WadCfg>
76: <StorageAccount></StorageAccount>
77: </PublicConfig>
78: <PrivateConfig>
79: <StorageAccount name="" key="" endpoint="" />
80: </PrivateConfig>
81: <IsEnabled>true</IsEnabled>
82: </DiagnosticsConfiguration>
Open the Package Manager Console from Tools > NuGet Package Manager. Enter the following commands to install the latest version of SignalR and the SignalR utilities package:
[!code-powershellMain]
1: install-package microsoft.aspnet.signalr
2: install-package microsoft.aspnet.signalr.utils
Configure the app to install the SignalR performance counters into the role instance when it starts up or recycles. In Solution Explorer, right-click on the WebRole1 project and select Add > New Folder. Name the new folder Startup.
Copy the signalr.exe file (added with the Microsoft.AspNet.SignalR.Utils package) from **<project folder>.AspNet.SignalR.Utils.<version>* to the Startup folder you created in the previous step.
In Solution Explorer, right-click the Startup folder and select Add > Existing Item. In the dialog that appears, select signalr.exe and select Add.
Right-click on the Startup folder you created. Select Add > New Item. Select the General node, select Text File, and name the new item SignalRPerfCounterInstall.cmd. This command file will install the SignalR performance counters into the web role.
When Visual Studio creates the SignalRPerfCounterInstall.cmd file, it will automatically open in the main window. Replace the contents of the file with the following script, then save and close the file. This script executes signalr.exe, which adds the SignalR performance counters to the role instance.
[!code-consoleMain]
1: SET SignalR_LogDir=%~dp0Log\
2: MKDIR "%SignalR_LogDir%"
3: cd %~dp0
4: signalr.exe ipc >> "%SignalR_LogDir%SignalR_Log.txt" 2>&1
5: net localgroup "Performance Monitor Users" "Network Service" /ADD >> "%SignalR_LogDir%NetworkAdd.txt" 2>&1
Select the signalr.exe file in Solution Explorer. In the file’s Properties, set Copy to Output Directory to Copy Always.
Repeat the previous step for the SignalRPerfCounterInstall.cmd file.
Right-click on the SignalRPerfCounterInstall.cmd file and select Open With. In the dialog that appears, select Binary Editor and select OK.
In the binary editor, select any leading bytes in the file and delete them. Save and close the file.
Open ServiceDefinition.csdef and add a startup task that executes the SignalrPerfCounterInstall.cmd file when the service starts up:
[!code-xmlMain]
1: <?xml version="1.0" encoding="utf-8"?>
2: <ServiceDefinition name="SignalRPerfCounters" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2015-04.2.6">
3: <WebRole name="WebRole1" vmsize="Small">
4: <Startup>
5: <Task commandLine="Startup\SignalRPerfCounterInstall.cmd" executionContext="elevated" taskType="background" />
6: </Startup>
7: <Sites>
8: <Site name="Web">
9: <Bindings>
10: <Binding name="Endpoint1" endpointName="Endpoint1" />
11: </Bindings>
12: </Site>
13: </Sites>
14: <Endpoints>
15: <InputEndpoint name="Endpoint1" protocol="http" port="80" />
16: </Endpoints>
17: </WebRole>
18: </ServiceDefinition>
Open
Views/Shared/_Layout.cshtml
and remove the jQuery bundle script from the end of the file.[!code-cshtmlMain]
1: <div class="container body-content">
2: @RenderBody()
3: <hr />
4: <footer>
5: <p>© @DateTime.Now.Year - My ASP.NET Application</p>
6: </footer>
7: </div>
8:
9: @Scripts.Render("~/bundles/jquery")
10: @Scripts.Render("~/bundles/bootstrap")
11: @RenderSection("scripts", required: false)
12: </body>
13: </html>
Add a JavaScript client that continuously calls the
increment
method on the server. OpenViews/Home/Index.cshtml
and replace the contents with the following code:[!code-cshtmlMain]
1: @{
2: ViewBag.Title = "Home Page";
3: }
4:
5: <script src="~/Scripts/jquery-1.10.2.min.js"></script>
6: <script src="~/Scripts/jquery.signalR-2.2.1.min.js"></script>
7: <script src="~/signalr/hubs" type="text/javascript"></script>
8:
9: <div id="body">
10: <section class="featured">
11: <div class="content-wrapper">
12: <p>
13: Hello World!
14: </p>
15: <div style="font-size:large;">
16: My Counter: <span id="counter"></span>
17: </div>
18: </div>
19: </section>
20: <section class="content-wrapper main-content clear-fix"></section>
21: </div>
22:
23: <script type="text/javascript">
24: $(document).ready(function () {
25: var hub = $.connection.myHub;
26:
27: hub.client.sendResult = function (x) {
28: console.log('sendResult(' + x + ')');
29: $("#counter").text(x);
30: window.setTimeout(function () {
31: hub.server.increment(x);
32: }, 1000);
33: };
34:
35: $.connection.hub.connected = function () {};
36: $.connection.hub.disconnected = function () {};
37:
38: $.connection.hub.stateChanged(function (change) {
39: console.log('new State' + change.newState);
40: if (change.newState === $.signalR.connectionState.disconnected) {
41: $.connection.hub.start();
42: }
43: if (change.newState === $.signalR.connectionState.reconnecting) {
44: console.log('Re-connecting');
45: } else if (change.newState === $.signalR.connectionState.connected) {
46: console.log('The server is online');
47: }
48: });
49:
50: $.connection.hub.error(function (error) {
51: console.log('error ' + error);
52: });
53:
54: $.connection.hub.logging = true;
55:
56: $.connection.hub.reconnected(function () {
57: console.log('Reconnected');
58: hub.server.increment(0);
59: });
60:
61: $.connection.hub.start().done(function () {
62: console.log('hub started');
63: hub.server.increment(0);
64: });
65: });
66: </script>
Create a new folder in the WebRole1 project named Hubs. Right-click the Hubs folder in Solution Explorer, select Web > SignalR, and select SignalR Hub Class (v2). Name the new hub MyHub.cs and select Add.
MyHub.cs will automatically open in the main window. Replace the contents with the following code, then save and close the file:
[!code-csharpMain]
1: using System.Threading.Tasks;
2: using Microsoft.AspNet.SignalR;
3:
4: namespace WebRole1.Hubs
5: {
6: public class MyHub : Hub
7: {
8: public async Task Increment(int x)
9: {
10: await this.Clients.Caller.sendResult(x + 1);
11: }
12: }
13: }
Crank.exe is a connection density testing tool provided with the SignalR codebase. Since Crank requires a persistent connection, you add one to your site for use when testing. Add a new folder to the WebRole1 project called PersistentConnections. Right-click this folder and select Add > Class. Name the new class file MyPersistentConnections.cs and select Add.
Visual Studio will open the MyPersistentConnections.cs file in the main window. Replace the contents with the following code, then save and close the file:
[!code-csharpMain]
1: using System.Threading.Tasks;
2: using Microsoft.AspNet.SignalR;
3: using Microsoft.AspNet.SignalR.Infrastructure;
4:
5: namespace WebRole1.PersistentConnections
6: {
7: public class MyPersistentConnection : PersistentConnection
8: {
9: protected override Task OnReceived(IRequest request, string connectionId, string data)
10: {
11: //Return data to calling user
12: return Connection.Send(connectionId, data);
13: }
14: }
15: }
Using the
Startup
class, the SignalR objects start when OWIN starts up. Open or create Startup.cs and replace the contents with the following code:[!code-csharpMain]
1: using Microsoft.Owin;
2: using Owin;
3: using WebRole1.PersistentConnections;
4:
5: // Marks this class for automatic OWIN startup
6: [assembly: OwinStartup(typeof(WebRole1.Startup))]
7: namespace WebRole1
8: {
9: public partial class Startup
10: {
11: public void Configuration(IAppBuilder app)
12: {
13: ConfigureAuth(app);
14: // Only needed if "No Authentication" was not selected for the project
15: app.MapSignalR();
16: app.MapSignalR<MyPersistentConnection>("/echo");
17: }
18: }
19: }
In the code above, the
OwinStartup
attribute marks this class to start OWIN. TheConfiguration
method starts SignalR.Test your application in the Microsoft Azure Emulator by pressing F5.
[!NOTE] If you encounter a FileLoadException at MapSignalR, change the binding redirects in web.config to the following:
[!code-xmlMain]
1: <dependentAssembly>
2: <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
3: <bindingRedirect oldVersion="0.0.0.0-2.0.2.0" newVersion="2.0.0.0" />
4: </dependentAssembly>
5: <dependentAssembly>
6: <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
7: <bindingRedirect oldVersion="0.0.0.0-2.0.2.0" newVersion="2.0.0.0" />
8: </dependentAssembly>
Wait about one minute. Open the Cloud Explorer tool window in Visual Studio (View > Cloud Explorer) and expand the path
(Local)\Storage Accounts\(Development)\Tables
. Double-click WADPerformanceCountersTable. You should see SignalR counters in the table data. If you don’t see the table, you may need to re-enter your Azure Storage credentials. You may need to select the Refresh button to see the table in Cloud Explorer or select the Refresh button in the open table window to see data in the table.To test your application in the cloud, update the ServiceConfiguration.Cloud.cscfg file and set the
Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString
to a valid Azure Storage account connection string.[!code-xmlMain]
1: <?xml version="1.0" encoding="utf-8"?>
2: <ServiceConfiguration serviceName="SignalRPerfCounters" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" osFamily="4" osVersion="*" schemaVersion="2015-04.2.6">
3: <Role name="WebRole1">
4: <Instances count="1" />
5: <ConfigurationSettings>
6: <Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=<account-name>;AccountKey=<account-key>" />
7: </ConfigurationSettings>
8: </Role>
9: </ServiceConfiguration>
Deploy the application to your Azure subscription. For details on how to deploy an application to Azure, see How to Create and Deploy a Cloud Service.
Wait a few minutes. In Cloud Explorer, locate the storage account you configured above and find the
WADPerformanceCountersTable
table in it. You should see SignalR counters in the table data. If you don’t see the table, you may need to re-enter your Azure Storage credentials. You may need to select the Refresh button to see the table in Cloud Explorer or select the Refresh button in the open table window to see data in the table.
Special thanks to Martin Richard for the original content used in this tutorial.
|