Skip to content

Commit 9227fe9

Browse files
coachroebuckSloy
authored andcommitted
I actually want to display the performance test results on the device, and have the option to repeat those tests. (#8)
1 parent 1e3b636 commit 9227fe9

File tree

8 files changed

+45
-15
lines changed

8 files changed

+45
-15
lines changed

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ dependencies {
2626
implementation 'androidx.appcompat:appcompat:1.0.2'
2727
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
2828
implementation 'com.google.android.material:material:1.0.0'
29+
implementation "androidx.core:core-ktx:1.2.0-alpha01"
30+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1"
31+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.1"
2932

3033
// Koin
3134
implementation "org.koin:koin-android:2.0.0-alpha-3"

app/src/main/java/com/sloydev/dependencyinjectionperformance/InjectionTest.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ import javax.inject.Inject
2828

2929
class InjectionTest : KoinComponent {
3030

31+
private lateinit var callback: (String) -> Unit
3132
private val kotlinDaggerTest = KotlinDaggerTest()
3233
private val javaDaggerTest = JavaDaggerTest()
3334

3435
private val rounds = 100
3536

36-
fun runTests() {
37+
fun runTests(callback: (String) -> Unit) {
38+
this.callback = callback
3739
val results = listOf(
3840
koinTest(),
3941
kodeinTest(),
@@ -45,15 +47,19 @@ class InjectionTest : KoinComponent {
4547
}
4648

4749
private fun reportMarkdown(results: List<LibraryResult>) {
48-
log("Done!")
49-
log(" ")
50-
log("${Build.BRAND} ${Build.DEVICE} with Android ${Build.VERSION.RELEASE}")
51-
log(" ")
52-
log("Library | Setup Kotlin | Setup Java | Inject Kotlin | Inject Java")
53-
log("--- | ---:| ---:| ---:| ---:")
50+
val sb = StringBuilder()
51+
log("Done!\n")
52+
sb.append("\n")
53+
sb.append("${Build.BRAND} ${Build.DEVICE} with Android ${Build.VERSION.RELEASE}\n")
54+
sb.append("\n")
55+
sb.append("Library | Setup Kotlin | Setup Java | Inject Kotlin | Inject Java\n")
56+
sb.append("--- | ---:| ---:| ---:| ---:\n")
5457
results.forEach {
55-
log("**${it.injectorName}** | ${it[Variant.KOTLIN].startupTime.median().format()} | ${it[Variant.JAVA].startupTime.median().format()} | ${it[Variant.KOTLIN].injectionTime.median().format()} | ${it[Variant.JAVA].injectionTime.median().format()}")
58+
sb.append("**${it.injectorName}** | ${it[Variant.KOTLIN].startupTime.median().format()} | ${it[Variant.JAVA].startupTime.median().format()} | ${it[Variant.KOTLIN].injectionTime.median().format()} | ${it[Variant.JAVA].injectionTime.median().format()}\n")
5659
}
60+
61+
log(sb.toString())
62+
this.callback.invoke(sb.toString())
5763
}
5864

5965
private fun runTest(

app/src/main/java/com/sloydev/dependencyinjectionperformance/MainActivity.kt

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,38 @@ import android.os.Bundle
44
import androidx.appcompat.app.AppCompatActivity
55
import com.google.android.material.snackbar.Snackbar
66
import kotlinx.android.synthetic.main.activity_main.*
7+
import kotlinx.android.synthetic.main.content_main.*
8+
import kotlinx.coroutines.Deferred
9+
import kotlinx.coroutines.GlobalScope
10+
import kotlinx.coroutines.async
711

812
class MainActivity : AppCompatActivity() {
913

14+
private lateinit var job: Deferred<Unit>
15+
1016
override fun onCreate(savedInstanceState: Bundle?) {
1117
super.onCreate(savedInstanceState)
1218
setContentView(R.layout.activity_main)
1319
setSupportActionBar(toolbar)
1420

1521
fab.setOnClickListener { view ->
16-
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
17-
.setAction("Action", null).show()
22+
runTests()
1823
}
1924

20-
InjectionTest().runTests()
25+
runTests()
26+
}
27+
28+
private fun runTests() {
29+
runOnUiThread {
30+
textView.text = getString(R.string.running_tests)
31+
}
32+
this.job = GlobalScope.async {
33+
InjectionTest().runTests {
34+
runOnUiThread {
35+
textView.text = it
36+
}
37+
}
38+
}
2139
}
2240

2341
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@
2929
android:layout_height="wrap_content"
3030
android:layout_gravity="bottom|end"
3131
android:layout_margin="@dimen/fab_margin"
32-
app:srcCompat="@android:drawable/ic_dialog_email"/>
32+
app:srcCompat="@android:drawable/ic_media_play"/>
3333

3434
</androidx.coordinatorlayout.widget.CoordinatorLayout>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
tools:context=".MainActivity">
1111

1212
<TextView
13+
android:id="@+id/textView"
1314
android:layout_width="wrap_content"
1415
android:layout_height="wrap_content"
1516
android:text="Hello World!"

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<resources>
22
<string name="app_name">Dependency Injection Performance</string>
33
<string name="action_settings">Settings</string>
4+
<string name="running_tests">Running tests...</string>
45
</resources>

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
buildscript {
2-
ext.kotlin_version = '1.3.11'
2+
ext.kotlin_version = '1.3.31'
33
repositories {
44
google()
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:3.2.1'
8+
classpath 'com.android.tools.build:gradle:3.4.1'
99
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1010
}
1111
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Thu May 02 15:26:21 MDT 2019
12
distributionBase=GRADLE_USER_HOME
23
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-5.1.1-all.zip

0 commit comments

Comments
 (0)