Nhiều ứng dụng sử dụng Hilt để chèn các hành vi khác nhau vào các biến thể bản dựng khác nhau. Điều này đặc biệt hữu ích khi bạn đo điểm chuẩn vi mô cho ứng dụng vì nó cho phép bạn thay thế một thành phần có thể làm sai lệch kết quả. Ví dụ: đoạn mã sau đây cho thấy một kho lưu trữ tìm nạp và sắp xếp danh sách tên:
Kotlin
class PeopleRepository @Inject constructor( @Kotlin private val dataSource: NetworkDataSource, @Dispatcher(DispatcherEnum.IO) private val dispatcher: CoroutineDispatcher ) { private val _peopleLiveData = MutableLiveData<List<Person>>() val peopleLiveData: LiveData<List<Person>> get() = _peopleLiveData suspend fun update() { withContext(dispatcher) { _peopleLiveData.postValue( dataSource.getPeople() .sortedWith(compareBy({ it.lastName }, { it.firstName })) ) } } }}
Java
public class PeopleRepository { private final MutableLiveData<List<Person>> peopleLiveData = new MutableLiveData<>(); private final NetworkDataSource dataSource; public LiveData<List<Person>> getPeopleLiveData() { return peopleLiveData; } @Inject public PeopleRepository(NetworkDataSource dataSource) { this.dataSource = dataSource; } private final Comparator<Person> comparator = Comparator.comparing(Person::getLastName) .thenComparing(Person::getFirstName); public void update() { Runnable task = new Runnable() { @Override public void run() { peopleLiveData.postValue( dataSource.getPeople() .stream() .sorted(comparator) .collect(Collectors.toList()) ); } }; new Thread(task).start(); } }
Nếu bạn đưa một lệnh gọi mạng vào khi đo điểm chuẩn, hãy triển khai một lệnh gọi mạng giả để có kết quả chính xác hơn.
Việc đưa một lệnh gọi mạng thực vào khi đo điểm chuẩn sẽ khiến bạn khó diễn giải kết quả đo điểm chuẩn hơn. Các lệnh gọi mạng có thể chịu ảnh hưởng của nhiều yếu tố bên ngoài và thời lượng của các lệnh gọi này có thể thay đổi giữa các lần lặp lại khi chạy phép đo điểm chuẩn. Thời lượng của các lệnh gọi mạng có thể lâu hơn thời gian sắp xếp.
Triển khai một lệnh gọi mạng giả bằng Hilt
Lệnh gọi đến dataSource.getPeople()
, như trong ví dụ trước, có chứa một lệnh gọi mạng. Tuy nhiên, phiên bản NetworkDataSource
được Hilt chèn và bạn có thể thay thế phiên bản này bằng cách triển khai giả sau để đo điểm chuẩn:
Kotlin
class FakeNetworkDataSource @Inject constructor( private val people: List<Person> ) : NetworkDataSource { override fun getPeople(): List<Person> = people }
Java
public class FakeNetworkDataSource implements NetworkDataSource{ private List<Person> people; @Inject public FakeNetworkDataSource(List<Person> people) { this.people = people; } @Override public List<Person> getPeople() { return people; } }
Lệnh gọi mạng giả này được thiết kế để chạy nhanh nhất có thể khi bạn gọi phương thức getPeople()
. Để Hilt có thể chèn thành phần này, nhà cung cấp sau đây sẽ được dùng:
Kotlin
@Module @InstallIn(SingletonComponent::class) object FakekNetworkModule { @Provides @Kotlin fun provideNetworkDataSource(@ApplicationContext context: Context): NetworkDataSource { val data = context.assets.open("fakedata.json").use { inputStream -> val bytes = ByteArray(inputStream.available()) inputStream.read(bytes) val gson = Gson() val type: Type = object : TypeToken<List<Person>>() {}.type gson.fromJson<List<Person>>(String(bytes), type) } return FakeNetworkDataSource(data) } }
Java
@Module @InstallIn(SingletonComponent.class) public class FakeNetworkModule { @Provides @Java NetworkDataSource provideNetworkDataSource( @ApplicationContext Context context ) { List<Person> data = new ArrayList<>(); try (InputStream inputStream = context.getAssets().open("fakedata.json")) { int size = inputStream.available(); byte[] bytes = new byte[size]; if (inputStream.read(bytes) == size) { Gson gson = new Gson(); Type type = new TypeToken<ArrayList<Person>>() { }.getType(); data = gson.fromJson(new String(bytes), type); } } catch (IOException e) { // Do something } return new FakeNetworkDataSource(data); } }
Dữ liệu được tải từ các tài sản bằng cách sử dụng lệnh gọi I/O có độ dài có thể thay đổi.
Tuy nhiên, việc này được thực hiện trong quá trình khởi chạy và sẽ không gây ra bất kỳ điểm bất thường nào khi getPeople()
được gọi trong quá trình đo điểm chuẩn.
Một số ứng dụng đã sử dụng dữ liệu giả trên các bản gỡ lỗi để loại bỏ mọi phần phụ thuộc vào phần phụ trợ. Tuy nhiên, bạn cần đo điểm chuẩn trên một bản dựng càng gần với bản dựng phát hành càng tốt. Phần còn lại của tài liệu này sử dụng cấu trúc nhiều mô-đun, nhiều biến thể như mô tả trong phần Thiết lập dự án đầy đủ.
Có 3 mô-đun:
benchmarkable
: chứa mã để đo điểm chuẩn.benchmark
: chứa mã đo điểm chuẩn.app
: chứa mã ứng dụng còn lại.
Mỗi mô-đun nêu trên đều có một biến thể bản dựng có tên là benchmark
cùng với các biến thể debug
và release
thông thường.
Định cấu hình mô-đun đo điểm chuẩn
Mã cho lệnh gọi mạng giả nằm trong nhóm tài nguyên debug
của mô-đun benchmarkable
và quá trình triển khai mạng đầy đủ nằm trong nhóm tài nguyên release
của cùng một mô-đun. Tệp tài sản chứa dữ liệu do quá trình triển khai giả trả về nằm trong bộ nguồn debug
để tránh mọi sự phình to APK trong bản dựng release
. Biến thể benchmark
cần dựa trên release
và sử dụng nhóm tài nguyên debug
. Cấu hình bản dựng cho biến thể benchmark
của mô-đun benchmarkable
chứa phương thức triển khai giả như sau:
Kotlin
android { ... buildTypes { release { isMinifyEnabled = false proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } create("benchmark") { initWith(getByName("release")) } } ... sourceSets { getByName("benchmark") { java.setSrcDirs(listOf("src/debug/java")) assets.setSrcDirs(listOf("src/debug/assets")) } } }
Groovy
android { ... buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' ) } benchmark { initWith release } } ... sourceSets { benchmark { java.setSrcDirs ['src/debug/java'] assets.setSrcDirs(listOf ['src/debug/assets'] } } }
Trong mô-đun benchmark
, hãy thêm một trình chạy kiểm thử tuỳ chỉnh tạo ra một Application
để các kiểm thử chạy trong đó hỗ trợ Hilt như sau:
Kotlin
class HiltBenchmarkRunner : AndroidBenchmarkRunner() { override fun newApplication( cl: ClassLoader?, className: String?, context: Context? ): Application { return super.newApplication(cl, HiltTestApplication::class.java.name, context) } }
Java
public class JavaHiltBenchmarkRunner extends AndroidBenchmarkRunner { @Override public Application newApplication( ClassLoader cl, String className, Context context ) throws InstantiationException, IllegalAccessException, ClassNotFoundException { return super.newApplication(cl, HiltTestApplication.class.getName(), context); } }
Điều này giúp đối tượng Application
mà các kiểm thử được chạy sẽ mở rộng lớp HiltTestApplication
. Thực hiện các thay đổi sau đối với cấu hình bản dựng:
Kotlin
plugins { alias(libs.plugins.android.library) alias(libs.plugins.benchmark) alias(libs.plugins.jetbrains.kotlin.android) alias(libs.plugins.kapt) alias(libs.plugins.hilt) } android { namespace = "com.example.hiltmicrobenchmark.benchmark" compileSdk = 34 defaultConfig { minSdk = 24 testInstrumentationRunner = "com.example.hiltbenchmark.HiltBenchmarkRunner" } testBuildType = "benchmark" buildTypes { debug { // Since isDebuggable can't be modified by Gradle for library modules, // it must be done in a manifest. See src/androidTest/AndroidManifest.xml. isMinifyEnabled = true proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "benchmark-proguard-rules.pro" ) } create("benchmark") { initWith(getByName("debug")) } } } dependencies { androidTestImplementation(libs.bundles.hilt) androidTestImplementation(project(":benchmarkable")) implementation(libs.androidx.runner) androidTestImplementation(libs.androidx.junit) androidTestImplementation(libs.junit) implementation(libs.androidx.benchmark) implementation(libs.google.dagger.hiltTesting) kaptAndroidTest(libs.google.dagger.hiltCompiler) androidTestAnnotationProcessor(libs.google.dagger.hiltCompiler) }
Groovy
plugins { alias libs.plugins.android.library alias libs.plugins.benchmark alias libs.plugins.jetbrains.kotlin.android alias libs.plugins.kapt alias libs.plugins.hilt } android { namespace = 'com.example.hiltmicrobenchmark.benchmark' compileSdk = 34 defaultConfig { minSdk = 24 testInstrumentationRunner 'com.example.hiltbenchmark.HiltBenchmarkRunner' } testBuildType "benchmark" buildTypes { debug { // Since isDebuggable can't be modified by Gradle for library modules, // it must be done in a manifest. See src/androidTest/AndroidManifest.xml. minifyEnabled true proguardFiles( getDefaultProguardFile('proguard-android-optimize.txt'), 'benchmark-proguard-rules.pro' ) } benchmark { initWith debug" } } } dependencies { androidTestImplementation libs.bundles.hilt androidTestImplementation project(':benchmarkable') implementation libs.androidx.runner androidTestImplementation libs.androidx.junit androidTestImplementation libs.junit implementation libs.androidx.benchmark implementation libs.google.dagger.hiltTesting kaptAndroidTest libs.google.dagger.hiltCompiler androidTestAnnotationProcessor libs.google.dagger.hiltCompiler }
Ví dụ trên thực hiện những việc sau:
- Áp dụng các trình bổ trợ gradle cần thiết cho bản dựng.
- Chỉ định rằng trình chạy kiểm thử tuỳ chỉnh được dùng để chạy các kiểm thử.
- Chỉ định rằng biến thể
benchmark
là loại kiểm thử cho mô-đun này. - Thêm biến thể
benchmark
. - Thêm các phần phụ thuộc bắt buộc.
Bạn cần thay đổi testBuildType
để đảm bảo Gradle tạo ra tác vụ connectedBenchmarkAndroidTest
, tác vụ này sẽ thực hiện việc đo điểm chuẩn.
Tạo microbenchmark
Điểm chuẩn được triển khai như sau:
Kotlin
@RunWith(AndroidJUnit4::class) @HiltAndroidTest class PeopleRepositoryBenchmark { @get:Rule val benchmarkRule = BenchmarkRule() @get:Rule val hiltRule = HiltAndroidRule(this) private val latch = CountdownLatch(1) @Inject lateinit var peopleRepository: PeopleRepository @Before fun setup() { hiltRule.inject() } @Test fun benchmarkSort() { benchmarkRule.measureRepeated { runBlocking { benchmarkRule.getStart().pauseTiming() withContext(Dispatchers.Main.immediate) { peopleRepository.peopleLiveData.observeForever(observer) } benchmarkRule.getStart().resumeTiming() peopleRepository.update() latch.await() assert(peopleRepository.peopleLiveData.value?.isNotEmpty() ?: false) } } } private val observer: Observer<List<Person>> = object : Observer<List<Person>> { override fun onChanged(people: List<Person>?) { peopleRepository.peopleLiveData.removeObserver(this) latch.countDown() } } }
Java
@RunWith(AndroidJUnit4.class) @HiltAndroidTest public class PeopleRepositoryBenchmark { @Rule public BenchmarkRule benchmarkRule = new BenchmarkRule(); @Rule public HiltAndroidRule hiltRule = new HiltAndroidRule(this); private CountdownLatch latch = new CountdownLatch(1); @Inject JavaPeopleRepository peopleRepository; @Before public void setup() { hiltRule.inject(); } @Test public void benchmarkSort() { BenchmarkRuleKt.measureRepeated(benchmarkRule, (Function1<BenchmarkRule.Scope, Unit>) scope -> { benchmarkRule.getState().pauseTiming(); new Handler(Looper.getMainLooper()).post(() -> { awaitValue(peopleRepository.getPeopleLiveData()); }); benchmarkRule.getState().resumeTiming(); peopleRepository.update(); try { latch.await(); } catch (InterruptedException e) { throw new RuntimeException(e); } assert (!peopleRepository.getPeopleLiveData().getValue().isEmpty()); return Unit.INSTANCE; }); } private <T> void awaitValue(LiveData<T> liveData) { Observer<T> observer = new Observer<T>() { @Override public void onChanged(T t) { liveData.removeObserver(this); latch.countDown(); } }; liveData.observeForever(observer); return; } }
Ví dụ trước đó tạo ra các quy tắc cho cả điểm chuẩn và Hilt.
benchmarkRule
thực hiện việc tính thời gian của phép đo điểm chuẩn. hiltRule
thực hiện việc chèn phần phụ thuộc vào lớp kiểm thử điểm chuẩn. Bạn phải gọi phương thức inject()
của quy tắc Hilt trong một hàm @Before
để thực hiện quá trình truyền trước khi chạy bất kỳ quy trình kiểm thử riêng lẻ nào.
Bản thân điểm chuẩn sẽ tạm dừng tính thời gian trong khi trình quan sát LiveData
được đăng ký. Sau đó, nó sẽ dùng một chốt để đợi cho đến khi LiveData
được cập nhật trước khi hoàn tất. Vì quá trình sắp xếp diễn ra trong khoảng thời gian từ khi peopleRepository.update()
được gọi đến khi LiveData
nhận được bản cập nhật, nên thời lượng sắp xếp sẽ được đưa vào thời gian đo điểm chuẩn.
Chạy phép đo điểm chuẩn vi mô
Chạy phép đo điểm chuẩn bằng ./gradlew :benchmark:connectedBenchmarkAndroidTest
để thực hiện phép đo điểm chuẩn qua nhiều lần lặp và in dữ liệu thời gian vào Logcat:
PeopleRepositoryBenchmark.log[Metric (timeNs) results: median 613408.3952380952, min 451949.30476190476, max 1412143.5142857144, standardDeviation: 273221.2328680522...
Ví dụ trước cho thấy kết quả đo điểm chuẩn từ 0,6 mili giây đến 1,4 mili giây để chạy thuật toán sắp xếp trên danh sách gồm 1.000 mục. Tuy nhiên, nếu bạn đưa lệnh gọi mạng vào điểm chuẩn, thì phương sai giữa các lần lặp lại sẽ lớn hơn thời gian mà chính thao tác sắp xếp mất để chạy, do đó, bạn cần tách thao tác sắp xếp khỏi lệnh gọi mạng.
Bạn luôn có thể tái cấu trúc mã để dễ dàng chạy quá trình sắp xếp riêng biệt, nhưng nếu đã sử dụng Hilt, bạn có thể dùng Hilt để chèn dữ liệu giả cho hoạt động đo điểm chuẩn.