package org.spoofax.jsglr2.benchmark.jsglr1;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.infra.Blackhole;
import org.spoofax.interpreter.terms.IStrategoTerm;
import org.spoofax.jsglr.client.InvalidParseTableException;
import org.spoofax.jsglr.client.NullTreeBuilder;
import org.spoofax.jsglr.client.SGLR;
import org.spoofax.jsglr.shared.SGLRException;
import org.spoofax.jsglr2.benchmark.BaseBenchmark;
import org.spoofax.jsglr2.integration.WithJSGLR1;
import org.spoofax.jsglr2.testset.TestSetWithParseTableReader;
import org.spoofax.jsglr2.testset.testinput.StringInput;
import org.spoofax.terms.ParseError;

public abstract class JSGLR1Benchmark extends BaseBenchmark<String, StringInput> implements WithJSGLR1 {

    protected TestSetWithParseTableReader<String, StringInput> testSetReader;

    protected SGLR jsglr1parse;
    protected SGLR jsglr1parseAndImplode;

    @Param({ "false", "true" }) public boolean implode;

    @Setup public void prepare() throws ParseError, InvalidParseTableException {
        jsglr1parseAndImplode = getJSGLR1();

        jsglr1parse = getJSGLR1();
        jsglr1parse.setTreeBuilder(new NullTreeBuilder());
    }

    public IStrategoTerm getParseTableTerm() {
        return testSetReader.getParseTableTerm();
    }

    @Benchmark public void jsglr1default(Blackhole bh) throws SGLRException, InterruptedException {
        if(implode) {
            for(StringInput input : inputs)
                bh.consume(jsglr1parseAndImplode.parse(input.content, null, null));
        } else {
            for(StringInput input : inputs)
                bh.consume(jsglr1parse.parse(input.content, null, null));
        }
    }

}

@@ -556,16 +556,65 @@
 JSGLR1;%0A
+import org.spoofax.jsglr2.testset.TestSetReader;%0A
 import o
@@ -1076,24 +1076,224 @@
 n implode;%0A%0A
+    protected void setTestSetReader(TestSetWithParseTableReader%3CString, StringInput%3E testSetReader) %7B%0A        super.setTestSetReader(testSetReader);%0A        this.testSetReader = testSetReader;%0A    %7D%0A%0A
     @Setup p

Fix external JSGLR1 benchmark NPE