How to run a benchmark with SecurityManager?

Aleksey Shipilev aleksey.shipilev at oracle.com
Wed Jan 28 17:52:35 UTC 2015


Hi,

Weijun, Sean, what Staffan is saying below is a way to go. Also, given
that we have a few users already struggling with this at some point, it
seems prudent to ship the default security policy with the JMH itself,
then you can just reference it via URI.

See:
 https://bugs.openjdk.java.net/browse/CODETOOLS-7901280
 http://hg.openjdk.java.net/code-tools/jmh/rev/ceb8cfe4701d

Here is a sample how you can enable the SecurityManager for a single
method only:
 http://hg.openjdk.java.net/code-tools/jmh/file/tip/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_33_SecurityManager.java

Thanks,
-Aleksey.

On 27.01.2015 21:34, Staffan Friberg wrote:
> Hi,
> 
> I have done a couple of benchmarks where I wanted to compare the
> performance with and without a security manager.
> 
> Since I wanted to compare I create a class with all benchmarks, which I
> then sub-classed and in the sub-class I added the following setup method.
> If you only care to run the benchmark with a security manager you can
> probably skip the sub-class part and simply add the setup method.
> 
> 
>     @Setup
>     public void setup() throws IOException, NoSuchAlgorithmException {
>         File policyFile = File.createTempFile("security", ".policy");
>         policyFile.deleteOnExit();
> 
>         try (PrintWriter writer = new PrintWriter(policyFile);
>              BufferedReader reader = new BufferedReader(new
> InputStreamReader(
> this.getclass().getResourceAsStream("security.policy")))) {
>             for (String line = reader.readLine(); line != null; line =
> reader.readLine()) {
>                 writer.write(line);
>             }
>         }
> 
>         Policy policy = Policy.getInstance("JavaPolicy", new
> URIParameter(policyFile.toURI()));
>         Policy.setPolicy(policy);
>         System.setSecurityManager(new SecurityManager());
>     }
> 
> //Staffan
> 
> On 01/27/2015 07:50 AM, Weijun Wang wrote:
>> Yes, it works. See my push at
>> http://sca00bkv.us.oracle.com:8000/jdk9/secperf/jdk/rev/603dd9f84399212a7d48fc3d7589d98e85dec820.
>>
>>
>> But the worst thing now is that system properties must be provided on
>> the command line and it means we cannot run multiple benchmark methods
>> in one command. (Suppose the other methods must run without a security
>> manager).
>>
>> --Max
>>
>> On 1/27/2015 20:53, Sean Mullan wrote:
>>> Have you tried granting the permissions that jmh needs in your policy
>>> file?
>>>
>>> --Sean
>>>
>>> On 01/27/2015 05:52 AM, Wang Weijun wrote:
>>>> Hi All
>>>>
>>>> I'm trying to benchmark a method that works differently when a
>>>> SecurityManager is on (and with different policy files). I've tried
>>>> adding -Djava.security.manager on the command line or calling
>>>> System.setSecurityManager(new SecurityManager()) inside a @Setup
>>>> method, but seems jmh itself cannot run correctly (because it needs
>>>> reflection and thread group manipulation).
>>>>
>>>> Is there anyway to do it? Since JM itself and the benchmark is
>>>> compiled into one jar, I cannot grant AllPermission to jmh and less
>>>> permission on my own code.
>>>>
>>>> Thanks
>>>> Max
>>>>
> 




More information about the jmh-dev mailing list