Skip to content

Commit 06a6df2

Browse files
committed
Add support for ordering results of Benchmarks. Update sample
1 parent 32d1f82 commit 06a6df2

File tree

12 files changed

+116
-16
lines changed

12 files changed

+116
-16
lines changed

app/src/main/AndroidManifest.xml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<uses-permission android:name="android.permission.READ_LOGS"/>
66

77
<application
8+
android:name=".App"
89
android:allowBackup="true"
910
android:icon="@mipmap/ic_launcher"
1011
android:label="@string/app_name"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.tspoon.benchit.sample;
2+
3+
import android.app.Application;
4+
5+
import timber.log.Timber;
6+
7+
public class App extends Application {
8+
9+
@Override
10+
public void onCreate() {
11+
super.onCreate();
12+
Timber.plant(new Timber.DebugTree());
13+
}
14+
}

app/src/main/java/com/tspoon/benchit/sample/comparisons/ArrayListComparison.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.tspoon.benchit.sample.comparisons;
22

3+
import com.tspoon.benchit.Benchit;
34
import com.tspoon.benchit.sample.data.Data;
45

56
import java.util.ArrayList;
@@ -15,6 +16,11 @@ public void setup() {
1516
mBenchmarks.add(new BenchmarkArrayListFast());
1617
}
1718

