Skip to content

Commit

Permalink
Merge branch 'statsFormat' of https://github.com/OndraZizka/windup in…
Browse files Browse the repository at this point in the history
…to OndraZizka-statsFormat
  • Loading branch information
jsight committed May 4, 2016
2 parents 8661959 + c5e4ad4 commit 711eb76
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 58 deletions.
Expand Up @@ -4,10 +4,11 @@

import com.tinkerpop.frames.Property;
import com.tinkerpop.frames.modules.typedgraph.TypeValue;
import java.util.Comparator;

/**
* This stores the time it takes to execute all of the rules within a particular phase of execution.
*
*
* @author <a href="mailto:jesse.sightler@gmail.com">Jesse Sightler</a>
*/
@TypeValue(RulePhaseExecutionStatisticsModel.TYPE)
Expand Down Expand Up @@ -53,4 +54,13 @@ public interface RulePhaseExecutionStatisticsModel extends WindupVertexFrame
*/
@Property(ORDER_EXECUTED)
public void setOrderExecuted(int orderExecuted);

public static final Comparator BY_ORDER_EXECUTED = new Comparator<RulePhaseExecutionStatisticsModel>()
{
@Override
public int compare(RulePhaseExecutionStatisticsModel o1, RulePhaseExecutionStatisticsModel o2)
{
return o1.getOrderExecuted() - o2.getOrderExecuted();
}
};
}
Expand Up @@ -2,12 +2,10 @@

