Skip to content
Max S edited this page Dec 13, 2016 · 4 revisions

Real-time Stats

Both the slave and cache servers have access to real-time statistics class that allows for some automation in statistics gathering and dumping it into statistics logs. The class is called snipe.lib.RealtimeStats and it's a parametric class. The class parameter is the statistics container that will hold the gathered values. You define the class extending RealtimeStats and set up some things in it like the name, init function, log function, and the message to write to logs. When the statistics value changes, you change it in the container, and call the RealtimeStats.updated() notification method. If a minute has passed since the last stats save to log, the RealtimeStats.update() method will be called. This method should write a message to log with statistics values.

Note that this class is not inherently thread-safe. You will need to implement some thread safety around it when you use it on cache server.

Let's take a look at an example taken from the core:

import snipe.lib.RealtimeStats;

private class RTStats extends RealtimeStats<{
  requestCalls: Float,
  requestTime: Float
  }>
{
  public function new(logf: String -> Void)
    {
      super(logf);
      name = 'url requester';
    }

  override function init()
    {
      return {
        requestCalls: 0.0,
        requestTime: 0.0
        };
    }

  override function update()
    {
      log(info.requestCalls + ' calls, total ' +
        info.requestTime + ' sec spent, avg ' +
        Util.round((info.requestTime / info.requestCalls) * 1000.0, 2) + ' ms');
    }
}

This example defines the actual stats class. The class parameter type has "requestCalls" and "requestTime" fields defined. This is the stats container type definition. The init() method will be used to set the container object up. The update() method logs the current contents to logfile. Note the local 'log()` method. This method should be given in constructor.

Let's take a look at the class that uses the real-time stats (also taken from the core):

class URLRequester
{
  // ...

  var _stats: RTStats;

  public function new(s: Server)
    {
      // ...

      _stats = new RTStats(server.stats);
    }

  public function request(params: Dynamic)
    {
      _stats.info.requestCalls++;
      var t1 = Sys.time();

      // ...

      _stats.info.requestTime += Sys.time() - t1;
      _stats.updated();
    }

We pass the Server.stats method in the constructor. Whenever the class receives a request() call, it increases the stats counters and calls the updated() method for notification. If enough time passed since the last save, the stats will be dumped into the logfile.

Clone this wiki locally