diff --git a/app/build.gradle b/app/build.gradle index 31a45c8fb6..eabcafae8f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,7 +10,6 @@ if (!isAndroidTestBuild()) { } //apply ByteX宿主 - if (!isAndroidTestBuild()) { apply plugin: 'bytex' ByteX { @@ -21,15 +20,15 @@ if (!isAndroidTestBuild()) { } if (!isAndroidTestBuild()) { -// apply plugin: 'bytex.threadOpt' -// thread_opt { -// enable true -// enableInDebug true -// rxJavaIoReplacer 'com/mogo/eagle/core/utilcode/util/ThreadUtils@@getIoPool@@()Ljava/util/concurrent/ExecutorService;' -// rxJavaComputationReplacer 'com/mogo/eagle/core/utilcode/util/ThreadUtils@@getCpuPool@@()Ljava/util/concurrent/ExecutorService;' -// coroutineIoReplacer 'com/mogo/eagle/core/utilcode/util/ThreadUtils@@getIoPool@@()Ljava/util/concurrent/ExecutorService;' -// coroutineDefaultReplacer 'com/mogo/eagle/core/utilcode/util/ThreadUtils@@getCpuPool@@()Ljava/util/concurrent/ExecutorService;' -// } + apply plugin: 'bytex.threadOpt' + thread_opt { + enable true + enableInDebug true + rxJavaIoReplacer 'com/mogo/eagle/core/utilcode/util/ThreadUtils@@getIoPool@@()Ljava/util/concurrent/ExecutorService;' + rxJavaComputationReplacer 'com/mogo/eagle/core/utilcode/util/ThreadUtils@@getCpuPool@@()Ljava/util/concurrent/ExecutorService;' + coroutineIoReplacer 'com/mogo/eagle/core/utilcode/util/ThreadUtils@@getIoPool@@()Ljava/util/concurrent/ExecutorService;' + coroutineDefaultReplacer 'com/mogo/eagle/core/utilcode/util/ThreadUtils@@getCpuPool@@()Ljava/util/concurrent/ExecutorService;' + } /** * 方便使用systrace工具,在工程侧打点,便于分析工程侧性能问题 diff --git a/app/src/androidTest/java/com/mogo/functions/test/KotlinCoroutineSchedulersTest.kt b/app/src/androidTest/java/com/mogo/functions/test/KotlinCoroutineSchedulersTest.kt new file mode 100644 index 0000000000..4b0ae4bb87 --- /dev/null +++ b/app/src/androidTest/java/com/mogo/functions/test/KotlinCoroutineSchedulersTest.kt @@ -0,0 +1,57 @@ +package com.mogo.functions.test + +import androidx.lifecycle.coroutineScope +import androidx.test.core.app.ActivityScenario +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.LargeTest +import com.mogo.eagle.core.function.main.MainLauncherActivity +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import kotlinx.coroutines.withContext +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import java.util.concurrent.TimeUnit + +@RunWith(AndroidJUnit4::class) +@LargeTest +class KotlinCoroutineSchedulersTest { + + lateinit var launch: ActivityScenario + + @Before + fun before() { + launch = ActivityScenario.launch(MainLauncherActivity::class.java) + } + + @Test + fun testKotlinCoroutineSchedulersIo() = runBlocking { + launch.onActivity { + it.lifecycle.coroutineScope.launchWhenCreated { + repeat(10) { + withContext(Dispatchers.IO) { + assert(Thread.currentThread().name.startsWith("io-pool-")) + } + delay(TimeUnit.SECONDS.toMillis(1)) + } + } + } + delay(TimeUnit.SECONDS.toMillis(20)) + } + + @Test + fun testKotlinCoroutineSchedulersCpu() = runBlocking { + launch.onActivity { + it.lifecycle.coroutineScope.launchWhenCreated { + repeat(10) { + withContext(Dispatchers.Default) { + assert(Thread.currentThread().name.startsWith("cpu-pool-")) + } + delay(TimeUnit.SECONDS.toMillis(1)) + } + } + } + delay(TimeUnit.SECONDS.toMillis(20)) + } +} \ No newline at end of file diff --git a/app/src/androidTest/java/com/mogo/functions/test/RxJavaSchedulersTest.kt b/app/src/androidTest/java/com/mogo/functions/test/RxJavaSchedulersTest.kt new file mode 100644 index 0000000000..58ac995932 --- /dev/null +++ b/app/src/androidTest/java/com/mogo/functions/test/RxJavaSchedulersTest.kt @@ -0,0 +1,76 @@ +package com.mogo.functions.test + +import androidx.test.core.app.ActivityScenario +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.filters.LargeTest +import com.mogo.eagle.core.function.main.MainLauncherActivity +import io.reactivex.Observable +import io.reactivex.ObservableOnSubscribe +import io.reactivex.schedulers.Schedulers +import kotlinx.coroutines.delay +import kotlinx.coroutines.runBlocking +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import java.util.concurrent.TimeUnit.SECONDS + +@RunWith(AndroidJUnit4::class) +@LargeTest +class RxJavaSchedulersTest { + + lateinit var launch: ActivityScenario + + @Before + fun before() { + launch = ActivityScenario.launch(MainLauncherActivity::class.java) + } + + @Test + fun testRxJavaIoSchedulers() = runBlocking { + val list = mutableListOf() + for (i in 1..10) { + list += i + } + val result = Observable.fromIterable(list) + .doOnNext { + assert(Thread.currentThread().name.startsWith("io-pool-")) + } + .subscribeOn(Schedulers.io()) + .reduce(0) { adder, toAdd -> + val sum = adder + toAdd + sum + } + .blockingGet() + assert(result == 55) + + delay(SECONDS.toMillis(20)) + } + + @Test + fun testRxJavaCpuSchedulers() = runBlocking { + val result = Observable.create(ObservableOnSubscribe { emitter -> + for (i in 1..10) { + emitter.onNext(i) + } + assert(Thread.currentThread().name.startsWith("cpu-pool-")) + emitter.onComplete() + }).reduce(0) { adder, toAdd -> + adder + toAdd + }.subscribeOn(Schedulers.computation()).blockingGet() + assert(result == 55) + delay(SECONDS.toMillis(20)) + } + + @Test + fun testRxJavaIntervalSchedulers() = runBlocking { + var counter = 0 + Observable.intervalRange(0, 10, 1, 1 ,SECONDS) + .doOnNext { + counter ++ + } + .subscribe() + delay(SECONDS.toMillis(20)) + assert(counter == 10) + } + +} \ No newline at end of file diff --git a/build.gradle b/build.gradle index 95ee9b6c7f..aa98125141 100644 --- a/build.gradle +++ b/build.gradle @@ -33,7 +33,7 @@ buildscript { classpath "com.bytedance.android.byteX:base-plugin:0.3.0" classpath "com.mogo.cloud:hook:${HOOK_LOG_VERSION}" classpath 'com.volcengine:apm_insight_plugin:1.4.1' - classpath 'com.mogo.cloud:thread_opt:1.0.0' + classpath 'com.mogo.cloud:thread_opt:1.0.1' classpath 'com.mogo.cloud:systrace:1.0.1' // classpath "com.bytedance.android.byteX:base-plugin:0.3.0"