ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SimpleLoops.java
Revision: 1.1
Committed: Mon May 2 19:19:38 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Put misc performance tests into CVS

File Contents

# Content
1 /*
2 * @test
3 * @synopsis multiple threads using a single lock
4 */
5 /*
6 * Written by Doug Lea with assistance from members of JCP JSR-166
7 * Expert Group and released to the public domain. Use, modify, and
8 * redistribute this code in any way without acknowledgement.
9 */
10
11 import java.util.concurrent.*;
12 import java.util.concurrent.locks.*;
13 import java.util.*;
14
15 public final class SimpleLoops {
16 static final ExecutorService pool = Executors.newCachedThreadPool();
17 static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
18 static boolean print = false;
19 static int iters = 10000000;
20
21 public static void main(String[] args) throws Exception {
22 int maxThreads = 100;
23 if (args.length > 0)
24 maxThreads = Integer.parseInt(args[0]);
25
26 print = true;
27
28 int k = 1;
29 for (int i = 1; i <= maxThreads;) {
30 System.out.print("Threads: " + 1);
31 new Loop(1).test();
32 Thread.sleep(100);
33 if (i == k) {
34 k = i << 1;
35 i = i + (i >>> 1);
36 }
37 else
38 i = k;
39 }
40 pool.shutdown();
41 }
42
43 static final class Loop implements Runnable {
44 private int v = rng.next();
45 private volatile int result = 17;
46 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
47 private final CyclicBarrier barrier;
48 private final int nthreads;
49 private volatile int readBarrier;
50 Loop(int nthreads) {
51 this.nthreads = nthreads;
52 barrier = new CyclicBarrier(nthreads+1, timer);
53 }
54
55 final void test() throws Exception {
56 for (int i = 0; i < nthreads; ++i)
57 pool.execute(this);
58 barrier.await();
59 barrier.await();
60 if (print) {
61 long time = timer.getTime();
62 long tpi = time / ((long)iters * nthreads);
63 System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock");
64 double secs = (double)(time) / 1000000000.0;
65 System.out.println("\t " + secs + "s run time");
66 }
67
68 int r = result;
69 if (r == 0) // avoid overoptimization
70 System.out.println("useless result: " + r);
71 }
72
73 public final void run() {
74 try {
75 barrier.await();
76 int sum = v + 1;
77 int x = 0;
78 int n = iters;
79 while (n-- > 0) {
80 int k = (sum & 3);
81 if (k > 0) {
82 x = v;
83 while (k-- > 0)
84 x = LoopHelpers.compute1(x);
85 v = x;
86 }
87 else x = sum + 1;
88 if ((x += readBarrier) == 0)
89 ++readBarrier;
90 for (int l = x & 7; l > 0; --l)
91 sum += LoopHelpers.compute1(sum);
92 }
93 barrier.await();
94 result += sum;
95 }
96 catch (Exception ie) {
97 return;
98 }
99 }
100 }
101
102 }