1 /*
   2  * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.openjdk.bench.java.io;
  24 
  25 import org.openjdk.bench.java.io.BlackholedOutputStream;
  26 import org.openjdk.jmh.annotations.Benchmark;
  27 import org.openjdk.jmh.annotations.BenchmarkMode;
  28 import org.openjdk.jmh.annotations.Mode;
  29 import org.openjdk.jmh.annotations.OutputTimeUnit;
  30 import org.openjdk.jmh.annotations.Scope;
  31 import org.openjdk.jmh.annotations.Setup;
  32 import org.openjdk.jmh.annotations.State;
  33 import org.openjdk.jmh.annotations.TearDown;
  34 import org.openjdk.jmh.infra.Blackhole;
  35 
  36 import java.io.IOException;
  37 import java.io.ObjectOutputStream;
  38 import java.io.ObjectStreamException;
  39 import java.io.Serializable;
  40 import java.util.concurrent.TimeUnit;
  41 
  42 @BenchmarkMode(Mode.AverageTime)
  43 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  44 @State(Scope.Thread)
  45 public class SerializationWriteReplace {
  46 
  47     private BlackholedOutputStream bos;
  48     private ObjectOutputStream os;
  49 
  50     @Setup
  51     public void setupStreams(Blackhole bh) throws IOException {
  52         bos = new BlackholedOutputStream(bh);
  53         os = new ObjectOutputStream(bos);
  54     }
  55 
  56     @TearDown
  57     public void downStreams() throws IOException {
  58         os.close();
  59         bos.close();
  60         os = null;
  61         bos = null;
  62     }
  63 
  64     @Benchmark
  65     public void writeReplace() throws IOException, ClassNotFoundException {
  66         os.writeObject(new Class2());
  67     }
  68 
  69     public abstract static class Base implements Serializable {
  70         private static final long serialVersionUID = 1L;
  71     }
  72 
  73     public static class Class1 extends Base {
  74         private static final long serialVersionUID = 2L;
  75     }
  76 
  77     public static class Class2 extends Class1 {
  78         private static final long serialVersionUID = 3L;
  79         Object writeReplace() throws ObjectStreamException {
  80             return new Class3();
  81         }
  82     }
  83 
  84     public static class Class3 extends Base {
  85         private static final long serialVersionUID = 4L;
  86         private String tuto = "tuto";
  87         private byte b = (byte) 0xff;
  88     }
  89 
  90 }