Skip to content

Commit c69c92b

Browse files
add parameterised benchmarks for construction and membership tests
1 parent 9f0be8e commit c69c92b

File tree

8 files changed

+109
-37
lines changed

8 files changed

+109
-37
lines changed

jmh/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,28 @@
4848
<showDeprecation>true</showDeprecation>
4949
</configuration>
5050
</plugin>
51+
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-shade-plugin</artifactId>
55+
<version>3.1.0</version>
56+
<executions>
57+
<execution>
58+
<phase>package</phase>
59+
<goals>
60+
<goal>shade</goal>
61+
</goals>
62+
<configuration>
63+
<transformers>
64+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
65+
<mainClass>org.openjdk.jmh.Main</mainClass>
66+
</transformer>
67+
</transformers>
68+
<finalName>benchmarks</finalName>
69+
</configuration>
70+
</execution>
71+
</executions>
72+
</plugin>
5173
</plugins>
5274
</build>
5375

jmh/src/main/java/org/fastfilter/ConstructionBenchmark.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66

77
import java.util.concurrent.TimeUnit;
88

9-
@OutputTimeUnit(TimeUnit.MILLISECONDS)
9+
@OutputTimeUnit(TimeUnit.SECONDS)
1010
public class ConstructionBenchmark {
1111

1212
@Benchmark
13-
public Filter construct(FilterConstructionState state) {
13+
public Filter construct(ConstructionState state) {
1414
return state.getConstructor().construct(state.getKeys(), 64);
1515
}
1616
}

jmh/src/main/java/org/fastfilter/FilterConstructionState.java renamed to jmh/src/main/java/org/fastfilter/ConstructionState.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import org.openjdk.jmh.annotations.*;
44

55
@State(Scope.Benchmark)
6-
public class FilterConstructionState {
6+
public class ConstructionState {
77

88
@Param({"1048576", "2097152"})
99
int size;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package org.fastfilter;
22

33
public class Environment {
4-
public static int SEED = Integer.parseInt(System.getProperty("org.fastfilter.seed", "0"));
4+
static int SEED = Integer.parseInt(System.getProperty("org.fastfilter.seed.primary", "0"));
55
}
Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
package org.fastfilter;
22

3+
import java.util.HashSet;
4+
import java.util.Set;
35
import java.util.SplittableRandom;
46

57
public enum KeyGenerationStrategy {
68
RANDOM64 {
79
SplittableRandom random = new SplittableRandom(Environment.SEED);
10+
811
@Override
9-
void fill(long[] keys) {
10-
for (int i = 0; i < keys.length; ++i) {
11-
keys[i] = random.nextLong();
12-
}
12+
long nextKey() {
13+
return random.nextLong();
1314
}
1415
},
1516
RANDOM32 {
1617
SplittableRandom random = new SplittableRandom(Environment.SEED);
18+
1719
@Override
18-
void fill(long[] keys) {
19-
for (int i = 0; i < keys.length; ++i) {
20-
keys[i] = random.nextInt();
21-
}
20+
long nextKey() {
21+
return random.nextInt() & 0xFFFFFFFFL;
2222
}
2323
};
2424

25-
abstract void fill(long[] keys);
25+
26+
abstract long nextKey();
27+
void fill(long[] keys) {
28+
Set<Long> distinct = new HashSet<>();
29+
for (int i = 0; i < keys.length; ++i) {
30+
do {
31+
long key = nextKey();
32+
if (!distinct.contains(key)) {
33+
keys[i] = key;
34+
distinct.add(key);
35+
break;
36+
}
37+
} while (true);
38+
}
39+
}
2640
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.fastfilter;
2+
3+
import org.openjdk.jmh.annotations.Benchmark;
4+
import org.openjdk.jmh.annotations.OutputTimeUnit;
5+
6+
import java.util.concurrent.TimeUnit;
7+
8+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
9+
public class MembershipBenchmark {
10+
11+
@Benchmark
12+
public void mayContain(MembershipState state, MembershipCounters counters) {
13+
if (state.getFilter().mayContain(state.nextKey())) {
14+
counters.found++;
15+
} else {
16+
counters.notFound++;
17+
}
18+
}
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.fastfilter;
2+
3+
import org.openjdk.jmh.annotations.*;
4+
5+
@State(Scope.Thread)
6+
@AuxCounters(AuxCounters.Type.EVENTS)
7+
public class MembershipCounters {
8+
public int found;
9+
public int notFound;
10+
11+
public int total() {
12+
return notFound + found;
13+
}
14+
15+
@Setup(Level.Iteration)
16+
public void reset() {
17+
found = 0;
18+
notFound = 0;
19+
}
20+
}

jmh/src/main/java/org/fastfilter/FilterQueryState.java renamed to jmh/src/main/java/org/fastfilter/MembershipState.java

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,11 @@
22

33
import org.openjdk.jmh.annotations.*;
44

5-
@State(Scope.Benchmark)
6-
public class FilterQueryState extends FilterConstructionState {
7-
8-
@AuxCounters
9-
static class Counters {
10-
private final long bitCount;
11-
int workDone;
12-
int found;
13-
int notFound;
5+
import java.util.HashSet;
6+
import java.util.Set;
147

15-
Counters(Filter filter) {
16-
this.bitCount = filter.getBitCount();
17-
}
18-
}
8+
@State(Scope.Benchmark)
9+
public class MembershipState extends ConstructionState {
1910

2011
@Param({"10", "15", "20", "25"})
2112
int logDistinctLookups;
@@ -24,24 +15,15 @@ static class Counters {
2415
float trueMatchProbability;
2516

2617
private long[] keysToFind;
27-
private Counters counters;
28-
29-
public void found() {
30-
++counters.found;
31-
}
32-
33-
public void notFound() {
34-
++counters.notFound;
35-
}
36-
18+
int workDone;
3719
Filter filter;
3820

3921
public Filter getFilter() {
4022
return filter;
4123
}
4224

4325
public long nextKey() {
44-
return keysToFind[(counters.workDone++) & (keysToFind.length - 1)];
26+
return keysToFind[(workDone++) & (keysToFind.length - 1)];
4527
}
4628

4729
@Override
@@ -56,5 +38,20 @@ public void init() {
5638
+ memberCount + " keys but have " + keys.length);
5739
}
5840
System.arraycopy(keys, 0, keysToFind, 0, memberCount);
41+
Set<Long> present = new HashSet<>();
42+
for (long key : keys) {
43+
present.add(key);
44+
}
45+
for (int i = memberCount; i < keysToFind.length; ++i) {
46+
do {
47+
long key = kgs.nextKey();
48+
if (!present.contains(key)) {
49+
present.add(key);
50+
keysToFind[i] = key;
51+
break;
52+
}
53+
} while (true);
54+
}
55+
5956
}
6057
}

0 commit comments

Comments
 (0)