| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.util; |
| 18 |
|
| 19 |
import android.perftests.utils.BenchmarkState; |
| 20 |
import android.perftests.utils.PerfStatusReporter; |
| 21 |
|
| 22 |
import androidx.test.filters.LargeTest; |
| 23 |
|
| 24 |
import dalvik.system.VMRuntime; |
| 25 |
|
| 26 |
import org.junit.Rule; |
| 27 |
import org.junit.Test; |
| 28 |
import org.junit.runner.RunWith; |
| 29 |
import org.junit.runners.Parameterized; |
| 30 |
|
| 31 |
import java.nio.charset.StandardCharsets; |
| 32 |
import java.util.Arrays; |
| 33 |
import java.util.Collection; |
| 34 |
|
| 35 |
@LargeTest |
| 36 |
@RunWith(Parameterized.class) |
| 37 |
public class CharsetUtilsPerfTest { |
| 38 |
@Rule |
| 39 |
public PerfStatusReporter mPerfStatusReporter = new PerfStatusReporter(); |
| 40 |
|
| 41 |
@Parameterized.Parameter(0) |
| 42 |
public String mName; |
| 43 |
@Parameterized.Parameter(1) |
| 44 |
public String mValue; |
| 45 |
|
| 46 |
@Parameterized.Parameters(name = "{0}") |
| 47 |
public static Collection<Object[]> getParameters() { |
| 48 |
return Arrays.asList(new Object[][] { |
| 49 |
{ "simple", "com.example.typical_package_name" }, |
| 50 |
{ "complex", "從不喜歡孤單一個 - 蘇永康/吳雨霏" }, |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void timeUpstream() { |
| 56 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 57 |
while (state.keepRunning()) { |
| 58 |
mValue.getBytes(StandardCharsets.UTF_8); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
@Test |
| 67 |
public void timeLocal_SmallBuffer() { |
| 68 |
final byte[] dest = (byte[]) VMRuntime.getRuntime().newNonMovableArray(byte.class, 64); |
| 69 |
final long destPtr = VMRuntime.getRuntime().addressOf(dest); |
| 70 |
|
| 71 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 72 |
while (state.keepRunning()) { |
| 73 |
CharsetUtils.toModifiedUtf8Bytes(mValue, destPtr, 0, dest.length); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
@Test |
| 82 |
public void timeLocal_LargeBuffer() { |
| 83 |
final byte[] dest = (byte[]) VMRuntime.getRuntime().newNonMovableArray(byte.class, 1024); |
| 84 |
final long destPtr = VMRuntime.getRuntime().addressOf(dest); |
| 85 |
|
| 86 |
final BenchmarkState state = mPerfStatusReporter.getBenchmarkState(); |
| 87 |
while (state.keepRunning()) { |
| 88 |
CharsetUtils.toModifiedUtf8Bytes(mValue, destPtr, 0, dest.length); |
| 89 |
} |
| 90 |
} |
| 91 |
} |