import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.apache.commons.io.FileUtils;
Expand All @@ -23,6 +21,7 @@
import org.jboss.windup.graph.service.GraphService;
import org.jboss.windup.graph.service.RuleProviderExecutionStatisticsService;
import org.jboss.windup.graph.service.WindupConfigurationService;
import org.jboss.windup.util.Checks;
import org.jboss.windup.util.ExecutionStatistics;
import org.jboss.windup.util.exception.WindupException;
import org.ocpsoft.rewrite.config.Configuration;
Expand Down Expand Up @@ -51,89 +50,55 @@ public Configuration getConfiguration(GraphContext context)
@Override
public void perform(GraphRewrite event, EvaluationContext context)
{
WindupConfigurationModel cfg = WindupConfigurationService.getConfigurationModel(event
.getGraphContext());
WindupConfigurationModel cfg = WindupConfigurationService.getConfigurationModel(event.getGraphContext());
String outputDir = cfg.getOutputPath().getFilePath();

// create a directory for the output
Path statsDir = Paths.get(outputDir, "stats");
FileUtils.deleteQuietly(statsDir.toFile());
try
{
Files.createDirectories(statsDir);
}
catch (IOException e)
{
throw new WindupException("Error creating output folder: " + outputDir + " due to: "
+ e.getMessage(), e);
}
Checks.createDirectoryToBeFilled(statsDir, "stats folder");

Path detailedExecutionStatsOutputPath = statsDir.resolve("detailed_stats.csv");
ExecutionStatistics.get().serializeTimingData(detailedExecutionStatsOutputPath);

Path ruleTimingOutputPath = statsDir.resolve("timing.txt");
try (FileWriter fw = new FileWriter(ruleTimingOutputPath.toFile()))
{

RuleProviderExecutionStatisticsService statsByRuleProviderService = new RuleProviderExecutionStatisticsService(
event.getGraphContext());
Iterable<RuleProviderExecutionStatisticsModel> ruleProviderStatModels = statsByRuleProviderService
.findAllOrderedByIndex();
Iterable<RuleProviderExecutionStatisticsModel> ruleProviderStatModels =
new RuleProviderExecutionStatisticsService(event.getGraphContext()).findAllOrderedByIndex();

// rule execution timings
fw.write("-----------------------------------------------------------\n");
fw.write("Rule execution timings:\n\n");
for (RuleProviderExecutionStatisticsModel model : ruleProviderStatModels)
{
fw.write(model.getRuleProviderID());
fw.write(": ");
fw.write(String.valueOf(model.getTimeTaken()));
fw.write(" ms");
fw.write(" (");
fw.write(String.valueOf(model.getTimeTaken() / 1000));
fw.write(" seconds)");
fw.write("\n");
int ms = model.getTimeTaken();
fw.write(String.format("% 5d.%03d, %s\n", ms / 1000, ms % 1000, model.getRuleProviderID()));
}
fw.write("-----------------------------------------------------------\n\n");

// phase execution timings
fw.write("Phase execution timings:\n\n");
GraphService<RulePhaseExecutionStatisticsModel> statsByPhaseService = new GraphService<>(
event.getGraphContext(), RulePhaseExecutionStatisticsModel.class);
Iterable<RulePhaseExecutionStatisticsModel> rulePhaseStatModelIterable = statsByPhaseService
.findAll();
GraphService<RulePhaseExecutionStatisticsModel> statsByPhaseService =
new GraphService<>(event.getGraphContext(), RulePhaseExecutionStatisticsModel.class);
Iterable<RulePhaseExecutionStatisticsModel> rulePhaseStatModelIterable = statsByPhaseService.findAll();
List<RulePhaseExecutionStatisticsModel> rulePhaseStatModelList = new ArrayList<>();
for (RulePhaseExecutionStatisticsModel model : rulePhaseStatModelIterable)
{
rulePhaseStatModelList.add(model);
}
Collections.sort(rulePhaseStatModelList,
new Comparator<RulePhaseExecutionStatisticsModel>()
{
@Override
public int compare(RulePhaseExecutionStatisticsModel o1,
RulePhaseExecutionStatisticsModel o2)
{
return o1.getOrderExecuted() - o2.getOrderExecuted();
}
});
Collections.sort(rulePhaseStatModelList, RulePhaseExecutionStatisticsModel.BY_ORDER_EXECUTED);

for (RulePhaseExecutionStatisticsModel model : rulePhaseStatModelList)
{
fw.write(model.getRulePhase());
fw.write(": ");
fw.write(String.valueOf(model.getTimeTaken()));
fw.write(" ms");
fw.write(" (");
fw.write(String.valueOf(model.getTimeTaken() / 1000));
fw.write(" seconds)");
fw.write("\n");
int ms = model.getTimeTaken();
fw.write(String.format("% 6d.%03d, %s\n", ms / 1000, ms % 1000, model.getRulePhase()));
}
fw.write("-----------------------------------------------------------\n");
}
catch (IOException e)
{
throw new WindupException("Error creating output file: " + ruleTimingOutputPath.toString()
throw new WindupException("Error creating output file: " + ruleTimingOutputPath
+ " due to: " + e.getMessage(), e);
}

Expand Down
19 changes: 19 additions & 0 deletions utils/src/main/java/org/jboss/windup/util/Checks.java
@@ -1,6 +1,12 @@
package org.jboss.windup.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jboss.windup.util.exception.WindupException;

/**
*
Expand Down Expand Up @@ -29,6 +35,19 @@ public static void checkDirectoryToBeFilled(File outputDir, String dirDesc) thro
throw new IllegalArgumentException(dirDesc + " is a file, expected a directory: " + outputDir.getAbsolutePath());
}

public static void createDirectoryToBeFilled(Path dir, String dirDesc) throws IllegalArgumentException
{
checkDirectoryToBeFilled(dir.toFile(), dirDesc);
try
{
Files.createDirectories(dir);
}
catch (IOException ex)
{
throw new WindupException("Error creating stats folder: " + dir.toString() + " due to: " + ex.getMessage(), ex);
}
}

public static void checkDirectoryToBeRead(File rootDir, String dirDesc)
{
if (rootDir == null)
Expand Down
19 changes: 11 additions & 8 deletions utils/src/main/java/org/jboss/windup/util/ExecutionStatistics.java
Expand Up @@ -8,23 +8,24 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;
import org.apache.commons.lang3.StringEscapeUtils;

/**
* <p>
* {@link ExecutionStatistics} provides a simple system for storing the time taken to perform operations.
* </p>
*
*
* <p>
* Example usage:
*
*
* <pre>
* ExecutionStatistics.get().begin(&quot;Process-01&quot;);
* // ... your code to be timed goes here
* ExecutionStatistics.get().end(&quot;Process-01&quot;);
* </pre>
*
*
* </p>
*
*
* @author <a href="mailto:jesse.sightler@gmail.com">Jesse Sightler</a>
*
*/
Expand All @@ -49,7 +50,7 @@ public static synchronized ExecutionStatistics get()
Thread currentThread = Thread.currentThread();
if (stats.get(currentThread) == null)
{
stats.put(currentThread,new ExecutionStatistics());
stats.put(currentThread, new ExecutionStatistics());
}
return stats.get(currentThread);
}
Expand Down Expand Up @@ -114,14 +115,16 @@ public void serializeTimingData(Path outputPath)

try (FileWriter fw = new FileWriter(outputPath.toFile()))
{
fw.write("Type~Number Of Executions~Total Milliseconds~Milliseconds per execution\n");
fw.write("Number Of Executions, Total Milliseconds, Milliseconds per execution, Type\n");
for (Map.Entry<String, TimingData> timing : executionInfo.entrySet())
{
TimingData data = timing.getValue();
long totalMillis = (data.totalNanos / 1000000);
double millisPerExecution = (double) totalMillis / (double) data.numberOfExecutions;
fw.write(timing.getKey() + "~" + data.numberOfExecutions + "~" + totalMillis + "~" + millisPerExecution);
fw.write("\n");
fw.write(String.format("%6d, %6d, %8.2f, %s\n",
data.numberOfExecutions, totalMillis, millisPerExecution,
StringEscapeUtils.escapeCsv(timing.getKey())
));
}
}
catch (Exception e)
Expand Down

0 comments on commit 711eb76

Please sign in to comment.