package org.commonmark.test;

import org.commonmark.parser.Parser;
import org.commonmark.renderer.html.HtmlRenderer;
import org.commonmark.testutil.TestResources;
import org.commonmark.testutil.example.ExampleReader;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.options.CommandLineOptions;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import java.util.Collections;
import java.util.List;

@State(Scope.Benchmark)
public class SpecBenchmark {

    private static final String SPEC = TestResources.readAsString(TestResources.getSpec());
    private static final List<String> SPEC_EXAMPLES = ExampleReader.readExampleSources(TestResources.getSpec());
    private static final Parser PARSER = Parser.builder().build();
    private static final HtmlRenderer RENDERER = HtmlRenderer.builder().build();

    public static void main(String[] args) throws Exception {
        Options options = new OptionsBuilder()
                .parent(new CommandLineOptions(args))
                .include(SpecBenchmark.class.getName() + ".*")
                .build();
        new Runner(options).run();
    }

    @Benchmark
    public long wholeSpec() {
        return parseAndRender(Collections.singletonList(SPEC));
    }

    @Benchmark
    public long examples() {
        return parseAndRender(SPEC_EXAMPLES);
    }

    private static long parseAndRender(List<String> examples) {
        long length = 0;
        for (String example : examples) {
            String result = RENDERER.render(PARSER.parse(example));
            length += result.length();
        }
        return length;
    }

}

@@ -23,16 +23,49 @@
 .test;%0A%0A
+import org.commonmark.node.Node;%0A
 import o
@@ -89,24 +89,24 @@
 ser.Parser;%0A
-
 import org.c
@@ -282,101 +282,9 @@
 ons.
-Benchmark;%0Aimport org.openjdk.jmh.annotations.Scope;%0Aimport org.openjdk.jmh.annotations.State
+*
 ;%0Aim
@@ -557,16 +557,80 @@
 chmark)%0A
+@Fork(5)%0A@Warmup(iterations = 10)%0A@Measurement(iterations = 20)%0A
 public c
@@ -1330,17 +1330,236 @@
 ic long 
-w
+parseWholeSpec() %7B%0A        return parse(Collections.singletonList(SPEC));%0A    %7D%0A%0A    @Benchmark%0A    public long parseExamples() %7B%0A        return parse(SPEC_EXAMPLES);%0A    %7D%0A%0A    @Benchmark%0A    public long parseAndRenderW
 holeSpec
@@ -1665,17 +1665,31 @@
 ic long 
-e
+parseAndRenderE
 xamples(
@@ -2023,10 +2023,305 @@
 %0A    %7D%0A%0A
+    private static long parse(List%3CString%3E examples) %7B%0A        long length = 0;%0A        for (String example : examples) %7B%0A            Node document = PARSER.parse(example);%0A            length += document.getFirstChild() == document.getLastChild() ? 0 : 1;%0A        %7D%0A        return length;%0A    %7D%0A
 %7D%0A

Add parse-only benchmarks