Path: blob/master/test/micro/org/openjdk/bench/java/util/UUIDBench.java
41161 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package org.openjdk.bench.java.util;2324import java.util.UUID;25import java.util.concurrent.TimeUnit;2627import org.openjdk.jmh.annotations.*;2829@State(Scope.Thread)30@OutputTimeUnit(TimeUnit.MICROSECONDS)31@Fork(value = 3)32@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)33@Measurement(iterations = 5, time = 2, timeUnit = TimeUnit.SECONDS)34public class UUIDBench {3536@Param("20000")37private int size;3839private byte[][] uuidBytes;4041private UUID[] uuids;4243private String[] uuidStrings;4445private int index = 0;4647@Setup48public void setup() {49uuidBytes = new byte[size][];50uuids = new UUID[size];51uuidStrings = new String[size];52java.util.Random r = new java.util.Random(0);53for (int i = 0; i < this.uuidStrings.length; i++) {54final UUID uuid = UUID.randomUUID();55this.uuidBytes[i] = new byte[16];56r.nextBytes(this.uuidBytes[i]);57this.uuids[i] = uuid;58this.uuidStrings[i] = uuid.toString();59}60}6162@Setup(Level.Iteration)63public void setupIteration() {64index++;65if (index >= size) {66index = 0;67}68}6970@Benchmark71public UUID fromString() {72return UUID.fromString(uuidStrings[index]);73}7475@Benchmark76public String toString() {77return uuids[index].toString();78}7980@Benchmark81public UUID fromType3Bytes() {82return UUID.nameUUIDFromBytes(uuidBytes[index]);83}84}858687