-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from dynatrace-oss/improved-consistent-weight…
…ed-sampling Improved consistent weighted sampling
- Loading branch information
Showing
12 changed files
with
551 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/jmh/java/com/dynatrace/hash4j/consistent/ConsistentJumpBucketHasherPerformanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2023 Dynatrace LLC | ||
* | ||
* 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 com.dynatrace.hash4j.consistent; | ||
|
||
import com.dynatrace.hash4j.random.PseudoRandomGeneratorProvider; | ||
import java.util.SplittableRandom; | ||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
public class ConsistentJumpBucketHasherPerformanceTest { | ||
|
||
private static final ConsistentBucketHasher CONSISTENT_BUCKET_HASHER = | ||
ConsistentHashing.jumpHash(PseudoRandomGeneratorProvider.splitMix64_V1()); | ||
|
||
@State(Scope.Thread) | ||
public static class TestState { | ||
|
||
@Param({"1", "10", "100", "1000", "10000", "100000", "1000000"}) | ||
int numBuckets; | ||
|
||
SplittableRandom random; | ||
|
||
@Setup | ||
public void init() { | ||
random = new SplittableRandom(0x87c5950e6677341eL); | ||
} | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
public void getBucket(TestState testState, Blackhole blackhole) { | ||
int bucket = | ||
CONSISTENT_BUCKET_HASHER.getBucket(testState.random.nextLong(), testState.numBuckets); | ||
blackhole.consume(bucket); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
...va/com/dynatrace/hash4j/consistent/ImprovedConsistentWeightedSamplingPerformanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Copyright 2023 Dynatrace LLC | ||
* | ||
* 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 com.dynatrace.hash4j.consistent; | ||
|
||
import com.dynatrace.hash4j.random.PseudoRandomGeneratorProvider; | ||
import java.util.SplittableRandom; | ||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
public class ImprovedConsistentWeightedSamplingPerformanceTest { | ||
|
||
private static final ConsistentBucketHasher CONSISTENT_BUCKET_HASHER = | ||
ConsistentHashing.improvedConsistentWeightedSampling( | ||
PseudoRandomGeneratorProvider.splitMix64_V1()); | ||
|
||
@State(Scope.Thread) | ||
public static class TestState { | ||
|
||
@Param({"1", "10", "100", "1000", "10000", "100000", "1000000"}) | ||
int numBuckets; | ||
|
||
SplittableRandom random; | ||
|
||
@Setup | ||
public void init() { | ||
random = new SplittableRandom(0x87c5950e6677341eL); | ||
} | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
public void getBucket(TestState testState, Blackhole blackhole) { | ||
int bucket = | ||
CONSISTENT_BUCKET_HASHER.getBucket(testState.random.nextLong(), testState.numBuckets); | ||
blackhole.consume(bucket); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/jmh/java/com/dynatrace/hash4j/consistent/ModuloPerformanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2023 Dynatrace LLC | ||
* | ||
* 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 com.dynatrace.hash4j.consistent; | ||
|
||
import java.util.SplittableRandom; | ||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
public class ModuloPerformanceTest { | ||
|
||
@State(Scope.Thread) | ||
public static class TestState { | ||
|
||
@Param({"1", "10", "100", "1000", "10000", "100000", "1000000"}) | ||
int numBuckets; | ||
|
||
SplittableRandom random; | ||
|
||
@Setup | ||
public void init() { | ||
random = new SplittableRandom(0x87c5950e6677341eL); | ||
} | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
public void getBucket(TestState testState, Blackhole blackhole) { | ||
int bucket = (int) ((testState.random.nextLong() & 0x7FFFFFFFFFFFFFFFL) % testState.numBuckets); | ||
blackhole.consume(bucket); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/jmh/java/com/dynatrace/hash4j/consistent/RandomNumberPerformanceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2023 Dynatrace LLC | ||
* | ||
* 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 com.dynatrace.hash4j.consistent; | ||
|
||
import java.util.SplittableRandom; | ||
import org.openjdk.jmh.annotations.*; | ||
import org.openjdk.jmh.infra.Blackhole; | ||
|
||
public class RandomNumberPerformanceTest { | ||
|
||
@State(Scope.Thread) | ||
public static class TestState { | ||
|
||
SplittableRandom random; | ||
|
||
@Setup | ||
public void init() { | ||
random = new SplittableRandom(0x87c5950e6677341eL); | ||
} | ||
} | ||
|
||
@Benchmark | ||
@BenchmarkMode(Mode.AverageTime) | ||
public void getBucket(TestState testState, Blackhole blackhole) { | ||
blackhole.consume(testState.random.nextLong()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/com/dynatrace/hash4j/consistent/ImprovedConsistentWeightedSampling.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright 2023 Dynatrace LLC | ||
* | ||
* 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 com.dynatrace.hash4j.consistent; | ||
|
||
import static com.dynatrace.hash4j.util.Preconditions.checkArgument; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
import com.dynatrace.hash4j.random.PseudoRandomGenerator; | ||
import com.dynatrace.hash4j.random.PseudoRandomGeneratorProvider; | ||
|
||
/** | ||
* Consistent hashing algorithm based on a simplified version of the algorithm described in Sergey | ||
* Ioffe, <a href="https://ieeexplore.ieee.org/abstract/document/5693978">"Improved Consistent | ||
* Sampling, Weighted Minhash and L1 Sketching,"</a> 2010 IEEE International Conference on Data | ||
* Mining, Sydney, NSW, Australia, 2010, pp. 246-255, doi: 10.1109/ICDM.2010.80. | ||
*/ | ||
class ImprovedConsistentWeightedSampling implements ConsistentBucketHasher { | ||
|
||
private final PseudoRandomGenerator pseudoRandomGenerator; | ||
|
||
ImprovedConsistentWeightedSampling(PseudoRandomGeneratorProvider pseudoRandomGeneratorProvider) { | ||
requireNonNull(pseudoRandomGeneratorProvider); | ||
this.pseudoRandomGenerator = pseudoRandomGeneratorProvider.create(); | ||
} | ||
|
||
@Override | ||
public strictfp int getBucket(long hash, int numBuckets) { | ||
checkArgument(numBuckets > 0, "buckets must be positive"); | ||
pseudoRandomGenerator.reset(hash); | ||
double r = pseudoRandomGenerator.nextExponential() + pseudoRandomGenerator.nextExponential(); | ||
double b = pseudoRandomGenerator.nextDouble(); | ||
double t = StrictMath.floor(StrictMath.log(numBuckets) / r + b); | ||
double y = StrictMath.exp(r * (t - b)); | ||
// y should always be in the range [0, numBuckets), | ||
// but could be larger due to numerical inaccuracies, | ||
// therefore limit result after rounding down to numBuckets - 1 | ||
return Math.min((int) y, numBuckets - 1); | ||
} | ||
} |
Oops, something went wrong.