| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
package org.dishevelled.timer.report; |
| 26 | |
|
| 27 | |
import java.io.IOException; |
| 28 | |
|
| 29 | |
import java.text.NumberFormat; |
| 30 | |
|
| 31 | |
import java.util.Map; |
| 32 | |
|
| 33 | |
import org.dishevelled.timer.Timer; |
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public final class CsvTimerReport |
| 39 | |
extends AbstractTimerReport |
| 40 | |
{ |
| 41 | |
private final NumberFormat numberFormat; |
| 42 | |
|
| 43 | |
public CsvTimerReport(final NumberFormat numberFormat) |
| 44 | 5 | { |
| 45 | 5 | if (numberFormat == null) |
| 46 | |
{ |
| 47 | 1 | throw new IllegalArgumentException("numberFormat must not be null"); |
| 48 | |
} |
| 49 | 4 | this.numberFormat = numberFormat; |
| 50 | 4 | } |
| 51 | |
|
| 52 | |
|
| 53 | |
public <T extends Appendable> T append(final Map<? extends Runnable, Timer> timers, final T appendable) |
| 54 | |
throws IOException |
| 55 | |
{ |
| 56 | 6 | if (timers == null) |
| 57 | |
{ |
| 58 | 2 | throw new IllegalArgumentException("timers must not be null"); |
| 59 | |
} |
| 60 | 4 | if (appendable == null) |
| 61 | |
{ |
| 62 | 1 | throw new IllegalArgumentException("appendable must not be null"); |
| 63 | |
} |
| 64 | 3 | append(appendable, "Runnable"); |
| 65 | 3 | append(appendable, "Size"); |
| 66 | 3 | append(appendable, "Min (ns)"); |
| 67 | 3 | append(appendable, "Mean (ns)"); |
| 68 | 3 | append(appendable, "Max (ns)"); |
| 69 | 3 | append(appendable, "Standard Deviation (ns)"); |
| 70 | 3 | appendLast(appendable, "Sum (ns)"); |
| 71 | |
|
| 72 | 3 | for (Map.Entry<? extends Runnable, Timer> entry : timers.entrySet()) |
| 73 | |
{ |
| 74 | 3 | append(appendable, String.valueOf(entry.getKey())); |
| 75 | 3 | append(appendable, numberFormat.format(entry.getValue().size())); |
| 76 | 3 | append(appendable, numberFormat.format(entry.getValue().min())); |
| 77 | 3 | append(appendable, numberFormat.format(entry.getValue().mean())); |
| 78 | 3 | append(appendable, numberFormat.format(entry.getValue().max())); |
| 79 | 3 | append(appendable, numberFormat.format(entry.getValue().standardDeviation())); |
| 80 | 3 | appendLast(appendable, numberFormat.format(entry.getValue().sum())); |
| 81 | 3 | } |
| 82 | 3 | return appendable; |
| 83 | |
} |
| 84 | |
|
| 85 | |
private static <T extends Appendable> void append(final T appendable, final String value) throws IOException |
| 86 | |
{ |
| 87 | 36 | appendable.append("\""); |
| 88 | 36 | appendable.append(value); |
| 89 | 36 | appendable.append("\","); |
| 90 | 36 | } |
| 91 | |
|
| 92 | |
private static <T extends Appendable> void appendLast(final T appendable, final String value) throws IOException |
| 93 | |
{ |
| 94 | 6 | appendable.append("\""); |
| 95 | 6 | appendable.append(value); |
| 96 | 6 | appendable.append("\"\n"); |
| 97 | 6 | } |
| 98 | |
} |