Skip to content

Instantly share code, notes, and snippets.

@lucamolteni
Created April 26, 2021 11:57
Show Gist options
  • Save lucamolteni/ee38c690dca8d52dc0f16af882c85189 to your computer and use it in GitHub Desktop.
Save lucamolteni/ee38c690dca8d52dc0f16af882c85189 to your computer and use it in GitHub Desktop.
3 ways
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* 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.drools.ancompiler;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.drools.core.util.ObjectHashMap;
import org.drools.model.functions.Predicate1;
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.Warmup;
import org.openjdk.jmh.infra.Blackhole;
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
@Fork(value = 2)
@Warmup(iterations = 5)
@Measurement(iterations = 2)
public class ANCBenchmark {
@Param({"10000"})
private int N;
private List<String> DATA_FOR_TESTING;
private List<Predicate1<String>> constraints = new ArrayList<>();
private ObjectHashMap hashedSinkMap = new ObjectHashMap();
@Setup
public void setup() {
DATA_FOR_TESTING = createData();
constraints.add(createConstraintEquals("10"));
constraints.add(createConstraintEquals("20"));
constraints.add(createConstraintEquals("30"));
hashedSinkMap.put("10", createConstraintEquals("10"));
hashedSinkMap.put("20", createConstraintEquals("20"));
hashedSinkMap.put("30", createConstraintEquals("30"));
}
private Predicate1.Impl<String> createConstraintEquals(String stringEquals) {
return new Predicate1.Impl<>(s -> s.equals(stringEquals));
}
@Benchmark
public void testBruteForce(Blackhole bh) throws Exception {
for (int i = 0; i < DATA_FOR_TESTING.size(); i++) {
String s = DATA_FOR_TESTING.get(i);
for (Predicate1<String> p : constraints) {
p.test(s);
bh.consume(s);
}
}
}
@Benchmark
public void testHashing(Blackhole bh) throws Exception {
for (int i = 0; i < DATA_FOR_TESTING.size(); i++) {
String s = DATA_FOR_TESTING.get(i);
Predicate1<String> p = (Predicate1<String>) hashedSinkMap.get(s);
if (p != null) {
p.test(s);
} else {
bh.consume(s); // no need to test just propagate assert
}
}
}
@Benchmark
public void testInlining(Blackhole bh) throws Exception {
for (int i = 0; i < DATA_FOR_TESTING.size(); i++) {
String s = DATA_FOR_TESTING.get(i);
switch (s) {
case "10":
bh.consume(s);
break;
case "20":
bh.consume(s);
break;
case "30":
bh.consume(s);
break;
default:
bh.consume(s);
}
}
}
private List<String> createData() {
List<String> data = new ArrayList<>();
for (int i = 0; i < N; i++) {
data.add(String.valueOf(i));
}
return data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment