Commit a7834690 authored by Noric Couderc's avatar Noric Couderc
Browse files

Save parser in history

Parser to parse output from the jmh perf output
Not useful anymore, but saving it in history.
parent 2b578911
package se.lth.cs.perf;
/**
* Parsing the perf output to get training data.
*/
import java.io.*;
import java.lang.reflect.Array;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Parser {
public static String parse() {
Path p = Paths.get("output-perf.txt");
ArrayList<String> lines = new ArrayList<>();
try {
// Getting all the lines in the output-perf.txt file
FileReader fr = new FileReader(p.toFile());
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
String message = "File not found. The working directory is:";
String currDir = "'" + System.getProperty("user.dir") + "'";
System.out.println(message + currDir);
}
ArrayList<String> data = filterLines(lines);
return "";
}
/**
* Skips through the beginning of the log up to "Perf stats:"
* @param lines
* @return
*/
public static ArrayList<String> filterLines(ArrayList<String> lines) {
String perfHeader = "Perf stats:";
boolean perfHeadFound = false;
// Read full file after the header "Perf stats:"
ArrayList<String> data = new ArrayList<>();
for (String line : lines) {
if (line == "Perf stats:") {
perfHeadFound = true;
}
if (perfHeadFound) {
data.add(line);
}
}
return data;
}
/**
* Takes a list of values and puts them into separated bins (one per run)
* @param lines
* @return
*/
public static ArrayList<ArrayList<String>> subDivide(ArrayList<String> lines) {
// First line should match this
Pattern beginning = Pattern.compile("task-clock");
// End line of chunk should match this
Pattern end = Pattern.compile("seconds time elapsed");
Pattern whitespace = Pattern.compile("\\s+");
ArrayList<String> lines2 = lines.stream().filter( whitespace.asPredicate() )
.collect(Collectors.toCollection(ArrayList::new));
ArrayList<ArrayList<String>> result = new ArrayList<>();
boolean capture = false;
ArrayList<String> current = null;
for(String line : lines2) {
if(beginning.matcher(line).find()) {
current = new ArrayList<>();
result.add(current);
capture = true;
}
if(capture) {
current.add(line);
}
if(end.matcher(line).find()) {
current = null;
capture = false;
}
}
return result;
}
public static HashMap<String, String> tokenizeLine(String line) {
Pattern whitespaceRegex = Pattern.compile("\\s+");
Pattern number = Pattern.compile("[0-9]+(\\.[0-9]+)?");
Pattern word = Pattern.compile("[a-zA-Z0-9]+");
Pattern propertyLabel = Pattern.compile( word + "(" + "-" + word + ")*");
Stream<String> chunks = Arrays.stream(whitespaceRegex.split(line)).filter((s) -> !s.isEmpty());
ArrayList<String> tokens = chunks.collect(Collectors.toCollection(ArrayList::new));
if (tokens.size() == 0) {
return new HashMap<>();
}
if (number.matcher(tokens.get(0)).matches()) {
if (propertyLabel.matcher(tokens.get(1)).matches()) {
HashMap<String, String> result = new HashMap<>();
result.put("label", tokens.get(1));
result.put("value", tokens.get(0));
return result;
}
}
return new HashMap<>();
}
}
package test.java;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
public class ParsingTest {
@Test
public void TestTokenizingLine() {
Assert.assertEquals(
new HashMap<>(),
se.lth.cs.perf.Parser.tokenizeLine("# Commented")
);
}
@Test
public void TestTokenizingLine2() {
Assert.assertEquals(
new HashMap<>(),
se.lth.cs.perf.Parser.tokenizeLine("# Commented 3040")
);
}
@Test
public void TestTokenizingLine3() {
Assert.assertEquals(
new HashMap<>(),
se.lth.cs.perf.Parser.tokenizeLine("Commented 3040")
);
}
@Test
public void TestTokenizingLine4() {
HashMap<String, String> expected = new HashMap<>();
expected.put("label", "L1-cache-misses");
expected.put("value", "3040");
Assert.assertEquals(
expected,
se.lth.cs.perf.Parser.tokenizeLine("3040 L1-cache-misses")
);
}
@Test
public void TestTokenizingLine5() {
HashMap<String, String> expected = new HashMap<>();
expected.put("label", "task-clock");
expected.put("value", "764.589321");
Assert.assertEquals(
expected,
se.lth.cs.perf.Parser.tokenizeLine(" 764.589321 task-clock (msec) # 1.942 CPUs utilized \n")
);
}
@Test
public void TestSubDivide0() {
ArrayList<ArrayList<String>> expected = new ArrayList<>();
Assert.assertEquals(
expected,
se.lth.cs.perf.Parser.subDivide(new ArrayList<>())
);
}
@Test
public void TestSubDivide1() {
ArrayList<String> data = new ArrayList<>();
data.add(" 764.589321 task-clock (msec) # 1.942 CPUs utilized \n");
data.add(" 0.382101616 seconds time elapsed\n");
Assert.assertEquals(
data,
se.lth.cs.perf.Parser.subDivide(data).get(0)
);
}
@Test
public void TestSubDivide2() {
ArrayList<String> data = new ArrayList<>();
data.add(" 764.589321 task-clock (msec) # 1.942 CPUs utilized \n");
data.add(" 1756 context-switches # 0.002 M/sec \n");
data.add(" 0.382101616 seconds time elapsed\n");
Assert.assertEquals(
data,
se.lth.cs.perf.Parser.subDivide(data).get(0)
);
}
@Test
public void TestSubDivide3() {
ArrayList<String> data = new ArrayList<>();
data.add(" 764.589321 task-clock (msec) # 1.942 CPUs utilized \n");
data.add(" 1756 context-switches # 0.002 M/sec \n");
data.add(" ");
data.add(" 0.382101616 seconds time elapsed\n");
data.add(" ");
data.add(" 764.589321 task-clock (msec) # 1.942 CPUs utilized \n");
data.add(" 1756 context-switches # 0.002 M/sec \n");
data.add(" 0.382101616 seconds time elapsed\n");
ArrayList<ArrayList<String>> expected = new ArrayList<>();
expected.add(new ArrayList<>());
expected.get(0).add(" 764.589321 task-clock (msec) # 1.942 CPUs utilized \n");
expected.get(0).add(" 1756 context-switches # 0.002 M/sec \n");
expected.get(0).add(" 0.382101616 seconds time elapsed\n");
expected.add(new ArrayList<>());
expected.get(1).add(" 764.589321 task-clock (msec) # 1.942 CPUs utilized \n");
expected.get(1).add(" 1756 context-switches # 0.002 M/sec \n");
expected.get(1).add(" 0.382101616 seconds time elapsed\n");
Assert.assertEquals(
expected,
se.lth.cs.perf.Parser.subDivide(data)
);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment