1 /*
   2  * Copyright (c) 2020, 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.util;
  24 
  25 import java.util.UUID;
  26 import java.util.concurrent.TimeUnit;
  27 
  28 import org.openjdk.jmh.annotations.*;
  29 
  30 @State(Scope.Thread)
  31 @OutputTimeUnit(TimeUnit.MICROSECONDS)
  32 @Fork(value = 3)
  33 @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
  34 @Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)
  35 public class UUIDBench {
  36 
  37     @Param("20000")
  38     private int size;
  39 
  40     private UUID[] uuids;
  41 
  42     private String[] uuidStrings;
  43 
  44     private int index = 0;
  45 
  46     @Setup
  47     public void setup() {
  48         uuids = new UUID[size];
  49         uuidStrings = new String[size];
  50         for (int i = 0; i < this.uuidStrings.length; i++) {
  51             final UUID uuid = UUID.randomUUID();
  52 
  53             this.uuids[i] = uuid;
  54             this.uuidStrings[i] = uuid.toString();
  55         }
  56     }
  57 
  58     @Setup(Level.Iteration)
  59     public void setupIteration() {
  60         index++;
  61         if (index >= size) {
  62             index = 0;
  63         }
  64     }
  65 
  66     @Benchmark
  67     public UUID fromString() {
  68         return UUID.fromString(uuidStrings[index]);
  69     }
  70 
  71     @Benchmark
  72     public String toString() {
  73         return uuids[index].toString();
  74     }
  75 }