Created
July 25, 2016 22:47
-
-
Save Deamon5550/e5ff6ad76e6bbbed57c72a151f87d58a to your computer and use it in GitHub Desktop.
Sponge event benchmarks
This file contains hidden or 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 org.spongepowered.benchmark; | |
| import com.google.inject.Inject; | |
| import org.slf4j.Logger; | |
| import org.spongepowered.api.GameState; | |
| import org.spongepowered.api.Sponge; | |
| import org.spongepowered.api.command.CommandResult; | |
| import org.spongepowered.api.command.spec.CommandSpec; | |
| import org.spongepowered.api.event.Listener; | |
| import org.spongepowered.api.event.SpongeEventFactory; | |
| import org.spongepowered.api.event.cause.Cause; | |
| import org.spongepowered.api.event.cause.NamedCause; | |
| import org.spongepowered.api.event.game.state.GameInitializationEvent; | |
| import org.spongepowered.api.event.game.state.GameStoppedEvent; | |
| import org.spongepowered.api.plugin.Plugin; | |
| @Plugin(id = "benchmarks") | |
| public class BenchmarkPlugin { | |
| private static final int WARMUP_ITERATIONS = 5; | |
| private static final int WARMUP_COUNT = 1000; | |
| private static final int BENCHMARK_ITERATIONS = 20; | |
| private static final int BENCHMARK_COUNT = 10000; | |
| private static final Cause scause = Cause.of(NamedCause.of("dummy", BenchmarkPlugin.class)); | |
| private static int event_count = 0; | |
| private static Logger slog; | |
| @Inject private Logger logger; | |
| @Listener | |
| public void onStart(GameInitializationEvent event) { | |
| slog = this.logger; | |
| Sponge.getCommandManager().register(this, CommandSpec.builder().executor((src, args) -> { | |
| run_benchmarks(); | |
| return CommandResult.success(); | |
| }).build(), "benchmark"); | |
| } | |
| @Listener | |
| public void listener1(GameStoppedEvent event) { | |
| event_count++; | |
| } | |
| @Listener | |
| public void listener2(GameStoppedEvent event) { | |
| event_count++; | |
| } | |
| @Listener | |
| public void listener3(GameStoppedEvent event) { | |
| event_count++; | |
| } | |
| @Listener | |
| public void listener4(GameStoppedEvent event) { | |
| event_count++; | |
| } | |
| @Listener | |
| public void listener5(GameStoppedEvent event) { | |
| event_count++; | |
| } | |
| private static void run_benchmarks() { | |
| slog.info("Starting event system benchmarks"); | |
| slog.info("Running " + WARMUP_ITERATIONS + " warmup iterations of " + WARMUP_COUNT + " cycles each"); | |
| event_count = 0; | |
| long start = System.nanoTime(); | |
| for(int i = 0; i < WARMUP_ITERATIONS; i++) { | |
| for(int e = 0; e < WARMUP_COUNT; e++) { | |
| throw_event(); | |
| } | |
| } | |
| if(event_count != WARMUP_COUNT * WARMUP_ITERATIONS * 5) { | |
| slog.error("Incorrect event count " + event_count + " (Expected " + (WARMUP_COUNT * WARMUP_ITERATIONS * 5) + ")"); | |
| return; | |
| } | |
| event_count = 0; | |
| long delta = System.nanoTime() - start; | |
| slog.info("Warmup cycles took " + (delta / 1000000f) + "ms"); | |
| slog.info("================================================================================"); | |
| slog.info("Running " + BENCHMARK_ITERATIONS + " benchmarking iterations of " + BENCHMARK_COUNT + " cycles each"); | |
| long[] times = new long[BENCHMARK_ITERATIONS]; | |
| long total_time = 0; | |
| int expected_count = BENCHMARK_COUNT * 5; | |
| for(int i = 0; i < BENCHMARK_ITERATIONS; i++) { | |
| start = System.nanoTime(); | |
| for(int e = 0; e < BENCHMARK_COUNT; e++) { | |
| throw_event(); | |
| } | |
| delta = System.nanoTime() - start; | |
| if(event_count != expected_count) { | |
| slog.error("Incorrect event count " + event_count + " (Expected " + expected_count + ")"); | |
| return; | |
| } | |
| event_count = 0; | |
| total_time += delta; | |
| times[i] = delta; | |
| slog.info(" Iteration " + i + ": " + (delta / 1000000f) + "ms"); | |
| } | |
| slog.info("================================================================================"); | |
| slog.info("Total Time: " + (total_time / 1000000f) + "ms"); | |
| slog.info("Mean Time: " + ((total_time / (double) BENCHMARK_ITERATIONS) / 1000000f) + "ms"); | |
| } | |
| private static void throw_event() { | |
| Sponge.getEventManager().post(SpongeEventFactory.createGameStoppedServerEvent(scause, GameState.GAME_STOPPED)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment