Skip to content

Commit

Permalink
fix(android): add res load reslut to record jni
Browse files Browse the repository at this point in the history
  • Loading branch information
siguangli2018 committed Sep 5, 2023
1 parent 47ac652 commit d9efa37
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ void OnNativeInitEnd(JNIEnv* j_env, jobject j_object, jint j_scope_id, jlong sta

void OnFirstFrameEnd(JNIEnv* j_env, jobject j_object, jint j_scope_id, jlong time);

void OnResourceLoadEnd(JNIEnv* j_env, jobject j_object, jint j_scope_id, jstring j_uri, jlong j_start_time, jlong j_end_time);

void OnResourceLoadError(JNIEnv* j_env, jobject j_object, jint j_scope_id, jstring j_uri, jlong j_ret_code, jstring j_error_msg);
void OnResourceLoadEnd(JNIEnv* j_env, jobject j_object, jint j_scope_id, jstring j_uri, jlong j_start_time, jlong j_end_time, jlong j_ret_code, jstring j_error_msg);

}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,9 @@ REGISTER_JNI("com/openhippy/connector/JsDriver", // NOLINT(cert-err58-cpp)

REGISTER_JNI("com/openhippy/connector/JsDriver", // NOLINT(cert-err58-cpp)
"onResourceLoadEnd",
"(ILjava/lang/String;JJ)V",
"(ILjava/lang/String;JJJLjava/lang/String;)V",
OnResourceLoadEnd)

REGISTER_JNI("com/openhippy/connector/JsDriver", // NOLINT(cert-err58-cpp)
"onResourceLoadError",
"(ILjava/lang/String;JLjava/lang/String;)V",
OnResourceLoadError)

using string_view = footstone::stringview::string_view;
using u8string = footstone::string_view::u8string;
using StringViewUtils = footstone::stringview::StringViewUtils;
Expand Down Expand Up @@ -195,44 +190,29 @@ void OnFirstFrameEnd(JNIEnv* j_env, jobject j_object, jint j_scope_id, jlong tim
}
}

