|
| 1 | +package com.blankj.utilcode.pkg.feature.bus |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.Intent |
| 5 | +import android.os.Bundle |
| 6 | +import android.view.View |
| 7 | +import com.blankj.lib.common.CommonTaskActivity |
| 8 | +import com.blankj.utilcode.pkg.R |
| 9 | +import com.blankj.utilcode.util.BusUtils |
| 10 | +import com.blankj.utilcode.util.ThreadUtils |
| 11 | +import kotlinx.android.synthetic.main.activity_busutils_vs_eventbus.* |
| 12 | +import org.greenrobot.eventbus.EventBus |
| 13 | +import java.util.* |
| 14 | + |
| 15 | + |
| 16 | +/** |
| 17 | + * ``` |
| 18 | + * author: Blankj |
| 19 | + * blog : http://blankj.com |
| 20 | + * time : 2019/07/14 |
| 21 | + * desc : demo about BusUtils |
| 22 | + * ``` |
| 23 | + */ |
| 24 | +class BusCompareActivity : CommonTaskActivity<Unit>() { |
| 25 | + |
| 26 | + override fun doInBackground() { |
| 27 | + |
| 28 | + } |
| 29 | + |
| 30 | + override fun runOnUiThread(data: Unit?) { |
| 31 | + |
| 32 | + } |
| 33 | + |
| 34 | + companion object { |
| 35 | + fun start(context: Context) { |
| 36 | + val starter = Intent(context, BusCompareActivity::class.java) |
| 37 | + context.startActivity(starter) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + override fun bindTitle(): CharSequence { |
| 42 | + return getString(R.string.demo_bus) |
| 43 | + } |
| 44 | + |
| 45 | + override fun initData(bundle: Bundle?) {} |
| 46 | + |
| 47 | + override fun bindLayout(): Int { |
| 48 | + return R.layout.activity_busutils_vs_eventbus |
| 49 | + } |
| 50 | + |
| 51 | + override fun initView(savedInstanceState: Bundle?, contentView: View?) { |
| 52 | + applyDebouncingClickListener( |
| 53 | + busCompareRegister10000TimesBtn, |
| 54 | + busComparePostTo1Subscriber1000000TimesBtn, |
| 55 | + busComparePostTo100Subscribers100000TimesBtn, |
| 56 | + busCompareUnregister10000TimesBtn |
| 57 | + ) |
| 58 | + } |
| 59 | + |
| 60 | + override fun doBusiness() { |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + override fun onDebouncingClick(view: View) { |
| 65 | + when (view.id) { |
| 66 | + R.id.busCompareRegister10000TimesBtn -> { |
| 67 | + compareRegister10000Times() |
| 68 | + } |
| 69 | + R.id.busComparePostTo1Subscriber1000000TimesBtn -> { |
| 70 | + comparePostTo1Subscriber1000000Times() |
| 71 | + } |
| 72 | + R.id.busComparePostTo100Subscribers100000TimesBtn -> { |
| 73 | + comparePostTo100Subscribers100000Times() |
| 74 | + } |
| 75 | + R.id.busCompareUnregister10000TimesBtn -> { |
| 76 | + compareUnregister10000Times() |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + override fun onDestroy() { |
| 82 | + super.onDestroy() |
| 83 | + ThreadUtils.cancel(ThreadUtils.getCpuPool()) |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * 注册 10000 个订阅者,共执行 10 次取平均值 |
| 88 | + */ |
| 89 | + private fun compareRegister10000Times() { |
| 90 | + val eventBusTests = java.util.ArrayList<BusEvent>() |
| 91 | + val busUtilsTests = java.util.ArrayList<BusEvent>() |
| 92 | + |
| 93 | + compareWithEventBus("Register 10000 times.", 10, 10000, object : CompareCallback { |
| 94 | + override fun runEventBus() { |
| 95 | + val test = BusEvent() |
| 96 | + EventBus.getDefault().register(test) |
| 97 | + eventBusTests.add(test) |
| 98 | + } |
| 99 | + |
| 100 | + override fun runBusUtils() { |
| 101 | + val test = BusEvent() |
| 102 | + BusUtils.register(test) |
| 103 | + busUtilsTests.add(test) |
| 104 | + } |
| 105 | + |
| 106 | + override fun restState() { |
| 107 | + for (test in eventBusTests) { |
| 108 | + EventBus.getDefault().unregister(test) |
| 109 | + } |
| 110 | + eventBusTests.clear() |
| 111 | + |
| 112 | + for (test in busUtilsTests) { |
| 113 | + BusUtils.unregister(test) |
| 114 | + } |
| 115 | + busUtilsTests.clear() |
| 116 | + } |
| 117 | + }, object : OnFinishCallback { |
| 118 | + override fun onFinish() { |
| 119 | + |
| 120 | + } |
| 121 | + }) |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * 向 1 个订阅者发送 * 1000000 次,共执行 10 次取平均值 |
| 126 | + */ |
| 127 | + private fun comparePostTo1Subscriber1000000Times() { |
| 128 | + comparePostTemplate("Post to 1 subscriber 1000000 times.", 1, 1000000) |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * 向 100 个订阅者发送 * 100000 次,共执行 10 次取平均值 |
| 133 | + */ |
| 134 | + private fun comparePostTo100Subscribers100000Times() { |
| 135 | + comparePostTemplate("Post to 100 subscribers 100000 times.", 100, 100000) |
| 136 | + } |
| 137 | + |
| 138 | + private fun comparePostTemplate(name: String, subscribeNum: Int, postTimes: Int) { |
| 139 | + val tests = java.util.ArrayList<BusEvent>() |
| 140 | + for (i in 0 until subscribeNum) { |
| 141 | + val test = BusEvent() |
| 142 | + EventBus.getDefault().register(test) |
| 143 | + BusUtils.register(test) |
| 144 | + tests.add(test) |
| 145 | + } |
| 146 | + |
| 147 | + compareWithEventBus(name, 10, postTimes, object : CompareCallback { |
| 148 | + override fun runEventBus() { |
| 149 | + EventBus.getDefault().post("EventBus") |
| 150 | + } |
| 151 | + |
| 152 | + override fun runBusUtils() { |
| 153 | + BusUtils.post("busUtilsFun", "BusUtils") |
| 154 | + } |
| 155 | + |
| 156 | + override fun restState() { |
| 157 | + |
| 158 | + } |
| 159 | + }, object : OnFinishCallback { |
| 160 | + override fun onFinish() { |
| 161 | + for (test in tests) { |
| 162 | + EventBus.getDefault().unregister(test) |
| 163 | + BusUtils.unregister(test) |
| 164 | + } |
| 165 | + } |
| 166 | + }) |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * 注销 10000 个订阅者,共执行 10 次取平均值 |
| 171 | + */ |
| 172 | + private fun compareUnregister10000Times() { |
| 173 | + val tests = ArrayList<BusEvent>() |
| 174 | + for (i in 0..9999) { |
| 175 | + val test = BusEvent() |
| 176 | + EventBus.getDefault().register(test) |
| 177 | + BusUtils.register(test) |
| 178 | + tests.add(test) |
| 179 | + } |
| 180 | + |
| 181 | + compareWithEventBus("Unregister 10000 times.", 10, 1, object : CompareCallback { |
| 182 | + override fun runEventBus() { |
| 183 | + for (test in tests) { |
| 184 | + EventBus.getDefault().unregister(test) |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + override fun runBusUtils() { |
| 189 | + for (test in tests) { |
| 190 | + BusUtils.unregister(test) |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + override fun restState() { |
| 195 | + for (test in tests) { |
| 196 | + EventBus.getDefault().register(test) |
| 197 | + BusUtils.register(test) |
| 198 | + } |
| 199 | + } |
| 200 | + }, object : OnFinishCallback { |
| 201 | + override fun onFinish() { |
| 202 | + for (test in tests) { |
| 203 | + EventBus.getDefault().unregister(test) |
| 204 | + BusUtils.unregister(test) |
| 205 | + } |
| 206 | + } |
| 207 | + }) |
| 208 | + } |
| 209 | + |
| 210 | + /** |
| 211 | + * @param name 传入的测试函数名 |
| 212 | + * @param sampleSize 样本数 |
| 213 | + * @param times 每次执行的次数 |
| 214 | + * @param callback 比较的回调函数 |
| 215 | + * @param onFinishCallback 执行结束的回调 |
| 216 | + */ |
| 217 | + private fun compareWithEventBus(name: String, sampleSize: Int, times: Int, |
| 218 | + callback: CompareCallback, onFinishCallback: OnFinishCallback) { |
| 219 | + setLoadingVisibility(true) |
| 220 | + setBtnEnabled(false) |
| 221 | + ThreadUtils.executeByCpu(object : ThreadUtils.Task<String>() { |
| 222 | + override fun doInBackground(): String { |
| 223 | + val dur = Array(2) { LongArray(sampleSize) } |
| 224 | + for (i in 0 until sampleSize) { |
| 225 | + var cur = System.currentTimeMillis() |
| 226 | + for (j in 0 until times) { |
| 227 | + callback.runEventBus() |
| 228 | + } |
| 229 | + dur[0][i] = System.currentTimeMillis() - cur |
| 230 | + cur = System.currentTimeMillis() |
| 231 | + for (j in 0 until times) { |
| 232 | + callback.runBusUtils() |
| 233 | + } |
| 234 | + dur[1][i] = System.currentTimeMillis() - cur |
| 235 | + callback.restState() |
| 236 | + } |
| 237 | + var eventBusAverageTime: Long = 0 |
| 238 | + var busUtilsAverageTime: Long = 0 |
| 239 | + for (i in 0 until sampleSize) { |
| 240 | + eventBusAverageTime += dur[0][i] |
| 241 | + busUtilsAverageTime += dur[1][i] |
| 242 | + } |
| 243 | + return name + |
| 244 | + "\nEventBusCostTime: " + eventBusAverageTime / sampleSize + |
| 245 | + "\nBusUtilsCostTime: " + busUtilsAverageTime / sampleSize; |
| 246 | + } |
| 247 | + |
| 248 | + override fun onSuccess(result: String?) { |
| 249 | + onFinishCallback.onFinish() |
| 250 | + setBtnEnabled(true) |
| 251 | + setLoadingVisibility(false) |
| 252 | + this@BusCompareActivity.busCompareAboutTv.text = result |
| 253 | + } |
| 254 | + |
| 255 | + override fun onCancel() { |
| 256 | + onFinishCallback.onFinish() |
| 257 | + setLoadingVisibility(false) |
| 258 | + setBtnEnabled(true) |
| 259 | + } |
| 260 | + |
| 261 | + override fun onFail(t: Throwable?) { |
| 262 | + onFinishCallback.onFinish() |
| 263 | + setLoadingVisibility(false) |
| 264 | + setBtnEnabled(true) |
| 265 | + } |
| 266 | + }) |
| 267 | + } |
| 268 | + |
| 269 | + private fun setBtnEnabled(enable: Boolean) { |
| 270 | + busCompareRegister10000TimesBtn.isEnabled = enable |
| 271 | + busComparePostTo1Subscriber1000000TimesBtn.isEnabled = enable |
| 272 | + busCompareUnregister10000TimesBtn.isEnabled = enable |
| 273 | + } |
| 274 | + |
| 275 | + interface CompareCallback { |
| 276 | + fun runEventBus() |
| 277 | + |
| 278 | + fun runBusUtils() |
| 279 | + |
| 280 | + fun restState() |
| 281 | + } |
| 282 | + |
| 283 | + interface OnFinishCallback { |
| 284 | + fun onFinish() |
| 285 | + } |
| 286 | +} |
0 commit comments