其他
来来来!一起动手整个性能检测工具吧~
https://blog.csdn.net/m0_46278918
https://github.com/markzhai/AndroidPerformanceMonitor
// 无论是开发版还是正式版都会检测(不建议)
// implementation 'com.github.markzhai:blockcanary-android:1.5.0'
// 仅在开发版时检测
debugImplementation 'com.github.markzhai:blockcanary-android:1.5.0'
releaseImplementation 'com.github.markzhai:blockcanary-no-op:1.5.0'
}
override fun onCreate() {
super.onCreate()
BlockCanary.install(this, BlockCanaryContext()).start()
}
}
SystemClock.sleep(2000)
}
使用总结
能够计算在主线程中运行方法的时长 定位运行的方法在代码上的位置
运行时长
final Looper me = myLooper();
···
for (;;) {
···
final Printer logging = me.mLogging;
if (logging != null) {
logging.println(">>>>> Dispatching to " + msg.target + " " +
msg.callback + ": " + msg.what);
}
···
msg.target.dispatchMessage(msg);
···
if (logging != null) {
logging.println("<<<<< Finished to " + msg.target + " " + msg.callback);
}
···
}
}
return sThreadLocal.get();
}
public void setMessageLogging(@Nullable Printer printer) {
mLogging = printer;
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initPrinter()
}
//使用 printerStart 进行判断是方法执行前还是执行后调用 println
private var printerStart = true
//记录方法执行前的时间
private var printerStartTime = 0L
private fun initPrinter() {
Looper.getMainLooper().setMessageLogging {
if (printerStart) {
printerStart = false
printerStartTime = System.currentTimeMillis();
} else {
printerStart = true
Log.i("Printer", "方法运行的总时长:${System.currentTimeMillis() - printerStartTime}")
}
}
}
fun clickView(view: View) {
SystemClock.sleep(2000)
}
}
com.example.testblockcanary I/Printer: 方法运行的总时长:3
com.example.testblockcanary I/Printer: 方法运行的总时长:3
com.example.testblockcanary I/Printer: 方法运行的总时长:4
com.example.testblockcanary I/Printer: 方法运行的总时长:3
com.example.testblockcanary I/Printer: 方法运行的总时长:2009
private val minTime = 1000L
private fun initPrinter() {
Looper.getMainLooper().setMessageLogging {
if (printerStart) {
printerStart = false
printerStartTime = System.currentTimeMillis();
} else {
printerStart = true
(System.currentTimeMillis() - printerStartTime).let {
if (it >= minTime){
Log.i("Printer", "方法运行的总时长:${it}")
}
}
}
}
}
Looper.getMainLooper().setMessageLogging {
if (printerStart) {
printerStart = false
printerStartTime = System.currentTimeMillis();
} else {
printerStart = true
(System.currentTimeMillis() - printerStartTime).let {
if (it >= minTime){
Log.i("Printer", "方法运行的总时长:${it}")
//--------------------------------------------//
//获取栈信息进行输出
val stringBuilder = StringBuilder()
for (stackTraceElement in Looper.getMainLooper().getThread().getStackTrace()) {
stringBuilder
.append(stackTraceElement.toString())
.append(BlockInfo.SEPARATOR)
}
Log.i("Printer", "StackTrace:${stringBuilder.toString()}")
//--------------------------------------------//
}
}
}
}
}
com.example.testblockcanary I/Printer: StackTrace:java.lang.System.currentTimeMillis(Native Method)
com.example.testblockcanary.MainActivity$initPrinter$1.println(MainActivity.kt:31)
android.os.Looper.loop(Looper.java:145)
android.app.ActivityThread.main(ActivityThread.java:6077)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
private val minTime = 1000L
private fun initDelayHandler() {
//让Handler的消息在子线程中运行
val handlerThread = HandlerThread("DelayThread")
handlerThread.start()
delayHandler = Handler(handlerThread.looper)
}
private val runnable = Runnable {
//获取栈信息进行记录
for (stackTraceElement in Looper.getMainLooper().getThread().getStackTrace()) {
stringBuilder
.append(stackTraceElement.toString())
.append(BlockInfo.SEPARATOR)
}
}
Looper.getMainLooper().setMessageLogging {
if (printerStart) {
printerStart = false
printerStartTime = System.currentTimeMillis();
delayHandler.removeCallbacks(runnable)
//延迟minTime * 0.8发送,用于记录阻塞时的栈信息
delayHandler.postDelayed(runnable, (minTime * 0.8).toLong())
} else {
printerStart = true
delayHandler.removeCallbacks(runnable)
(System.currentTimeMillis() - printerStartTime).let {
if (it >= minTime) {
Log.i("Printer", "方法运行的总时长:${it}")
Log.i("Printer", "StackTrace:${stringBuilder.toString()}")
}
}
}
}
}
com.example.testblockcanary I/Printer: StackTrace:java.lang.Thread.sleep(Native Method)
java.lang.Thread.sleep(Thread.java:371)
java.lang.Thread.sleep(Thread.java:313)
android.os.SystemClock.sleep(SystemClock.java:120)
com.example.testblockcanary.MainActivity.clickView(MainActivity.kt:67)
java.lang.reflect.Method.invoke(Native Method)
androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:409)
android.view.View.performClick(View.java:5610)
android.view.View$PerformClick.run(View.java:22265)
android.os.Handler.handleCallback(Handler.java:751)
android.os.Handler.dispatchMessage(Handler.java:95)
android.os.Looper.loop(Looper.java:154)
android.app.ActivityThread.main(ActivityThread.java:6077)
java.lang.reflect.Method.invoke(Native Method)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)