Skip to content

Commit dfd996e

Browse files
authored
KAFKA-18336: Improve jmh tests on ACL in AuthorizerBenchmark and StandardAuthorizerUpdateBenchmark (#18293)
1. JMH test should return value against return void (compiler can eliminate returned value and benchmark would be incorrect). 2. Also move constant variable from method to class, to prevent JIT to unfold. 3. Increase warm up iterations Reviewers: Lucas Brutschy <lucasbru@apache.org>
1 parent ed22a8b commit dfd996e

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

jmh-benchmarks/src/main/java/org/apache/kafka/jmh/acl/AuthorizerBenchmark.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.apache.kafka.metadata.authorizer.StandardAcl;
3737
import org.apache.kafka.metadata.authorizer.StandardAuthorizer;
3838
import org.apache.kafka.server.authorizer.Action;
39+
import org.apache.kafka.server.authorizer.AuthorizationResult;
3940

4041
import org.openjdk.jmh.annotations.Benchmark;
4142
import org.openjdk.jmh.annotations.BenchmarkMode;
@@ -87,13 +88,19 @@ public class AuthorizerBenchmark {
8788
private List<Action> actions = new ArrayList<>();
8889
private RequestContext authorizeContext;
8990
private RequestContext authorizeByResourceTypeContext;
91+
private AclBindingFilter filter;
92+
private AclOperation op;
93+
private ResourceType resourceType;
9094

9195
Random rand = new Random(System.currentTimeMillis());
9296
double eps = 1e-9;
9397

9498
@Setup(Level.Trial)
9599
public void setup() throws Exception {
96100
authorizer = new StandardAuthorizer();
101+
filter = AclBindingFilter.ANY;
102+
op = AclOperation.READ;
103+
resourceType = ResourceType.TOPIC;
97104
prepareAclCache();
98105
// By adding `-95` to the resource name prefix, we cause the `TreeMap.from/to` call to return
99106
// most map entries. In such cases, we rely on the filtering based on `String.startsWith`
@@ -196,17 +203,17 @@ public void tearDown() throws IOException {
196203
}
197204

198205
@Benchmark
199-
public void testAclsIterator() {
200-
authorizer.acls(AclBindingFilter.ANY);
206+
public Iterable<AclBinding> testAclsIterator() {
207+
return authorizer.acls(filter);
201208
}
202209

203210
@Benchmark
204-
public void testAuthorizer() {
205-
authorizer.authorize(authorizeContext, actions);
211+
public List<AuthorizationResult> testAuthorizer() {
212+
return authorizer.authorize(authorizeContext, actions);
206213
}
207214

208215
@Benchmark
209-
public void testAuthorizeByResourceType() {
210-
authorizer.authorizeByResourceType(authorizeByResourceTypeContext, AclOperation.READ, ResourceType.TOPIC);
216+
public AuthorizationResult testAuthorizeByResourceType() {
217+
return authorizer.authorizeByResourceType(authorizeByResourceTypeContext, op, resourceType);
211218
}
212219
}

jmh-benchmarks/src/main/java/org/apache/kafka/jmh/acl/StandardAuthorizerUpdateBenchmark.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454

5555
@State(Scope.Benchmark)
5656
@Fork(value = 1)
57-
@Warmup(iterations = 0)
58-
@Measurement(iterations = 4)
57+
@Warmup(iterations = 7, timeUnit = TimeUnit.SECONDS, time = 2)
58+
@Measurement(iterations = 10, timeUnit = TimeUnit.SECONDS, time = 2)
5959
@BenchmarkMode(Mode.AverageTime)
6060
@OutputTimeUnit(TimeUnit.MILLISECONDS)
6161
public class StandardAuthorizerUpdateBenchmark {
@@ -71,13 +71,13 @@ public class StandardAuthorizerUpdateBenchmark {
7171
private int aclCount;
7272
int index = 0;
7373

74-
@Setup(Level.Trial)
74+
@Setup(Level.Iteration)
7575
public void setup() throws Exception {
7676
authorizer = new StandardAuthorizer();
7777
addAcls(aclCount);
7878
}
7979

80-
@TearDown(Level.Trial)
80+
@TearDown(Level.Iteration)
8181
public void tearDown() throws IOException {
8282
authorizer.close();
8383
}

0 commit comments

Comments
 (0)