mobench-sdk 0.1.39

Rust SDK for mobile benchmarking with timing harness and Android/iOS builders
Documentation
package {{PACKAGE_NAME}}

import android.util.Log
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.withId
import androidx.test.espresso.matcher.ViewMatchers.withText
import java.util.concurrent.TimeUnit
import org.hamcrest.Matchers.containsString
import org.junit.Assert.fail
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class MainActivityTest {

    private val benchmarkTimeoutMs = TimeUnit.MINUTES.toMillis(30)
    private val heartbeatMs = TimeUnit.SECONDS.toMillis(10)

    @get:Rule
    val activityRule = ActivityScenarioRule(MainActivity::class.java)

    @Test
    fun showsBenchOutput() {
        var timeoutMs = benchmarkTimeoutMs
        var pollMs = heartbeatMs
        activityRule.scenario.onActivity { activity ->
            timeoutMs = TimeUnit.SECONDS.toMillis(activity.benchmarkTimeoutSecs())
            pollMs = TimeUnit.SECONDS.toMillis(activity.heartbeatIntervalSecs()).coerceAtLeast(1000L)
        }

        val deadline = System.currentTimeMillis() + timeoutMs
        var completed = false
        var failed = false
        var failureJson: String? = null

        while (System.currentTimeMillis() < deadline) {
            activityRule.scenario.onActivity { activity ->
                activity.checkWorkerExit()
                completed = activity.isBenchmarkComplete()
                failed = activity.isBenchmarkFailed()
                failureJson = activity.getBenchmarkFailureJson()
            }
            if (completed || failed) {
                break
            }

            Log.i("BenchRunnerTest", "Waiting for benchmark output...")
            try {
                Thread.sleep(pollMs)
            } catch (e: InterruptedException) {
                Thread.currentThread().interrupt()
                fail("Interrupted while waiting for benchmark output")
            }
        }

        if (!completed && !failed) {
            activityRule.scenario.onActivity { activity ->
                failureJson = activity.emitTimeoutFailureFromTest()
                failed = true
            }
        }

        if (failed) {
            fail("Benchmark failed before BENCH_JSON: ${failureJson ?: "missing failure payload"}")
        }

        onView(withId(R.id.result_text))
            .check(matches(withText(containsString("Samples"))))
    }
}