import com.visibleworkings.trace.Trace;
import com.visibleworkings.trace.TraceController;
import java.util.Properties;

public class TimingBuffer {

    // Send trace messages to this object. The argument to the
    // constructor is a "subsystem". Each subsystem's tracing can be
    // controlled independently.
    static private Trace tr = new Trace("TimingBuffer");

    public static void main(String[] args) {
        TraceController.start(); 

        // This is how timing messages are turned on for a subsystem.
        // This says that we want timing messages to go to the buffer.
        TraceController.setProperty("TraceLog_TimingBuffer_timing", "on");

        System.out.println("Please be patient. This example takes about");
        System.out.println("30 seconds on a fast (circa 02000) laptop with jdk1.2.2.");
        System.out.println("It takes about 70 seconds with jdk1.3.");

        // Testing the "timing" variable is a way of avoiding the
        // function call if timing isn't turned on. Except for tight
        // inner loops, this kind of optimization is probably a waste
        // of your (human) time.
        if (tr.timing) tr.timingm("Starting message loop.");

        for (int i = 0; i < 1000000; i++)
            tr.usagem("By default, usage messages go only into the transient buffer.");

        if (tr.timing) tr.timingm("Ending message loop.");

        // As you'll see, that was pretty fast. Let's dump the
        // transient buffer into the log, just to make sure there's
        // something there.
        TraceController.setProperty("TraceBuffer_dump", "true");

        // Just for fun, let's see what cost string concatenation of
        // objects adds...
        if (tr.timing) tr.timingm("Starting concatenation message loop.");

        for (int i = 0; i < 1000000; i++)
            tr.usagem("Iteration " + i);

        if (tr.timing) tr.timingm("Ending message loop.");

    }
}


