Last active
September 20, 2018 05:25
-
-
Save minborg/8b2f978531f21833c2ee90eb54e9d1dc to your computer and use it in GitHub Desktop.
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
package com.example; | |
import com.company.sakila.SakilaApplication; | |
import com.company.sakila.SakilaApplicationBuilder; | |
import com.company.sakila.sakila.sakila.film.Film; | |
import com.company.sakila.sakila.sakila.film.FilmManager; | |
import com.company.sakila.sakila.sakila.film.generated.GeneratedFilm; | |
import com.speedment.enterprise.application.InMemoryBundle; | |
import com.speedment.enterprise.datastore.runtime.DataStoreComponent; | |
import com.speedment.runtime.core.Speedment; | |
import com.speedment.runtime.core.manager.Manager; | |
import java.util.Comparator; | |
import java.util.IntSummaryStatistics; | |
import java.util.function.Consumer; | |
import java.util.function.Predicate; | |
import java.util.stream.Stream; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.Mode; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.Setup; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.annotations.TearDown; | |
import org.openjdk.jmh.annotations.Threads; | |
import org.openjdk.jmh.runner.Runner; | |
import org.openjdk.jmh.runner.RunnerException; | |
import org.openjdk.jmh.runner.options.Options; | |
import org.openjdk.jmh.runner.options.OptionsBuilder; | |
@State(Scope.Benchmark) | |
public class Bench { | |
private static final Predicate<Film> RATING_EQUALS_PG_13 = Film.RATING.equal(GeneratedFilm.Rating.PG13); | |
private static final Comparator<Film> LENGTH_DESCENDING = Film.LENGTH.reversed(); | |
private Speedment app; | |
private Manager<Film> films; | |
@Setup(/*Level.Iteration*/) | |
public void setup() { | |
app = new SakilaApplicationBuilder() | |
.withPassword("sakila-password") | |
.withBundle(InMemoryBundle.class) | |
.build(); | |
app.get(DataStoreComponent.class).ifPresent(DataStoreComponent::load); | |
films = app.getOrThrow(FilmManager.class); | |
} | |
@TearDown(/*Level.Iteration*/) | |
public void tearDown() { | |
app.close(); | |
} | |
@Benchmark | |
public long filterAndCount() { | |
return films.stream() | |
.filter(RATING_EQUALS_PG_13) | |
.count(); | |
} | |
@Benchmark | |
public IntSummaryStatistics complex() { | |
return films.stream() | |
.sorted(LENGTH_DESCENDING) | |
.skip(745) | |
.limit(5) | |
.mapToInt(Film.RENTAL_DURATION.asInt()) | |
.summaryStatistics(); | |
} | |
public static void main(String[] args) throws RunnerException { | |
Options opt = new OptionsBuilder() | |
.include(Bench.class.getSimpleName()) | |
.mode(Mode.Throughput) | |
.threads(Threads.MAX) | |
.forks(1) | |
.warmupIterations(5) | |
.measurementIterations(5) | |
.build(); | |
new Runner(opt).run(); | |
} | |
public static <T> Consumer<T> blackHole() { | |
return t -> { | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment