-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show duration in more readable units in HTML report (Issue-#24)
- Loading branch information
Jan Schäfer
committed
Sep 11, 2014
1 parent
36b2629
commit 3b71d2a
Showing
5 changed files
with
84 additions
and
21 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
jgiven-core/src/main/java/com/tngtech/jgiven/impl/util/DurationFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.tngtech.jgiven.impl.util; | ||
|
||
import java.util.Formatter; | ||
import java.util.Locale; | ||
|
||
public class DurationFormatter { | ||
|
||
static class Duration { | ||
String unit; | ||
double value; | ||
} | ||
|
||
public static String format( long durationInNanos ) { | ||
Formatter usFormatter = new Formatter( Locale.US ); | ||
|
||
Duration duration = getHumanReadableDuration( durationInNanos ); | ||
|
||
try { | ||
return usFormatter.format( "%.2f ", duration.value ) + duration.unit; | ||
} finally { | ||
usFormatter.close(); | ||
} | ||
} | ||
|
||
private static Duration getHumanReadableDuration( double durationInNanos ) { | ||
Duration result = new Duration(); | ||
result.value = durationInNanos / 1000000; | ||
result.unit = "ms"; | ||
|
||
if( result.value < 1000 ) { | ||
return result; | ||
} | ||
|
||
result.value /= 1000; | ||
result.unit = "s"; | ||
|
||
if( result.value < 60 ) { | ||
return result; | ||
} | ||
|
||
result.value /= 60; | ||
result.unit = "min"; | ||
|
||
return result; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
jgiven-core/src/test/java/com/tngtech/jgiven/impl/util/DurationFormatterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.tngtech.jgiven.impl.util; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import com.tngtech.java.junit.dataprovider.DataProvider; | ||
import com.tngtech.java.junit.dataprovider.DataProviderRunner; | ||
|
||
@RunWith( DataProviderRunner.class ) | ||
public class DurationFormatterTest { | ||
|
||
@Test | ||
@DataProvider( splitBy = ";", value = { | ||
"678; 0.00 ms", | ||
"345678; 0.35 ms", | ||
"12345678; 12.35 ms", | ||
"123456789; 123.46 ms", | ||
"500006789; 500.01 ms", | ||
"1500006789; 1.50 s", | ||
"15000067890; 15.00 s", | ||
"60000067890; 1.00 min", | ||
} ) | ||
public void test( long nanos, String expectedResult ) { | ||
assertThat( DurationFormatter.format( nanos ) ).isEqualTo( expectedResult ); | ||
} | ||
} |