BAEL-1205 Introduction to SPF4J (#7064)
* BAEL-1205 Introduction to SPF4j
This commit is contained in:
parent
7b0daba254
commit
d5f8b4e852
21
spf4j/pom.xml
Normal file
21
spf4j/pom.xml
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
<?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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<groupId>com.baeldung.spf4j</groupId>
|
||||||
|
<artifactId>spf4j</artifactId>
|
||||||
|
<name>spf4j</name>
|
||||||
|
<packaging>pom</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<modules>
|
||||||
|
<module>spf4j-core-app</module>
|
||||||
|
<module>spf4j-aspects-app</module>
|
||||||
|
</modules>
|
||||||
|
|
||||||
|
</project>
|
83
spf4j/spf4j-aspects-app/pom.xml
Normal file
83
spf4j/spf4j-aspects-app/pom.xml
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
<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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>spf4j-aspects-app</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>spf4j-aspects-app</name>
|
||||||
|
<parent>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../../</relativePath>
|
||||||
|
</parent>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.spf4j</groupId>
|
||||||
|
<artifactId>spf4j-aspects</artifactId>
|
||||||
|
<version>${spf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.spf4j</groupId>
|
||||||
|
<artifactId>spf4j-ui</artifactId>
|
||||||
|
<version>${spf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<finalName>spf4j-aspects-app</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.8.0</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-dependency-plugin</artifactId>
|
||||||
|
<version>3.1.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>copy-dependencies</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>copy-dependencies</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<outputDirectory>
|
||||||
|
${project.build.directory}/dependency-jars/
|
||||||
|
</outputDirectory>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>com.baeldung.spf4j.aspects.App</mainClass>
|
||||||
|
<addClasspath>true</addClasspath>
|
||||||
|
<classpathPrefix>dependency-jars/</classpathPrefix>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<spf4j.version>8.6.10</spf4j.version>
|
||||||
|
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.spf4j.aspects;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import org.spf4j.annotations.PerformanceMonitor;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
Spf4jConfig.initialize();
|
||||||
|
Random random = new Random();
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
long numberToCheck = random.nextInt(999_999_999 - 100_000_000 + 1) + 100_000_000;
|
||||||
|
isPrimeNumber(numberToCheck);
|
||||||
|
}
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PerformanceMonitor(warnThresholdMillis = 1, errorThresholdMillis = 100, recorderSource = Spf4jConfig.RecorderSourceForIsPrimeNumber.class)
|
||||||
|
public static boolean isPrimeNumber(long number) {
|
||||||
|
for (long i = 2; i <= number / 2; i++) {
|
||||||
|
if (number % i == 0)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.spf4j.aspects;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.spf4j.annotations.RecorderSourceInstance;
|
||||||
|
import org.spf4j.perf.MeasurementRecorderSource;
|
||||||
|
import org.spf4j.perf.impl.RecorderFactory;
|
||||||
|
|
||||||
|
public class Spf4jConfig {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||||
|
|
||||||
|
public static void initialize() {
|
||||||
|
String tsDbFile = System.getProperty("user.dir") + File.separator + "spf4j-performance-monitoring.tsdb2";
|
||||||
|
String tsTextFile = System.getProperty("user.dir") + File.separator + "spf4j-performance-monitoring.txt";
|
||||||
|
|
||||||
|
LOGGER.info("\nTime Series DB (TSDB) : {}\nTime Series text file : {}", tsDbFile, tsTextFile);
|
||||||
|
System.setProperty("spf4j.perf.ms.config", "TSDB@" + tsDbFile + "," + "TSDB_TXT@" + tsTextFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class RecorderSourceForIsPrimeNumber extends RecorderSourceInstance {
|
||||||
|
public static final MeasurementRecorderSource INSTANCE;
|
||||||
|
|
||||||
|
static {
|
||||||
|
Object forWhat = App.class + " isPrimeNumber";
|
||||||
|
String unitOfMeasurement = "ms";
|
||||||
|
int sampleTimeMillis = 1_000;
|
||||||
|
int factor = 10;
|
||||||
|
int lowerMagnitude = 0;
|
||||||
|
int higherMagnitude = 4;
|
||||||
|
int quantasPerMagnitude = 10;
|
||||||
|
INSTANCE = RecorderFactory.createScalableQuantizedRecorderSource(forWhat, unitOfMeasurement,
|
||||||
|
sampleTimeMillis, factor, lowerMagnitude, higherMagnitude, quantasPerMagnitude);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
spf4j/spf4j-aspects-app/src/main/resources/META-INF/aop.xml
Normal file
12
spf4j/spf4j-aspects-app/src/main/resources/META-INF/aop.xml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<aspectj>
|
||||||
|
<aspects>
|
||||||
|
<aspect
|
||||||
|
name="org.spf4j.perf.aspects.PerformanceMonitorAspect" />
|
||||||
|
</aspects>
|
||||||
|
<weaver options="-verbose">
|
||||||
|
<!-- make sure the classes you want to apply aspects to are included -->
|
||||||
|
<include within="com..*" />
|
||||||
|
<include
|
||||||
|
within="org.spf4j.perf.aspects.PerformanceMonitorAspect" />
|
||||||
|
</weaver>
|
||||||
|
</aspectj>
|
11
spf4j/spf4j-aspects-app/src/main/resources/logback.xml
Normal file
11
spf4j/spf4j-aspects-app/src/main/resources/logback.xml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT"
|
||||||
|
class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>[%level] %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<root level="debug">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
83
spf4j/spf4j-core-app/pom.xml
Normal file
83
spf4j/spf4j-core-app/pom.xml
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
<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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>spf4j-core-app</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>spf4j-core-app</name>
|
||||||
|
<parent>
|
||||||
|
<artifactId>parent-modules</artifactId>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
|
<relativePath>../../</relativePath>
|
||||||
|
</parent>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.spf4j</groupId>
|
||||||
|
<artifactId>spf4j-core</artifactId>
|
||||||
|
<version>${spf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.spf4j</groupId>
|
||||||
|
<artifactId>spf4j-ui</artifactId>
|
||||||
|
<version>${spf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
<build>
|
||||||
|
<finalName>spf4j-core-app</finalName>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>3.8.0</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-dependency-plugin</artifactId>
|
||||||
|
<version>3.1.1</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>copy-dependencies</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>copy-dependencies</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<outputDirectory>
|
||||||
|
${project.build.directory}/dependency-jars/
|
||||||
|
</outputDirectory>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<archive>
|
||||||
|
<manifest>
|
||||||
|
<mainClass>com.baeldung.spf4j.core.App</mainClass>
|
||||||
|
<addClasspath>true</addClasspath>
|
||||||
|
<classpathPrefix>dependency-jars/</classpathPrefix>
|
||||||
|
</manifest>
|
||||||
|
</archive>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<spf4j.version>8.6.10</spf4j.version>
|
||||||
|
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||||
|
</properties>
|
||||||
|
</project>
|
@ -0,0 +1,34 @@
|
|||||||
|
package com.baeldung.spf4j.core;
|
||||||
|
|
||||||
|
import java.util.Random;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.spf4j.perf.MeasurementRecorder;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||||
|
|
||||||
|
public static void main(String[] args) throws InterruptedException {
|
||||||
|
Spf4jConfig.initialize();
|
||||||
|
MeasurementRecorder measurementRecorder = Spf4jConfig.getMeasurementRecorder(App.class + " isPrimeNumber");
|
||||||
|
Random random = new Random();
|
||||||
|
for (int i = 0; i < 100; i++) {
|
||||||
|
long numberToCheck = random.nextInt(999_999_999 - 100_000_000 + 1) + 100_000_000;
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
boolean isPrime = isPrimeNumber(numberToCheck);
|
||||||
|
measurementRecorder.record(System.currentTimeMillis() - startTime);
|
||||||
|
LOGGER.info("{}. {} is prime? {}", i + 1, numberToCheck, isPrime);
|
||||||
|
}
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isPrimeNumber(long number) {
|
||||||
|
for (long i = 2; i <= number / 2; i++) {
|
||||||
|
if (number % i == 0)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package com.baeldung.spf4j.core;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.spf4j.perf.MeasurementRecorder;
|
||||||
|
import org.spf4j.perf.impl.RecorderFactory;
|
||||||
|
|
||||||
|
public class Spf4jConfig {
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||||
|
|
||||||
|
public static void initialize() {
|
||||||
|
String tsDbFile = System.getProperty("user.dir") + File.separator + "spf4j-performance-monitoring.tsdb2";
|
||||||
|
String tsTextFile = System.getProperty("user.dir") + File.separator + "spf4j-performance-monitoring.txt";
|
||||||
|
|
||||||
|
LOGGER.info("\nTime Series DB (TSDB) : {}\nTime Series text file : {}", tsDbFile, tsTextFile);
|
||||||
|
System.setProperty("spf4j.perf.ms.config", "TSDB@" + tsDbFile + "," + "TSDB_TXT@" + tsTextFile);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static MeasurementRecorder getMeasurementRecorder(Object forWhat) {
|
||||||
|
String unitOfMeasurement = "ms";
|
||||||
|
int sampleTimeMillis = 1_000;
|
||||||
|
int factor = 10;
|
||||||
|
int lowerMagnitude = 0;
|
||||||
|
int higherMagnitude = 4;
|
||||||
|
int quantasPerMagnitude = 10;
|
||||||
|
|
||||||
|
return RecorderFactory.createScalableQuantizedRecorder(forWhat, unitOfMeasurement, sampleTimeMillis, factor, lowerMagnitude, higherMagnitude, quantasPerMagnitude);
|
||||||
|
}
|
||||||
|
}
|
11
spf4j/spf4j-core-app/src/main/resources/logback.xml
Normal file
11
spf4j/spf4j-core-app/src/main/resources/logback.xml
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT"
|
||||||
|
class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>[%level] %msg%n</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<root level="debug">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
Loading…
x
Reference in New Issue
Block a user