Path: blob/master/test/micro/org/openjdk/bench/java/security/CacheBench.java
41161 views
/*1* Copyright (c) 2021, Dynatrace LLC. 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*/2223package org.openjdk.bench.java.security;2425import org.openjdk.jmh.annotations.Benchmark;26import org.openjdk.jmh.annotations.BenchmarkMode;27import org.openjdk.jmh.annotations.Fork;28import org.openjdk.jmh.annotations.Level;29import org.openjdk.jmh.annotations.Mode;30import org.openjdk.jmh.annotations.OutputTimeUnit;31import org.openjdk.jmh.annotations.Param;32import org.openjdk.jmh.annotations.Scope;33import org.openjdk.jmh.annotations.Setup;34import org.openjdk.jmh.annotations.State;35import org.openjdk.jmh.annotations.TearDown;3637import java.util.concurrent.TimeUnit;38import java.util.stream.IntStream;3940import sun.security.util.Cache;4142@BenchmarkMode(Mode.AverageTime)43@OutputTimeUnit(TimeUnit.NANOSECONDS)44@Fork(jvmArgsAppend = {"--add-exports", "java.base/sun.security.util=ALL-UNNAMED", "-Xmx1g"})45public class CacheBench {4647@State(Scope.Benchmark)48public static class SharedState {49Cache<Integer, Integer> cache;5051@Param({"20480", "204800", "5120000"})52int size;5354@Param({"86400", "0"})55int timeout;5657@Setup58public void setup() {59cache = Cache.newSoftMemoryCache(size, timeout);60IntStream.range(0, size).boxed().forEach(i -> cache.put(i, i));61}62}6364@State(Scope.Thread)65public static class GetPutState {66Integer[] intArray;67int index;6869@Setup70public void setup(SharedState benchState) {71intArray = IntStream.range(0, benchState.size + 1).boxed().toArray(Integer[]::new);72index = 0;73}7475@TearDown(Level.Invocation)76public void tearDown() {77index++;78if (index >= intArray.length) {79index = 0;80}81}82}8384@Benchmark85public void put(SharedState benchState, GetPutState state) {86Integer i = state.intArray[state.index];87benchState.cache.put(i, i);88}8990@Benchmark91public Integer get(SharedState benchState, GetPutState state) {92Integer i = state.intArray[state.index];93return benchState.cache.get(i);94}9596@State(Scope.Thread)97public static class RemoveState {98Integer[] intArray;99int index;100SharedState benchState;101102@Setup103public void setup(SharedState benchState) {104this.benchState = benchState;105intArray = IntStream.range(0, benchState.size).boxed().toArray(Integer[]::new);106index = 0;107}108109@TearDown(Level.Invocation)110public void tearDown() {111// add back removed item112Integer i = intArray[index];113benchState.cache.put(i, i);114115index++;116if (index >= intArray.length) {117index = 0;118}119}120}121122@Benchmark123public void remove(SharedState benchState, RemoveState state) {124Integer i = state.intArray[state.index];125benchState.cache.remove(i);126}127}128129130