From 3b1c008ec98175d8877cb1c7555cf00d612ffb6c Mon Sep 17 00:00:00 2001 From: mha Date: Thu, 15 Aug 2019 20:09:32 +0200 Subject: [PATCH] Cleanup of formatting --- README.md | 22 ++++++------------- .../java/com/ethlo/time/DurationUtil.java | 8 +++---- src/main/java/com/ethlo/time/Report.java | 5 +++++ .../java/com/ethlo/time/ChronographTest.java | 8 ++++++- .../java/com/ethlo/time/DurationUtilTest.java | 12 +++++----- src/test/java/com/ethlo/time/SleepUtil.java | 20 +++++++++++++++++ 6 files changed, 49 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 3771f3f..e7d68f4 100644 --- a/README.md +++ b/README.md @@ -19,22 +19,14 @@ Easy to use Java Chronograph (stopwatch) allowing measurement of elapsed time fo final Chronograph chronograph = Chronograph.create(); final String taskName1 = "foo" -final String taskName2 = "bar"; +final String taskName2 = "bar bar"; final String taskName3 = "baz baz baz baz baz baz"; for (int i = 0; i < 100_000; i++) { - chronograph.start(taskName); - microsecondTask(); - chronograph.stop(taskName); - - chronograph.start(taskName2); - microsecondTask(); - chronograph.stop(taskName2); - - chronograph.start(taskName3); - microsecondTask(); - chronograph.stop(taskName3); + chronograph.timed("foo", this::microsecondTask); + chronograph.timed("bar", this::microsecondTask); + chronograph.timed("baz baz baz baz baz baz", this::microsecondTask); } System.out.println(chronograph.prettyPrint()); @@ -45,9 +37,9 @@ Output: -------------------------------------------------------------------------------- | Task | Average | Total | Invocations | % | -------------------------------------------------------------------------------- -| a long task name | 1.18μ | 118.06m | 100,000 | 33.4% | -| bar | 1.18μ | 117.64m | 100,000 | 33.2% | -| baz baz baz baz baz b | 1.18μ | 118.28m | 100,000 | 33.4% | +| a long task name | 1.18 μ | 118.06 ms | 100,000 | 33.4% | +| bar ba | 1.18 μ | 117.64 ms | 100,000 | 33.2% | +| baz baz baz baz baz b | 1.18 μ | 118.28 ms | 100,000 | 33.4% | -------------------------------------------------------------------------------- | Total: 353.98m | -------------------------------------------------------------------------------- diff --git a/src/main/java/com/ethlo/time/DurationUtil.java b/src/main/java/com/ethlo/time/DurationUtil.java index a48babc..2e6b7c6 100644 --- a/src/main/java/com/ethlo/time/DurationUtil.java +++ b/src/main/java/com/ethlo/time/DurationUtil.java @@ -74,7 +74,7 @@ public static String humanReadable(Duration duration) dfSec.setMaximumFractionDigits(0); dfSec.setMinimumIntegerDigits(3); dfSec.setMaximumIntegerDigits(3); - sb.append(seconds).append('.').append(dfSec.format(nanos / (double) NANOS_PER_MILLI)).append("s"); + sb.append(seconds).append('.').append(dfSec.format(nanos / (double) NANOS_PER_MILLI)).append(" s"); } else if (hasSecondOrMore) { @@ -85,17 +85,17 @@ else if (hasSecondOrMore) // Sub-second if (millis > 0) { - sb.append(df.format(nanos / (double) NANOS_PER_MILLI)).append("m "); + sb.append(df.format(nanos / (double) NANOS_PER_MILLI)).append(" ms "); } if (millis == 0 && micros > 0) { - sb.append(df.format(nanos / (double) NANOS_PER_MICRO)).append("μ "); + sb.append(df.format(nanos / (double) NANOS_PER_MICRO)).append(" μ "); } if (millis == 0 && micros == 0 && nano > 0) { - sb.append(nano).append("n "); + sb.append(nano).append(" n "); } } diff --git a/src/main/java/com/ethlo/time/Report.java b/src/main/java/com/ethlo/time/Report.java index 7d7fbf2..c329494 100644 --- a/src/main/java/com/ethlo/time/Report.java +++ b/src/main/java/com/ethlo/time/Report.java @@ -34,6 +34,11 @@ public class Report */ public static String prettyPrint(Chronograph chronograph) { + if (chronograph.getTaskInfo().isEmpty()) + { + return "No performance data"; + } + final StringBuilder sb = new StringBuilder(); sb.append("\n--------------------------------------------------------------------------------\n"); sb.append("| Task | Average | Total | Invocations | % | \n"); diff --git a/src/test/java/com/ethlo/time/ChronographTest.java b/src/test/java/com/ethlo/time/ChronographTest.java index 8d60bb1..54383df 100644 --- a/src/test/java/com/ethlo/time/ChronographTest.java +++ b/src/test/java/com/ethlo/time/ChronographTest.java @@ -140,8 +140,14 @@ public void nullTask() fail("Should throw"); } + @Test + public void noData() + { + assertThat(Chronograph.create().prettyPrint()).isEqualTo("No performance data"); + } + @Test(expected = IllegalStateException.class) - public void sequentialStartk() + public void sequentialStart() { final Chronograph chronograph = Chronograph.create(); chronograph.start(taskName); diff --git a/src/test/java/com/ethlo/time/DurationUtilTest.java b/src/test/java/com/ethlo/time/DurationUtilTest.java index cba7dbd..efff0f8 100644 --- a/src/test/java/com/ethlo/time/DurationUtilTest.java +++ b/src/test/java/com/ethlo/time/DurationUtilTest.java @@ -43,36 +43,36 @@ public void humanReadableFormatLessThanHour() @Test public void humanReadableFormatLessThanMinute() { - assertThat(DurationUtil.humanReadable(Duration.ofSeconds(8).withNanos(125_956_789))).isEqualTo("8.126s"); + assertThat(DurationUtil.humanReadable(Duration.ofSeconds(8).withNanos(125_956_789))).isEqualTo("8.126 s"); } @Test public void humanReadableFormatLessThanMinute2() { - assertThat(DurationUtil.humanReadable(Duration.ofSeconds(8).withNanos(1_000_000))).isEqualTo("8.001s"); + assertThat(DurationUtil.humanReadable(Duration.ofSeconds(8).withNanos(1_000_000))).isEqualTo("8.001 s"); } @Test public void humanReadableFormatLessThanMinute3() { - assertThat(DurationUtil.humanReadable(Duration.ofSeconds(8))).isEqualTo("8.000s"); + assertThat(DurationUtil.humanReadable(Duration.ofSeconds(8))).isEqualTo("8.000 s"); } @Test public void humanReadableFormatLessThanSecond() { - assertThat(DurationUtil.humanReadable(Duration.ofNanos(123_456_789))).isEqualTo("123.46m"); + assertThat(DurationUtil.humanReadable(Duration.ofNanos(123_456_789))).isEqualTo("123.46 ms"); } @Test public void humanReadableFormatLessThanMillisecond() { - assertThat(DurationUtil.humanReadable(Duration.ofNanos(456_789))).isEqualTo("456.79μ"); + assertThat(DurationUtil.humanReadable(Duration.ofNanos(456_789))).isEqualTo("456.79 μ"); } @Test public void humanReadableFormatLessThanMicrosecond() { - assertThat(DurationUtil.humanReadable(Duration.ofNanos(489))).isEqualTo("489n"); + assertThat(DurationUtil.humanReadable(Duration.ofNanos(489))).isEqualTo("489 n"); } } diff --git a/src/test/java/com/ethlo/time/SleepUtil.java b/src/test/java/com/ethlo/time/SleepUtil.java index 7ff6886..da9ccdf 100644 --- a/src/test/java/com/ethlo/time/SleepUtil.java +++ b/src/test/java/com/ethlo/time/SleepUtil.java @@ -1,5 +1,25 @@ package com.ethlo.time; +/*- + * #%L + * Chronograph + * %% + * Copyright (C) 2019 Morten Haraldsen (ethlo) + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + import java.util.concurrent.TimeUnit; public class SleepUtil