From 2cb0640a7b790efd9713ae249c9c457e3ea63720 Mon Sep 17 00:00:00 2001 From: mainmain <57514971+mainmainer@users.noreply.github.com> Date: Wed, 8 Nov 2023 17:25:23 +0800 Subject: [PATCH] (improvement)(chat) add timecost aspect (#339) --- .../supersonic/chat/service/TimeCost.java | 14 ++++++++ .../supersonic/chat/service/TimeCostAOP.java | 33 +++++++++++++++++++ .../chat/service/impl/QueryServiceImpl.java | 5 +-- 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 chat/core/src/main/java/com/tencent/supersonic/chat/service/TimeCost.java create mode 100644 chat/core/src/main/java/com/tencent/supersonic/chat/service/TimeCostAOP.java diff --git a/chat/core/src/main/java/com/tencent/supersonic/chat/service/TimeCost.java b/chat/core/src/main/java/com/tencent/supersonic/chat/service/TimeCost.java new file mode 100644 index 000000000..29d915c73 --- /dev/null +++ b/chat/core/src/main/java/com/tencent/supersonic/chat/service/TimeCost.java @@ -0,0 +1,14 @@ +package com.tencent.supersonic.chat.service; + +import java.lang.annotation.Documented; +import java.lang.annotation.Target; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Target({ElementType.PARAMETER, ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface TimeCost { + +} diff --git a/chat/core/src/main/java/com/tencent/supersonic/chat/service/TimeCostAOP.java b/chat/core/src/main/java/com/tencent/supersonic/chat/service/TimeCostAOP.java new file mode 100644 index 000000000..f1c094a76 --- /dev/null +++ b/chat/core/src/main/java/com/tencent/supersonic/chat/service/TimeCostAOP.java @@ -0,0 +1,33 @@ +package com.tencent.supersonic.chat.service; + +import com.tencent.supersonic.chat.api.pojo.response.QueryResult; +import lombok.extern.slf4j.Slf4j; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Pointcut; +import org.springframework.stereotype.Component; + +@Slf4j +@Component +@Aspect +public class TimeCostAOP { + + @Pointcut("@annotation(com.tencent.supersonic.chat.service.TimeCost)") + private void timeCostAdvicePointcut() { + + } + + @Around("timeCostAdvicePointcut()") + public Object timeCostAdvice(ProceedingJoinPoint joinPoint) throws Throwable { + log.info("begin to add time cost!"); + Long startTime = System.currentTimeMillis(); + Object object = joinPoint.proceed(); + if (object instanceof QueryResult) { + QueryResult queryResult = (QueryResult) object; + queryResult.setQueryTimeCost(System.currentTimeMillis() - startTime); + return queryResult; + } + return object; + } +} diff --git a/chat/core/src/main/java/com/tencent/supersonic/chat/service/impl/QueryServiceImpl.java b/chat/core/src/main/java/com/tencent/supersonic/chat/service/impl/QueryServiceImpl.java index b52b101e4..63aee2288 100644 --- a/chat/core/src/main/java/com/tencent/supersonic/chat/service/impl/QueryServiceImpl.java +++ b/chat/core/src/main/java/com/tencent/supersonic/chat/service/impl/QueryServiceImpl.java @@ -35,6 +35,7 @@ import com.tencent.supersonic.chat.service.ChatService; import com.tencent.supersonic.chat.service.QueryService; import com.tencent.supersonic.chat.service.SemanticService; import com.tencent.supersonic.chat.service.StatisticsService; +import com.tencent.supersonic.chat.service.TimeCost; import com.tencent.supersonic.chat.utils.ComponentFactory; import com.tencent.supersonic.chat.utils.SolvedQueryManager; import com.tencent.supersonic.common.pojo.DateConf; @@ -208,8 +209,8 @@ public class QueryServiceImpl implements QueryService { } @Override + @TimeCost public QueryResult performExecution(ExecuteQueryReq queryReq) throws Exception { - Long executeTime = System.currentTimeMillis(); ChatParseDO chatParseDO = chatService.getParseInfo(queryReq.getQueryId(), queryReq.getParseId()); ChatQueryDO chatQueryDO = chatService.getLastQuery(queryReq.getChatId()); @@ -249,7 +250,6 @@ public class QueryServiceImpl implements QueryService { } else { chatService.deleteChatQuery(queryReq.getQueryId()); } - queryResult.setQueryTimeCost(System.currentTimeMillis() - executeTime); return queryResult; } @@ -334,6 +334,7 @@ public class QueryServiceImpl implements QueryService { //mainly used for executing after revising filters,for example:"fans_cnt>=100000"->"fans_cnt>500000", //"style='流行'"->"style in ['流行','爱国']" @Override + @TimeCost public QueryResult executeDirectQuery(QueryDataReq queryData, User user) throws SqlParseException { ChatParseDO chatParseDO = chatService.getParseInfo(queryData.getQueryId(), queryData.getParseId());