Skip to content

Commit

Permalink
[#234] 메서드 실행 시간 측정 기능 구현 (#237)
Browse files Browse the repository at this point in the history
* refactor: Config 재설정하기

- SchedulerConfig 추가
- Scheduler 도메인 별 분리
- AsyncConfig 및 AsyncExceptionHandler 추가

* chore(package): 패키지 명 및 구조 변경

* chore: 설정 파일 명칭 변경 및 내용 이동

* feat: 메서드 실행시간 측정 로깅 추가 및 P6spy stack trace 추가
  • Loading branch information
JoosungKwon authored Jul 23, 2023
1 parent 48b3878 commit 75b474b
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.prgrms.mukvengers.global.config.async;

import java.util.concurrent.Executor;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import lombok.RequiredArgsConstructor;

@EnableAsync
@Configuration
@RequiredArgsConstructor
public class AsyncConfig implements AsyncConfigurer {

private final ThreadPoolTaskExecutor threadPoolTaskExecutor;

@Override
public Executor getAsyncExecutor() {
return threadPoolTaskExecutor;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.prgrms.mukvengers.global.config.p6spy;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Locale;

Expand Down Expand Up @@ -29,7 +30,7 @@ public String formatMessage(int connectionId, String now, long elapsed, String c

SimpleDateFormat formatter = new SimpleDateFormat("yy.MM.dd HH:mm:ss");

return formatter.format(currentDate) + " | " + "OperationTime : " + elapsed + "ms" + sql;
return category + " | " + "OperationTime : " + elapsed + "ms" + sql;
}

private String formatSql(String category, String sql) {
Expand All @@ -39,16 +40,21 @@ private String formatSql(String category, String sql) {

if (Category.STATEMENT.getName().equals(category)) {
String tmpsql = sql.trim().toLowerCase(Locale.ROOT);
if (tmpsql.startsWith("create") || tmpsql.startsWith("alter") || tmpsql.startsWith(
"comment")) {
if (tmpsql.startsWith("create") || tmpsql.startsWith("alter")) {
sql = FormatStyle.DDL.getFormatter().format(sql);
} else {
sql = FormatStyle.BASIC.getFormatter().format(sql);
}
sql = "|\n Hibernate FormatSql(P6Spy sql, Hibernate format): " + sql + "\n";
sql = "\n" + stackTrace() + "\n" + sql + "\n";
}

return sql;
}

private String stackTrace() {
return Arrays.toString(Arrays.stream(new Throwable().getStackTrace())
.filter(t -> t.toString().startsWith("com.prgrms.mukvengers"))
.toArray()).replace(", ", "\n");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.prgrms.mukvengers.global.config.scheduler;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

@EnableScheduling
@Configuration
@ComponentScan(basePackages = "com.prgrms.mukvengers.global.scheduler")
public class SchedulerConfig implements SchedulingConfigurer {

private static final int POOL_SIZE = 10;

@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
threadPoolTaskScheduler.setPoolSize(POOL_SIZE);
threadPoolTaskScheduler.setThreadNamePrefix("scheduled-task-pool-");
threadPoolTaskScheduler.initialize();

taskRegistrar.setTaskScheduler(threadPoolTaskScheduler);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter;
import org.springframework.security.web.SecurityFilterChain;

import com.prgrms.mukvengers.global.security.token.filter.JwtAuthenticationEntryPoint;
import com.prgrms.mukvengers.global.security.token.filter.JwtAuthenticationFilter;
import com.prgrms.mukvengers.global.security.oauth.repository.HttpCookieOAuthAuthorizationRequestRepository;
import com.prgrms.mukvengers.global.security.oauth.handler.OAuthAuthenticationFailureHandler;
import com.prgrms.mukvengers.global.security.oauth.handler.OAuthAuthenticationSuccessHandler;
import com.prgrms.mukvengers.global.security.oauth.repository.HttpCookieOAuthAuthorizationRequestRepository;
import com.prgrms.mukvengers.global.security.token.exception.ExceptionHandlerFilter;
import com.prgrms.mukvengers.global.security.token.filter.JwtAuthenticationEntryPoint;
import com.prgrms.mukvengers.global.security.token.filter.JwtAuthenticationFilter;

import lombok.RequiredArgsConstructor;

@Configuration
@EnableWebSecurity
@Configuration
@RequiredArgsConstructor
public class SecurityConfig {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@

import lombok.RequiredArgsConstructor;

@EnableWebSocketMessageBroker
@Configuration
@RequiredArgsConstructor
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

private final StompHandler stompHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.springframework.web.servlet.NoHandlerFoundException;

import com.prgrms.mukvengers.global.common.dto.ErrorResponse;
import com.prgrms.mukvengers.global.slack.annotation.SlackNotification;
import com.prgrms.mukvengers.global.report.annotation.SlackNotification;
import com.prgrms.mukvengers.global.utils.MessageUtil;

import lombok.extern.slf4j.Slf4j;
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/com/prgrms/mukvengers/global/log/LoggingAspect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.prgrms.mukvengers.global.log;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Aspect
@Component
// @Profile("performance")
public class LoggingAspect {

@Pointcut("execution(* com.prgrms.mukvengers.domain..*(..))")
public void methodInDomain() {
}

// 메서드 실행시간 분석
@Around("methodInDomain()")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
Object result = joinPoint.proceed();
long endTime = System.currentTimeMillis();

long executionTimeMillis = endTime - startTime;

Signature signature = joinPoint.getSignature();

String methodName = signature.getName();
String className = signature.getDeclaringType().getSimpleName();
log.info("[ExecutionTime] : {}.{} took {}ms", className, methodName, executionTimeMillis);

return result;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.prgrms.mukvengers.global.slack;
package com.prgrms.mukvengers.global.report;

import static java.util.Collections.*;

Expand All @@ -23,7 +23,7 @@
import net.gpedro.integrations.slack.SlackField;
import net.gpedro.integrations.slack.SlackMessage;

import com.prgrms.mukvengers.global.slack.dto.RequestInfo;
import com.prgrms.mukvengers.global.report.dto.RequestInfo;

@Aspect
@Component
Expand All @@ -40,7 +40,7 @@ public SlackNotificationAspect(@Value("${spring.slack.webhook}") String webhook,
this.env = env;
}

@Around("@annotation(com.prgrms.mukvengers.global.slack.annotation.SlackNotification) && args(request, e)")
@Around("@annotation(com.prgrms.mukvengers.global.report.annotation.SlackNotification) && args(request, e)")
public void slackNotificate(ProceedingJoinPoint proceedingJoinPoint, HttpServletRequest request,
Exception e) throws Throwable {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.prgrms.mukvengers.global.slack.annotation;
package com.prgrms.mukvengers.global.report.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.prgrms.mukvengers.global.slack.dto;
package com.prgrms.mukvengers.global.report.dto;

import javax.servlet.http.HttpServletRequest;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.prgrms.mukvengers.global.scheduler;

import java.time.LocalDateTime;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.prgrms.mukvengers.domain.crew.repository.CrewRepository;

import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
public class CrewScheduler {

private final CrewRepository crewRepository;

@Async
@Scheduled(cron = "0 0 * * * *", zone = "Asia/Seoul")
@Transactional
public void changStatusOvertimeCrew() {
crewRepository.updateAllStatusToFinish(LocalDateTime.now());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,20 @@
import java.time.LocalDateTime;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.prgrms.mukvengers.domain.crew.repository.CrewRepository;
import com.prgrms.mukvengers.domain.proposal.repository.ProposalRepository;

import lombok.RequiredArgsConstructor;

@Component
@EnableAsync
@EnableScheduling
@RequiredArgsConstructor
public class Scheduler {
public class ProposalScheduler {

private final CrewRepository crewRepository;
private final ProposalRepository proposalRepository;

@Async
@Scheduled(cron = "0 0 * * * *", zone = "Asia/Seoul")
@Transactional
public void changStatusOvertimeCrew() {
crewRepository.updateAllStatusToFinish(LocalDateTime.now());
}

@Async
@Scheduled(cron = "0 0 0 * * *", zone = "Asia/Seoul")
@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
spring:
servlet:
multipart:
max-request-size: 10MB
max-file-size: 10MB

cloud:
aws:
s3:
Expand Down
8 changes: 6 additions & 2 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ spring:
include:
- db
- security
- aws
- slack
- cloud
# ResourceHandler 설정 (Static Resource Mapping)
web:
resources:
Expand All @@ -26,6 +25,11 @@ spring:
encoding: UTF-8
basename: messages/exceptions/exception, messages/notification/messages

servlet:
multipart:
max-request-size: 10MB
max-file-size: 10MB

slack:
webhook: ${SLACK_WEBHOOK}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.prgrms.mukvengers.global.slack;
package com.prgrms.mukvengers.global.report;

import static org.mockito.BDDMockito.*;

Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ spring:
include:
- test
- security
- aws
- cloud

0 comments on commit 75b474b

Please sign in to comment.