Skip to content

Commit

Permalink
Rework benchmark with new RandomSamplingImplementation API (#52)
Browse files Browse the repository at this point in the history
* Use the new RandomSamplingImplementation API

* Multiple iterations per implementation

* Use fixed number of iterations

* Rename singlePerformance to performance
  • Loading branch information
gstamatelat authored Sep 4, 2022
1 parent cd4c2c2 commit b696686
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions src/test/java/gr/james/sampling/Benchmark.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
package gr.james.sampling;

import gr.james.sampling.implementations.*;

import java.util.Arrays;
import java.util.List;
import java.util.Random;

public class Benchmark {

private static final Random random = new Random();
private static final int sample = 10;
private static final int stream = 100000000;
private static final int reps = 10;

private static final WatermanSampling<Object> waterman = new WatermanSampling<>(sample, random);
private static final VitterXSampling<Object> vitterx = new VitterXSampling<>(sample, random);
private static final VitterZSampling<Object> vitterz = new VitterZSampling<>(sample, random);
private static final LiLSampling<Object> lil = new LiLSampling<>(sample, random);
private static final LiLSamplingThreadSafe<Object> lilThreadSafe = new LiLSamplingThreadSafe<>(sample, random);
private static final EfraimidisSampling<Object> efraimidis = new EfraimidisSampling<>(sample, random);
private static final ChaoSampling<Object> chao = new ChaoSampling<>(sample, random);
private static final SequentialPoissonSampling<Object> sequentialPoisson = new SequentialPoissonSampling<>(sample, random);
private static final ParetoSampling<Object> pareto = new ParetoSampling<>(sample, random);
private static final List<RandomSamplingImplementation<Object>> implementations = Arrays.asList(
new WatermanImplementation<>(),
new VitterXImplementation<>(),
new VitterZImplementation<>(),
new LiLImplementation<>(),
new LiLThreadSafeImplementation<>(),
new EfraimidisImplementation<>(),
new ChaoImplementation<>(),
new SequentialPoissonImplementation<>(),
new ParetoImplementation<>()
);

public static void main(String[] args) {
System.out.printf("%18s %5d ms%n", "Waterman", performance(waterman) / 1000000);
System.out.printf("%18s %5d ms%n", "VitterX", performance(vitterx) / 1000000);
System.out.printf("%18s %5d ms%n", "VitterZ", performance(vitterz) / 1000000);
System.out.printf("%18s %5d ms%n", "LiL", performance(lil) / 1000000);
System.out.printf("%18s %5d ms%n", "LiL Thread Safe", performance(lilThreadSafe) / 1000000);
System.out.printf("%18s %5d ms%n", "Efraimidis", performance(efraimidis) / 1000000);
System.out.printf("%18s %5d ms%n", "Chao", performance(chao) / 1000000);
System.out.printf("%18s %5d ms%n", "Sequential Poisson", performance(sequentialPoisson) / 1000000);
System.out.printf("%18s %5d ms%n", "Pareto", performance(pareto) / 1000000);
for (RandomSamplingImplementation<Object> impl : implementations) {
double sum = 0;
for (int rep = 0; rep < reps; rep++) {
sum += performance(impl) / 1000000.0;
}
System.out.printf("%18s %.0f ms%n", impl, sum / reps);
}
}

private static long performance(RandomSampling<Object> alg) {
private static long performance(RandomSamplingImplementation<Object> impl) {
final RandomSampling<Object> alg = impl.implementation().apply(sample, random);
final long start = System.nanoTime();
for (int i = 0; i < 100000000; i++) {
for (int i = 0; i < stream; i++) {
alg.feed(i);
}
return System.nanoTime() - start;
Expand Down

0 comments on commit b696686

Please sign in to comment.