Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35410
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Logging database statement duration in a DRY way |
| Date | Fri, 19 Aug 2016 09:05:57 -0400 |
| Organization | A noiseless patient Spider |
| Lines | 46 |
| Message-ID | <np7083$rmt$1@dont-email.me> (permalink) |
| References | <87wpjdhrbm.fsf@Equus.decebal.nl> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Fri, 19 Aug 2016 13:06:11 -0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="2d081e2f7155c28005d4472f67c2f43d"; logging-data="28381"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+/qZmeQjisQjq3v59HcEVV" |
| User-Agent | Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 |
| In-Reply-To | <87wpjdhrbm.fsf@Equus.decebal.nl> |
| Cancel-Lock | sha1:PcWM2cWZ/Q1Et/TJ+GYc+mVDqYI= |
| Xref | csiph.com comp.lang.java.programmer:35410 |
Show key headers only | View raw
On 8/19/2016 8:32 AM, Cecil Westerhof wrote:
> I like to log the time it takes for certain database actions. At the
> moment I do this like this:
> 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();
> logMessage(String.format("preparedStatement saveMemoryLineNoGC took %f milliseconds",
> (stopTime - startTime) * 1E-6));
>
> This works, but I need to copy the first and last two statements and
> edit the last one, so I was wondering if this could be done more DRY
> (Do not Repeat Yourself). In C(++) you would use a macro, but as far
> as I know this is not possible with Java.
I suppose you could put the "guts" in an instance implementing a
suitable interface and hand the instance to a time-it-and-log-it
method. You might end up with something like
static void timeAndLog(String label, Runnable thing) {
long startTime = System.nanoTime();
thing.run();
long stopTime = System.nanoTime();
logMessage(String.format("%s took %f milliseconds",
(stopTime - startTime) * 1E-6);
}
// ...
timeAndLog("preparedStatement saveMemoryLineNoGC", () -> {
ps = conn.prepareStatement(insertLine);
// ...
ps.close();
});
Only you can judge whether this is worth while.
--
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
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