package com.limegroup.gnutella.performance;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* Class to analyze the results generated by output of {@link KeywordIndexPerformanceSearcher}
*/
public class KeywordIndexPerformanceAnalyzer {
private static final Log LOG = LogFactory.getLog(KeywordIndexPerformanceAnalyzer.class);
// config options
private int numberOfQueriesToThrowAwayDueToJit;
private String rawDataFileName;
// map each search term to list of latencies
private Map<String, List<Double>> latencyMap;
KeywordIndexPerformanceAnalyzer(AnalyzeConfig config) {
this.numberOfQueriesToThrowAwayDueToJit = config.getNumberOfQueriesToThrowAwayDueToJit();
this.rawDataFileName = config.getRawDataFileName();
}
public void execute() throws KeywordIndexPerformanceException {
try {
readStatisticsFile();
} catch (IOException e) {
throw new KeywordIndexPerformanceException("Error reading statistics" +
" file: " + rawDataFileName, e);
}
calculateStatistics();
}
private void readStatisticsFile() throws IOException {
BufferedReader statsReader = new BufferedReader(new FileReader(rawDataFileName));
int latencyStatsProcessed = 0;
latencyMap = new HashMap<String, List<Double>>();
String currentLine;
while ((currentLine = statsReader.readLine()) != null) {
if (currentLine.startsWith("query:")) {
latencyStatsProcessed++;
if (latencyStatsProcessed <= numberOfQueriesToThrowAwayDueToJit) {
continue;
}
String queryStatisticsString = currentLine.substring(6);
String[] latencyStats = queryStatisticsString.split(":");
String searchTerm = latencyStats[0];
String latencyInNanoSeconds = latencyStats[1];
List<Double> latencyList;
if (latencyMap.containsKey(searchTerm)) {
latencyList = latencyMap.get(searchTerm);
} else {
latencyList = new ArrayList<Double>();
latencyMap.put(searchTerm, latencyList);
}
latencyList.add(Double.parseDouble(latencyInNanoSeconds));
}
}
}
private void calculateStatistics() {
for (String searchTerm : latencyMap.keySet()) {
LOG.info("\n\nStatistics for search term '" + searchTerm + "'\n");
DescriptiveStatistics statsCalculator = new DescriptiveStatistics();
List<Double> stats = latencyMap.get(searchTerm);
for (double latencyNum : stats) {
statsCalculator.addValue(latencyNum);
}
double avg = statsCalculator.getMean();
double median = statsCalculator.getPercentile(50);
double worst5Percent = statsCalculator.getPercentile(95);
double worstlatency = statsCalculator.getMax();
System.out.println("Total Queries (post JIT): " + stats.size());
System.out.println("Average: " + (int)avg);
System.out.println("Median: " + (int)median);
System.out.println("Worst 5 Percent: " + (int)worst5Percent);
System.out.println("Worst Latency: " + (int)worstlatency);
}
}
}