|
| 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 | + |
| 24 | +package org.openjdk.bench.java.net; |
| 25 | + |
| 26 | +import org.openjdk.jmh.annotations.State; |
| 27 | +import org.openjdk.jmh.annotations.Scope; |
| 28 | +import org.openjdk.jmh.annotations.Benchmark; |
| 29 | +import org.openjdk.jmh.annotations.Fork; |
| 30 | +import org.openjdk.jmh.annotations.CompilerControl; |
| 31 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 32 | +import org.openjdk.jmh.annotations.Mode; |
| 33 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 34 | +import org.openjdk.jmh.annotations.Setup; |
| 35 | + |
| 36 | +import java.lang.invoke.MethodHandle; |
| 37 | +import java.lang.invoke.MethodHandles; |
| 38 | +import java.net.URI; |
| 39 | +import java.net.URL; |
| 40 | +import java.util.concurrent.TimeUnit; |
| 41 | + |
| 42 | +import static java.lang.invoke.MethodType.methodType; |
| 43 | + |
| 44 | +@BenchmarkMode(Mode.AverageTime) |
| 45 | +@OutputTimeUnit(TimeUnit.NANOSECONDS) |
| 46 | +@State(Scope.Thread) |
| 47 | +@Fork(value = 1, jvmArgsAppend = "--add-exports=java.base/sun.net.www=ALL-UNNAMED") |
| 48 | +public class ThreadLocalParseUtil { |
| 49 | + |
| 50 | + private static final MethodHandle MH_DECODE; |
| 51 | + private static final MethodHandle MH_TO_URI; |
| 52 | + |
| 53 | + static { |
| 54 | + final MethodHandles.Lookup lookup = MethodHandles.lookup(); |
| 55 | + try { |
| 56 | + Class<?> c = Class.forName("sun.net.www.ParseUtil"); |
| 57 | + MH_DECODE = lookup.findStatic(c, "decode", methodType(String.class, String.class)); |
| 58 | + MH_TO_URI = lookup.findStatic(c, "toURI", methodType(URI.class, URL.class)); |
| 59 | + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) { |
| 60 | + throw new ExceptionInInitializerError(e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Benchmark |
| 65 | + public String decodeTest() throws Throwable { |
| 66 | + return (String) MH_DECODE.invokeExact("/xyz/\u00A0\u00A0"); |
| 67 | + } |
| 68 | + |
| 69 | + @Benchmark |
| 70 | + public URI appendEncodedTest() throws Throwable { |
| 71 | + URL url = new URL("https://example.com/xyz/abc/def?query=#30"); |
| 72 | + return (URI) MH_TO_URI.invokeExact(url); |
| 73 | + } |
| 74 | +} |
0 commit comments