Guest User

Untitled

a guest
Mar 29th, 2014
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.61 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2005, 2013, 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.  Oracle designates this
  8.  * particular file as subject to the "Classpath" exception as provided
  9.  * by Oracle in the LICENSE file that accompanied this code.
  10.  *
  11.  * This code is distributed in the hope that it will be useful, but WITHOUT
  12.  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13.  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14.  * version 2 for more details (a copy is included in the LICENSE file that
  15.  * accompanied this code).
  16.  *
  17.  * You should have received a copy of the GNU General Public License version
  18.  * 2 along with this work; if not, write to the Free Software Foundation,
  19.  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22.  * or visit www.oracle.com if you need additional information or have any
  23.  * questions.
  24.  */
  25.  
  26. package org.sample;
  27.  
  28. import java.util.concurrent.atomic.AtomicBoolean;
  29.  
  30. import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
  31. import org.openjdk.jmh.annotations.Group;
  32. import org.openjdk.jmh.annotations.GroupThreads;
  33. import org.openjdk.jmh.annotations.Scope;
  34. import org.openjdk.jmh.annotations.State;
  35. import org.openjdk.jmh.runner.Runner;
  36. import org.openjdk.jmh.runner.RunnerException;
  37. import org.openjdk.jmh.runner.options.Options;
  38. import org.openjdk.jmh.runner.options.OptionsBuilder;
  39.  
  40. @State(Scope.Group)
  41. public class MyBenchmark {
  42.  
  43.     public AtomicBoolean testBool = new AtomicBoolean(true);
  44.  
  45.     @GenerateMicroBenchmark
  46.     @Group("CAS")
  47.     @GroupThreads(4)
  48.     public boolean testMethod() {
  49.         return testBool.compareAndSet(false, true);
  50.     }
  51.  
  52.     @GenerateMicroBenchmark
  53.     @Group("GET")
  54.     @GroupThreads(4)
  55.     public boolean testVolatileGet() {
  56.         if (testBool.get() == false) {
  57.             testBool.set(true);
  58.             return true;
  59.         } else {
  60.             return false;
  61.         }
  62.     }
  63.  
  64.     public static void main(String[] args) throws RunnerException {
  65.         Options opt = new OptionsBuilder()
  66.                 .include(".*" + MyBenchmark.class.getSimpleName() + ".*")
  67.                 .warmupIterations(5)
  68.                 .measurementIterations(5)
  69.                 .forks(1)
  70.                 .build();
  71.  
  72.         new Runner(opt).run();
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment