Eval benchmark
This commit is contained in:
parent
1fb0adb351
commit
fd1d82bd7c
4 changed files with 88 additions and 2 deletions
|
@ -26,4 +26,20 @@ public class Common {
|
|||
}
|
||||
}
|
||||
|
||||
public static int div(int a, int b){
|
||||
if (b == 0) return 0x7fffffff;
|
||||
if (a == 0x80000000 && b == -1) {
|
||||
return 0x7fffffff;
|
||||
}
|
||||
return a / b;
|
||||
}
|
||||
|
||||
public static int modulus(int a, int b) {
|
||||
if (b == 0) return 0x7fffffff;
|
||||
if (a == 0x80000000 && b == -1) {
|
||||
return 0x7fffffff;
|
||||
}
|
||||
return a % b;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
70
benchmark/src/main/java/org/capnproto/benchmark/Eval.java
Normal file
70
benchmark/src/main/java/org/capnproto/benchmark/Eval.java
Normal file
|
@ -0,0 +1,70 @@
|
|||
package org.capnproto.benchmark;
|
||||
|
||||
import org.capnproto.MessageBuilder;
|
||||
import org.capnproto.StructList;
|
||||
import org.capnproto.Text;
|
||||
import org.capnproto.benchmark.EvalSchema.*;
|
||||
|
||||
public class Eval {
|
||||
public static int makeExpression(Common.FastRand rng, Expression.Builder exp, int depth) {
|
||||
exp.setOp(Operation.values()[rng.nextLessThan(Operation.values().length)]);
|
||||
|
||||
int left = 0;
|
||||
if (rng.nextLessThan(8) < depth) {
|
||||
int tmp = rng.nextLessThan(128) + 1;
|
||||
exp.getLeft().setValue(tmp);
|
||||
left = tmp;
|
||||
} else {
|
||||
left = makeExpression(rng, exp.getLeft().initExpression(), depth + 1);
|
||||
}
|
||||
|
||||
int right = 0;
|
||||
if (rng.nextLessThan(8) < depth) {
|
||||
int tmp = rng.nextLessThan(128) + 1;
|
||||
exp.getRight().setValue(tmp);
|
||||
right = tmp;
|
||||
} else {
|
||||
right = makeExpression(rng, exp.getRight().initExpression(), depth + 1);
|
||||
}
|
||||
|
||||
switch (exp.getOp()) {
|
||||
case ADD: return left + right;
|
||||
case SUBTRACT: return left - right;
|
||||
case MULTIPLY: return left * right;
|
||||
case DIVIDE: return Common.div(left, right);
|
||||
case MODULUS: return Common.modulus(left, right);
|
||||
default:
|
||||
throw new Error("impossible");
|
||||
}
|
||||
}
|
||||
|
||||
public static int evaluateExpression(Expression.Reader exp) {
|
||||
int left = 0, right = 0;
|
||||
|
||||
switch (exp.getLeft().which()) {
|
||||
case VALUE:
|
||||
left = exp.getLeft().getValue();
|
||||
break;
|
||||
case EXPRESSION:
|
||||
left = evaluateExpression(exp.getLeft().getExpression());
|
||||
}
|
||||
|
||||
switch (exp.getRight().which()) {
|
||||
case VALUE:
|
||||
right = exp.getRight().getValue();
|
||||
break;
|
||||
case EXPRESSION:
|
||||
right = evaluateExpression(exp.getRight().getExpression());
|
||||
}
|
||||
|
||||
switch (exp.getOp()) {
|
||||
case ADD: return left + right;
|
||||
case SUBTRACT: return left - right;
|
||||
case MULTIPLY: return left * right;
|
||||
case DIVIDE: return Common.div(left, right);
|
||||
case MODULUS: return Common.modulus(left, right);
|
||||
default:
|
||||
throw new Error("impossible");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -28,7 +28,7 @@ $Cxx.namespace("capnp::benchmark::capnp");
|
|||
|
||||
using Java = import "/java_support/java.capnp";
|
||||
$Java.package("org.capnproto.benchmark");
|
||||
$Java.outerClassname("Catrank");
|
||||
$Java.outerClassname("CatrankSchema");
|
||||
|
||||
struct SearchResultList {
|
||||
results@0: List(SearchResult);
|
||||
|
|
|
@ -28,7 +28,7 @@ $Cxx.namespace("capnp::benchmark::capnp");
|
|||
|
||||
using Java = import "/java_support/java.capnp";
|
||||
$Java.package("org.capnproto.benchmark");
|
||||
$Java.outerClassname("Eval");
|
||||
$Java.outerClassname("EvalSchema");
|
||||
|
||||
enum Operation {
|
||||
add @0;
|
||||
|
|
Loading…
Reference in a new issue