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.vm.gc;
  24 
  25 import org.openjdk.jmh.annotations.Benchmark;
  26 import org.openjdk.jmh.annotations.BenchmarkMode;
  27 import org.openjdk.jmh.annotations.Mode;
  28 import org.openjdk.jmh.annotations.OutputTimeUnit;
  29 import org.openjdk.jmh.annotations.Scope;
  30 import org.openjdk.jmh.annotations.State;
  31 import org.openjdk.jmh.infra.Blackhole;
  32 
  33 import java.util.concurrent.TimeUnit;
  34 
  35 @BenchmarkMode(Mode.AverageTime)
  36 @OutputTimeUnit(TimeUnit.NANOSECONDS)
  37 @State(Scope.Thread)
  38 public class Alloc {
  39 
  40     public static final int LENGTH = 400;
  41     public static final int ARR_LEN = 100;
  42     public int largeLen = 100;
  43     public int smalllen = 6;
  44 
  45     @Benchmark
  46     public void testLargeConstArray(Blackhole bh) throws Exception {
  47         int localArrlen = ARR_LEN;
  48         for (int i = 0; i < LENGTH; i++) {
  49             Object[] tmp = new Object[localArrlen];
  50             bh.consume(tmp);
  51         }
  52     }
  53 
  54     @Benchmark
  55     public void testLargeVariableArray(Blackhole bh) throws Exception {
  56         int localArrlen = largeLen;
  57         for (int i = 0; i < LENGTH; i++) {
  58             Object[] tmp = new Object[localArrlen];
  59             bh.consume(tmp);
  60         }
  61     }
  62 
  63     @Benchmark
  64     public void testSmallConstArray(Blackhole bh) throws Exception {
  65         int localArrlen = largeLen;
  66         for (int i = 0; i < LENGTH; i++) {
  67             Object[] tmp = new Object[localArrlen];
  68             bh.consume(tmp);
  69         }
  70     }
  71 
  72     @Benchmark
  73     public void testSmallObject(Blackhole bh) throws Exception {
  74         FortyBytes localDummy = null;
  75         for (int i = 0; i < LENGTH; i++) {
  76             FortyBytes tmp = new FortyBytes();
  77             tmp.next = localDummy;
  78             localDummy = tmp;
  79             bh.consume(tmp);
  80         }
  81     }
  82 
  83     @Benchmark
  84     public void testSmallVariableArray(Blackhole bh) throws Exception {
  85         int localArrlen = smalllen;
  86         for (int i = 0; i < LENGTH; i++) {
  87             Object[] tmp = new Object[localArrlen];
  88             bh.consume(tmp);
  89         }
  90     }
  91 
  92     final class FortyBytes {
  93         Object next;
  94         int y, z, k, f, g, e, t;
  95     }
  96 
  97 }