Monday, April 8, 2019

Timer Objects for Network Latency

The heart of the Timer class.
I left out a simple tip from my "Tricks I Learned At Apple: Steve Jobs Load Testing" piece about timer objects. Below, is a complete, yet simple, Timer object class I wrote shortly after leaving Apple when I was working with SMS Hayes AT commands and RESTful APIs.


Exponential Notification

Timer objects do nothing more than measure the time it takes for a server's request/response loop to complete. Since this type of call is made over a network, it might finish very quickly (as expected) or, if the network is down or congested, it could take along time. If it takes a long time, the system admins will want to know. A good notification method is not to send an e-mail update or text message every single minute, or so – that ends up flooding people's inboxes. Instead, an exponential notification would be a much better idea. For example, notify the system administrators immediately, then wait one minute before the next notification, then wait two minutes, four minutes, eight minutes, etc. Finally, send a last notification once the issue's fixed.

Initiating the timer is simple...

Timer timer = Timer.startNewTimer();
NSLog.debug.appendln("Start time = " + timer.startTime());
Response response = saleTransaction.submitTransaction();
timer.stop();
NSLog.debug.appendln("Stop time = " + timer.stopTime());


And, lastly, the complete Java timer class is anticlimactic.


package com.woextras;

import com.webobjects.foundation.NSTimestamp;

public class Timer
{
private NSTimestamp _startTime = null;
private NSTimestamp _stopTime = null;

public static Timer startNewTimer()
{
Timer timer = new Timer();
timer.start();
return timer;
}
public void start()
{
_startTime = new NSTimestamp();
}

public void stop()
{
_stopTime = new NSTimestamp();
}
public NSTimestamp startTime()
{
return _startTime;
}
public NSTimestamp stopTime()
{
return _stopTime;
}
public Long elapsedTime()
{
long completionTime = -1;
if (_startTime != null)
{
long startTime = _startTime.getTime();
long stopTime;
if (_stopTime != null)
{
stopTime = _stopTime.getTime();
} else
{
stopTime = new NSTimestamp().getTime();
}
completionTime = (stopTime - startTime) / 1000L;
}
return completionTime;
}
}

No comments: