private void runSingleRound()

in src/org/pushingpixels/lightbeam/DynamicPerformanceSuite.java [236:386]


    private void runSingleRound(boolean toTime, String specificScenarioId) {
        System.out.println(">>>>>>>>>>>>>>>>>>>>> START >>>>>>>>>>>>>>>>>>>>>");
        isCancelled = false;
        long total = 0;
        for (final Map.Entry<String, ComponentInfo> scenarioEntry : scenarios.entrySet()) {
            if (isCancelled)
                break;

            String tabTitle = scenarioEntry.getKey();
            final Component tabComponent = scenarioEntry.getValue().tabComponent;
            List<PerformanceScenario> scenarios = scenarioEntry.getValue().scenarios;
            for (final PerformanceScenario scenario : scenarios) {
                if (specificScenarioId != null) {
                    if (!specificScenarioId.equals(scenario.getName()))
                        continue;
                }

                // System.out.println("Running "
                // + scenario.getName());

                final boolean isTabPanel = (tabs.indexOfComponent(tabComponent) >= 0);
                try {
                    // must run scenario setup on EDT
                    // since most probably it involves
                    // UI-related operations
                    SwingUtilities.invokeAndWait(new Runnable() {
                        @Override
                        public void run() {
                            tabs.setVisible(isTabPanel);
                            if (!isTabPanel) {
                                tabs.setSelectedIndex(0);
                            } else {
                                tabs.setSelectedComponent(tabComponent);
                            }
                            scenario.setup();
                        }
                    });
                } catch (Exception exc) {
                    exc.printStackTrace();
                    System.exit(1);
                }

                final int iterationCount = scenario.getIterationCount();
                final CountDownLatch latch = new CountDownLatch(1);
                long start = System.nanoTime();

                long startEdtUser = threadBean.getThreadUserTime(edtThreadId);
                long startEdtCPU = threadBean.getThreadCpuTime(edtThreadId);

                SwingWorker<Void, Integer> worker = new SwingWorker<Void, Integer>() {
                    @Override
                    protected Void doInBackground() throws Exception {
                        // System.out
                        // .println("Started working");
                        for (int i = 0; i < iterationCount; i++) {
                            this.publish(i);
                        }
                        return null;
                    }

                    @Override
                    protected void process(List<Integer> chunks) {
                        // System.out.println("Running "
                        // + chunks.size());
                        for (int chunk : chunks) {
                            if (!isCancelled)
                                scenario.runSingleIteration(chunk);
                        }
                    }

                    @Override
                    protected void done() {
                        // System.out.println("Done");
                        latch.countDown();
                    }
                };
                // System.out.println("Executing worker"
                // );
                worker.execute();

                try {
                    latch.await();
                } catch (InterruptedException ie) {
                }

                long end = System.nanoTime();
                long time = end - start;

                long endEdtUser = threadBean.getThreadUserTime(edtThreadId);
                long endEdtCPU = threadBean.getThreadCpuTime(edtThreadId);

                long edtUserTime = endEdtUser - startEdtUser;
                long edtCPUTime = endEdtCPU - startEdtCPU;

                try {
                    SwingUtilities.invokeAndWait(new Runnable() {
                        public void run() {
                            scenario.tearDown();
                        }
                    });
                } catch (Exception exc) {
                    exc.printStackTrace();
                    System.exit(1);
                }

                StringBuilder sbKey = new StringBuilder();
                Formatter keyFormatter = new Formatter(sbKey, Locale.US);
                keyFormatter.format("%1$15s : %2$s", tabTitle, scenario.getName());

                StringBuilder sb = new StringBuilder();
                Formatter formatter = new Formatter(sb, Locale.US);
                formatter.format("%1$4d [cpu %2$4d / usr %3$4d]", time / 1000000,
                        edtCPUTime / 1000000, edtUserTime / 1000000);
                System.out.println(sb.toString() + keyFormatter.toString());
                keyFormatter.close();
                formatter.close();
                total += time;

                if (toTime) {
                    getTimes(tabTitle, scenario.getName()).times.add(time / 1000000);
                }

                // System.out.println(tabTitle + " : " + scenario.getName());
                // System.out.println("\tTime: " + time / 1000000
                // + " ms, EDT CPU time: " + edtCPUTime / 1000000
                // + " ms, EDT user time: " + edtUserTime / 1000000
                // + " ms");
                // long heapSize = Runtime.getRuntime().totalMemory();
                // long heapFreeSize = Runtime.getRuntime().freeMemory();
                // int heapSizeKB = (int) (heapSize / 1024);
                // int takenHeapSizeKB = (int) ((heapSize - heapFreeSize) / 1024
                // );
                // System.out.println("\tHeap before GC: " + takenHeapSizeKB
                // + " out of " + heapSizeKB);
                //
                // System.gc();
                //
                // heapSize = Runtime.getRuntime().totalMemory();
                // heapFreeSize = Runtime.getRuntime().freeMemory();
                // heapSizeKB = (int) (heapSize / 1024);
                // takenHeapSizeKB = (int) ((heapSize - heapFreeSize) / 1024);
                // System.out.println("\tHeap after GC: " + takenHeapSizeKB
                // + " out of " + heapSizeKB);
            }
        }
        System.out.println("\n" + total / 1000000 + " total");
        startButton.setEnabled(true);
        cancelButton.setEnabled(false);
        System.out.println(">>>>>>>>>>>>>>>>>>>>> END >>>>>>>>>>>>>>>>>>>>>");
        System.out.println();
    }