/* * Copyright (c) 2015, Oracle America, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * Neither the name of Oracle nor the names of its contributors may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ package org.sample; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; import org.openjdk.jmh.annotations.CompilerControl; import org.openjdk.jmh.annotations.Fork; import org.openjdk.jmh.annotations.Level; 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.Setup; import org.openjdk.jmh.annotations.State; import org.openjdk.jmh.annotations.TearDown; import org.openjdk.jmh.annotations.Warmup; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; import java.io.UncheckedIOException; import java.util.concurrent.TimeUnit; @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 10) @Measurement(iterations = 5) @Fork(2) public class DeserialAllBenchmark { @Benchmark public Object p00() throws Exception { return newSerialStream(p00Bytes).readObject(); } @Benchmark public Object p01() throws Exception { return newSerialStream(p01Bytes).readObject(); } @Benchmark public Object p08() throws Exception { return newSerialStream(p08Bytes).readObject(); } @Benchmark public Object p16() throws Exception { return newSerialStream(p16Bytes).readObject(); } @Benchmark public Object f01() throws Exception { return newSerialStream(f01Bytes).readObject(); } @Benchmark public Object f08() throws Exception { return newSerialStream(f08Bytes).readObject(); } @Benchmark public Object f16() throws Exception { return newSerialStream(f16Bytes).readObject(); } static class P00 implements Serializable { } static class P01 implements Serializable { public int f1; public P01() { f1 = 42; } } static class P08 implements Serializable { public int f1, f2, f3, f4, f5, f6, f7, f8; public P08() { f1 = f2= f3= f4 =f5 = f6 = f7 = f8 = 42; } } static class P16 implements Serializable { public int f1, f2, f3, f4, f5, f6, f7, f8; public int f9, f10, f11, f12, f13, f14, f15, f16; public P16() { f1 = f2= f3= f4 =f5 = f6 = f7 = f8 = 42; f9 = f10= f11 = f12 = f13 = f14 = f15 = f16 = 42; } } static class F01 implements Serializable { public final int f1; public F01() { f1 = 42; } } static class F08 implements Serializable { public final int f1, f2, f3, f4, f5, f6, f7, f8; public F08() { f1 = f2= f3= f4 =f5 = f6 = f7 = f8 = 42; } } static class F16 implements Serializable { public final int f1, f2, f3, f4, f5, f6, f7, f8; public final int f9, f10, f11, f12, f13, f14, f15, f16; public F16() { f1 = f2= f3= f4 =f5 = f6 = f7 = f8 = 42; f9 = f10= f11 = f12 = f13 = f14 = f15 = f16 = 42; } } private final static byte[] p00Bytes = toSerialBytes(new P00()); private final static byte[] p01Bytes = toSerialBytes(new P01()); private final static byte[] p08Bytes = toSerialBytes(new P08()); private final static byte[] p16Bytes = toSerialBytes(new P16()); private final static byte[] f01Bytes = toSerialBytes(new F01()); private final static byte[] f08Bytes = toSerialBytes(new F08()); private final static byte[] f16Bytes = toSerialBytes(new F16()); @CompilerControl(CompilerControl.Mode.INLINE) static ObjectInputStream newSerialStream(byte[] bytes) { try { ByteArrayInputStream bais = new ByteArrayInputStream(bytes); return new ObjectInputStream(bais); } catch (IOException x) { throw new UncheckedIOException(x); } } static byte[] toSerialBytes(Object obj) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(obj); oos.close(); return baos.toByteArray(); } catch (IOException x) { throw new UncheckedIOException(x); } } }