feat:add global anchor hook to monitor all performance hook

This commit is contained in:
pengfei.zhou
2021-07-20 10:50:24 +08:00
committed by osborn
parent ba3635769a
commit 17e14e7119
16 changed files with 303 additions and 12 deletions

View File

@@ -12,6 +12,8 @@
android:theme="@style/Theme.Design.Light.NoActionBar" />
<activity android:name=".qrcode.activity.CaptureActivity" />
<activity android:name=".ui.DoricShowNodeTreeActivity" />
<activity android:name=".ui.DoricDevPerfActivity" />
<activity
android:name=".ui.DoricDevPerfActivity"
android:theme="@style/Theme.Design.Light.NoActionBar" />
</application>
</manifest>

View File

@@ -21,6 +21,7 @@ import pub.doric.DoricContextManager;
import pub.doric.DoricNativeDriver;
import pub.doric.devkit.ui.DoricDevActivity;
import pub.doric.devkit.util.SimulatorUtil;
import pub.doric.performance.DoricPerformanceProfile;
import pub.doric.utils.DoricLog;
public class DoricDev {
@@ -51,6 +52,12 @@ public class DoricDev {
private DoricDev() {
this.isRunningInEmulator = SimulatorUtil.isSimulator(Doric.application());
DoricNativeDriver.getInstance().getRegistry().registerMonitor(new DoricDevMonitor());
DoricNativeDriver.getInstance().getRegistry().setGlobalPerformanceAnchorHook(new DoricPerformanceProfile.AnchorHook() {
@Override
public void onAnchor(String name, long prepare, long start, long end) {
}
});
}
public static DoricDev getInstance() {
@@ -240,7 +247,7 @@ public class DoricDev {
if (reloadingContexts.get(context.getContextId()) == null) {
reloadingContexts.put(context.getContextId(), context);
}
for (StatusCallback callback : callbacks) {
callback.onReload(context, script);
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright [2021] [Doric.Pub]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package pub.doric.devkit;
import pub.doric.performance.DoricPerformanceProfile;
/**
* @Description: pub.doric.devkit
* @Author: pengfei.zhou
* @CreateDate: 2021/7/20
*/
public class DoricDevPerformanceAnchorHook implements DoricPerformanceProfile.AnchorHook {
@Override
public void onAnchor(String name, long prepare, long start, long end) {
}
}

View File

@@ -417,7 +417,6 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat
Intent intent = new Intent(holder.itemView.getContext(), DoricDevPerfActivity.class);
intent.putExtra(DORIC_CONTEXT_ID_KEY, context.getContextId());
v.getContext().startActivity(intent);
((Activity) v.getContext()).finish();
}
});
}
@@ -428,7 +427,6 @@ public class DoricDevActivity extends AppCompatActivity implements DoricDev.Stat
Intent intent = new Intent(holder.itemView.getContext(), DoricShowNodeTreeActivity.class);
intent.putExtra(DORIC_CONTEXT_ID_KEY, context.getContextId());
v.getContext().startActivity(intent);
((Activity) v.getContext()).finish();
}
});
final String[] items = actionMap.keySet().toArray(new String[0]);

View File

@@ -16,9 +16,26 @@
package pub.doric.devkit.ui;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import pub.doric.devkit.R;
/**
* @Description: pub.doric.devkit.ui
@@ -29,5 +46,88 @@ public class DoricDevPerfActivity extends DoricDevBaseActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doricdev_perf);
TextView textView = findViewById(R.id.tv_title);
textView.setText(String.format("Doric %s <%s>", doricContext.getSource(), doricContext.getContextId()));
RecyclerView recyclerView = findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(new MyAdapter());
}
private static class PerfCellHolder extends RecyclerView.ViewHolder {
private TextView tvName;
private LinearLayout layoutWaterfall;
private View waterfallPrepared;
private View waterfallWorked;
private LinearLayout layoutExpand;
private TextView tvFuncName;
private TextView tvParameter;
private TextView tvCost;
public PerfCellHolder(@NonNull View itemView) {
super(itemView);
}
}
private static class AnchorNode {
String name;
long prepare;
}
private class MyAdapter extends RecyclerView.Adapter<PerfCellHolder> {
private List<AnchorNode> anchorNodes = new ArrayList<>();
private MyAdapter() {
Map<String, Long> anchorMap = doricContext.getPerformanceProfile().getAnchorMap();
for (String key : anchorMap.keySet()) {
if (key.endsWith("#prepare")) {
Long prepare = anchorMap.get(key);
if (prepare != null) {
AnchorNode anchorNode = new AnchorNode();
anchorNode.name = key.substring(0, key.lastIndexOf("#prepare"));
anchorNode.prepare = prepare;
anchorNodes.add(anchorNode);
}
}
}
Collections.sort(anchorNodes, new Comparator<AnchorNode>() {
@Override
public int compare(AnchorNode o1, AnchorNode o2) {
return (int) (o1.prepare - o2.prepare);
}
});
}
@Override
public PerfCellHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View cell = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_doricdev_perf, parent, false);
PerfCellHolder cellHolder = new PerfCellHolder(cell);
cellHolder.tvName = cell.findViewById(R.id.tv_name);
cellHolder.layoutWaterfall = cell.findViewById(R.id.layout_waterfall);
cellHolder.waterfallPrepared = cell.findViewById(R.id.waterfall_prepared);
cellHolder.waterfallWorked = cell.findViewById(R.id.waterfall_worked);
cellHolder.layoutExpand = cell.findViewById(R.id.layout_expand);
cellHolder.tvFuncName = cell.findViewById(R.id.tv_func_name);
cellHolder.tvParameter = cell.findViewById(R.id.tv_parameter);
cellHolder.tvCost = cell.findViewById(R.id.tv_cost);
return cellHolder;
}
@Override
public void onBindViewHolder(@NonNull PerfCellHolder holder, int position) {
holder.itemView.setBackgroundColor(position % 2 == 0 ? Color.parseColor("#ecf0f1") : Color.WHITE);
holder.layoutExpand.setVisibility(View.GONE);
AnchorNode anchorNode = anchorNodes.get(position);
if (anchorNode.name.startsWith("Call")) {
holder.tvName.setText("Call");
} else {
holder.tvName.setText(anchorNode.name);
}
}
@Override
public int getItemCount() {
return anchorNodes.size();
}
}
}

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#7f8c8d"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:text="Context"
android:textColor="#fff"
android:textSize="16dp" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>

View File

@@ -0,0 +1,79 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ecf0f1"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_name"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:background="#3498db"
android:gravity="center"
android:text="Destroy"
android:textColor="#fff"
android:textSize="16dp" />
<LinearLayout
android:id="@+id/layout_waterfall"
android:layout_width="0dp"
android:layout_height="20dp"
android:layout_weight="1">
<View
android:id="@+id/waterfall_prepared"
android:layout_width="30dp"
android:layout_height="20dp"
android:background="#1abc9c" />
<View
android:id="@+id/waterfall_worked"
android:layout_width="60dp"
android:layout_height="20dp"
android:background="#f1c40f" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/layout_expand"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#bdc3c7"
android:gravity="center_vertical"
android:orientation="vertical"
android:paddingLeft="15dp">
<TextView
android:id="@+id/tv_func_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="__onCreate__"
android:textSize="12dp" />
<TextView
android:id="@+id/tv_parameter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="{width:392.72726,height:669.4545}"
android:textSize="12dp" />
<TextView
android:id="@+id/tv_cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="100 ms"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>