移动性能测试 BlockCanary 拿不到主线程卡顿堆栈?

李雷雷 · August 17, 2018 · 1172 hits

BlockCanary接入

我在SDK中集成了BlockCanary,新增加了一个BlockObserver用于后续上报问题堆栈到自己的服务器

public class BlockObserver implements BlockMonitor.BlockObserver{

@Override
public void onBlock(BlockInfo blockInfo) {
report(blockInfo);
}

private void report(BlockInfo blockInfo) {
Log.d("BlockReport", "getFrequentMethods: "
+ blockInfo.getFrequentMethods()
+ "getHeavyMethods: " + blockInfo.getHeavyMethods()
+ "getEnvInfo: " + blockInfo.getEnvInfo()
+ "getTopHeavyMethod: " + blockInfo.getTopHeavyMethod());
printThreadStackTrace();
}

private void printThreadStackTrace(){
final Thread mainThread = Looper.getMainLooper().getThread();
Log.d("BlockReport","\nmainThread: " + mainThread.getStackTrace().toString());
for (StackTraceElement stackTrace : mainThread.getStackTrace()){
Log.d("BlockReport","\nstackTraceElement: " + stackTrace);
}
// for (Map.Entry<Thread, StackTraceElement[]> stackTrace : Thread.getAllStackTraces().entrySet()) {
// Thread thread = stackTrace.getKey();
// if (thread!=null) {
// StackTraceElement[] stack = stackTrace.getValue();
// for (StackTraceElement stackTraceElement : stack) {
// Log.d("BlockReport","\nstackTraceElement: " + stackTraceElement);
// }
//
// }
// }
}

}

然后我在SDK初始化的时候注册自己的observer

public void start() {
...
BlockMonitor.registerBlockObserver(new BlockObserver());
BlockCanaryEx.install(new Config(context));
...
}

卡顿测试

我在demoApp中新增一个2s的sleep

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
Button gcButton = (Button) findViewById(R.id.gc_button);
gcButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// doGc();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.d("BlockReport", "onClick: Sleep->1000");
Toast.makeText(MainActivity.this,"GC Finished",Toast.LENGTH_SHORT).show();
}
});
}

但是为啥没有堆栈!
这个是我debug时的blockInfo对象

下面这个则是blockCanaryUI堆栈信息显示

发现两者都没有拿到堆栈信息!有谁知道这个是什么情况吗?

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
No Reply at the moment.
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up