mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-01-16 10:52:03 -06:00
make graal js and polyglot support primary
This commit is contained in:
parent
b2a5ba03da
commit
24f32f31b0
@ -1,37 +1,39 @@
|
||||
/*
|
||||
* Copyright 2016 jshook
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
* Copyright 2016 jshook
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package io.nosqlbench.engine.core.script;
|
||||
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import com.codahale.metrics.MetricRegistry;
|
||||
import io.nosqlbench.engine.core.ProgressIndicator;
|
||||
import io.nosqlbench.engine.core.ScenarioController;
|
||||
import io.nosqlbench.engine.core.ScenarioLogger;
|
||||
import io.nosqlbench.engine.core.ScenarioResult;
|
||||
import com.oracle.truffle.js.scriptengine.GraalJSScriptEngine;
|
||||
import io.nosqlbench.engine.core.*;
|
||||
import io.nosqlbench.engine.core.metrics.PolyglotMetricRegistryBindings;
|
||||
import io.nosqlbench.nb.api.errors.BasicError;
|
||||
import io.nosqlbench.engine.api.extensions.ScriptingPluginInfo;
|
||||
import io.nosqlbench.engine.api.metrics.ActivityMetrics;
|
||||
import io.nosqlbench.engine.core.metrics.MetricRegistryBindings;
|
||||
import io.nosqlbench.engine.core.metrics.NashornMetricRegistryBindings;
|
||||
import io.nosqlbench.engine.api.scripting.ScriptEnvBuffer;
|
||||
import org.graalvm.polyglot.Context;
|
||||
import org.graalvm.polyglot.EnvironmentAccess;
|
||||
import org.graalvm.polyglot.HostAccess;
|
||||
import org.graalvm.polyglot.PolyglotAccess;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.script.*;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
@ -55,7 +57,6 @@ public class Scenario implements Callable<ScenarioResult> {
|
||||
private String name;
|
||||
private ScenarioLogger scenarioLogger;
|
||||
private ScriptParams scenarioScriptParams;
|
||||
private boolean areChartsEnabled;
|
||||
private Engine engine = Engine.Graalvm;
|
||||
|
||||
public enum Engine {
|
||||
@ -107,46 +108,69 @@ public class Scenario implements Callable<ScenarioResult> {
|
||||
scriptEngine = engineManager.getEngineByName("nashorn");
|
||||
break;
|
||||
case Graalvm:
|
||||
scriptEngine = engineManager.getEngineByName("graal.js");
|
||||
Bindings bindings = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
|
||||
bindings.put("polyglot.js.allowHostAccess", true);
|
||||
bindings.put("polyglot.js.allowNativeAccess", true);
|
||||
bindings.put("polyglot.js.allowCreateThread", true);
|
||||
bindings.put("polyglot.js.allowIO", true);
|
||||
bindings.put("polyglot.js.allowHostClassLookup", true);
|
||||
bindings.put("polyglot.js.allowHostClassLoading", true);
|
||||
bindings.put("polyglot.js.allowAllAccess", true);
|
||||
Context.Builder contextSettings = Context.newBuilder("js")
|
||||
.allowHostAccess(HostAccess.ALL)
|
||||
.allowNativeAccess(true)
|
||||
.allowCreateThread(true)
|
||||
.allowIO(true)
|
||||
.allowHostClassLookup(s -> true)
|
||||
.allowHostClassLoading(true)
|
||||
.allowCreateProcess(true)
|
||||
.allowAllAccess(true)
|
||||
.allowEnvironmentAccess(EnvironmentAccess.INHERIT)
|
||||
.allowPolyglotAccess(PolyglotAccess.ALL)
|
||||
.option("js.ecmascript-version", "2020")
|
||||
.option("js.nashorn-compat", "true");
|
||||
|
||||
scriptEngine = GraalJSScriptEngine.create(null, contextSettings);
|
||||
|
||||
try {
|
||||
scriptEngine.put("javaObj", new Object());
|
||||
scriptEngine.eval("(javaObj instanceof Java.type('java.lang.Object'));");
|
||||
} catch (ScriptException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
scenarioController = new ScenarioController();
|
||||
if (!progressInterval.equals("disabled")) {
|
||||
progressIndicator = new ProgressIndicator(scenarioController,progressInterval);
|
||||
progressIndicator = new ProgressIndicator(scenarioController, progressInterval);
|
||||
}
|
||||
|
||||
scriptEnv = new ScenarioContext(scenarioController);
|
||||
scriptEngine.setContext(scriptEnv);
|
||||
|
||||
scriptEngine.put("params", scenarioScriptParams);
|
||||
scriptEngine.put("scenario", scenarioController);
|
||||
scriptEngine.put("activities", new ActivityBindings(scenarioController));
|
||||
scriptEngine.put("metrics", new MetricRegistryBindings(metricRegistry));
|
||||
|
||||
for (ScriptingPluginInfo extensionDescriptor : SandboxExtensionFinder.findAll()) {
|
||||
if (engine == Engine.Graalvm) {
|
||||
scriptEngine.put("scenario", new PolyglotScenarioController(scenarioController));
|
||||
scriptEngine.put("metrics", new PolyglotMetricRegistryBindings(metricRegistry));
|
||||
scriptEngine.put("activities", new NashornActivityBindings(scenarioController));
|
||||
} else if (engine == Engine.Nashorn) {
|
||||
scriptEngine.put("scenario", scenarioController);
|
||||
scriptEngine.put("metrics", new NashornMetricRegistryBindings(metricRegistry));
|
||||
scriptEngine.put("activities", new NashornActivityBindings(scenarioController));
|
||||
} else {
|
||||
throw new RuntimeException("Unsupported engine: " + engine);
|
||||
}
|
||||
|
||||
for (ScriptingPluginInfo<?> extensionDescriptor : SandboxExtensionFinder.findAll()) {
|
||||
if (!extensionDescriptor.isAutoLoading()) {
|
||||
logger.info("Not loading " + extensionDescriptor + ", autoloading is false");
|
||||
continue;
|
||||
}
|
||||
|
||||
org.slf4j.Logger extensionLogger =
|
||||
LoggerFactory.getLogger("extensions." + extensionDescriptor.getBaseVariableName());
|
||||
LoggerFactory.getLogger("extensions." + extensionDescriptor.getBaseVariableName());
|
||||
Object extensionObject = extensionDescriptor.getExtensionObject(
|
||||
extensionLogger,
|
||||
metricRegistry,
|
||||
scriptEnv
|
||||
extensionLogger,
|
||||
metricRegistry,
|
||||
scriptEnv
|
||||
);
|
||||
logger.debug("Adding extension object: name=" + extensionDescriptor.getBaseVariableName() +
|
||||
" class=" + extensionObject.getClass().getSimpleName());
|
||||
" class=" + extensionObject.getClass().getSimpleName());
|
||||
scriptEngine.put(extensionDescriptor.getBaseVariableName(), extensionObject);
|
||||
}
|
||||
|
||||
@ -173,7 +197,7 @@ public class Scenario implements Callable<ScenarioResult> {
|
||||
logger.debug("<- scenario completed (interpreted)");
|
||||
}
|
||||
|
||||
if (result!=null) {
|
||||
if (result != null) {
|
||||
logger.debug("scenario result: type(" + result.getClass().getCanonicalName() + "): value:" + result.toString());
|
||||
}
|
||||
System.err.flush();
|
||||
@ -200,11 +224,11 @@ public class Scenario implements Callable<ScenarioResult> {
|
||||
scenarioController.forceStopScenario(5000);
|
||||
throw new RuntimeException("Non-Script error while running scenario:" + o.getMessage(), o);
|
||||
} finally {
|
||||
System.out.flush();
|
||||
System.err.flush();
|
||||
System.out.flush();
|
||||
System.err.flush();
|
||||
}
|
||||
}
|
||||
int awaitCompletionTime = 86400*365*1000;
|
||||
int awaitCompletionTime = 86400 * 365 * 1000;
|
||||
logger.debug("Awaiting completion of scenario for " + awaitCompletionTime + " millis.");
|
||||
scenarioController.awaitCompletion(awaitCompletionTime);
|
||||
logger.debug("scenario completed without errors");
|
||||
@ -256,8 +280,11 @@ public class Scenario implements Callable<ScenarioResult> {
|
||||
public void addScenarioScriptParams(ScriptParams scenarioScriptParams) {
|
||||
this.scenarioScriptParams = scenarioScriptParams;
|
||||
}
|
||||
public void addScenarioScriptParams(Map<String,String> scriptParams) {
|
||||
addScenarioScriptParams(new ScriptParams() {{ putAll(scriptParams);}});
|
||||
|
||||
public void addScenarioScriptParams(Map<String, String> scriptParams) {
|
||||
addScenarioScriptParams(new ScriptParams() {{
|
||||
putAll(scriptParams);
|
||||
}});
|
||||
}
|
||||
|
||||
public void enableCharting() {
|
||||
|
Loading…
Reference in New Issue
Block a user