Created
October 1, 2012 02:50
-
-
Save aadnk/3809215 to your computer and use it in GitHub Desktop.
ProtocolLib Benchmark
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.comphenix.testing; | |
import java.util.concurrent.TimeUnit; | |
import com.comphenix.protocol.injector.StructureCache; | |
import com.comphenix.protocol.reflect.FieldAccessException; | |
import com.comphenix.protocol.reflect.StructureModifier; | |
import com.comphenix.protocol.reflect.compiler.BackgroundCompiler; | |
import com.google.common.base.Stopwatch; | |
import net.minecraft.server.*; | |
class Test { | |
public static void main(String[] args) { | |
testCompiling(); | |
} | |
public static void testCompiling() { | |
Packet20NamedEntitySpawn target = new Packet20NamedEntitySpawn(); | |
StructureModifier<Integer> reflectionStructure = StructureCache.getStructure(20). | |
<Integer>withType(int.class).withTarget(target); | |
BackgroundCompiler compiler = new BackgroundCompiler(Test.class.getClassLoader()); | |
Stopwatch watch = new Stopwatch(); | |
watch.start(); | |
StructureModifier<Integer> compiled = compiler.getCompiler().compile(reflectionStructure); | |
watch.stop(); | |
System.out.println("Class compiled in " + watch.elapsedMillis() + " ms"); | |
// Test the differences | |
try { | |
for (int i = 2; i <= 2097152; i = i << 1) { | |
testSpeed(target, reflectionStructure, compiled, i); | |
} | |
} catch (FieldAccessException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static void testSpeed(Packet20NamedEntitySpawn target, StructureModifier<Integer> a, | |
StructureModifier<Integer> b, int iterations) throws FieldAccessException { | |
Stopwatch watch = new Stopwatch(); | |
methodIndirect(a, iterations); | |
a.write(0, 0); | |
watch.start(); | |
methodIndirect(a, iterations); | |
watch.stop(); | |
System.out.print(getMilliseconds(watch.elapsedTime(TimeUnit.NANOSECONDS)) + "\t"); | |
methodIndirect(b, iterations); | |
a.write(0, 0); | |
watch.reset(); | |
watch.start(); | |
methodIndirect(b, iterations); | |
watch.stop(); | |
System.out.print(getMilliseconds(watch.elapsedTime(TimeUnit.NANOSECONDS)) + "\t"); | |
methodDirect(target, iterations); | |
a.write(0, 0); | |
watch.reset(); | |
watch.start(); | |
methodDirect(target, iterations); | |
watch.stop(); | |
System.out.print(getMilliseconds(watch.elapsedTime(TimeUnit.NANOSECONDS)) + "\t"); | |
System.out.println(); | |
} | |
private static void methodDirect(Packet20NamedEntitySpawn target, int iterations) { | |
for (int i = 0; i < iterations; i++) { | |
target.a++; | |
} | |
} | |
private static void methodIndirect(StructureModifier<Integer> a, int iterations) throws FieldAccessException { | |
for (int i = 0; i < iterations; i++) { | |
a.write(0, a.read(0) + 1); | |
} | |
} | |
private static double getMilliseconds(long nanoseconds) { | |
return nanoseconds / 1000000.0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment