Skip to content

Instantly share code, notes, and snippets.

@garcia-jj
Last active December 24, 2015 01:49
Show Gist options
  • Save garcia-jj/6726261 to your computer and use it in GitHub Desktop.
Save garcia-jj/6726261 to your computer and use it in GitHub Desktop.
Testes de performance entre instanciar objetos com Objenesis ou Reflection. Original: https://github.com/easymock/objenesis
/**
* Copyright 2006-2013 the original author or authors.
*
* 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 org.objenesis.benchmark;
import java.util.concurrent.TimeUnit;
import org.objenesis.instantiator.ObjectInstantiator;
import org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator;
import org.objenesis.instantiator.sun.UnsafeFactoryInstantiator;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.logic.BlackHole;
/**
* Benchmark comparing different instantiators
*
* @author Henri Tremblay
*/
@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@State(Scope.Thread)
public class CreateObject {
ObjectInstantiator<Object> sun = new SunReflectionFactoryInstantiator<>(Object.class);
ObjectInstantiator<Object> unsafe = new UnsafeFactoryInstantiator<>(Object.class);
@GenerateMicroBenchmark
public void createObjectWithConstructor(BlackHole bh) {
bh.consume(new Object());
}
@GenerateMicroBenchmark
public void createObjectWithConstructorReflection(BlackHole bh) {
try {
bh.consume(Object.class.newInstance());
} catch(Exception e) {}
}
@GenerateMicroBenchmark
public void createObjectWithMungedConstructor(BlackHole bh) {
bh.consume(sun.newInstance());
}
@GenerateMicroBenchmark
public void createObjectWithUnsafe(BlackHole bh) {
bh.consume(unsafe.newInstance());
}
}
# Fork: 1 of 1
# Warmup: 5 iterations, 1000 ms each
# Measurement: 5 iterations, 1000 ms each
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Running: org.objenesis.benchmark.CreateObject.createObjectWithConstructor
# Warmup Iteration 1: 8.457 ns/op
# Warmup Iteration 2: 7.982 ns/op
# Warmup Iteration 3: 6.296 ns/op
# Warmup Iteration 4: 6.383 ns/op
# Warmup Iteration 5: 6.362 ns/op
Iteration 1: 6.326 ns/op
Iteration 2: 6.500 ns/op
Iteration 3: 6.337 ns/op
Iteration 4: 6.293 ns/op
Iteration 5: 6.371 ns/op
Result : 6.366 ±(95%) 0.100 ±(99%) 0.166 ns/op
Statistics: (min, avg, max) = (6.293, 6.366, 6.500), stdev = 0.081
Confidence intervals: 95% [6.266, 6.466], 99% [6.200, 6.531]
# Fork: 1 of 1
# Warmup: 5 iterations, 1000 ms each
# Measurement: 5 iterations, 1000 ms each
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Running: org.objenesis.benchmark.CreateObject.createObjectWithConstructorReflection
# Warmup Iteration 1: 16.834 ns/op
# Warmup Iteration 2: 15.070 ns/op
# Warmup Iteration 3: 12.682 ns/op
# Warmup Iteration 4: 12.734 ns/op
# Warmup Iteration 5: 12.818 ns/op
Iteration 1: 12.879 ns/op
Iteration 2: 13.234 ns/op
Iteration 3: 12.799 ns/op
Iteration 4: 13.044 ns/op
Iteration 5: 12.917 ns/op
Result : 12.975 ±(95%) 0.211 ±(99%) 0.349 ns/op
Statistics: (min, avg, max) = (12.799, 12.975, 13.234), stdev = 0.170
Confidence intervals: 95% [12.764, 13.185], 99% [12.625, 13.324]
# Fork: 1 of 1
# Warmup: 5 iterations, 1000 ms each
# Measurement: 5 iterations, 1000 ms each
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Running: org.objenesis.benchmark.CreateObject.createObjectWithMungedConstructor
# Warmup Iteration 1: 15.803 ns/op
# Warmup Iteration 2: 14.727 ns/op
# Warmup Iteration 3: 11.597 ns/op
# Warmup Iteration 4: 11.600 ns/op
# Warmup Iteration 5: 11.861 ns/op
Iteration 1: 11.800 ns/op
Iteration 2: 13.201 ns/op
Iteration 3: 12.372 ns/op
Iteration 4: 11.734 ns/op
Iteration 5: 11.732 ns/op
Result : 12.168 ±(95%) 0.791 ±(99%) 1.311 ns/op
Statistics: (min, avg, max) = (11.732, 12.168, 13.201), stdev = 0.637
Confidence intervals: 95% [11.377, 12.958], 99% [10.856, 13.479]
# Fork: 1 of 1
# Warmup: 5 iterations, 1000 ms each
# Measurement: 5 iterations, 1000 ms each
# Threads: 1 thread, will synchronize iterations
# Benchmark mode: Average time, time/op
# Running: org.objenesis.benchmark.CreateObject.createObjectWithUnsafe
# Warmup Iteration 1: 41.980 ns/op
# Warmup Iteration 2: 39.323 ns/op
# Warmup Iteration 3: 35.225 ns/op
# Warmup Iteration 4: 35.404 ns/op
# Warmup Iteration 5: 35.299 ns/op
Iteration 1: 35.443 ns/op
Iteration 2: 35.387 ns/op
Iteration 3: 35.324 ns/op
Iteration 4: 35.596 ns/op
Iteration 5: 35.642 ns/op
Result : 35.478 ±(95%) 0.169 ±(99%) 0.280 ns/op
Statistics: (min, avg, max) = (35.324, 35.478, 35.642), stdev = 0.136
Confidence intervals: 95% [35.310, 35.647], 99% [35.199, 35.758]
Benchmark Mode Thr Cnt Sec Mean Mean error Units
o.o.b.CreateObject.createObjectWithConstructor avgt 1 5 1 6.366 0.166 ns/op
o.o.b.CreateObject.createObjectWithConstructorReflection avgt 1 5 1 12.975 0.349 ns/op
o.o.b.CreateObject.createObjectWithMungedConstructor avgt 1 5 1 12.168 1.311 ns/op
o.o.b.CreateObject.createObjectWithUnsafe avgt 1 5 1 35.478 0.280 ns/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment