API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Tracking Elapsed Time In LotusScript Agent Using LS2J
The standard NotesDateTime objects are pretty good for tracking the time it takes your agent to run, but it only tracks down to the second. What if you need something more granular? I know that the folks who created TeamStudio have a product that will tell you how long each and every statement of an agent takes to run. But here's a simplified method that you can use to track times down to the millisecond in LotusScript.

For this implementation, we're going to make use of the date time options in Java. Since the agent we're writing is in LotusScript (otherwise you could just use the Java method directly) we need to make use of LS2J (LotusScript to Java) in Notes 6. First, let's go over the Java piece. Create a new Java script library (not a "JavaScript script library", a "Java script library"). Name it ElapsedTime and put in this Java code:

import java.util.*;

public class ElapsedTime {
    private Date startDate = new Date();
    private Date endDate = new Date();
    
    public void start() {
        startDate = new Date();
    }
    public void end() {
        endDate = new Date();
    }
    public long duration() {
        long t1 = startDate.getTime();
        long t2 = endDate.getTime();
        long difference = t2 - t1;
        return difference;
    }
}

Save and close the script library. If you know Java, this is pretty straightforward. If you don't know Java, the script library is basically creating a new object where you can flag the start and stop time and then compute the difference between those times.

Now, if we want to get a granular "stopwatch" of elapsed time in our LotusScript agent, we need to do a few things. First, in the (Options) area we need to bring in the LotusScript extension to interface with Java, and bring in our library.

(Options)
Uselsx "*javacon"
Use "ElapsedTime"

Now, in our agent we can interface with that library. We have to call the start method when you want to start the "stopwatch" and the end method when you want to stop the "stopwatch". Then you can call the duration method to find out the time difference. For example:

Sub Process()
   Dim js As JAVASESSION
   Dim timerClass As JAVACLASS
   Dim timerObject As JavaObject
   Dim diff As Long
   
   Set js = New JAVASESSION
   Set timerClass = js.GetClass("ElapsedTime")
   Set timerObject = timerClass.CreateObject
   
   Call timerObject.start
   ' ... Process here ...
   Call timerObject.end
   diff = timerObject.duration()
   Print "The process took " & Cstr(diff) & " milliseconds."
End Sub

The first part is what you always need to do to implement LS2J - building the LotusScript objects that will interface with the Java object and then initialize it. Next, you start the stopwatch, do your processing, then immediately stop the stopwatch. You can then find the time difference in milliseconds.