Changes requested by reviewer
This commit is contained in:
@@ -18,7 +18,7 @@ import net.sourceforge.pmd.PMD;
|
||||
import net.sourceforge.pmd.benchmark.TimeTracker.TimedResult;
|
||||
|
||||
/**
|
||||
* A text based renderer for {@link TimingReport}
|
||||
* A text based renderer for {@link TimingReport}.
|
||||
* @author Juan Martín Sotuyo Dodero
|
||||
*/
|
||||
public class TextTimingReportRenderer implements TimingReportRenderer {
|
||||
@@ -38,18 +38,18 @@ public class TextTimingReportRenderer implements TimingReportRenderer {
|
||||
@Override
|
||||
public void render(final TimingReport report, final Writer writer) throws IOException {
|
||||
for (final TimedOperationCategory category : TimedOperationCategory.values()) {
|
||||
final Map<String, TimedResult> labeledMeassurements = report.getLabeledMeassurements(category);
|
||||
if (!labeledMeassurements.isEmpty()) {
|
||||
renderCategoryMeassurements(category, labeledMeassurements, writer);
|
||||
final Map<String, TimedResult> labeledMeasurements = report.getLabeledMeasurements(category);
|
||||
if (!labeledMeasurements.isEmpty()) {
|
||||
renderCategoryMeasurements(category, labeledMeasurements, writer);
|
||||
}
|
||||
}
|
||||
|
||||
renderHeader("Summary", writer);
|
||||
|
||||
for (final TimedOperationCategory category : TimedOperationCategory.values()) {
|
||||
final TimedResult timedResult = report.getUnlabeledMeassurements(category);
|
||||
final TimedResult timedResult = report.getUnlabeledMeasurements(category);
|
||||
if (timedResult != null) {
|
||||
renderMeassurement(category.displayName(), timedResult, writer);
|
||||
renderMeasurement(category.displayName(), timedResult, writer);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ public class TextTimingReportRenderer implements TimingReportRenderer {
|
||||
writer.flush();
|
||||
}
|
||||
|
||||
private void renderMeassurement(final String label, final TimedResult timedResult,
|
||||
private void renderMeasurement(final String label, final TimedResult timedResult,
|
||||
final Writer writer) throws IOException {
|
||||
writer.write(StringUtils.rightPad(label, LABEL_COLUMN_WIDTH));
|
||||
|
||||
@@ -87,8 +87,8 @@ public class TextTimingReportRenderer implements TimingReportRenderer {
|
||||
writer.write(PMD.EOL);
|
||||
}
|
||||
|
||||
private void renderCategoryMeassurements(final TimedOperationCategory category,
|
||||
final Map<String, TimedResult> labeledMeassurements, final Writer writer) throws IOException {
|
||||
private void renderCategoryMeasurements(final TimedOperationCategory category,
|
||||
final Map<String, TimedResult> labeledMeasurements, final Writer writer) throws IOException {
|
||||
renderHeader(category.displayName(), writer);
|
||||
|
||||
final TimedResult grandTotal = new TimedResult();
|
||||
@@ -96,24 +96,18 @@ public class TextTimingReportRenderer implements TimingReportRenderer {
|
||||
new Comparator<Map.Entry<String, TimedResult>>() {
|
||||
@Override
|
||||
public int compare(final Entry<String, TimedResult> o1, final Entry<String, TimedResult> o2) {
|
||||
final long diff = o1.getValue().selfTime.get() - o2.getValue().selfTime.get();
|
||||
if (diff > 0) {
|
||||
return 1;
|
||||
} else if (diff < 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
return Long.compare(o1.getValue().selfTime.get(), o2.getValue().selfTime.get());
|
||||
}
|
||||
});
|
||||
sortedKeySet.addAll(labeledMeassurements.entrySet());
|
||||
sortedKeySet.addAll(labeledMeasurements.entrySet());
|
||||
|
||||
for (final Map.Entry<String, TimedResult> entry : sortedKeySet) {
|
||||
renderMeassurement(entry.getKey(), entry.getValue(), writer);
|
||||
renderMeasurement(entry.getKey(), entry.getValue(), writer);
|
||||
grandTotal.mergeTimes(entry.getValue());
|
||||
}
|
||||
|
||||
writer.write(PMD.EOL);
|
||||
renderMeassurement("Total " + category.displayName(), grandTotal, writer);
|
||||
renderMeasurement("Total " + category.displayName(), grandTotal, writer);
|
||||
writer.write(PMD.EOL);
|
||||
}
|
||||
|
||||
|
@@ -12,6 +12,8 @@ import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
/**
|
||||
* A time tracker class to measure time spent on different sections of PMD analysis.
|
||||
* The class is thread-aware, allowing to differentiate CPU and wall clock time.
|
||||
@@ -97,7 +99,7 @@ public final class TimeTracker {
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts tracking an operation
|
||||
* Starts tracking an operation.
|
||||
* @param category The category under which to track the operation.
|
||||
*/
|
||||
public static void startOperation(final TimedOperationCategory category) {
|
||||
@@ -105,7 +107,7 @@ public final class TimeTracker {
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts tracking an operation
|
||||
* Starts tracking an operation.
|
||||
* @param category The category under which to track the operation.
|
||||
* @param label A label to be added to the category. Allows to differentiate measures within a single category.
|
||||
*/
|
||||
@@ -118,15 +120,17 @@ public final class TimeTracker {
|
||||
}
|
||||
|
||||
/**
|
||||
* Finishes tracking an operation
|
||||
* Finishes tracking an operation.
|
||||
*/
|
||||
public static void finishOperation() {
|
||||
finishOperation(0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finishes tracking an operation
|
||||
* @param extraDataCounter An optional additional data counter to track along the measurements
|
||||
* Finishes tracking an operation.
|
||||
* @param extraDataCounter An optional additional data counter to track along the measurements.
|
||||
* Users are free to track any extra value they want (ie: number of analyzed nodes,
|
||||
* iterations in a loop, etc.)
|
||||
*/
|
||||
public static void finishOperation(final long extraDataCounter) {
|
||||
if (!trackTime) {
|
||||
@@ -150,6 +154,9 @@ public final class TimeTracker {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An entry in the open timers queue. Defines an operation that has started and hasn't finished yet.
|
||||
*/
|
||||
private static class TimerEntry {
|
||||
/* package */ final TimedOperation operation;
|
||||
/* package */ final long start;
|
||||
@@ -166,12 +173,21 @@ public final class TimeTracker {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Aggregate results measured so far for a given category + label.
|
||||
*/
|
||||
/* package */ static class TimedResult {
|
||||
/* package */ AtomicLong totalTime = new AtomicLong();
|
||||
/* package */ AtomicLong selfTime = new AtomicLong();
|
||||
/* package */ AtomicInteger callCount = new AtomicInteger();
|
||||
/* package */ AtomicLong extraDataCounter = new AtomicLong();
|
||||
|
||||
/**
|
||||
* Adds a new {@link TimerEntry} to the results.
|
||||
* @param timerEntry The entry to be added
|
||||
* @param extraData Any extra data counter to be added
|
||||
* @return The delta time transcurred since the {@link TimerEntry} began.
|
||||
*/
|
||||
/* package */ long accumulate(final TimerEntry timerEntry, final long extraData) {
|
||||
final long delta = System.nanoTime() - timerEntry.start;
|
||||
|
||||
@@ -183,12 +199,19 @@ public final class TimeTracker {
|
||||
return delta;
|
||||
}
|
||||
|
||||
/**
|
||||
* Merges the times (and only the times) from another {@link TimedResult} into self.
|
||||
* @param timedResult The {@link TimedResult} to merge
|
||||
*/
|
||||
/* package */ void mergeTimes(final TimedResult timedResult) {
|
||||
totalTime.getAndAdd(timedResult.totalTime.get());
|
||||
selfTime.getAndAdd(timedResult.selfTime.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A unique identifier for a timed operation
|
||||
*/
|
||||
/* package */ static class TimedOperation {
|
||||
/* package */ final TimedOperationCategory category;
|
||||
/* package */ final String label;
|
||||
@@ -222,14 +245,7 @@ public final class TimeTracker {
|
||||
if (category != other.category) {
|
||||
return false;
|
||||
}
|
||||
if (label == null) {
|
||||
if (other.label != null) {
|
||||
return false;
|
||||
}
|
||||
} else if (!label.equals(other.label)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
return Objects.equal(label, other.label);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -25,7 +25,7 @@ public class TimingReport {
|
||||
results = accumulatedResults;
|
||||
}
|
||||
|
||||
public Map<String, TimedResult> getLabeledMeassurements(final TimedOperationCategory category) {
|
||||
public Map<String, TimedResult> getLabeledMeasurements(final TimedOperationCategory category) {
|
||||
final Map<String, TimedResult> ret = new HashMap<>();
|
||||
|
||||
for (final Map.Entry<TimedOperation, TimedResult> entry : results.entrySet()) {
|
||||
@@ -38,7 +38,7 @@ public class TimingReport {
|
||||
return ret;
|
||||
}
|
||||
|
||||
public TimedResult getUnlabeledMeassurements(final TimedOperationCategory category) {
|
||||
public TimedResult getUnlabeledMeasurements(final TimedOperationCategory category) {
|
||||
for (final Map.Entry<TimedOperation, TimedResult> entry : results.entrySet()) {
|
||||
final TimedOperation timedOperation = entry.getKey();
|
||||
if (timedOperation.category == category && timedOperation.label == null) {
|
||||
|
@@ -8,13 +8,13 @@ import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
|
||||
/**
|
||||
* Defines a renderer for {@link TimingReport}
|
||||
* Defines a renderer for {@link TimingReport}.
|
||||
* @author Juan Martín Sotuyo Dodero
|
||||
*/
|
||||
public interface TimingReportRenderer {
|
||||
|
||||
/**
|
||||
* Renders the given report into the given writer
|
||||
* Renders the given report into the given writer.
|
||||
* @param report The report data to render
|
||||
* @param writer The writer on which to render
|
||||
* @throws IOException if the write operation fails
|
||||
|
Reference in New Issue
Block a user