Skip to main content

Custom Performance Counters in Alive

If you've missed previous posts about Alive, you can read more about it at

The real power of Alive, is when it comes to custom performance counters. In your application you have events that would be nice to track in real time. A new order is put, someone paid with their credit card, a user logged in.  For this you can create custom performance counters.

Test application for creating custom performance counters

Create a new performance counter

It is easy to create a new performance counter on your development machine. Open up server explorer in Visual Studio. Right click on Performance Counters and select "Create New Category".

Create a new performance counter category in Server Explorer

This gives you a slick interface where you can create your new counter.

Create a new performance counter category interface

Create a new web application and include Alive.

Install-Package LiteMediaAlive

Edit your web.config so Alive configuration looks like following.

<Alive>
  <settings columns="3" />
  <counters>
    <groups>
      <group name="Test" updateLatency="1000">
        <counter name="Test" categoryName="Test Category" counterName="Test Increment" />
      </group>
    </groups>
  </counters>
</Alive>

Create a new ASPX page.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Index.aspx.cs" Inherits="LiteMedia.Alive.Web.Test.Index" %>
<!DOCTYPE html5>
<html>
    <head>
        <title>Alive custom performance counter</title>
        <style>
            div { display: block; }
            iframe { width: 100%; height: 500px; border: none; }
        </style>
    </head>
    <body>
        <form runat="server">
        <div>
            <h1>Press the button to increment the counter</h1>
            <asp:ScriptManager runat="server">
            </asp:ScriptManager>
            <asp:UpdatePanel runat="server">
                <ContentTemplate>
                    <asp:Button runat="server" Text="Press me" OnClick="IncreaseCounter" />
                </ContentTemplate>
            </asp:UpdatePanel>
            <iframe src="/Alive.axd/" />
        </div>
        </form>
    </body>
</html>

We bind the button to a method in code behind that increases the counter.

protected void IncreaseCounter(object sender, EventArgs e)
{
    using (var counter = new PerformanceCounter("Test Category", "Test Increment", readOnly: false))
    {
        counter.Increment();
    }
}

Bam! You're done! You can download the whole example from here.

LiteMedia.Alive.CustomPerformanceCounter.zip

There are a lot of performance counter types to explore. There's an excellent article on The Code Project about performance counters.

Create performance counters in your production environment

It is a common scenario that you can't reach the production environment from your development machine. In that case I use this piece of F# to create my performance counters.

open System.Diagnostics

let args = System.Environment.GetCommandLineArgs()

printf "LiteMedia, Mikael Lundin\n" printf "Create performance counters\n"

if args.Length <> 3 then printf "Usage: CreatePerfmon [category name] [category description] "

else let name = args.[1] let description = args.[2]

let counterCreation = new CounterCreationDataCollection()

let create name description counterType = new CounterCreationData(name, description, counterType)

create "# operations executed" "Number of total operations executed" PerformanceCounterType.NumberOfItems32 |> counterCreation.Add |> ignore

create "# operations / sec" "Number of operations executed per second" PerformanceCounterType.RateOfCountsPerSecond32 |> counterCreation.Add |> ignore

create "average time per operation" "Average duration per operation execution" PerformanceCounterType.AverageTimer32 |> counterCreation.Add |> ignore

create "average time per operation base" "Average duration per operation execution base" PerformanceCounterType.AverageBase |> counterCreation.Add |> ignore

PerformanceCounterCategory.Create(name, description, PerformanceCounterCategoryType.MultiInstance, counterCreation) |> ignore

Compile it into a runnable exe and execute on your production environment like this.

CreateCounter.exe "New orders" "Increments when new orders are added to the system"

This will give you a performance category on the server called "New orders" and it will contain the following counters.

  • # operations executed
  • # operations / sec
  • average time per operation

This is a really simple way to keep an eye on the production environment and that you're recieving new orders in the rate that you should.

comments powered by Disqus