Skip to content

Commit

Permalink
Use SrcUtil.source to make sure IGV can find .enso source on disk
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Feb 29, 2024
1 parent f34d56b commit 6bcd8dc
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.enso.interpreter.bench.benchmarks.semantic;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import org.enso.interpreter.bench.Utils;
import org.enso.polyglot.RuntimeOptions;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
Expand Down Expand Up @@ -198,7 +198,7 @@ public class AtomBenchmarks {
private Value mapReverseListCurry;

@Setup
public void initializeBenchmarks(BenchmarkParams params) {
public void initializeBenchmarks(BenchmarkParams params) throws IOException {
this.context =
Context.newBuilder()
.allowExperimentalOptions(true)
Expand All @@ -211,19 +211,50 @@ public void initializeBenchmarks(BenchmarkParams params) {
Paths.get("../../distribution/component").toFile().getAbsolutePath())
.build();

var millionElemListMethod = Utils.getMainMethod(context, MILLION_ELEMENT_LIST);
var lastDot = params.getBenchmark().lastIndexOf('.');
var name = params.getBenchmark().substring(lastDot + 1);
var millionElemListMethod = mainMethod(context, name, MILLION_ELEMENT_LIST);
this.millionElementsList = millionElemListMethod.execute();
this.generateList = Utils.getMainMethod(context, GENERATE_LIST_CODE);
this.generateListQualified = Utils.getMainMethod(context, GENERATE_LIST_QUALIFIED_CODE);
this.generateListAutoscoping = Utils.getMainMethod(context, GENERATE_LIST_AUTOSCOPING_CODE);
this.reverseList = Utils.getMainMethod(context, REVERSE_LIST_CODE);
this.reverseListMethods = Utils.getMainMethod(context, REVERSE_LIST_METHODS_CODE);
this.sumList = Utils.getMainMethod(context, SUM_LIST_CODE);
this.sumListLeftFold = Utils.getMainMethod(context, SUM_LIST_LEFT_FOLD_CODE);
this.sumListFallback = Utils.getMainMethod(context, SUM_LIST_FALLBACK_CODE);
this.sumListMethods = Utils.getMainMethod(context, SUM_LIST_METHODS_CODE);
this.mapReverseList = Utils.getMainMethod(context, MAP_REVERSE_LIST_CODE);
this.mapReverseListCurry = Utils.getMainMethod(context, MAP_REVERSE_LIST_CURRY_CODE);
switch (name) {
case "benchGenerateList" -> {
this.generateList = mainMethod(context, name, GENERATE_LIST_CODE);
}
case "benchGenerateListQualified" -> {
this.generateListQualified = mainMethod(context, name, GENERATE_LIST_QUALIFIED_CODE);
}
case "benchGenerateListAutoscoping" -> {
this.generateListAutoscoping = mainMethod(context, name, GENERATE_LIST_AUTOSCOPING_CODE);
}
case "benchReverseList" -> {
this.reverseList = mainMethod(context, name, REVERSE_LIST_CODE);
}
case "benchReverseListMethods" -> {
this.reverseListMethods = mainMethod(context, name, REVERSE_LIST_METHODS_CODE);
}
case "benchSumList" -> {
this.sumList = mainMethod(context, name, SUM_LIST_CODE);
}
case "benchSumListLeftFold" -> {
this.sumListLeftFold = mainMethod(context, name, SUM_LIST_LEFT_FOLD_CODE);
}
case "benchSumListFallback" -> {
this.sumListFallback = mainMethod(context, name, SUM_LIST_FALLBACK_CODE);
}
case "benchSumListMethods" -> {
this.sumListMethods = mainMethod(context, name, SUM_LIST_METHODS_CODE);
}
case "benchMapReverseList" -> {
this.mapReverseList = mainMethod(context, name, MAP_REVERSE_LIST_CODE);
}
case "benchMapReverseListCurry" -> {
this.mapReverseListCurry = mainMethod(context, name, MAP_REVERSE_LIST_CURRY_CODE);
}
default -> throw new IllegalArgumentException(name);
}
}

private static Value mainMethod(Context context, String name, String code) throws IOException {
return SrcUtil.getMainMethod(context, name, code);
}

@Benchmark
Expand Down Expand Up @@ -266,7 +297,7 @@ public void benchSumList(Blackhole bh) {
}

@Benchmark
public void sumListLeftFold(Blackhole bh) {
public void benchSumListLeftFold(Blackhole bh) {
var res = sumListLeftFold.execute(millionElementsList);
if (!res.fitsInLong()) {
throw new AssertionError("Should return a number");
Expand Down Expand Up @@ -299,7 +330,7 @@ public void benchMapReverseList(Blackhole bh) {
}

@Benchmark
public void benchMapReverseCurryList(Blackhole bh) {
public void benchMapReverseListCurry(Blackhole bh) {
var res = mapReverseListCurry.execute(millionElementsList);
bh.consume(res);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;
import org.enso.polyglot.MethodNames.Module;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.openjdk.jmh.infra.BenchmarkParams;

final class SrcUtil {
Expand All @@ -30,4 +33,16 @@ static Source read(String benchmarkName) throws IOException {
Objects.requireNonNull(url, "Searching for " + resource);
return Source.newBuilder("enso", url).name(resource).build();
}

static Value getMainMethod(Context context, String benchmarkName, String code)
throws IOException {
var src = source(benchmarkName, code);
var module = context.eval(src);
var moduleType = module.invokeMember(Module.GET_ASSOCIATED_TYPE);
var main = module.invokeMember(Module.GET_METHOD, moduleType, "main");
if (!main.canExecute()) {
throw new AssertionError("Main method should be executable");
}
return main;
}
}

0 comments on commit 6bcd8dc

Please sign in to comment.