-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example implementation for AOP performance test annotation using …
…Spring Boot
- Loading branch information
Showing
6 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
16 changes: 16 additions & 0 deletions
16
week-9/aop-performance-test/src/main/java/com/epam/training/CustomTimer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} | ||
|
32 changes: 32 additions & 0 deletions
32
week-9/aop-performance-test/src/main/java/com/epam/training/CustomTimerAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
week-9/aop-performance-test/src/main/java/com/epam/training/ExampleService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"); | ||
} | ||
|
||
} | ||
|
24 changes: 24 additions & 0 deletions
24
week-9/aop-performance-test/src/main/java/com/epam/training/MainApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
|