Skip to content
Browse files

Moved things around to support testing on Android

  • Loading branch information...
1 parent d2c0b8c commit a24d4fe4536d1e10ae1a394931349ff1a28f5fcc @steveliles committed Apr 12, 2012
View
62 src/test/java/com/sjl/dsl4xml/performance/ComparativePerformanceMain.java
@@ -1,62 +0,0 @@
-package com.sjl.dsl4xml.performance;
-
-import java.io.*;
-
-import com.sjl.dsl4xml.performance.Statistics.Entry;
-import com.sjl.dsl4xml.performance.impl.*;
-
-public class ComparativePerformanceMain {
-
- public static void main(String... anArgs) throws Exception {
- int _concurrency = 8;
- int _iterations = 1000;
-
- ComparativeStatistics _stats = new ComparativeStatistics(
- collectStats(new DOMPerformanceTest(_concurrency, _iterations)),
- collectStats(new DOMXPathPerformanceTest(_concurrency, _iterations)),
- collectStats(new PullParserPerformanceTest(_concurrency, _iterations)),
- collectStats(new Dsl4XmlPerformanceTest(_concurrency, _iterations)),
- collectStats(new SJXPPerformanceTest(_concurrency, _iterations)),
- collectStats(new SAXParserPerformanceTest(_concurrency, _iterations))
- );
-
- String _chartHtml = _stats.createChartHtml();
-
- Streams.copy(
- new ByteArrayInputStream(_chartHtml.getBytes()),
- new FileOutputStream(new File(anArgs[0]))
- );
- }
-
- private static Statistics collectStats(PerformanceTest aTest) throws Exception {
- System.out.println(aTest.getParserName());
-
- aTest.prepareXml();
- aTest.oneTimeParserSetup();
-
- int _runs = 3;
- Statistics[] _stats = new Statistics[_runs];
- for (int i=0; i<_runs; i++) {
- _stats[i] = aTest.collectStatisticsForMultithreadedParsing();
- }
-
- Statistics _result = new Statistics(_stats[0].getName());
-
- for (int i=0; i<_stats[0].size(); i++) {
- int _threads = 0;
- double _docs = 0;
- double _time = 0;
-
- for (int j=0; j<_runs; j++) {
- Entry _e = _stats[j].getEntry(i);
- _threads = _e.getThreads();
- _docs += _e.getDocs();
- _time += _e.getElapsedNanos();
- }
-
- _result.add(_threads, _time/_runs, _docs/_runs);
- }
-
- return _result;
- }
-}
View
112 src/test/java/com/sjl/dsl4xml/performance/PerformanceTest.java
@@ -1,112 +0,0 @@
-package com.sjl.dsl4xml.performance;
-
-import java.io.*;
-import java.util.*;
-import java.util.concurrent.*;
-
-import org.junit.*;
-
-public abstract class PerformanceTest {
-
- private int maxConcurrency;
- private int iterationsPerThread;
- private byte[] xml;
-
- public PerformanceTest(int aMaxConcurrency, int anIterationsPerThread) {
- maxConcurrency = aMaxConcurrency;
- iterationsPerThread = anIterationsPerThread;
- }
-
- public PerformanceTest() {
- this(8, 1000);
- }
-
- protected abstract ReadingThread newReadingThread(CyclicBarrier aGate)
- throws Exception;
-
- protected abstract String getParserName();
-
- @Before
- public void oneTimeParserSetup() throws Exception {
- // may be overridden to perform one time setup of parser
- }
-
- @Before
- public void prepareXml() throws Exception {
- xml = Streams.readInputIntoByteArray(Tweets.class.getResourceAsStream("twitter-atom.xml"));
- }
-
- @Test
- public void testMultithreadedParsing() throws Exception {
- Statistics _stats = collectStatisticsForMultithreadedParsing();
- System.out.println(_stats);
- }
-
- public Statistics collectStatisticsForMultithreadedParsing() throws Exception {
- Statistics _stats = new Statistics(getParserName());
- waitForGC();
- for (int i=1; i<=maxConcurrency; i++) {
- long _start = System.nanoTime();
- testWithNThreads(i);
- long _stop = System.nanoTime();
-
- _stats.add(i, _stop-_start, i * iterationsPerThread);
- }
- return _stats;
- }
-
- private void waitForGC() {
- System.gc();
- try { Thread.sleep(200L); } catch (InterruptedException anExc){}
- }
-
- private void testWithNThreads(int aNumberOfThreads) throws Exception {
- List<Thread> _threads = new ArrayList<Thread>();
- CyclicBarrier _gate = new CyclicBarrier(aNumberOfThreads);
-
- for (int i=0; i<aNumberOfThreads; i++) {
- _threads.add(new Thread(newReadingThread(_gate)));
- }
-
- for (Thread _t : _threads) {
- _t.start();
- }
-
- for (Thread _t : _threads) {
- _t.join();
- }
- }
-
- public abstract class ReadingThread implements Runnable {
- private CyclicBarrier gate;
-
- public ReadingThread(CyclicBarrier aGate) {
- gate = aGate;
- }
-
- @Override
- public void run() {
- try {
- gate.await();
-
- for (int i=0; i<iterationsPerThread; i++) {
- InputStream _in = new ByteArrayInputStream(xml);
- Tweets _tw = read(_in);
-
- // do some simple correctness tests...
-
- if (_tw.size() != 15)
- throw new RuntimeException("Expected 15 tweets but read " + _tw.size());
-
- if (!"LowellSunSports (Lowell Sun Sports)".equals(_tw.get(14).getAuthor().getName()))
- throw new RuntimeException("Expected correct name, but got " + _tw.get(14).getAuthor().getName());
- }
- } catch (Exception anExc) {
- anExc.printStackTrace();
- System.err.println("Thread did not complete its batch");
- }
- }
-
- public abstract Tweets read(InputStream anInputStream);
- }
-}
View
32 src/test/java/com/sjl/dsl4xml/performance/chart-template.html
@@ -1,32 +0,0 @@
-<html>
- <head>
- <script type="text/javascript" src="https://www.google.com/jsapi"></script>
- <script type="text/javascript">
- google.load("visualization", "1", {packages:["corechart"]});
- google.setOnLoadCallback(drawChart);
- function drawChart() {
- var data = new google.visualization.DataTable();
- data.addColumn('number', 'Concurrency (threads)');
-
- ::column-defs::
-
- data.addRows([
- ::data::
- ]);
-
- var options = {
- title: 'Parsing Throughput (higher is better)',
- curveType: 'function',
- hAxis: { title: 'Concurrency (threads)' },
- vAxis: { title: 'Throughput (documents)' }
- };
-
- var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
- chart.draw(data, options);
- }
- </script>
- </head>
- <body>
- <div id="chart_div" style="width: 600px; height: 500px;"></div>
- </body>
-</html>

0 comments on commit a24d4fe

Please sign in to comment.
Something went wrong with that request. Please try again.