Path: blob/master/test/micro/org/openjdk/bench/java/security/GetContext.java
41161 views
/*1* Copyright (c) 2014, 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. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package org.openjdk.bench.java.security;2526import org.openjdk.jmh.annotations.Benchmark;27import org.openjdk.jmh.annotations.BenchmarkMode;28import org.openjdk.jmh.annotations.Mode;29import org.openjdk.jmh.annotations.OutputTimeUnit;30import org.openjdk.jmh.annotations.Param;31import org.openjdk.jmh.annotations.Scope;32import org.openjdk.jmh.annotations.State;3334import java.security.AccessControlContext;35import java.security.AccessController;36import java.security.PrivilegedAction;37import java.util.concurrent.TimeUnit;3839/**40* Benchmark measuring AccessController.getContext41*/42@BenchmarkMode(Mode.AverageTime)43@OutputTimeUnit(TimeUnit.NANOSECONDS)44@State(Scope.Thread)45public abstract class GetContext {4647public static class Top extends GetContext {4849@SuppressWarnings("removal")50@Benchmark51public AccessControlContext testNonPriv() {52return AccessController.getContext();53}5455@SuppressWarnings("removal")56@Benchmark57public AccessControlContext testPriv() {58PrivilegedAction<AccessControlContext> pa = () -> AccessController.getContext();59return AccessController.doPrivileged(pa);60}61}6263public static class Deep extends GetContext {6465@Param({"2", "50"})66int depth;6768@SuppressWarnings("removal")69private AccessControlContext recurse(int depth) {70if (depth > 0) {71return recurse(depth - 1);72} else {73return AccessController.getContext();74}75}7677@SuppressWarnings("removal")78@Benchmark79public AccessControlContext testNonPrivRecurse() {80return recurse(depth);81}8283@SuppressWarnings("removal")84@Benchmark85public AccessControlContext testPrivInline() {86PrivilegedAction<AccessControlContext> pa = () -> recurse(depth);87return AccessController.doPrivileged(pa);88}89}90}919293