Skip to content

Commit

Permalink
Show duration in more readable units in HTML report (Issue-#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Schäfer committed Sep 11, 2014
1 parent 36b2629 commit 3b71d2a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 21 deletions.
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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void visitEnd( ScenarioCaseModel scenarioCase ) {
writer.println( "<div class='failed'>Failed: " + scenarioCase.errorMessage + "</div>" );
}

writeDuration( scenarioCase.durationInNanos );
utils.writeDuration( scenarioCase.durationInNanos );

writer.print( "</td>" );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import static java.lang.String.format;

import java.io.PrintWriter;
import java.util.Formatter;
import java.util.Locale;

import com.tngtech.jgiven.impl.util.DurationFormatter;

public class HtmlWriterUtils {

Expand All @@ -30,11 +30,7 @@ public void writeHtmlFooter() {
}

public void writeDuration( long durationInNanos ) {
// TODO: provide a configuration value to configure the locale
double durationInMs = ( (double) durationInNanos ) / 1000000;
Formatter usFormatter = new Formatter( Locale.US );
writer.print( usFormatter.format( " <span class='duration'>(%.2f ms)</span>", durationInMs ) );
usFormatter.close();
writer.print( " <span class='duration'>(" + DurationFormatter.format( durationInNanos ) + ")" );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import static java.lang.String.format;

import java.io.PrintWriter;
import java.util.Formatter;
import java.util.Locale;

import com.google.common.html.HtmlEscapers;
import com.tngtech.jgiven.impl.util.WordUtil;
Expand All @@ -24,9 +22,11 @@ public class ScenarioHtmlWriter extends ReportModelVisitor {

ScenarioModel scenarioModel;
ScenarioCaseModel scenarioCase;
HtmlWriterUtils utils;

public ScenarioHtmlWriter( PrintWriter writer ) {
this.writer = writer;
this.utils = new HtmlWriterUtils( writer );

}

Expand All @@ -43,7 +43,7 @@ public void visit( ScenarioModel scenarioModel ) {

writer.print( " " + WordUtil.capitalize( scenarioModel.description ) );

writeDuration( scenarioModel.getDurationInNanos() );
utils.writeDuration( scenarioModel.getDurationInNanos() );
writer.println( "</h3>" );

writeTagLine( scenarioModel );
Expand Down Expand Up @@ -124,7 +124,7 @@ void printCaseHeader( ScenarioCaseModel scenarioCase ) {
}
}

writeDuration( scenarioCase.durationInNanos );
utils.writeDuration( scenarioCase.durationInNanos );
writer.println( "</h4>" );
}
}
Expand Down Expand Up @@ -165,19 +165,11 @@ public void visit( StepModel stepModel ) {
writer.print( format( " <span class='badge %s'>%s</span>", WordUtil.camelCase( lowerCase ), lowerCase.replace( '_', ' ' ) ) );
}

writeDuration( stepModel.getDurationInNanos() );
utils.writeDuration( stepModel.getDurationInNanos() );

writer.println( "</li>" );
}

protected void writeDuration( long durationInNanos ) {
// TODO: provide a configuration value to configure the locale
double durationInMs = ( (double) durationInNanos ) / 1000000;
Formatter usFormatter = new Formatter( Locale.US );
writer.print( usFormatter.format( " <span class='duration'>(%.2f ms)</span>", durationInMs ) );
usFormatter.close();
}

private void printArg( Word word ) {
String value = word.getArgumentInfo().isCaseArg() ? formatCaseArgument( word ) : HtmlEscapers.htmlEscaper().escape( word.value );
value = escapeToHtml( value );
Expand Down
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 );
}
}

0 comments on commit 3b71d2a

Please sign in to comment.