Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions core/src/jmh/java/org/projectnessie/cel/CompileBuildBench.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (C) 2021 The Authors of CEL-Java
*
* 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 org.projectnessie.cel;

import static org.projectnessie.cel.Env.newEnv;
import static org.projectnessie.cel.EnvOption.declarations;

import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.projectnessie.cel.Env.AstIssuesTuple;
import org.projectnessie.cel.checker.Decls;
import org.projectnessie.cel.common.types.pb.ProtoTypeRegistry;
import org.projectnessie.cel.tools.Script;
import org.projectnessie.cel.tools.ScriptHost;

@Warmup(iterations = 1, time = 1500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 300, timeUnit = TimeUnit.MILLISECONDS)
@Fork(1)
@Threads(2)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class CompileBuildBench {

@State(Scope.Thread)
public static class CompileState {
@Param({"simplePredicate", "deepSelectors", "macroPipeline"})
public String expression;

String source() {
switch (expression) {
case "simplePredicate":
return "resource == 'projects/p1' && user == 'alice'";
case "deepSelectors":
return "request.auth.claims.email.endsWith('@example.com')"
+ " && request.resource.labels['env'] == 'prod'";
case "macroPipeline":
return "items.filter(i, i.score > 10).map(i, i.name).exists(n, n.startsWith('a'))";
default:
throw new IllegalArgumentException("Unknown compile benchmark expression: " + expression);
}
}
}

@Benchmark
public void envCompileAndProgram(CompileState state, Blackhole blackhole) {
Env env =
newEnv(
declarations(
Decls.newVar("resource", Decls.String),
Decls.newVar("user", Decls.String),
Decls.newVar("request", Decls.Dyn),
Decls.newVar("items", Decls.newListType(Decls.Dyn))));
AstIssuesTuple ast = env.compile(state.source());
if (ast.hasIssues()) {
throw ast.getIssues().err();
}
blackhole.consume(env.program(ast.getAst()));
}

@Benchmark
public void scriptHostBuild(CompileState state, Blackhole blackhole) throws Exception {
ScriptHost host = ScriptHost.newBuilder().build();
Script script =
host.buildScript(state.source())
.withDeclarations(
Decls.newVar("resource", Decls.String),
Decls.newVar("user", Decls.String),
Decls.newVar("request", Decls.Dyn),
Decls.newVar("items", Decls.newListType(Decls.Dyn)))
.build();
blackhole.consume(script);
}

@Benchmark
public void protoRegistryCreation(Blackhole blackhole) {
blackhole.consume(ProtoTypeRegistry.newRegistry());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,13 @@ public void dynamicMapLiteral(DynamicMapLiteralState state, Blackhole blackhole)

@State(Scope.Benchmark)
public static class ProtoInputState {
@Param({"mapLookup", "repeatedUintExistsEarly", "repeatedUintExistsLate"})
@Param({
"mapLookup",
"mapLookupMissingKeyError",
"mapLookupRepeated",
"repeatedUintExistsEarly",
"repeatedUintExistsLate"
})
public String kind;

@Param({"10", "1000"})
Expand All @@ -164,6 +170,24 @@ public void init() {
mapOf(
"msg", protoMessage(size), "key", "key-" + (size - 1), "target", (long) size - 1);
return;
case "mapLookupMissingKeyError":
program =
protoProgram(
"msg.map_string_uint64[key] == target",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pardon my ignorance: does this run into an error or evaluate to False?

Decls.newVar("key", Decls.String),
Decls.newVar("target", Decls.Uint));
vars = mapOf("msg", protoMessage(size), "key", "missing", "target", 0L);
return;
case "mapLookupRepeated":
program =
protoProgram(
"msg.map_string_uint64[key] == target && msg.map_string_uint64[key] == target",
Decls.newVar("key", Decls.String),
Decls.newVar("target", Decls.Uint));
vars =
mapOf(
"msg", protoMessage(size), "key", "key-" + (size - 1), "target", (long) size - 1);
return;
case "repeatedUintExistsEarly":
program =
protoProgram(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (C) 2021 The Authors of CEL-Java
*
* 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 org.projectnessie.cel.interpreter;

import static org.projectnessie.cel.Env.newEnv;
import static org.projectnessie.cel.EnvOption.declarations;
import static org.projectnessie.cel.ProgramOption.evalOptions;
import static org.projectnessie.cel.Util.mapOf;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Threads;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
import org.projectnessie.cel.Env;
import org.projectnessie.cel.Env.AstIssuesTuple;
import org.projectnessie.cel.EvalOption;
import org.projectnessie.cel.Program;
import org.projectnessie.cel.checker.Decls;

@Warmup(iterations = 1, time = 1500, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 300, timeUnit = TimeUnit.MILLISECONDS)
@Fork(1)
@Threads(2)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class MacroRuntimeBench {

@State(Scope.Benchmark)
public static class MacroState {
@Param({"existsEarly", "existsLate", "all", "filter", "map"})
public String expression;

@Param({"10", "1000"})
public int size;

Program program;
Map<Object, Object> vars;

@Setup
public void init() {
switch (expression) {
case "existsEarly":
program = program("items.exists(i, i == target)");
vars = mapOf("items", list(size), "target", 0L);
return;
case "existsLate":
program = program("items.exists(i, i == target)");
vars = mapOf("items", list(size), "target", (long) size - 1);
return;
case "all":
program = program("items.all(i, i >= 0)");
vars = mapOf("items", list(size));
return;
case "filter":
program = program("items.filter(i, i > target)");
vars = mapOf("items", list(size), "target", (long) size / 2);
return;
case "map":
program = program("items.map(i, i + 1)");
vars = mapOf("items", list(size));
return;
default:
throw new IllegalArgumentException("Unknown macro benchmark expression: " + expression);
}
}
}

@Benchmark
public void macroRuntime(MacroState state, Blackhole blackhole) {
blackhole.consume(state.program.eval(state.vars));
}

private static Program program(String expression) {
Env env =
newEnv(
declarations(
Decls.newVar("items", Decls.newListType(Decls.Int)),
Decls.newVar("target", Decls.Int)));
AstIssuesTuple ast = env.compile(expression);
if (ast.hasIssues()) {
throw ast.getIssues().err();
}
return env.program(ast.getAst(), evalOptions(EvalOption.OptOptimize));
}

private static List<Long> list(int size) {
List<Long> values = new ArrayList<>(size);
for (long i = 0; i < size; i++) {
values.add(i);
}
return values;
}
}
10 changes: 10 additions & 0 deletions jackson/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ plugins {
`java-library`
`maven-publish`
signing
alias(libs.plugins.jmh)
id("cel-conventions")
}

Expand All @@ -43,4 +44,13 @@ dependencies {
testImplementation(libs.bundles.junit.testing)
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
testRuntimeOnly("org.junit.platform:junit-platform-launcher")

jmhImplementation(libs.jmh.core)
jmhAnnotationProcessor(libs.jmh.generator.annprocess)
}

jmh { jmhVersion.set(libs.versions.jmh.get()) }

tasks.named("check") { dependsOn(tasks.named("jmh")) }

tasks.named("assemble") { dependsOn(tasks.named("jmhJar")) }
Loading
Loading