After Profiling is done or JMH Microbenchmark in action | DIMI’s place

DIMI's place

My thoughts on different things

17 October 2017

After Profiling is done or JMH Microbenchmark in action

Which method is faster, when reading single 5mb file?

public static String readFileContents1(Path pathToFile){
    return new Scanner(pathToFile).useDelimiter("\\Z").next();
}

public static String readFileContents2(Path pathToFile){
    return new String(Files.readAllBytes(pathToFile));
}

Another one, since java8 is around for a while - getGroupDir has around 80 files, 100K each :

public List<String> method1() throws IOException {
    List<String> singleIds = new ArrayList<>();

    for (File groupFile : fileHandler.getGroupDir().toFile().listFiles()) {
        String groupFileContents = FileHandler.readFileContents(groupFile.toPath());
        Collection<String> results = extractLinks(groupFileContents);
        singleIds.addAll(results);
    }

    fileHandler.saveSingleIdsToFile(singleIds);
    return singleIds;
}
public List<String> method2() throws IOException {
    List<String> singleIds =
            Files.list(fileHandler.getGroupDir())
                    .map(file -> readFileContents(file.toFile()))
                    .filter(s -> !s.isEmpty())
                    .map(this::extractLinks)
                    .flatMap(Collection::stream)
                    .collect(Collectors.toList());

    fileHandler.saveSingleIdsToFile(singleIds);

    return singleIds;
}
public List<String> method3() throws IOException {
    List<String> singleIds =
            Files.list(fileHandler.getGroupDir())
                    .parallel()
                    .map(FileHandler::readFileContents)
                    .map(this::extractLinks)
                    .collect(ArrayList::new, List::addAll, List::addAll);

    fileHandler.saveSingleIdsToFile(singleIds);

    return singleIds;
}

The answer is - JMH

What is JMH

JMH is micro performnace test framework that allows you to get performance of test methods instead of whole applications.

Example of test definition

Here is results from profiling execution:

Benchmark Mode  Cnt   Score   Error  Units
method1   avgt   40  41.099 ± 1.433  ms/op
method2   avgt   40  43.211 ± 3.390  ms/op
method3   avgt   40  39.395 ± 0.808  ms/op

(No much differnece, who would thought ?)

Benchmark          Mode  Cnt    Score   Error  Units
readFileContents1  avgt   40  102.399 ± 7.475  ms/op
readFileContents2  avgt   40   10.083 ± 0.426  ms/op

(Huuuge x10 diference on reading each file contents)

There are also some goodies that can be used with JMH:

tags: java - performance - jmh