1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
| public static final String expression = "(a+(b-c)*d/e) > 123";
public static void main(String[] args) {
int times = 100000; Map<String,Object> paraMap = new HashMap<>(); paraMap.put("a",122); paraMap.put("b",100); paraMap.put("c",93); paraMap.put("d",3); paraMap.put("e",7);
System.out.println("aviator cost " + aviatorKest(expression,paraMap,times)); System.out.println("scriptEngine cost " + scriptEngineKest(expression,paraMap,times)); System.out.println("qlExpress cost " + qlExpressKest(expression,paraMap,times)); System.out.println("groovy cost " + groovykest(expression,paraMap,times)); System.out.println("apacheJexl cost " + jexlKest(expression,paraMap,times));
}
public static long aviatorKest(String expression, Map<String,Object> paraMap, int times){
long start = System.currentTimeMillis();
AviatorEvaluatorInstance aviatorEvaluatorInstance = AviatorEvaluator.getInstance();
Expression compile = aviatorEvaluatorInstance.compile(expression, true);
for (int i = 0 ;i<times;i++){
Boolean ret = (Boolean)compile.execute(paraMap);
if(!ret){ System.err.println("aviatorKest return error"); } }
return System.currentTimeMillis()-start; }
public static long scriptEngineKest(String expression, Map<String,Object> paraMap, int times){
ScriptContext context = new SimpleScriptContext();
for (Map.Entry<String ,Object> entry : paraMap.entrySet()){ context.setAttribute(entry.getKey(),entry.getValue(),ENGINE_SCOPE); }
long start = System.currentTimeMillis();
ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine scriptEngine = factory.getEngineByName("JavaScript");
for (int i = 0 ;i<times;i++){
try { Boolean res = (Boolean)scriptEngine.eval(expression,context); if(!res){ System.err.println("aviatorKest return error"); } }catch (Exception e){ e.printStackTrace(); } } return System.currentTimeMillis()-start; }
public static long qlExpressKest(String expression, Map<String,Object> paraMap, int times){
long start = System.currentTimeMillis();
ExpressRunner runner = new ExpressRunner();
DefaultContext<String, Object> context = new DefaultContext<>(); for (Map.Entry<String ,Object> entry : paraMap.entrySet()){ context.put(entry.getKey(),entry.getValue()); }
for (int i = 0 ;i<times;i++){
try { Boolean ret = (Boolean)runner.execute(expression, context, null, true, false);
if(!ret){ System.err.println("aviatorKest return error"); } } catch (Exception e) { e.printStackTrace(); } }
return System.currentTimeMillis()-start; }
public static long groovykest(String expression, Map<String,Object> paraMap, int times){
long start = System.currentTimeMillis();
GroovyShell groovyShell = new GroovyShell(); Script script = groovyShell.parse(expression,"test");
for (int i = 0 ;i<times;i++) { script.getBinding().getVariables().putAll(paraMap); Boolean ret = (Boolean)script.run(); if(!ret){ System.err.println("aviatorKest return error"); } }
return System.currentTimeMillis()-start; }
public static long jexlKest(String expression, Map<String,Object> paraMap, int times){ long start = System.currentTimeMillis();
JexlEngine jexl = new JexlEngine();
JexlContext jc = new MapContext(); for (Map.Entry<String ,Object> entry : paraMap.entrySet()){ jc.set(entry.getKey(),entry.getValue()); }
org.apache.commons.jexl2.Expression e = jexl.createExpression(expression);
for (int i = 0 ;i<times;i++) {
Boolean ret = (Boolean)e.evaluate(jc); if (!ret) { System.err.println("aviatorKest return error"); } }
return System.currentTimeMillis()-start; }
|