2

This must be easy but my brain refuses to work it out.

In ASP.net it's straight-forward to create a global "page request counter" using the application state. Just increment an application variable:

Application("Hitcount") = Application("HitCount") + 1 

or whatever. But how would I measure only those hits received in the past minute? I require an ongoing count of the application hits over the previous 60 seconds, to enable me to do some traffic management of the incoming requests. E.g. if the current load is over 1,000 hits per minute then redirect somewhere.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
mel
  • 88
  • 6
  • 1
    Insert into a database (or Application) a "hit record" along with a timestamp. Then your query would retrieve the average number of requests per minute over the past 10 minutes or so. – mason Sep 19 '14 at 16:24
  • There are a lot of metrics available through Windows already (although they tend to be per second rather than per minute). There are quite a few sites with recommendations on which ones are probably most significant and why, e.g. [Important IIS7 Counters](http://blog.monitis.com/2012/04/02/important-iis7-counters/). – Andrew Morton Sep 19 '14 at 16:40

2 Answers2

3

You might be able to do this using memory cache. This would calculate a rolling request count over 10 minutes. I haven't tested the performance of this, though.

public BaseController() : base() 
{
    CacheItemPolicy policy = new CacheItemPolicy();
    policy.AbsoluteExpiration = DateTime.UtcNow.AddMinutes(10);

    MemoryCache.Default.Add(Guid.NewGuid(), "RequestCount", policy);
}

public int RequestCountPerMinute
{
    get
    {
        return MemoryCache.Default
           .Where(kv => kv.Value.ToString() == "RequestCount").Count() / 10;
    }
}
jamesSampica
  • 12,230
  • 3
  • 63
  • 85
  • Referring to [http://stackoverflow.com/a/16689455/1115360](http://stackoverflow.com/a/16689455/1115360) it seems to me that that would not survive an application pool restart and perhaps not extreme memory pressure. – Andrew Morton Sep 19 '14 at 16:55
  • @AndrewMorton An app pool restart isn't that big of a deal. It only takes 10 minutes to build up the rolling average. If all we are concerned about is "the past minute" you could change the cache policy to 60 seconds. – jamesSampica Sep 19 '14 at 17:45
  • Thanks for this; cache expiration policy is a good solution. However I found that using 'MemoryCache.Default.Add()' was a bit unstable for me and seemed to reset frequently. Currently working well with 'cache.insert()' with 60 sec expiry. – mel Sep 25 '14 at 12:29
2

You are probably better off using a log analysis tool that will give you those stats from analyzing the request log, using a wider time window. There are some pretty good ones that let you switch between historical view time windows and view your peak traffic in a day, week, month.

I recommend you not insert a hit record into a database unless your site volume is low. Avoid tying your request latency to a database unless you are sure that the performance overhead is within allowable tolerances. Instrumentation should strive not to actually impact the server that it measures, except for a very mild overhead, and it is why log analysis after the fact works well.

The first order of business is to tweak IIS to add some non-default fields that are part of the standard W3C logging format.

http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/676400bc-8969-4aa7-851a-9319490a9bbb.mspx?mfr=true

IIS Logging

One way to configure these advanced fields is through the IIS Manager, under -> Logging Logging -> Select Fields

Fields like bytes recvd/sent and timespent are needed in order to use one of the 3rd party analyzers to get good charts.

codenheim
  • 20,467
  • 1
  • 59
  • 80
  • How would the OP do a redirect based on the data from the IIS logs? – jamesSampica Sep 19 '14 at 17:50
  • @Shoe - What do redirects have to do with analyzing site traffic? – codenheim Sep 19 '14 at 20:13
  • As per last sentence of OP `if the current load is over 1,000 hits per minute then redirect somewhere.` – jamesSampica Sep 19 '14 at 20:33
  • Oh, I didn't read that the first time, you are correct, log analysis isn't realtime and wouldn't solve the problem. I would have recommended a smart firewall or load balancer, even a free VM based load balancer running Linux could do that. – codenheim Sep 20 '14 at 17:41