ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SubmissionPublisherLoops4.java
Revision: 1.2
Committed: Thu Jul 23 14:58:13 2015 UTC (8 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.1: +10 -10 lines
Log Message:
whitespace

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/publicdomain/zero/1.0/
5 */
6
7 import java.util.*;
8 import java.util.concurrent.*;
9
10 /**
11 * Create PRODUCERS publishers each with PROCESSORS processors
12 * each with CONSUMERS subscribers, each sent ITEMS items, with
13 * max CAP buffering; repeat REPS times
14 */
15 public class SubmissionPublisherLoops4 {
16 static final int ITEMS = 1 << 20;
17 static final int PRODUCERS = 32;
18 static final int PROCESSORS = 32;
19 static final int CONSUMERS = 32;
20 static final int CAP = Flow.defaultBufferSize();
21
22 static final int REPS = 9;
23
24 static final int SINKS = PRODUCERS * PROCESSORS * CONSUMERS;
25 static final long NEXTS = (long)ITEMS * SINKS;
26 static final Phaser phaser = new Phaser(SINKS + 1);
27
28 public static void main(String[] args) throws Exception {
29 int reps = REPS;
30 if (args.length > 0)
31 reps = Integer.parseInt(args[0]);
32
33 System.out.println("ITEMS: " + ITEMS +
34 " PRODUCERS: " + PRODUCERS +
35 " PROCESSORS: " + PROCESSORS +
36 " CONSUMERS: " + CONSUMERS +
37 " CAP: " + CAP);
38 for (int rep = 0; rep < reps; ++rep) {
39 long startTime = System.nanoTime();
40 for (int i = 0; i < PRODUCERS; ++i)
41 new Pub().fork();
42 phaser.arriveAndAwaitAdvance();
43 long elapsed = System.nanoTime() - startTime;
44 double secs = ((double)elapsed) / (1000L * 1000 * 1000);
45 double ips = NEXTS / secs;
46 System.out.printf("Time: %7.2f", secs);
47 System.out.printf(" items per sec: %14.2f\n", ips);
48 System.out.println(ForkJoinPool.commonPool());
49 }
50 }
51
52 static final class Sub implements Flow.Subscriber<Boolean> {
53 int count;
54 Flow.Subscription subscription;
55 public void onSubscribe(Flow.Subscription s) {
56 (subscription = s).request(CAP);
57 }
58 public void onNext(Boolean b) {
59 if (b && (++count & ((CAP >>> 1) - 1)) == 0)
60 subscription.request(CAP >>> 1);
61 }
62 public void onComplete() {
63 if (count != ITEMS)
64 System.out.println("Error: remaining " + (ITEMS - count));
65 phaser.arrive();
66 }
67 public void onError(Throwable t) { t.printStackTrace(); }
68 }
69
70 static final class Proc extends SubmissionPublisher<Boolean>
71 implements Flow.Processor<Boolean,Boolean> {
72 Flow.Subscription subscription;
73 int count;
74 Proc(Executor executor, int maxBufferCapacity) {
75 super(executor, maxBufferCapacity);
76 }
77 public void onSubscribe(Flow.Subscription subscription) {
78 (this.subscription = subscription).request(CAP);
79 }
80 public void onNext(Boolean item) {
81 if ((++count & ((CAP >>> 1) - 1)) == 0)
82 subscription.request(CAP >>> 1);
83 submit(item);
84 }
85 public void onError(Throwable ex) { closeExceptionally(ex); }
86 public void onComplete() { close(); }
87 }
88
89 static final class Pub extends RecursiveAction {
90 final SubmissionPublisher<Boolean> pub =
91 new SubmissionPublisher<Boolean>(ForkJoinPool.commonPool(), CAP);
92 public void compute() {
93 SubmissionPublisher<Boolean> p = pub;
94 for (int j = 0; j < PROCESSORS; ++j) {
95 Proc t = new Proc(ForkJoinPool.commonPool(), CAP);
96 for (int i = 0; i < CONSUMERS; ++i)
97 t.subscribe(new Sub());
98 p.subscribe(t);
99 }
100 for (int i = 0; i < ITEMS; ++i)
101 p.submit(Boolean.TRUE);
102 p.close();
103 }
104 }
105 }