Skip to content

Commit

Permalink
Merge pull request #22 from epam-deik-cooperation/week-9/aop-performa…
Browse files Browse the repository at this point in the history
…nce-test

Add example implementation for AOP performance test annotation using Spring Boot
  • Loading branch information
lamfalusy authored Nov 13, 2020
2 parents 5d94c54 + 9035a07 commit ad9c3c4
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 0 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<module>week-7/ceasar-coding</module>
<module>week-8/dependency-injection-framework</module> <module>week-8/exception-handling</module>
<module>week-8/json-serialization</module>
<module>week-9/aop-performance-test</module>
</modules>

<dependencyManagement>
Expand Down Expand Up @@ -85,6 +86,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
Expand Down
30 changes: 30 additions & 0 deletions week-9/aop-performance-test/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>unideb-prog2-parent</artifactId>
<groupId>com.epam.training</groupId>
<version>1.0-SNAPSHOT</version>
<relativePath>../..</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>aop-performance-test</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.epam.training;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Custom performance test marker annotation.
* Execution time for every method marked with this annotation will be collected and printed to stdout.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomTimer {
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.epam.training;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;

/**
* Custom aspect for collecting execution time for methods annotated with {@link CustomTimer}.
*/
@Aspect
@Service
public class CustomTimerAspect {

@Around("@annotation(com.epam.training.CustomTimer)")
public Object timeAnnotatedMethod(ProceedingJoinPoint pjp) throws Throwable {
// start stopwatch
StopWatch stopWatch = new StopWatch(pjp.getTarget().toString());
stopWatch.start();

Object retVal = pjp.proceed();

// stop stopwatch
stopWatch.stop();

System.out.println("Execution time for " + pjp.getTarget().toString() + ": " + stopWatch.getTotalTimeMillis() + " ms");

return retVal;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.epam.training;

import org.springframework.stereotype.Service;

@Service
public class ExampleService {

@CustomTimer
public void exampleMethodToTime() throws InterruptedException {
System.out.println("Testee start!");

Thread.sleep(1000);

System.out.println("Testee end!");
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.epam.training;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@SpringBootApplication
@EnableAspectJAutoProxy
public class MainApplication {

public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}

@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> ctx.getBean(ExampleService.class).exampleMethodToTime();
}

}

0 comments on commit ad9c3c4

Please sign in to comment.