19+
@Override
20+
public Benchit.Precision getPrecision() {
21+
return Benchit.Precision.MILLI;
22+
}
23+
1824
class BenchmarkArrayListNaive extends Benchmark {
1925

2026
private ArrayList<Integer> mData = Data.getInstance().getSampleArrayList();

app/src/main/java/com/tspoon/benchit/sample/comparisons/Benchmark.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ public void setup() {
1414
mResults = new ArrayList<>();
1515
}
1616

17-
public final void runBenchmarks() {
17+
public final void runBenchmarks(Benchit.Precision precision) {
1818
String name = getBenchmarkName();
1919
for (int i = 0; i < Config.NUM_TESTS; i++) {
2020
Benchit.begin(name);
2121
benchmark();
22-
Benchit.end(name);
22+
Benchit.end(name).precision(precision);
2323
}
2424
mResults.add(Benchit.analyze(name).log());
2525
}

app/src/main/java/com/tspoon/benchit/sample/comparisons/Comparison.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.tspoon.benchit.sample.comparisons;
22

3+
import com.tspoon.benchit.Benchit;
4+
35
import java.util.List;
46

57
import timber.log.Timber;
@@ -14,12 +16,16 @@ public final void runComparisons() {
1416
Timber.d("Begin Comparison: " + getName());
1517
for (Benchmark benchmark : mBenchmarks) {
1618
benchmark.setup();
17-
benchmark.runBenchmarks();
19+
benchmark.runBenchmarks(getPrecision());
1820
}
1921
Timber.d("End Comparison: " + getName());
22+
Benchit.compare(Benchit.Stat.STANDARD_DEVIATION).log();
23+
Benchit.clear();
2024
}
2125

2226
public String getName() {
2327
return getClass().getSimpleName();
2428
}
29+
30+
public abstract Benchit.Precision getPrecision();
2531
}

app/src/main/java/com/tspoon/benchit/sample/comparisons/InternalGetterComparison.java

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.tspoon.benchit.sample.comparisons;
22

3+
import com.tspoon.benchit.Benchit;
4+
35
import java.util.ArrayList;
46

57
public class InternalGetterComparison extends Comparison {
@@ -13,6 +15,11 @@ public void setup() {
1315
mBenchmarks.add(new BenchmarkInternalGetter());
1416
}
1517

18+
@Override
19+
public Benchit.Precision getPrecision() {
20+
return Benchit.Precision.MICRO;
21+
}
22+
1623

1724
private class BenchmarkInternalAccess extends Benchmark {
1825

app/src/main/res/layout/activity_sample.xml

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<LinearLayout
22
xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:app="http://schemas.android.com/apk/res-auto"
43
android:layout_width="match_parent"
54
android:layout_height="match_parent"
65
android:orientation="vertical">
@@ -9,7 +8,7 @@
98
android:id="@+id/toolbar"
109
android:layout_width="match_parent"
1110
android:layout_height="wrap_content"
12-
app:theme="@style/AppTheme.Toolbar"/>
11+
android:theme="@style/AppTheme.Toolbar"/>
1312

1413
<ListView
1514
android:id="@+id/benchmarks_list"

app/src/main/res/values/styles.xml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<item name="android:listSelector">@android:color/transparent</item>
2828
<item name="android:dividerHeight">8dp</item>
2929
<item name="android:divider">@null</item>
30+
<item name="android:scrollbars">none</item>
3031
</style>
3132

3233
<style name="Benchit.ListView.ListItem" parent="android:Widget">

library/src/main/java/com/tspoon/benchit/Benchit.java

+25-6
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,15 @@ public static Benchit clear() {
6363
return singleton.clearInternal();
6464
}
6565

66-
public static ComparisonResult compare(Stat orderBy, String... tags) {
66+
public static ComparisonResult compare(Stat orderBy, Order order, String... tags) {
6767
if (singleton == null) {
6868
singleton = get();
6969
}
70-
return singleton.compareInternal(orderBy, tags);
70+
return singleton.compareInternal(orderBy, order, tags);
71+
}
72+
73+
public static ComparisonResult compare(Stat orderBy, String... tags) {
74+
return compare(orderBy, Order.ASCENDING, tags);
7175
}
7276

7377
public static void setDefaultPrecision(Precision precision) {
@@ -112,13 +116,24 @@ private Benchit clearInternal() {
112116
return this;
113117
}
114118

115-
private ComparisonResult compareInternal(Stat orderBy, String... tags) {
119+
private ComparisonResult compareInternal(Stat orderBy, Order order, String... tags) {
116120
ArrayList<Result> results = new ArrayList<>();
117-
for (String tag : tags) {
118-
results.add(analyzeInternal(tag));
121+
122+
if (tags.length == 0) {
123+
for (String tag : benchmarks.keySet()) {
124+
results.add(analyzeInternal(tag));
125+
}
126+
} else {
127+
for (String tag : tags) {
128+
results.add(analyzeInternal(tag));
129+
}
119130
}
120131

121-
return new ComparisonResult(results);
132+
return new ComparisonResult(orderBy, order, results);
133+
}
134+
135+
static void log(String message) {
136+
Log.d(TAG, message);
122137
}
123138

124139
static void log(String type, String tag, String result) {
@@ -141,6 +156,10 @@ public static enum Stat {
141156
AVERAGE, RANGE, STANDARD_DEVIATION
142157
}
143158

159+
public static enum Order {
160+
ASCENDING, DESCENDING
161+
}
162+
144163
public static enum Precision {
145164
NANO("ns", 1),
146165
MICRO("µs", 1000),

library/src/main/java/com/tspoon/benchit/Benchmark.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class Benchmark {
1111
this.tag = tag;
1212
times = new ArrayList<>();
1313
times.add(time);
14-
precision = Benchit.Precision.MILLI;
14+
precision = Benchit.DEFAULT_PRECISION;
1515
}
1616

1717
public Benchmark precision(Benchit.Precision precision) {
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,46 @@
11
package com.tspoon.benchit;
22

3+
import java.util.Collections;
4+
import java.util.Comparator;
35
import java.util.List;
46

57
public class ComparisonResult {
68

79
List<Result> results;
10+
Benchit.Stat orderBy;
11+
Benchit.Order order;
812

9-
public ComparisonResult(List<Result> results) {
13+
public ComparisonResult(Benchit.Stat orderBy, Benchit.Order order, List<Result> results) {
14+
this.orderBy = orderBy;
15+
this.order = order;
1016
this.results = results;
17+
18+
sort();
19+
}
20+
21+
private void sort() {
22+
Collections.sort(results, new Comparator<Result>() {
23+
@Override
24+
public int compare(Result lhs, Result rhs) {
25+
26+
double statOne = lhs.getStat(orderBy);
27+
double statTwo = rhs.getStat(orderBy);
28+
if (order == Benchit.Order.ASCENDING) {
29+
return Double.compare(statOne, statTwo);
30+
} else {
31+
return Double.compare(statTwo, statOne);
32+
}
33+
}
34+
});
35+
}
36+
37+
public void log() {
38+
Benchit.log("------");
39+
Benchit.log("Comparison Results: Number of Benchmarks[" + results.size() + "], Ordered By[" + orderBy.name() + " " + order.name() + "]");
40+
Benchit.log("------");
41+
for (Result result : results) {
42+
result.log();
43+
}
44+
Benchit.log("------");
1145
}
1246
}

library/src/main/java/com/tspoon/benchit/Result.java

+16-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Result {
3131
max = minMax[1];
3232
}
3333

34-
double average() {
34+
private double average() {
3535
int size = times.size();
3636
long total = 0;
3737
for (int i = 0; i < size; i++) {
@@ -40,7 +40,7 @@ public class Result {
4040
return total / (double) size;
4141
}
4242

43-
long[] range() {
43+
private long[] range() {
4444
int size = times.size();
4545
long min = times.get(0);
4646
long max = times.get(0);
@@ -56,7 +56,7 @@ long[] range() {
5656
return new long[]{min, max};
5757
}
5858

59-
double deviation() {
59+
private double deviation() {
6060
double mean = average();
6161
double temp = 0;
6262

@@ -68,6 +68,19 @@ long[] range() {
6868
return Math.sqrt(temp / size);
6969
}
7070

71+
double getStat(Benchit.Stat stat) {
72+
switch (stat) {
73+
case AVERAGE:
74+
return average;
75+
case STANDARD_DEVIATION:
76+
return deviation;
77+
case RANGE:
78+
return max - min;
79+
default:
80+
throw new IllegalArgumentException("Stat not implemented yet: " + stat);
81+
}
82+
}
83+
7184
public Result log() {
7285
List<Pair<String, String>> stats = new ArrayList<>();
7386

0 commit comments

Comments
 (0)