diff --git a/src/com/martinadamek/jsonandroid/MainActivity.java b/src/com/martinadamek/jsonandroid/MainActivity.java index 4a0c774..1c17187 100644 --- a/src/com/martinadamek/jsonandroid/MainActivity.java +++ b/src/com/martinadamek/jsonandroid/MainActivity.java @@ -12,128 +12,158 @@ import android.widget.TextView; public class MainActivity extends Activity { - - private static final String TAG = MainActivity.class.getName(); - private String mPath; - private TextView mTextView; - - private final Runnable mTestTask = new Runnable() { - public void run() { - - final Map results = new HashMap(); - - testImpl(new AndroidJson(), results); - testImpl(new SimpleJson(), results); - testImpl(new SmartJson(), results); - testImpl(new GsonJson(), results); - testImpl(new JacksonJson(), results); - - runOnUiThread(new Runnable() { - public void run() { - writeToTextView("== Done!"); - writeToTextView("\n"); - - List keys = new ArrayList(results.keySet()); - Collections.sort(keys); - - for (String key: keys) { - writeToTextView(padRight(key, 12) + ": " + results.get(key) + "ms"); - } - } - }); - - } - }; - - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.main); - - mTextView = (TextView) findViewById(R.id.text); - - mPath = "com/martinadamek/jsonandroid/public_timeline.json"; - - mTextView.setText("Running tests..."); - writeToTextView("-----------------"); - - new Thread(mTestTask).start(); - } - - private long test(final TestJson testJson, int repeats) { - InputStream inputStream = getClass().getClassLoader().getResourceAsStream(mPath); - - List result = testJson.parsePublicTimeline(inputStream); - verify(result); - - long duration = 0; - - for (int i = 0; i < repeats; i++) { - inputStream = getClass().getClassLoader().getResourceAsStream(mPath); - long start = System.currentTimeMillis(); - testJson.parsePublicTimeline(inputStream); - duration += (System.currentTimeMillis() - start); - } - - return duration; - } - - private void testImpl(final TestJson testJson, Map results) { - runOnUiThread(new Runnable() { - public void run() { - writeToTextView("== Running tests for '" + testJson.getName() + "'"); - } - }); - - warmUp(testJson); - - long duration = test(testJson, 1); - results.put("[1 run] " + testJson.getName(), duration); - duration = test(testJson, 5); - results.put("[5 runs] " + testJson.getName(), duration); - duration = test(testJson, 100); - results.put("[100 runs] " + testJson.getName(), duration); - } - - private void warmUp(final TestJson testJson) { - InputStream inputStream; - for (int i = 0; i < 5; i++) { - inputStream = getClass().getClassLoader().getResourceAsStream(mPath); - testJson.parsePublicTimeline(inputStream); - } - } - - private void writeToTextView(String text){ - mTextView.append("\n"); - mTextView.append(text); - } - - private static String map2json(Map map) { - StringBuilder sb = new StringBuilder("{"); - - for (Object key: map.keySet()) { - sb.append('"').append(key).append('"').append(':').append('"').append(map.get(key)).append('"').append(","); - } - - sb.append("}"); - return sb.toString(); - } - - private static String padRight(String s, int n) { - return String.format("%1$-" + n + "s", s); - } - - private static void verify(List result) { - if (result.size() != 20) { - throw new IllegalStateException("Expected 20 but was " + result.size()); - } - for (Map map: result) { - if (map.size() != 52) { - throw new IllegalStateException("Expected 52 but was " + result.size()); - } - - } - } + private static final String DATA_LINE_PADDING = " "; + + private static final String TAG = MainActivity.class.getName(); + private String mPath; + private TextView mTextView; + + private final Runnable mTestTask = new Runnable() { + public void run() { + + final Map results = new HashMap(); + + testImpl(new AndroidJson(), results); + testImpl(new SimpleJson(), results); + testImpl(new SmartJson(), results); + testImpl(new GsonJson(), results); + testImpl(new JacksonJson(), results); + + runOnUiThread(new Runnable() { + public void run() { + writeToTextView("== Done!"); + + List keys = new ArrayList(results.keySet()); + Collections.sort(keys); + + + int minKeyLength = 0; + for (String key: keys) { + int length = String.valueOf(results.get(key).getParserName()).length(); + + if(length > minKeyLength){ + minKeyLength = length; + } + } + + int minValueLength = 0; + for (String key: keys) { + int length = String.valueOf(results.get(key).getDuration()).length(); + + if(length > minValueLength){ + minValueLength = length; + } + } + + ResultsContainer result; + final String label = "Runs: "; + + int runs = -1; + for (String key: keys) { + result = results.get(key); + + if(runs != result.getTestRepeats()){ + writeToTextView( + "\n" + + label + result.getTestRepeats()); + + runs = result.getTestRepeats(); + } + + writeToTextView( + DATA_LINE_PADDING + + StringUtils.padRight(result.getParserName(), minKeyLength) + + ": " + + StringUtils.padLeft(String.valueOf(result.getDuration()), minValueLength) + + "ms"); + + writeToTextView(DATA_LINE_PADDING + StringUtils.padRight(" ", minKeyLength) + " " + (result.getDuration() / result.getTestRepeats()) + "/run"); + } + } + }); + + } + }; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + setContentView(R.layout.main); + + mTextView = (TextView) findViewById(R.id.text); + + mPath = "com/martinadamek/jsonandroid/public_timeline.json"; + + mTextView.setText("Running tests..."); + writeToTextView("-----------------"); + + new Thread(mTestTask).start(); + } + + private long test(final TestJson testJson, int repeats) { + InputStream inputStream = getClass().getClassLoader().getResourceAsStream(mPath); + + List result = testJson.parsePublicTimeline(inputStream); + verify(result); + + long duration = 0; + + for (int i = 0; i < repeats; i++) { + inputStream = getClass().getClassLoader().getResourceAsStream(mPath); + long start = System.currentTimeMillis(); + testJson.parsePublicTimeline(inputStream); + duration += (System.currentTimeMillis() - start); + } + + return duration; + } + + private void testImpl(final TestJson testJson, Map results) { + runOnUiThread(new Runnable() { + public void run() { + writeToTextView("== Testing '" + testJson.getName() + "'"); + } + }); + + warmUp(testJson); + + int runs = 1; + long duration = test(testJson, runs); + results.put(StringUtils.padLeft(runs, 5) + "_" + testJson.getName(), new ResultsContainer(testJson.getName(), duration, runs)); + + runs = 5; + duration = test(testJson, runs); + results.put(StringUtils.padLeft(runs, 5) + "_" + testJson.getName(), new ResultsContainer(testJson.getName(), duration, runs)); + + runs = 100; + duration = test(testJson, runs); + results.put(StringUtils.padLeft(runs, 5) + "_" + testJson.getName(), new ResultsContainer(testJson.getName(), duration, runs)); + } + + private void warmUp(final TestJson testJson) { + InputStream inputStream; + for (int i = 0; i < 5; i++) { + inputStream = getClass().getClassLoader().getResourceAsStream(mPath); + testJson.parsePublicTimeline(inputStream); + } + } + + private void writeToTextView(String text){ + mTextView.append("\n"); + mTextView.append(text); + } + + private static void verify(List result) { + if (result.size() != 20) { + throw new IllegalStateException("Expected 20 but was " + result.size()); + } + for (Map map: result) { + if (map.size() != 52) { + throw new IllegalStateException("Expected 52 but was " + result.size()); + } + + } + } } diff --git a/src/com/martinadamek/jsonandroid/ResultsContainer.java b/src/com/martinadamek/jsonandroid/ResultsContainer.java new file mode 100644 index 0000000..5cf769a --- /dev/null +++ b/src/com/martinadamek/jsonandroid/ResultsContainer.java @@ -0,0 +1,26 @@ +package com.martinadamek.jsonandroid; + +public class ResultsContainer { + private final long mDuration; + private final int mTestRepeats; + private final String mParserName; + + public long getDuration() { + return mDuration; + } + + public int getTestRepeats() { + return mTestRepeats; + } + + public String getParserName() { + return mParserName; + } + + public ResultsContainer(String parserName, long duration, int testRepeats){ + mParserName = parserName; + mDuration = duration; + mTestRepeats = testRepeats; + } + +} diff --git a/src/com/martinadamek/jsonandroid/StringUtils.java b/src/com/martinadamek/jsonandroid/StringUtils.java new file mode 100644 index 0000000..e7f9035 --- /dev/null +++ b/src/com/martinadamek/jsonandroid/StringUtils.java @@ -0,0 +1,28 @@ +package com.martinadamek.jsonandroid; + +import java.util.Map; + +public class StringUtils { + public static String map2json(Map map) { + StringBuilder sb = new StringBuilder("{"); + + for (Object key: map.keySet()) { + sb.append('"').append(key).append('"').append(':').append('"').append(map.get(key)).append('"').append(","); + } + + sb.append("}"); + return sb.toString(); + } + + public static String padLeft(String s, int n) { + return String.format("%1$" + n + "s", s); + } + + public static String padRight(String s, int n) { + return String.format("%1$-" + n + "s", s); + } + + public static String padLeft(long l, int n){ + return String.format("%0"+n+"d", l); + } +}