Alexei
Alexei

Reputation: 15656

Android. How test duration execution of method?

I write unit test (JUnit, Espresso) for Android app.

And I want to test the duration of execution of any method in Activity. And my test must be fail if duration more than 1 second.

Is it possible in Android?

Upvotes: 0

Views: 688

Answers (3)

shadox
shadox

Reputation: 3729

Yes, you can do that although it's hard to predict actual execution time as it depends on many factors. With JUnit you could do something like:

@Test(timeout=1000) //milliseconds
public void performanceTest() {
    //my code
}

This test will fail if not executed under 1000ms and will pass for any other value.

With espresso test I believe you could adjust timeout rules but I don't see how that helps you in any way. UI test my depend on many things, how long it takes to "press" buttons, network response, other threads that have their own time management and depend on other factors. In any case check this:

Android Doc

Upvotes: 1

from56
from56

Reputation: 4127

long startTime = System.currentTimeMillis();

// here code to be time measured

Log.i ("Execution time ", "milliseconds : " + System.currentTimeMillis() - startTime);

Upvotes: 0

Kuva
Kuva

Reputation: 2437

Here's an example:

 private long durationTest() {
    long timeBeforeExecution = System.currentTimeMillis();

    someMethod();

    return System.currentTimeMillis() - timeBeforeExecution;  // difference in millis
}

private void someMethod() {
    ...
}

Upvotes: 1

Related Questions