Skip to content

Commit

Permalink
Added per/run measurements
Browse files Browse the repository at this point in the history
  • Loading branch information
alt236 committed Jan 30, 2013
1 parent 7b9cda7 commit e1781f9
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 123 deletions.
276 changes: 153 additions & 123 deletions src/com/martinadamek/jsonandroid/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Long> results = new HashMap<String, Long>();

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<String> keys = new ArrayList<String>(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<Map> 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<String, Long> 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<Map> 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<String, ResultsContainer> results = new HashMap<String, ResultsContainer>();

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<String> keys = new ArrayList<String>(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<Map> 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<String, ResultsContainer> 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<Map> 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());
}

}
}

}
26 changes: 26 additions & 0 deletions src/com/martinadamek/jsonandroid/ResultsContainer.java
Original file line number Diff line number Diff line change
@@ -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;
}

}
28 changes: 28 additions & 0 deletions src/com/martinadamek/jsonandroid/StringUtils.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit e1781f9

Please sign in to comment.