|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava UI components for Java Swing. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2010 - 2015 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison. |
| 7 | + * %% |
| 8 | + * Redistribution and use in source and binary forms, with or without |
| 9 | + * modification, are permitted provided that the following conditions are met: |
| 10 | + * |
| 11 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 12 | + * this list of conditions and the following disclaimer. |
| 13 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer in the documentation |
| 15 | + * and/or other materials provided with the distribution. |
| 16 | + * |
| 17 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 21 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | + * POSSIBILITY OF SUCH DAMAGE. |
| 28 | + * #L% |
| 29 | + */ |
| 30 | + |
| 31 | +package org.scijava.ui.swing.console; |
| 32 | + |
| 33 | +import static org.junit.Assert.assertEquals; |
| 34 | +import static org.junit.Assert.assertTrue; |
| 35 | + |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.concurrent.Future; |
| 38 | + |
| 39 | +import javax.swing.JTextPane; |
| 40 | +import javax.swing.text.BadLocationException; |
| 41 | +import javax.swing.text.Document; |
| 42 | + |
| 43 | +import org.scijava.Context; |
| 44 | +import org.scijava.thread.ThreadService; |
| 45 | +import org.scijava.ui.UIService; |
| 46 | +import org.scijava.ui.swing.sdi.SwingSDIUI; |
| 47 | + |
| 48 | +/** |
| 49 | + * A behavioral test and benchmark of {@link SwingConsolePane}. |
| 50 | + * |
| 51 | + * @author Curtis Rueden |
| 52 | + */ |
| 53 | +public class SwingConsolePaneBenchmark { |
| 54 | + |
| 55 | + // -- Main method -- |
| 56 | + |
| 57 | + /** A manual test drive of the Swing UI's console pane. */ |
| 58 | + public static void main(final String[] args) throws Exception { |
| 59 | + final Context context = new Context(); |
| 60 | + context.service(UIService.class).showUI(); |
| 61 | + |
| 62 | + System.out.print("Hello "); |
| 63 | + System.err.println("world!"); |
| 64 | + |
| 65 | + final int numThreads = 50; |
| 66 | + final int numOperations = 20; |
| 67 | + |
| 68 | + final String[] streamLabels = |
| 69 | + { ": {ERR} iteration #", ": {OUT} iteration #" }; |
| 70 | + final String outLabel = streamLabels[1]; |
| 71 | + final String errLabel = streamLabels[0]; |
| 72 | + final int numStreams = streamLabels.length; |
| 73 | + |
| 74 | + final int initialDelay = 500; |
| 75 | + |
| 76 | + Thread.sleep(initialDelay); |
| 77 | + |
| 78 | + final long start = System.currentTimeMillis(); |
| 79 | + |
| 80 | + // emit a bunch of output on multiple threads concurrently |
| 81 | + final ThreadService threadService = context.service(ThreadService.class); |
| 82 | + final Future<?>[] f = new Future<?>[numThreads]; |
| 83 | + for (int t = 0; t < numThreads; t++) { |
| 84 | + final int tNo = t; |
| 85 | + f[t] = threadService.run(new Runnable() { |
| 86 | + |
| 87 | + @Override |
| 88 | + public void run() { |
| 89 | + for (int i = 0; i < numOperations; i++) { |
| 90 | + System.out.print(str(tNo, outLabel, i) + "\n"); |
| 91 | + System.err.print(str(tNo, errLabel, i) + "\n"); |
| 92 | + } |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | + |
| 97 | + // wait for all output threads to finish |
| 98 | + for (int t = 0; t < numThreads; t++) { |
| 99 | + f[t].get(); |
| 100 | + } |
| 101 | + |
| 102 | + System.err.print("Goodbye "); |
| 103 | + System.out.println("cruel world!"); |
| 104 | + |
| 105 | + final long end = System.currentTimeMillis(); |
| 106 | + System.out.println(); |
| 107 | + System.out.println("Benchmark took " + (end - start) + " ms"); |
| 108 | + |
| 109 | + // Finally, check for completeness of output. |
| 110 | + // NB: We do this **also on the EDT** so that all output has flushed. |
| 111 | + final String completenessMessage = "Checking for completeness of output..."; |
| 112 | + System.out.println(); |
| 113 | + System.out.println(completenessMessage); |
| 114 | + threadService.queue(new Runnable() { |
| 115 | + |
| 116 | + @Override |
| 117 | + public void run() { |
| 118 | + System.out.println(); |
| 119 | + final SwingSDIUI ui = |
| 120 | + (SwingSDIUI) context.service(UIService.class).getVisibleUIs().get(0); |
| 121 | + final JTextPane textPane = ui.getConsolePane().getTextPane(); |
| 122 | + final Document doc = textPane.getDocument(); |
| 123 | + try { |
| 124 | + final String text = doc.getText(0, doc.getLength()); |
| 125 | + final String[] lines = text.split("\n"); |
| 126 | + Arrays.sort(lines); |
| 127 | + |
| 128 | + int lineIndex = 0; |
| 129 | + assertEquals("", lines[lineIndex++]); |
| 130 | + assertEquals("", lines[lineIndex++]); |
| 131 | + for (int t = 0; t < numThreads; t++) { |
| 132 | + for (int s = 0; s < numStreams; s++) { |
| 133 | + for (int i = 0; i < numOperations; i++) { |
| 134 | + final String expected = str(t, streamLabels[s], i); |
| 135 | + final String actual = lines[lineIndex++]; |
| 136 | + assertEquals(expected, actual); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + assertTrue(lines[lineIndex++].startsWith("Benchmark took ")); |
| 141 | + assertEquals(completenessMessage, lines[lineIndex++]); |
| 142 | + assertEquals("Goodbye cruel world!", lines[lineIndex++]); |
| 143 | + assertEquals("Hello world!", lines[lineIndex++]); |
| 144 | + assertEquals(lineIndex, lines.length); |
| 145 | + |
| 146 | + System.out.println("Success! All output accounted for!"); |
| 147 | + } |
| 148 | + catch (final BadLocationException exc) { |
| 149 | + exc.printStackTrace(); |
| 150 | + } |
| 151 | + } |
| 152 | + }); |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + // - Helper methods -- |
| 157 | + |
| 158 | + private static String str(final int t, final String separator, final int i) { |
| 159 | + return pad(t) + separator + pad(i); |
| 160 | + } |
| 161 | + |
| 162 | + private static String pad(final int n) { |
| 163 | + return n < 10 ? "0" + n : "" + n; |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments