Skip to content

Commit

Permalink
Test parallelism in EnsoParser. Using synchronized to avoid it. (#1…
Browse files Browse the repository at this point in the history
…1174)

* Stress test to verify how EnsoParser behaves under multi threaded load

* Avoid concurrency in native code by synchronizing parser methods
  • Loading branch information
JaroslavTulach authored Sep 26, 2024
1 parent a63a616 commit 7269cdf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ public EnsoParser() {
}

@Override
public void close() throws Exception {
public synchronized void close() throws Exception {
if (parser != null) {
parser.close();
}
}

public Module compile(CharSequence src) {
public synchronized Module compile(CharSequence src) {
var tree = parser.parse(src);
return generateIR(tree);
}

public Tree parse(CharSequence src) {
public synchronized Tree parse(CharSequence src) {
return parser.parse(src);
}

public Module generateIR(Tree t) {
public synchronized Module generateIR(Tree t) {
return TreeToIr.MODULE.translate(t);
}

public Module generateModuleIr(Tree t, Map<Location, UUID> idMap) {
public synchronized Module generateModuleIr(Tree t, Map<Location, UUID> idMap) {
var treeToIr = new TreeToIr(idMap);
return treeToIr.translate(t);
}

public scala.Option<Expression> generateIRInline(Tree t) {
public synchronized scala.Option<Expression> generateIRInline(Tree t) {
return TreeToIr.MODULE.translateInline(t);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.enso.compiler.core;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ForkJoinPool;
import org.enso.compiler.core.ir.Module;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class EnsoParserMultiThreadedTest {
private EnsoParser parser;

@Before
public void initParser() throws Exception {
parser = new EnsoParser();
}

@After
public void closeParser() throws Exception {
parser.close();
}

@Test
public void stressLoadFromManyThreads() throws Exception {
List<Callable<Module>> cases = new ArrayList<>();
final var testCases = 1000;
for (var i = 0; i < 2 * testCases; i++) {
var number = i % testCases;
var code =
"""
from Standard.Base import all
main = %n
"""
.replace("%n", "" + number);
cases.add(
() -> {
return parser.compile(code);
});
}

var results = ForkJoinPool.commonPool().invokeAll(cases);

for (var i = 0; i < testCases; i++) {
var r1 = results.get(i).get();
var r2 = results.get(testCases + i).get();

EnsoParserTest.assertIR("Run #" + i + " should produce identical IR", r1, r2);
}
}
}

0 comments on commit 7269cdf

Please sign in to comment.