(jsrt) fix: elapsed time calc

This commit is contained in:
lollipopkit🏳️‍⚧️
2025-07-16 12:53:35 +08:00
parent 9a48ed47f4
commit 9d9070c899
4 changed files with 19 additions and 13 deletions

View File

@@ -502,6 +502,11 @@ func JSRuntimeMiddleware() *gin.HandlerFunc {
return
}
duration := time.Since(start)
if duration > time.Millisecond*100 {
common.SysLog(fmt.Sprintf("JS Runtime PreProcess took %v", duration))
}
// 后处理
if pool.hasPostProcessFunction() {
writer := newResponseWriter(c.Writer)
@@ -511,6 +516,8 @@ func JSRuntimeMiddleware() *gin.HandlerFunc {
// 后处理响应
if writer.body.Len() > 0 {
start := time.Now()
statusCode, body, err := pool.PostProcessResponse(c, writer.statusCode, writer.body.Bytes())
if err == nil {
c.Writer = writer.ResponseWriter
@@ -548,6 +555,11 @@ func JSRuntimeMiddleware() *gin.HandlerFunc {
common.SysError(fmt.Sprintf("JS Runtime PostProcess Error: %v", err))
}
duration := time.Since(start)
if duration > time.Millisecond*100 {
common.SysLog(fmt.Sprintf("JS Runtime PostProcess took %v", duration))
}
} else {
// 没有响应体时恢复原始writer
c.Writer = writer.ResponseWriter
@@ -555,12 +567,6 @@ func JSRuntimeMiddleware() *gin.HandlerFunc {
} else {
c.Next()
}
// 记录处理时间
duration := time.Since(start)
if duration > time.Millisecond*100 {
common.SysLog(fmt.Sprintf("JS Runtime processing took %v", duration))
}
}
return &fn
}

View File

@@ -1,8 +1,8 @@
// Utility functions for JavaScript runtime
function logWithTimestamp(message) {
const timestamp = new Date().toISOString();
console.log(`[${timestamp}] ${message}`);
function logWithReq(message) {
let reqPath = req.url || 'unknown path';
console.log(`[${req.method} ${reqPath}] ${message}`);
}
function safeJsonParse(str, defaultValue = null) {

View File

@@ -1,5 +1,5 @@
// Pre-processing function for incoming requests
function preProcessRequest(req) {
logWithTimestamp('Pre-processing request: ' + req.method + ' ' + req.url);
function preProcessRequest() {
logWithReq('Pre-processing request');
}

View File

@@ -1,5 +1,5 @@
// Post-processing function for outgoing responses
function postProcessResponse(req, res) {
logWithTimestamp('Post-processing response with status: ' + res.statusCode);
function postProcessResponse() {
logWithReq('Post-processing response with status: ' + req.statusCode);
}