grafana jpprof 是为了方便grafana phlare 对于java 语言支持开发的一个包,可以让java 语言方便的自持pprof 格式的内容,进行持续性能优化
以下是一个简单的试用
环境准备
- pom 文件
<?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.dalong</groupId>
<artifactId>jjprofilerapps</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>com.grafana</groupId>
<artifactId>jpprof</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.9.4</version>
</dependency>
</dependencies>
<repositories>
<repository>
<name>maven</name>
<id>maven</id>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<url>https://repo1.maven.org/maven2/</url>
</repository>
</repositories>
<build>
<finalName>myinstance-app</finalName>
<plugins>
<plugin>
<groupId>com.google.cloud.tools</groupId>
<artifactId>jib-maven-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<from>
<image>eclipse-temurin:11-jdk-alpine</image>
</from>
<to>
<image>dalongrong/jpprof-learning</image>
</to>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.dalong.Application</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
- 入口代码
基于了spark java web 框架
package com.dalong;
import jpprof.CPUProfiler;
import spark.Service;
import java.io.IOException;
import java.time.Duration;
public class Application {
public static void main(String[] args) throws IOException {
Service service = Service.ignite();
service.port(9999);
// 内容参考子PprofHttpHandler 做了一些修改,方便支持sparkjava
service.get("/debug/pprof/profile", (req, res) -> {
String seconds = req.queryParams("seconds");
Duration duration = Duration.ofSeconds((long) Integer.parseInt(seconds));
try {
res.header("Content-Encoding", "gzip");
res.status(200);
CPUProfiler.start(duration, res.raw().getOutputStream());
} catch (Exception var9) {
res.status(500);
} finally {
res.raw().getOutputStream().close();
}
return null;
});
service.get("/demo", (request, response) -> {
return "dalongdemo";
});
}
}
使用
启动服务,并进行压测,然后使用go pprof 获取pprof 内容,显示效果
- 压测
ab -n 4000 -c 100 http://localhost:9999/demo
- 获取内容
go tool pprof -http :6060 http://localhost:9999/debug/pprof/profile\?seconds\=10
- 效果
说明
jpprof 是基于jdk 11 开发的,所以目前会有一些问题(尤其是国内还是jdk 8 为主流的时候),jpprof 是一个很不错的工具包,让我们可以体验golang pprof 便捷的能力
一个现成可以运行试用的镜像dalongrong/jpprof-learning
参考资料
https://sparkjava.com/documentation#redirects
https://github.com/grafana/JPProf
https://github.com/rongfengliang/grafana-jpprof-learning