void OnResourceLoadEnd(JNIEnv* j_env, jobject j_object, jint j_scope_id, jstring j_uri, jlong j_start_time, jlong j_end_time) {
void OnResourceLoadEnd(JNIEnv* j_env, jobject j_object, jint j_scope_id, jstring j_uri,
jlong j_start_time, jlong j_end_time, jlong j_ret_code, jstring j_error_msg) {
if (!j_uri) {
return;
}
auto uri = JniUtils::ToStrView(j_env, j_uri);
auto ret_code = static_cast<int32_t>(j_ret_code);
auto error_msg = j_error_msg ? JniUtils::ToStrView(j_env, j_error_msg) : string_view("");
auto scope = GetScope(j_scope_id);
auto runner = scope->GetEngine().lock()->GetJsTaskRunner();
if (runner) {
std::weak_ptr<Scope> weak_scope = scope;
auto task = [weak_scope, uri, j_start_time, j_end_time]() {
auto task = [weak_scope, uri, j_start_time, j_end_time, ret_code, error_msg]() {
auto scope = weak_scope.lock();
if (scope) {
auto entry = scope->GetPerformance()->PerformanceResource(uri);
if (entry) {
entry->SetLoadSourceStart(footstone::TimePoint::FromEpochDelta(footstone::TimeDelta::FromMilliseconds(j_start_time)));
entry->SetLoadSourceEnd(footstone::TimePoint::FromEpochDelta(footstone::TimeDelta::FromMilliseconds(j_end_time)));
}
}
};
runner->PostTask(std::move(task));
}
}

void OnResourceLoadError(JNIEnv* j_env, jobject j_object, jint j_scope_id, jstring j_uri, jlong j_ret_code, jstring j_error_msg) {
if (!j_uri) {
return;
}
auto uri = JniUtils::ToStrView(j_env, j_uri);
auto ret_code = static_cast<int32_t>(j_ret_code);
auto error_msg = j_error_msg ? JniUtils::ToStrView(j_env, j_error_msg) : string_view("");
auto scope = GetScope(j_scope_id);
auto runner = scope->GetEngine().lock()->GetJsTaskRunner();
if (runner) {
std::weak_ptr<Scope> weak_scope = scope;
auto task = [weak_scope, uri, ret_code, error_msg]() {
auto scope = weak_scope.lock();
if (scope) {
scope->HandleUriLoaderError(uri, ret_code, error_msg);
if (ret_code != 0) {
scope->HandleUriLoaderError(uri, ret_code, error_msg);
}
}
};
runner->PostTask(std::move(task));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,9 @@ public void recordFirstFrameEndTime(long time) {
onFirstFrameEnd(mInstanceId, time);
}

public void recordResourceLoadEndTime(@NonNull String uri, long startTime, long endTime) {
onResourceLoadEnd(mInstanceId, uri, startTime, endTime);
}

public void recordResourceLoadError(@NonNull String uri, long retCode, String errorMsg) {
onResourceLoadError(mInstanceId, uri, retCode, errorMsg);
public void doRecordResourceLoadResult(@NonNull String uri, long startTime, long endTime,
long retCode, @Nullable String errorMsg) {
onResourceLoadEnd(mInstanceId, uri, startTime, endTime, retCode, errorMsg);
}

public void onResourceReady(ByteBuffer output, long resId) {
Expand Down Expand Up @@ -113,7 +110,8 @@ public void callFunction(String action, NativeCallback callback,

public boolean runScriptFromUri(String uri, AssetManager assetManager, boolean canUseCodeCache,
String codeCacheDir, int vfsId, NativeCallback callback) {
return runScriptFromUri(mInstanceId, uri, assetManager, canUseCodeCache, codeCacheDir, vfsId,
return runScriptFromUri(mInstanceId, uri, assetManager, canUseCodeCache, codeCacheDir,
vfsId,
callback);
}

Expand Down Expand Up @@ -150,7 +148,8 @@ private native int onCreate(byte[] globalConfig, boolean useLowMemoryMode,
private native void onDestroy(int instanceId, boolean useLowMemoryMode, boolean isReload,
NativeCallback callback);

private native void loadInstance(int instanceId, byte[] buffer, int offset, int length, NativeCallback callback);
private native void loadInstance(int instanceId, byte[] buffer, int offset, int length,
NativeCallback callback);

private native void unloadInstance(int instanceId, byte[] buffer, int offset, int length);

Expand All @@ -169,7 +168,6 @@ private native void callFunction(int instanceId, String action, NativeCallback c

private native void onFirstFrameEnd(int instanceId, long time);

private native void onResourceLoadEnd(int instanceId, String uri, long startTime, long endTime);

private native void onResourceLoadError(int instanceId, String uri, long retCode, String errorMsg);
private native void onResourceLoadEnd(int instanceId, String uri, long startTime, long endTime,
long retCode, String errorMsg);
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,8 @@ public void handleResponseAsync(@NonNull ResourceDataHolder holder,
@NonNull ProcessorCallback callback) {
HippyEngineContext engineContext = mEngineContextRef.get();
if (shouldDoRecord(holder) && engineContext != null) {
engineContext.getJsDriver().recordResourceLoadEndTime(holder.uri, holder.loadStartTime,
System.currentTimeMillis());
if (holder.resultCode != 0) {
engineContext.getJsDriver().recordResourceLoadError(holder.uri, holder.resultCode,
holder.errorMessage);
}
engineContext.getJsDriver().doRecordResourceLoadResult(holder.uri, holder.loadStartTime,
System.currentTimeMillis(), holder.resultCode, holder.errorMessage);
}
super.handleResponseAsync(holder, callback);
}
Expand All @@ -72,12 +68,8 @@ public void handleResponseAsync(@NonNull ResourceDataHolder holder,
public void handleResponseSync(@NonNull ResourceDataHolder holder) {
HippyEngineContext engineContext = mEngineContextRef.get();
if (shouldDoRecord(holder) && engineContext != null) {
engineContext.getJsDriver().recordResourceLoadEndTime(holder.uri, holder.loadStartTime,
System.currentTimeMillis());
if (holder.resultCode != 0) {
engineContext.getJsDriver().recordResourceLoadError(holder.uri, holder.resultCode,
holder.errorMessage);
}
engineContext.getJsDriver().doRecordResourceLoadResult(holder.uri, holder.loadStartTime,
System.currentTimeMillis(), holder.resultCode, holder.errorMessage);
}
super.handleResponseSync(holder);
}
Expand Down

0 comments on commit d9efa37

Please sign in to comment.