Path: blob/master/test/micro/org/openjdk/bench/java/lang/StringOther.java
41161 views
/*1* Copyright (c) 2014, 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.lang;2324import org.openjdk.jmh.annotations.Benchmark;25import org.openjdk.jmh.annotations.BenchmarkMode;26import org.openjdk.jmh.annotations.Mode;27import org.openjdk.jmh.annotations.OutputTimeUnit;28import org.openjdk.jmh.annotations.Scope;29import org.openjdk.jmh.annotations.Setup;30import org.openjdk.jmh.annotations.State;31import org.openjdk.jmh.infra.Blackhole;3233import java.util.Random;34import java.util.concurrent.TimeUnit;3536@BenchmarkMode(Mode.AverageTime)37@OutputTimeUnit(TimeUnit.NANOSECONDS)38@State(Scope.Thread)39public class StringOther {4041private String testString;42private Random rnd;4344private String str1, str2, str3, str4;4546@Setup47public void setup() {48testString = "Idealism is what precedes experience; cynicism is what follows.";49str1 = "vm-guld vm-guld vm-guld";50str2 = "vm-guld vm-guld vm-guldx";51str3 = "vm-guld vm-guld vm-guldx";52str4 = "adadaskasdjierudks";53rnd = new Random();54}5556@Benchmark57public void charAt(Blackhole bh) {58for (int i = 0; i < testString.length(); i++) {59bh.consume(testString.charAt(i));60}61}6263@Benchmark64public int compareTo() {65int total = 0;66total += str1.compareTo(str2);67total += str2.compareTo(str3);68total += str3.compareTo(str4);69return total;70}7172/**73* Creates (hopefully) unique Strings and internizes them, creating a zillion forgettable strings in the JVMs string74* pool.75* <p/>76* This will test 1.) The data structure/whatever for getting and adding Strings to intern table. 2.) The77* intern-caches (java) behaviour on negative lookup (the string is new) 3.) GC's handling of weak handles. Since78* every gc we must process and pretty much kill a zillion interned strings that are now not referenced anymore, the79* majority of GC time will be spent in handle processing. So we get a picture of how well the pathological case of80* this goes.81*/82@Benchmark83public String internUnique() {84return String.valueOf(rnd.nextInt()).intern();85}8687}888990