Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35414

Re: Logging database statement duration in a DRY way

Newsgroups comp.lang.java.programmer
Date 2016-08-19 10:06 -0700
References <87wpjdhrbm.fsf@Equus.decebal.nl> <np7ap5$sqd$1@dont-email.me>
Message-ID <0fb3031a-9cb3-46c7-8d8d-5c2cf25416ea@googlegroups.com> (permalink)
Subject Re: Logging database statement duration in a DRY way
From Eric Douglas <e.d.programmer@gmail.com>

Show all headers | View raw


On Friday, August 19, 2016 at 12:06:09 PM UTC-4, Martin Gregorie wrote:
> You could write a StopWatch class with start() and stop() methods:

Someone already did.  I made a copy.


import java.text.SimpleDateFormat;
import java.util.Calendar;  

/**
 * @author Carlos Quintanilla
 */

public class StopWatch {
    // constants
    private final long nsPerTick = 100;
    private final long nsPerMs = 1000000;
    private final long nsPerSs = 1000000000;
    private final long nsPerMm = 60000000000L;
    private final long nsPerHh = 3600000000000L;

    private long startTime = 0;
    private long stopTime = 0;
    private boolean running = false;
    
    /**
     * Starts measuring elapsed time
     * for an interval.
     */
    public void start() {
        this.startTime = System.nanoTime();       
        this.running = true;
    }
    
    /**
     * Stops measuring elapsed time
     * for an interval.
     */
    public void stop() {
        this.stopTime = System.nanoTime();
        this.running = false;
    }
    
    /**
     * Stops time interval measurement 
     * and resets the elapsed time to zero.
     */ 
    public void reset() {
        this.startTime = 0;
        this.stopTime = 0;
        this.running = false;
    }
    
    /**
     * Gets the total elapsed time measured 
     * by the current instance, in nanoseconds.
     * 1 Tick = 100 nanoseconds 
     * @return number of ticks 
     */
    public long getElapsedTicks() {
        long elapsed;
        if (running) {
             elapsed = (System.nanoTime() - startTime);
        }
        else {
            elapsed = (stopTime - startTime);
        }
        return elapsed / nsPerTick;
    }
    
    /**
     * Gets the total elapsed time measured 
     * by the current instance, in milliseconds.
     * 10000 Ticks = 1 millisecond (1000000 nanoseconds)
     * @return number of millis 
     */
    public long getElapsedMilliseconds() {
        long elapsed;
        if (running) {
             elapsed = (System.nanoTime() - startTime);
        }
        else {
            elapsed = (stopTime - startTime);
        }
        return elapsed / nsPerMs;
    }
    
    /**
     * Gets the total elapsed time measured 
     * by the current instance, in seconds.
     * 10000000 Ticks = 1 second (1000 milliseconds)
     * @return number of secs 
     */
    public long getElapsedSeconds() {
        long elapsed;
        if (running) {
             elapsed = (System.nanoTime() - startTime);
        }
        else {
            elapsed = (stopTime - startTime);
        }        
        return elapsed / nsPerSs;
    }
    
    /**
     * Gets the total elapsed time measured 
     * by the current instance, in minutes.
     * 600000000 Ticks = 1 minute (60 seconds)
     * @return number of mins 
     */
    public long getElapsedMinutes() {
        long elapsed;
        if (running) {
             elapsed = (System.nanoTime() - startTime);
        }
        else {
            elapsed = (stopTime - startTime);
        }        
        return elapsed / nsPerMm;
    }
    
    /**
     * Gets the total elapsed time measured 
     * by the current instance, in hours.
     * 36000000000 Ticks = 1 hour (60 minutes)
     * @return number of hrs 
     */
    public long getElapsedHours() {
        long elapsed;
        if (running) {
             elapsed = (System.nanoTime() - startTime);
        }
        else {
            elapsed = (stopTime - startTime);
        }        
        return elapsed / nsPerHh;
    }
    
    /**
     * Gets the total elapsed time with format 
     * 00:00:00.0000000 = 00:mm:ss.SSS + 9999 Ticks
     * @return formatted time string 
     */ 
    public String getElapsed() {
        String timeFormatted = "";
        timeFormatted = this.formatTime(this.getElapsedTicks());        
        return timeFormatted;
    }
    
    /**
     * Gets the total elapsed time with format 
     * 00:00:00.0000000 = 00:mm:ss.SSS + #### Ticks
     * @param elapsedTicks elapsed ticks between start and stop nano time
     */ 
    private String formatTime(final long elapsedTicks) {        
        String formattedTime = "";
        // should be hh:mm:ss.SSS, but 00 starts with 01 
        SimpleDateFormat formatter = new SimpleDateFormat("00:mm:ss.SSS");
        Calendar calendar = Calendar.getInstance();        
        
        if (elapsedTicks <= 9999) {
            calendar.setTimeInMillis(0);
            formattedTime = formatter.format(calendar.getTime()) 
                    + String.valueOf(String.format("%04d", elapsedTicks));
        }
        else {
            calendar.setTimeInMillis(elapsedTicks * nsPerTick / nsPerMs);            
            String formattedTicks = String.format("%07d", elapsedTicks);
            formattedTicks = formattedTicks.substring(formattedTicks.length() - 4);
            formattedTime = formatter.format(calendar.getTime()) + formattedTicks;
        }
        return formattedTime;
    }
}

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Logging database statement duration in a DRY way Cecil Westerhof <Cecil@decebal.nl> - 2016-08-19 14:32 +0200
  Re: Logging database statement duration in a DRY way Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-19 09:05 -0400
    Re: Logging database statement duration in a DRY way Cecil Westerhof <Cecil@decebal.nl> - 2016-08-19 20:54 +0200
  Re: Logging database statement duration in a DRY way Cecil Westerhof <Cecil@decebal.nl> - 2016-08-19 15:12 +0200
    Re: Logging database statement duration in a DRY way Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-19 10:01 -0400
    Re: Logging database statement duration in a DRY way Arne Vajhøj <arne@vajhoej.dk> - 2016-08-19 22:17 -0400
      Re: Logging database statement duration in a DRY way Arne Vajhøj <arne@vajhoej.dk> - 2016-08-19 22:37 -0400
  Re: Logging database statement duration in a DRY way Martin Gregorie <martin@address-in-sig.invalid> - 2016-08-19 16:05 +0000
    Re: Logging database statement duration in a DRY way Eric Douglas <e.d.programmer@gmail.com> - 2016-08-19 10:06 -0700
  Re: Logging database statement duration in a DRY way Steven Simpson <ss@my.domain.invalid> - 2016-08-19 22:34 +0100

csiph-web