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() {

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>

View File

@ -108,6 +108,10 @@ public class DoricContext {
this.source = source;
this.extra = extra;
this.performanceProfile = new DoricPerformanceProfile(contextId);
DoricPerformanceProfile.AnchorHook anchorHook = getDriver().getRegistry().getGlobalPerformanceAnchorHook();
if (anchorHook != null) {
this.performanceProfile.addAnchorHook(anchorHook);
}
}
public DoricPerformanceProfile getPerformanceProfile() {

View File

@ -28,6 +28,7 @@ import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import pub.doric.engine.DoricJSEngine;
import pub.doric.performance.DoricPerformanceProfile;
import pub.doric.plugin.AnimatePlugin;
import pub.doric.plugin.CoordinatorPlugin;
import pub.doric.plugin.DoricJavaPlugin;
@ -246,4 +247,16 @@ public class DoricRegistry {
public void setDefaultErrorDrawable(Drawable defaultErrorDrawable) {
this.defaultErrorDrawable = defaultErrorDrawable;
}
private DoricPerformanceProfile.GlobalAnchorHook globalPerformanceAnchorHook;
public void setGlobalPerformanceAnchorHook(DoricPerformanceProfile.GlobalAnchorHook anchorHook) {
globalPerformanceAnchorHook = anchorHook;
}
public DoricPerformanceProfile.GlobalAnchorHook getGlobalPerformanceAnchorHook() {
return globalPerformanceAnchorHook;
}
}

View File

@ -67,6 +67,10 @@ public class DoricJSEngine implements Handler.Callback, DoricTimerExtension.Time
public DoricJSEngine() {
mDoricRegistry = new DoricRegistry(this);
DoricPerformanceProfile.AnchorHook anchorHook = mDoricRegistry.getGlobalPerformanceAnchorHook();
if (anchorHook != null) {
globalProfile.addAnchorHook(anchorHook);
}
globalProfile.prepare(DoricPerformanceProfile.PART_INIT);
handlerThread = new HandlerThread(this.getClass().getSimpleName());
handlerThread.start();

View File

@ -57,6 +57,10 @@ public class DoricPerformanceProfile {
void onAnchor(String name, long prepare, long start, long end);
}
public interface GlobalAnchorHook extends AnchorHook {
void onAnchor(DoricPerformanceProfile profile, String name, long prepare, long start, long end);
}
public DoricPerformanceProfile(String name) {
this.name = name;
}
@ -113,10 +117,6 @@ public class DoricPerformanceProfile {
print(anchorName);
}
public Map<String, Long> getAnchorMap() {
return this.anchorMap;
}
private void print(final String anchorName) {
if (!enable) {
return;
@ -139,9 +139,13 @@ public class DoricPerformanceProfile {
Log.d(TAG, String.format("%s: %s prepared %dms, cost %dms.",
name, anchorName, start - prepare, end - start));
for (AnchorHook hook : hooks) {
if (hook instanceof GlobalAnchorHook) {
((GlobalAnchorHook) hook).onAnchor(DoricPerformanceProfile.this, anchorName, prepare, start, end);
} else {
hook.onAnchor(anchorName, prepare, start, end);
}
}
}
});
}
}

View File

@ -54,6 +54,9 @@ + (instancetype)instance {
- (void)createContext:(DoricContext *)context script:(NSString *)script source:(NSString *)source {
context.contextId = [NSString stringWithFormat:@"%ld", (long) ++self.counter];
context.performanceProfile = [[DoricPerformanceProfile alloc] initWithName:context.contextId];
if (context.driver.registry.globalPerformanceAnchorHook) {
[context.performanceProfile addAnchorHook:context.driver.registry.globalPerformanceAnchorHook];
}
dispatch_sync(self.mapQueue, ^() {
[self.contextMapTable setObject:context forKey:context.contextId];
});

View File

@ -27,10 +27,12 @@
NS_ASSUME_NONNULL_BEGIN
@class DoricLibrary;
@class DoricJSEngine;
@protocol DoricPerformanceGlobalAnchorHookProtocol;
@interface DoricRegistry : NSObject <DoricMonitorProtocol>
@property(nonatomic, strong) UIImage *defaultPlaceHolderImage;
@property(nonatomic, strong) UIImage *defaultErrorImage;
@property(nonatomic, strong) id <DoricPerformanceGlobalAnchorHookProtocol> globalPerformanceAnchorHook;
- (instancetype)initWithJSEngine:(DoricJSEngine *)jsEngine;

View File

@ -58,7 +58,11 @@ @implementation DoricJSEngine
- (instancetype)init {
if (self = [super init]) {
_initialized = NO;
_registry = [[DoricRegistry alloc] initWithJSEngine:self];
_profile = [[DoricPerformanceProfile alloc] initWithName:@"JSEngine"];
if (_registry.globalPerformanceAnchorHook) {
[_profile addAnchorHook:_registry.globalPerformanceAnchorHook];
}
[_profile prepare:@"Init"];
_jsThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadRun) object:nil];
[_jsThread start];
@ -95,7 +99,6 @@ - (instancetype)init {
@"localeLanguage": [[NSLocale currentLocale] objectForKey:NSLocaleLanguageCode] ?: @"",
@"localeCountry": [[NSLocale currentLocale] objectForKey:NSLocaleCountryCode] ?: @"",
}.mutableCopy;
self.registry = [[DoricRegistry alloc] initWithJSEngine:self];
[self ensureRunOnJSThread:^() {
[self.profile start:@"Init"];
self.timers = [[NSMutableDictionary alloc] init];

View File

@ -31,9 +31,17 @@ NS_ASSUME_NONNULL_BEGIN
end:(NSNumber *)end;
@end
@class DoricPerformanceProfile;
@protocol DoricPerformanceGlobalAnchorHookProtocol <NSObject, DoricPerformanceAnchorHookProtocol>
- (void)onAnchorName:(NSString *)name
prepare:(NSNumber *)prepare
start:(NSNumber *)start
end:(NSNumber *)end
in:(DoricPerformanceProfile *)profile;
@end
@interface DoricPerformanceProfile : NSObject
@property(nonatomic, strong) NSMutableDictionary <NSString *, NSNumber *> *anchorMap;
- (instancetype)initWithName:(NSString *)name;

View File

@ -28,6 +28,7 @@ @interface DoricPerformanceProfile ()
@property(nonatomic, strong) dispatch_queue_t anchorQueue;
@property(nonatomic, assign) BOOL enable;
@property(nonatomic, strong) NSHashTable<id <DoricPerformanceAnchorHookProtocol>> *hooks;
@property(nonatomic, strong) NSMutableDictionary <NSString *, NSNumber *> *anchorMap;
@end
@implementation DoricPerformanceProfile
@ -114,8 +115,12 @@ - (void)print:(NSString *)anchorName {
@(end.integerValue - start.integerValue)
);
for (id <DoricPerformanceAnchorHookProtocol> hook in self.hooks) {
if ([hook conformsToProtocol:@protocol(DoricPerformanceGlobalAnchorHookProtocol)]) {
[hook onAnchorName:anchorName prepare:end start:end end:end in:self];
} else {
[hook onAnchorName:anchorName prepare:prepare start:start end:end];
}
}
});
}
@end