Skip to content

Commit

Permalink
Cleanup of formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
ethlo committed Aug 15, 2019
1 parent 550cd95 commit 3b1c008
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 26 deletions.
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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 |
--------------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/ethlo/time/DurationUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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 ");
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/ethlo/time/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
8 changes: 7 additions & 1 deletion src/test/java/com/ethlo/time/ChronographTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/test/java/com/ethlo/time/DurationUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
20 changes: 20 additions & 0 deletions src/test/java/com/ethlo/time/SleepUtil.java
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 3b1c008

Please sign in to comment.