Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

[WEEX-545] [Android] apm for weex perofrmance #1387

Merged
merged 1 commit into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.WXSDKManager;
import com.taobao.weex.adapter.IWXImgLoaderAdapter;
import com.taobao.weex.common.WXImageStrategy;
import com.taobao.weex.dom.WXImageQuality;
import com.taobao.weex.ui.IFComponentHolder;

public class ImageAdapter implements IWXImgLoaderAdapter {

Expand All @@ -50,6 +52,10 @@ public void run() {
view.setImageBitmap(null);
return;
}
if (null != strategy){
recordImgLoadAction(strategy.instanceId);
}

String temp = url;
if (url.startsWith("//")) {
temp = "http:" + url;
Expand All @@ -72,6 +78,7 @@ public void onSuccess() {
if(strategy.getImageListener()!=null){
strategy.getImageListener().onImageFinish(url,view,true,null);
}
recordImgLoadResult(strategy.instanceId,true,null);

if(!TextUtils.isEmpty(strategy.placeHolder)){
((Picasso) view.getTag(strategy.placeHolder.hashCode())).cancelRequest(view);
Expand All @@ -83,6 +90,7 @@ public void onError() {
if(strategy.getImageListener()!=null){
strategy.getImageListener().onImageFinish(url,view,false,null);
}
recordImgLoadResult(strategy.instanceId,false,null);
}
});
}
Expand All @@ -93,4 +101,19 @@ public void onError() {
WXSDKManager.getInstance().postOnUiThread(runnable, 0);
}
}
private void recordImgLoadAction(String instanceId){
WXSDKInstance instance = WXSDKManager.getInstance().getAllInstanceMap().get(instanceId);
if (null == instance || instance.isDestroy()){
return;
}
instance.getApmForInstance().actionLoadImg();
}

