Skip to content

Instantly share code, notes, and snippets.

@kimchy
Created January 30, 2011 12:57
Show Gist options
  • Select an option

  • Save kimchy/802841 to your computer and use it in GitHub Desktop.

Select an option

Save kimchy/802841 to your computer and use it in GitHub Desktop.
import groovy.lang.Binding;
import groovy.lang.GroovyClassLoader;
import groovy.lang.Script;
import org.mvel2.MVEL;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
public class PerfTest {
static enum TYPE {
MVEL,
GROOVY,
}
public static void main(String[] args) throws Exception {
final long COUNT = 1000000;
TYPE type = TYPE.GROOVY;
System.out.println("Type: " + type + ", count " + COUNT);
Map<String, Object> params = new HashMap<String, Object>();
params.put("time", System.currentTimeMillis());
Map<String, Object> doc = new HashMap<String, Object>();
doc.put("dt", new Value(System.currentTimeMillis() - 5000));
params.put("doc", doc);
// warmup
if (type == TYPE.MVEL) {
perfMvel(10000, params);
} else if (type == TYPE.GROOVY) {
perfGroovy(10000, params);
}
long took = 0;
if (type == TYPE.MVEL) {
took = perfMvel(COUNT, params);
} else if (type == PerfTest.TYPE.GROOVY) {
took = perfGroovy(COUNT, params);
}
System.out.println("Took " + took + "ms, Per Ms: " + (COUNT / took));
}
public static long perfGroovy(long count, Map<String, Object> params) throws Exception {
GroovyClassLoader loader = new GroovyClassLoader();
Class compiled = loader.parseClass("_score * (800 / (10.0e-9 * (time - doc['dt'].value) + 1))", "test1");
Script scriptObject = (Script) compiled.newInstance();
Binding binding = new Binding();
binding.getVariables().putAll(params);
scriptObject.setBinding(binding);
long time = System.currentTimeMillis();
for (long i = 0; i < count; i++) {
float f = 0.4567f;
scriptObject.getBinding().getVariables().put("_score", f);
scriptObject.run();
}
return System.currentTimeMillis() - time;
}
public static long perfMvel(long count, Map<String, Object> params) throws Exception {
Serializable script = MVEL.compileExpression("_score * (800 / (10.0e-9 * (time - doc['dt'].value) + 1))");
long time = System.currentTimeMillis();
for (long i = 0; i < count; i++) {
float f = 0.4567f;
params.put("_score", f);
MVEL.executeExpression(script, params);
}
return System.currentTimeMillis() - time;
}
public static class Value {
private final long value;
public Value(long value) {
this.value = value;
}
public long getValue() {
return value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment