| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.text; |
| 18 |
|
| 19 |
import android.perftests.utils.BenchmarkState; |
| 20 |
import android.perftests.utils.PerfStatusReporter; |
| 21 |
|
| 22 |
import androidx.test.filters.LargeTest; |
| 23 |
import androidx.test.runner.AndroidJUnit4; |
| 24 |
|
| 25 |
import org.junit.Rule; |
| 26 |
import org.junit.Test; |
| 27 |
import org.junit.runner.RunWith; |
| 28 |
|
| 29 |
import java.util.function.Supplier; |
| 30 |
|
| 31 |
@RunWith(AndroidJUnit4.class) |
| 32 |
@LargeTest |
| 33 |
public class TextUtilsPerfTest { |
| 34 |
@Rule |
| 35 |
public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 36 |
|
| 37 |
public static final String TEMPLATE = "Template that combines %s and %d together"; |
| 38 |
|
| 39 |
public String mVar1 = "example"; |
| 40 |
public int mVar2 = 42; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
@Test |
| 46 |
public void timeFormatUpstream() { |
| 47 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 48 |
while (state.keepRunning()) { |
| 49 |
String res = String.format(TEMPLATE, mVar1, mVar2); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
@Test |
| 58 |
public void timeFormatLocal() { |
| 59 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 60 |
while (state.keepRunning()) { |
| 61 |
String res = TextUtils.formatSimple(TEMPLATE, mVar1, mVar2); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
@Test |
| 69 |
public void timeFormatInline() { |
| 70 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 71 |
while (state.keepRunning()) { |
| 72 |
String res = "Template that combines " + mVar1 + " and " + mVar2 + " together"; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
@Test |
| 81 |
public void timeFormat_Skip_Lambda() { |
| 82 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 83 |
while (state.keepRunning()) { |
| 84 |
requireNonNull(this, () -> { |
| 85 |
return String.format(TEMPLATE, mVar1, mVar2); |
| 86 |
}); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
@Test |
| 95 |
public void timeFormat_Skip_Varargs() { |
| 96 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 97 |
while (state.keepRunning()) { |
| 98 |
requireNonNull(this, TEMPLATE, mVar1, mVar2); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
private static <T> T requireNonNull(T obj, Supplier<String> messageSupplier) { |
| 103 |
return obj; |
| 104 |
} |
| 105 |
|
| 106 |
private static <T> T requireNonNull(T obj, String format, Object... args) { |
| 107 |
return obj; |
| 108 |
} |
| 109 |
} |