private void recordImgLoadResult(String instanceId,boolean succeed,String errorCode){
WXSDKInstance instance = WXSDKManager.getInstance().getAllInstanceMap().get(instanceId);
if (null == instance || instance.isDestroy()){
return;
}
instance.getApmForInstance().actionLoadImgResult(succeed,errorCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.alibaba.weex.commons.adapter.DefaultWebSocketAdapterFactory;
import com.alibaba.weex.commons.adapter.ImageAdapter;
import com.alibaba.weex.commons.adapter.JSExceptionAdapter;
import com.alibaba.weex.extend.adapter.ApmGenerator;
import com.alibaba.weex.extend.adapter.DefaultAccessibilityRoleAdapter;
import com.alibaba.weex.extend.adapter.InterceptWXHttpAdapter;
import com.alibaba.weex.extend.component.RichText;
Expand Down Expand Up @@ -74,6 +75,7 @@ public void onCreate() {
.setWebSocketAdapterFactory(new DefaultWebSocketAdapterFactory())
.setJSExceptionAdapter(new JSExceptionAdapter())
.setHttpAdapter(new InterceptWXHttpAdapter())
.setApmGenerater(new ApmGenerator())
.build()
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 com.alibaba.weex.extend.adapter;

import com.taobao.weex.performance.IApmGenerator;
import com.taobao.weex.performance.IWXApmMonitorAdapter;

/**
* @author zhongcang
* @date 2018/7/13
*/
public class ApmGenerator implements IApmGenerator {
@Override
public IWXApmMonitorAdapter generateApmInstance(String type) {
return new WXInstanceApmAdapter();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 com.alibaba.weex.extend.adapter;

import java.util.HashMap;
import java.util.Map;

import android.util.Log;
import com.taobao.weex.performance.IWXApmMonitorAdapter;
import com.taobao.weex.performance.WXInstanceApm;
import org.json.JSONException;
import org.json.JSONObject;

public class WXInstanceApmAdapter implements IWXApmMonitorAdapter {
private final APMInfo mInstanceInfo;
private final Map<String, APMInfo> mSubProcedureInfo;
private boolean isAppear = true;

WXInstanceApmAdapter() {
mInstanceInfo = new APMInfo(WXInstanceApm.WEEX_PAGE_TOPIC);
mSubProcedureInfo = new HashMap<>(1);
}

@Override
public void onStart(String instanceId) {
}

@Override
public void onEnd() {
printVale();
}

@Override
public void onEvent(String name, Object value) {
if (!isAppear){
return;
}
mInstanceInfo.eventMap.put(name, value);
}

@Override
public void onStage(String name, long timestamp) {
mInstanceInfo.stageMap.put(name, timestamp);
}

@Override
public void addProperty(String key, Object value) {
mInstanceInfo.propertyMap.put(key, value);
}

@Override
public void addStats(String key, double value) {
mInstanceInfo.statsMap.put(key, value);
}

@Override
public void onSubProcedureStage(String procedureName, String stageName) {
getAndCheckAndSubProcedureMap(procedureName).stageMap.put(stageName, System.currentTimeMillis());
}

@Override
public void onSubProcedureEvent(String procedureName, String eventName) {
getAndCheckAndSubProcedureMap(procedureName).eventMap.put(eventName, System.currentTimeMillis());
}

@Override
public void setSubProcedureStats(String procedureName, String name, double value) {
getAndCheckAndSubProcedureMap(procedureName).statsMap.put(name, value);
}

@Override
public void setSubProcedureProperties(String procedureName, String name, Object value) {
getAndCheckAndSubProcedureMap(procedureName).propertyMap.put(name, value);
}

@Override
public void onAppear() {
isAppear = true;
}

@Override
public void onDisappear() {
isAppear = false;
}

private APMInfo getAndCheckAndSubProcedureMap(String procedureName) {
APMInfo info = mSubProcedureInfo.get(procedureName);
if (null != info) {
return info;
}
synchronized (mSubProcedureInfo) {
info = new APMInfo(procedureName);
mSubProcedureInfo.put(procedureName, info);
}
return info;
}

private void printVale() {
String val = "";
try {
JSONObject subProcedureJson = new JSONObject();
for (Map.Entry<String, APMInfo> entry : mSubProcedureInfo.entrySet()) {
subProcedureJson.put(entry.getKey(), entry.getValue().toJson());
}
val = mInstanceInfo.toJson()
.put("subProcedures", subProcedureJson)
.toString();
} catch (JSONException e) {
e.printStackTrace();
}
Log.i("wxApmInstance", val);
//WXLogUtils.d("wxApmInstance", val);
}

private class APMInfo {
private String infoName;
private Map<String, Object> stageMap;
private Map<String, Object> propertyMap;
private Map<String, Object> eventMap;
private Map<String, Object> statsMap;

APMInfo(String name) {
this.infoName = name;
stageMap = new HashMap<>();
propertyMap = new HashMap<>();
eventMap = new HashMap<>();
statsMap = new HashMap<>();
}

JSONObject toJson() throws JSONException {
return new JSONObject()
.put("stage", convertMapToJSON(stageMap))
.put("property", convertMapToJSON(propertyMap))
.put("event", convertMapToJSON(eventMap))
.put("stats", convertMapToJSON(statsMap));
}

private JSONObject convertMapToJSON(Map<String, Object> params) throws JSONException {
JSONObject json = new JSONObject();
for (Map.Entry<String, Object> entry : params.entrySet()) {
json.put(entry.getKey(), entry.getValue());
}
return json;
}
}
}
17 changes: 15 additions & 2 deletions android/sdk/src/main/java/com/taobao/weex/InitConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* to you 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
Expand All @@ -28,6 +28,7 @@
import com.taobao.weex.adapter.URIAdapter;
import com.taobao.weex.appfram.storage.IWXStorageAdapter;
import com.taobao.weex.appfram.websocket.IWebSocketAdapterFactory;
import com.taobao.weex.performance.IApmGenerator;

/**
* Created by sospartan on 5/31/16.
Expand All @@ -44,6 +45,7 @@ public class InitConfig {
private IWXJSExceptionAdapter mJSExceptionAdapter;
private String framework;
private ClassLoaderAdapter classLoaderAdapter;
private IApmGenerator apmGenerater;

public IWXHttpAdapter getHttpAdapter() {
return httpAdapter;
Expand Down Expand Up @@ -85,6 +87,10 @@ public ClassLoaderAdapter getClassLoaderAdapter() {
return classLoaderAdapter;
}

public IApmGenerator getApmGenerater() {
return apmGenerater;
}

public InitConfig setClassLoaderAdapter(ClassLoaderAdapter classLoaderAdapter) {
this.classLoaderAdapter = classLoaderAdapter;
return this;
Expand All @@ -109,6 +115,7 @@ public static class Builder{
String framework;
IWebSocketAdapterFactory webSocketAdapterFactory;
ClassLoaderAdapter classLoaderAdapter;
IApmGenerator apmGenerater;

public Builder(){

Expand Down Expand Up @@ -169,6 +176,11 @@ public Builder setClassLoaderAdapter(ClassLoaderAdapter classLoaderAdapter) {
return this;
}

public Builder setApmGenerater(IApmGenerator apmGenerater){
this.apmGenerater =apmGenerater;
return this;
}

public InitConfig build(){
InitConfig config = new InitConfig();
config.httpAdapter = this.httpAdapter;
Expand All @@ -182,6 +194,7 @@ public InitConfig build(){
config.webSocketAdapterFactory = this.webSocketAdapterFactory;
config.mJSExceptionAdapter=this.mJSExceptionAdapter;
config.classLoaderAdapter = this.classLoaderAdapter;
config.apmGenerater = this.apmGenerater;
return config;
}
}
Expand Down
Loading