Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//program that loads dust-full.js + the compiled template hp.js and
//renders the compiled template using feed.json data
var iterations = arguments[0];
if (!this.readFully) readFully = readAll;
load("dust-full.js");
load("hp.js");
function renderTime(templateName, templateJsonFile) {
var start = Date.now();
var data = JSON.parse(readFully(templateJsonFile));
dust.render(templateName, data, function(err, out) {
if (err) throw err;
// print(out);
});
return Date.now() - start;
}
/*
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of Oracle nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import javax.script.Bindings;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;
import java.io.File;
import java.net.URL;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import jdk.nashorn.api.scripting.URLReader;
/**
* Example for using the Nashorn internal class cache with multiple threads.
*/
public class ThreadedClassCacheDemo {
public static void main(String[] args) throws Exception {
// Get a Nashorn script engine with default options
final ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine(new String[] {"-scripting" /*, "--log=classcache" */});
for (int i = 0; i < 8; i++) {
runInThread(engine);
}
}
private static void runInThread(final ScriptEngine engine) {
new Thread() {
@Override
public void run() {
System.err.println("Thread starting");
final Bindings global = engine.createBindings();
final ScriptContext context = new SimpleScriptContext();
context.setBindings(global, ScriptContext.ENGINE_SCOPE);
final long start = System.currentTimeMillis();
try {
final URL bench = new File("bench.js").toURI().toURL();
engine.eval(new URLReader(bench), context);
for (int i = 0; i < 100; i++) {
System.err.println(((Invocable)engine).invokeMethod(global, "renderTime", "ozfeed-templates/feed", "hp.json"));
}
} catch (final Exception e) {
System.err.println("Error evaluating script: " + e);
}
System.out.println("Thread finished in " + (System.currentTimeMillis() - start) + " milliseconds");
}
}.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.