From e715fe64e96acb6131af937d0c8bba2537ea13f4 Mon Sep 17 00:00:00 2001 From: Ningsheng Jian Date: Tue, 7 Nov 2017 18:38:01 +0800 Subject: Port and add more cases. 1. Port benchmark cases from art-testing. 2. Add some more benchmarks for basic OPs. 3. Fix some jmh result variance. Signed-off-by: Zhongwei Yao Signed-off-by: Yang Zhang Signed-off-by: Ningsheng Jian Change-Id: I514658696b63e468158325be3b84494553773705 --- .../benchmarks/benchmarksgame/threadring.java | 120 +++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 src/main/java/org/linaro/benchmarks/benchmarksgame/threadring.java (limited to 'src/main/java/org/linaro/benchmarks/benchmarksgame/threadring.java') diff --git a/src/main/java/org/linaro/benchmarks/benchmarksgame/threadring.java b/src/main/java/org/linaro/benchmarks/benchmarksgame/threadring.java new file mode 100644 index 0000000..c30713a --- /dev/null +++ b/src/main/java/org/linaro/benchmarks/benchmarksgame/threadring.java @@ -0,0 +1,120 @@ +/* + * This benchmark has been ported from "The Computer Language Benchmarks Game" suite and + * significantly modified to fit the benchmarking framework. + * + * Originally the benchmark finished with 'System.exit(0)'. A new state of message "-1" was + * introduced to avoid this. + * + * The original file is `threadring/threadring.java-3.java` from the archive + * available at + * http://benchmarksgame.alioth.debian.org/download/benchmarksgame-sourcecode.zip. + * See LICENSE file in the same folder (BSD 3-clause) + * + * The Computer Language Benchmarks Game + * http://benchmarksgame.alioth.debian.org/ + * contributed by Klaus Friedel + */ + +/* + * Description: Switch from thread to thread passing one token. + * Main Focus: TODO + * + */ + +package org.linaro.benchmarks.benchmarksgame; + +import java.util.concurrent.locks.LockSupport; +import org.openjdk.jmh.annotations.*; +import java.util.concurrent.TimeUnit; + +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MICROSECONDS) +@State(Scope.Benchmark) + +// CHECKSTYLE.OFF: .* +public class threadring { +// CHECKSTYLE.ON: .* + static final int THREAD_COUNT = 503; + + private static String lastActedThreadName; + + public static class MessageThread extends Thread { + MessageThread nextThread; + volatile Integer message; + + public MessageThread(MessageThread nextThread, int name) { + super("" + name); + this.nextThread = nextThread; + } + + public void run() { + Integer msg = dequeue(); + while (nextThread.enqueue(msg)) { + msg = dequeue(); + if (msg == -1) { + break; + } + } + } + + public boolean enqueue(Integer hopsRemaining) { + if (hopsRemaining == 0) { + message = -1; + lastActedThreadName = this.getName(); + + // notify all the threads that transmission is over + LockSupport.unpark(this); + MessageThread current = nextThread; + while (current != this) { + current.message = -1; + LockSupport.unpark(current); + current = current.nextThread; + } + + return false; + } + + message = hopsRemaining - 1; + LockSupport.unpark(this); + return true; + } + + private Integer dequeue() { + while (message == null) { + LockSupport.park(); + } + + Integer msg = message; + if (msg != -1) { + message = null; + } + return msg; + } + } + @Benchmark + public void jmhTimeThreadRing() throws Exception { + + int hopCount = 1000; + + MessageThread first = null; + MessageThread last = null; + for (int i = THREAD_COUNT; i >= 1; i--) { + first = new MessageThread(first, i); + if (i == THREAD_COUNT) { + last = first; + } + } + // close the ring: + last.nextThread = first; + + // start all Threads + MessageThread t = first; + do { + t.start(); + t = t.nextThread; + } while (t != first); + + first.enqueue(hopCount); + first.join(); // wait for System.exit + } +} -- cgit v1.2.3