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


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

Re: Logging database statement duration in a DRY way

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Logging database statement duration in a DRY way
Date 2016-08-19 10:01 -0400
Organization A noiseless patient Spider
Message-ID <np73ge$72h$1@dont-email.me> (permalink)
References <87wpjdhrbm.fsf@Equus.decebal.nl> <87pop4j41i.fsf@Equus.decebal.nl>

Show all headers | View raw


On 8/19/2016 9:12 AM, Cecil Westerhof wrote:
> On Friday 19 Aug 2016 14:32 CEST, Cecil Westerhof wrote:
>[...]
> For this simple case I found a solution. The only part that really can
> change is the last part, so I now have:
>     startTime = System.nanoTime();
>     ps        = conn.prepareStatement(insertLine);
>     ps.setString(1, className);
>     ps.setLong(  2, pid);
>     ps.setLong(  3, free);
>     ps.setLong(  4, rt.maxMemory() / divideValue);
>     ps.setLong(  5, total);
>     ps.executeUpdate();
>     ps.close();
>     stopTime = System.nanoTime();
>     logDatabaseAction("preparedStatement saveMemoryLineNoGC", stopTime - startTime);
>
> And logDatabaseAction is:
>     private static void logDatabaseAction(String message, long duration) {
>         logMessage(String.format("%-40s took %e milliseconds",
>                                  message,
>                                  duration * 1E-6));
>      }
>
> I like to have the times outlined under each-other. When there comes a
> longer string I just have to change the -40. I also changed from %e to
> %f to have a better (in my opinion) display of the times. Probably I
> should change %f to %.2f.

     You could achieve a further tiny abbreviation by passing startTime
into the logging function and letting it call nanoTime() internally.
(Someone may say "That would pollute the timing!" but since you're only
planning to report 10-microsecond units that seems irrelevant.)

> The output:
> 14:55:47: preparedStatement freeMemoryThread       took 1.366532e+02 milliseconds
> 14:56:48: preparedStatement saveMemoryLine         took 1.603480e+02 milliseconds

     Some further avoidance of formatting worries could be attained
by rearranging the output a bit, if that's permissible:

	14:55:47: 136.65 ms for preparedStatement freeMemoryThread
	14:56:48: 160.35 ms for preparedStatement saveMemoryLine

... so you stop fretting about "%-40s" and just use "%s".

-- 
esosman@comcast-dot-net.invalid
The Tooth Fairy has gone high-tech, now pays in Bitecoin.

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