├── .coveralls.yml ├── _config.yml ├── MyPerf4J-ASM └── src │ ├── test │ ├── resources │ │ └── config │ │ │ └── myPerf4J.properties │ └── java │ │ └── MyPerf4J │ │ ├── test1 │ │ ├── Foo.java │ │ ├── ExitingAdapter.java │ │ ├── EnteringAdapter.java │ │ ├── Test.java │ │ ├── FinallyAdapter.java │ │ └── MyClassAdapter.java │ │ ├── dynamic │ │ ├── DaoInvocationHandler.java │ │ ├── DaoInvocationHandlerV2.java │ │ ├── Test.java │ │ ├── DynamicMethodVisitor.java │ │ └── DynamicClassAdapter.java │ │ ├── ClassToTest.java │ │ └── TypeDestUtilsTest.java │ └── main │ └── java │ └── cn │ └── myperf4j │ └── asm │ ├── PreMain.java │ ├── ASMRecorderMaintainer.java │ ├── aop │ └── ProfilingDynamicMethodVisitor.java │ └── ASMBootstrap.java ├── .gitignore ├── MyPerf4J-Base ├── src │ ├── main │ │ └── java │ │ │ └── cn │ │ │ └── myperf4j │ │ │ └── base │ │ │ ├── util │ │ │ ├── SysProperties.java │ │ │ ├── collections │ │ │ │ ├── ArrayUtils.java │ │ │ │ ├── SetUtils.java │ │ │ │ ├── ListUtils.java │ │ │ │ └── MapUtils.java │ │ │ ├── io │ │ │ │ ├── IOUtils.java │ │ │ │ └── InputStreamUtils.java │ │ │ ├── text │ │ │ │ ├── DateFormatUtils.java │ │ │ │ └── NumFormatUtils.java │ │ │ ├── net │ │ │ │ └── NetUtils.java │ │ │ ├── NumUtils.java │ │ │ ├── concurrent │ │ │ │ ├── ExecutorManager.java │ │ │ │ ├── ThreadUtils.java │ │ │ │ └── IntHashCounter.java │ │ │ ├── LineProtocolUtils.java │ │ │ ├── StrMatchUtils.java │ │ │ ├── PkgExpUtils.java │ │ │ └── DateUtils.java │ │ │ ├── influxdb │ │ │ ├── InfluxDbClient.java │ │ │ └── InfluxDbClientFactory.java │ │ │ ├── log │ │ │ ├── ILogger.java │ │ │ ├── NullLogger.java │ │ │ ├── StdoutLogger.java │ │ │ ├── LoggerFactory.java │ │ │ └── AutoRollingLogger.java │ │ │ ├── metric │ │ │ ├── exporter │ │ │ │ ├── JvmGcMetricsExporter.java │ │ │ │ ├── MethodMetricsExporter.java │ │ │ │ ├── JvmGcMetricsV3Exporter.java │ │ │ │ ├── JvmClassMetricsExporter.java │ │ │ │ ├── JvmMemoryMetricsExporter.java │ │ │ │ ├── JvmThreadMetricsExporter.java │ │ │ │ ├── JvmBufferPoolMetricsExporter.java │ │ │ │ ├── JvmCompilationMetricsExporter.java │ │ │ │ ├── JvmFileDescMetricsExporter.java │ │ │ │ ├── discard │ │ │ │ │ ├── DiscardJvmGcMetricsExporter.java │ │ │ │ │ ├── DiscardJvmGcMetricsV3Exporter.java │ │ │ │ │ ├── DiscardJvmClassMetricsExporter.java │ │ │ │ │ ├── DiscardJvmMemoryMetricsExporter.java │ │ │ │ │ ├── DiscardJvmThreadMetricsExporter.java │ │ │ │ │ ├── DiscardJvmBufferPoolMetricsExporter.java │ │ │ │ │ ├── DiscardJvmFileDescMetricsExporter.java │ │ │ │ │ └── DiscardJvmCompilationMetricsExporter.java │ │ │ │ ├── log │ │ │ │ │ ├── AbstractLogJvmGcMetricsExporter.java │ │ │ │ │ ├── AbstractLogJvmGcMetricsV3Exporter.java │ │ │ │ │ ├── AbstractLogMethodMetricsExporter.java │ │ │ │ │ ├── AbstractLogJvmClassMetricsExporter.java │ │ │ │ │ ├── AbstractLogJvmMemoryMetricsExporter.java │ │ │ │ │ ├── AbstractLogJvmThreadMetricsExporter.java │ │ │ │ │ ├── AbstractLogJvmFileDescMetricsExporter.java │ │ │ │ │ ├── AbstractLogJvmBufferPoolMetricsExporter.java │ │ │ │ │ ├── AbstractLogJvmCompilationMetricsExporter.java │ │ │ │ │ ├── influxdb │ │ │ │ │ │ ├── InfluxLogJvmGcMetricsExporter.java │ │ │ │ │ │ ├── InfluxLogMethodMetricsExporter.java │ │ │ │ │ │ ├── InfluxLogJvmGcMetricsV3Exporter.java │ │ │ │ │ │ ├── InfluxLogJvmClassMetricsExporter.java │ │ │ │ │ │ ├── InfluxLogJvmMemoryMetricsExporter.java │ │ │ │ │ │ ├── InfluxLogJvmThreadMetricsExporter.java │ │ │ │ │ │ ├── InfluxLogJvmFileDescMetricsExporter.java │ │ │ │ │ │ ├── InfluxLogJvmBufferPoolMetricsExporter.java │ │ │ │ │ │ └── InfluxLogJvmCompilationMetricsExporter.java │ │ │ │ │ └── standard │ │ │ │ │ │ ├── StdLogJvmGcMetricsExporter.java │ │ │ │ │ │ ├── StdLogMethodMetricsExporter.java │ │ │ │ │ │ ├── StdLogJvmGcMetricsV3Exporter.java │ │ │ │ │ │ ├── StdLogJvmClassMetricsExporter.java │ │ │ │ │ │ ├── StdLogJvmMemoryMetricsExporter.java │ │ │ │ │ │ └── StdLogJvmThreadMetricsExporter.java │ │ │ │ └── MetricsExporter.java │ │ │ ├── formatter │ │ │ │ ├── JvmGcMetricsFormatter.java │ │ │ │ ├── MethodMetricsFormatter.java │ │ │ │ ├── JvmGcMetricsV3Formatter.java │ │ │ │ ├── JvmClassMetricsFormatter.java │ │ │ │ ├── JvmMemoryMetricsFormatter.java │ │ │ │ ├── JvmThreadMetricsFormatter.java │ │ │ │ ├── JvmBufferPoolMetricsFormatter.java │ │ │ │ ├── JvmCompilationMetricsFormatter.java │ │ │ │ ├── JvmFileDescMetricsFormatter.java │ │ │ │ ├── MetricsFormatter.java │ │ │ │ ├── standard │ │ │ │ │ ├── StdJvmCompilationMetricsFormatter.java │ │ │ │ │ ├── StdJvmClassMetricsFormatter.java │ │ │ │ │ ├── StdJvmGcMetricsV3Formatter.java │ │ │ │ │ ├── StdJvmFileDescMetricsFormatter.java │ │ │ │ │ └── StdJvmBufferPoolMetricsFormatter.java │ │ │ │ └── influxdb │ │ │ │ │ ├── InfluxJvmCompilationMetricsFormatter.java │ │ │ │ │ ├── InfluxJvmFileDescMetricsFormatter.java │ │ │ │ │ ├── InfluxJvmClassMetricsFormatter.java │ │ │ │ │ ├── InfluxJvmBufferPoolMetricsFormatter.java │ │ │ │ │ └── InfluxJvmGcMetricsV3Formatter.java │ │ │ ├── Metrics.java │ │ │ ├── collector │ │ │ │ ├── JvmClassCollector.java │ │ │ │ ├── JvmBufferPoolCollector.java │ │ │ │ ├── JvmCompilationCollector.java │ │ │ │ └── JvmFileDescCollector.java │ │ │ ├── JvmCompilationMetrics.java │ │ │ ├── JvmClassMetrics.java │ │ │ ├── JvmFileDescriptorMetrics.java │ │ │ ├── JvmGcMetricsV3.java │ │ │ └── JvmBufferPoolMetrics.java │ │ │ ├── http │ │ │ ├── server │ │ │ │ └── Dispatcher.java │ │ │ ├── HttpMethod.java │ │ │ ├── HttpResponse.java │ │ │ ├── HttpStatusClass.java │ │ │ └── HttpHeaders.java │ │ │ ├── Scheduler.java │ │ │ ├── config │ │ │ ├── ConfigKey.java │ │ │ ├── ProfilingParams.java │ │ │ └── ProfilingConfig.java │ │ │ ├── constant │ │ │ ├── ClassLevels.java │ │ │ └── PropertyValues.java │ │ │ ├── Version.java │ │ │ └── file │ │ │ ├── MinutelyRollingFileWriter.java │ │ │ ├── HourlyRollingFileWriter.java │ │ │ └── DailyRollingFileWriter.java │ └── test │ │ └── java │ │ └── cn │ │ └── myperf4j │ │ └── base │ │ ├── test │ │ ├── LoggerTest.java │ │ ├── SetUtilsTest.java │ │ ├── CompilationTest.java │ │ ├── DateFormatUtilsTest.java │ │ ├── ProfilingParamsTest.java │ │ ├── MapUtilsTest.java │ │ ├── DateUtilsTest.java │ │ ├── LineProtocolUtilsTest.java │ │ ├── RollingFileWriterTest.java │ │ ├── MyPropertiesTest.java │ │ ├── StrUtilsTest.java │ │ ├── NumFormatUtilsTest.java │ │ ├── ClassLevelMapping.java │ │ ├── ExpUtilsTest.java │ │ └── ILoggerTest.java │ │ ├── http │ │ ├── HttpMethodTest.java │ │ ├── QueryStringDecoderTest.java │ │ ├── HttpRespStatusTest.java │ │ ├── HttpHeadersTest.java │ │ └── HttpStatusClassTest.java │ │ ├── util │ │ ├── net │ │ │ └── IpUtilsTest.java │ │ ├── Base64Test.java │ │ ├── NetUtilsTest.java │ │ ├── StrMatchUtilsTest.java │ │ └── DateUtilsTest.java │ │ ├── influxdb │ │ ├── InfluxDbV1ClientTest.java │ │ └── InfluxDbV2ClientTest.java │ │ └── buffer │ │ └── LongBuffTest.java └── pom.xml ├── .github ├── ISSUE_TEMPLATE │ ├── -----.md │ ├── bug---.md │ ├── ----.md │ └── feature_request.md └── pull_request_template.md ├── MyPerf4J-Core ├── src │ ├── main │ │ └── java │ │ │ └── cn │ │ │ └── myperf4j │ │ │ └── core │ │ │ ├── AbstractMethodTagMaintainer.java │ │ │ ├── recorder │ │ │ └── Recorder.java │ │ │ └── MethodMetricsInfo.java │ └── test │ │ └── java │ │ └── cn │ │ └── myperf4j │ │ └── core │ │ ├── SystemPropertiesTest.java │ │ ├── MethodMetricsBenchmark.java │ │ └── BaseTest.java └── pom.xml ├── MyPerf4J-Benchmark ├── src │ └── main │ │ └── java │ │ └── cn │ │ └── myperf4j │ │ └── bench │ │ ├── util │ │ └── concurrent │ │ │ └── SimpleRandom.java │ │ ├── profiling │ │ ├── ThroughputBench.java │ │ ├── TimeCostBench.java │ │ └── AbstractBench.java │ │ ├── EnvUtils.java │ │ └── RecorderBenchmark.java └── pom.xml ├── .travis.yml └── LICENSE /.coveralls.yml: -------------------------------------------------------------------------------- 1 | service_name: travis-ci -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/resources/config/myPerf4J.properties: -------------------------------------------------------------------------------- 1 | MyPerf4J.RecMode=accurate 2 | MyPerf4J.MillTimeSlice=10000 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .classpath 2 | .project 3 | .settings 4 | target 5 | *.class 6 | *.iml 7 | .idea 8 | .idea/*.xml 9 | *.versionsBackup 10 | .DS_Store 11 | */.DS_Store 12 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/SysProperties.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util; 2 | 3 | /** 4 | * Created by LinShunkang on 2019/04/05 5 | */ 6 | public interface SysProperties { 7 | 8 | String LINE_SEPARATOR = System.lineSeparator(); 9 | } 10 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/influxdb/InfluxDbClient.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.influxdb; 2 | 3 | public interface InfluxDbClient { 4 | 5 | boolean writeMetricsSync(String content); 6 | 7 | boolean writeMetricsAsync(String content); 8 | 9 | boolean close(); 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/log/ILogger.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.log; 2 | 3 | public interface ILogger { 4 | 5 | void log(String msg); 6 | 7 | void logAndFlush(String msg); 8 | 9 | void flushLog(); 10 | 11 | void preCloseLog(); 12 | 13 | void closeLog(); 14 | 15 | } 16 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/test1/Foo.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J.test1; 2 | 3 | /** 4 | * Created by LinShunkang on 2018/4/23 5 | */ 6 | public final class Foo { 7 | 8 | private Foo() { 9 | //empty 10 | } 11 | 12 | public static void test1() { 13 | System.out.println("Foo.test1()"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/JvmGcMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter; 2 | 3 | import cn.myperf4j.base.metric.JvmGcMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/21 7 | */ 8 | public interface JvmGcMetricsExporter extends MetricsExporter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/MethodMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter; 2 | 3 | import cn.myperf4j.base.metric.MethodMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/21 7 | */ 8 | public interface MethodMetricsExporter extends MetricsExporter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/JvmGcMetricsV3Exporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter; 2 | 3 | import cn.myperf4j.base.metric.JvmGcMetricsV3; 4 | 5 | /** 6 | * Created by LinShunkang on 2024/02/08 7 | */ 8 | public interface JvmGcMetricsV3Exporter extends MetricsExporter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/JvmGcMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter; 2 | 3 | import cn.myperf4j.base.metric.JvmGcMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/21 7 | */ 8 | public interface JvmGcMetricsFormatter extends MetricsFormatter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/MethodMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter; 2 | 3 | import cn.myperf4j.base.metric.MethodMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/21 7 | */ 8 | public interface MethodMetricsFormatter extends MetricsFormatter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/Metrics.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric; 2 | 3 | import java.io.Serializable; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/19 7 | */ 8 | public abstract class Metrics implements Serializable { 9 | 10 | private static final long serialVersionUID = -242448401980489223L; 11 | 12 | } 13 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/JvmClassMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter; 2 | 3 | import cn.myperf4j.base.metric.JvmClassMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/21 7 | */ 8 | public interface JvmClassMetricsExporter extends MetricsExporter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/JvmGcMetricsV3Formatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter; 2 | 3 | import cn.myperf4j.base.metric.JvmGcMetricsV3; 4 | 5 | /** 6 | * Created by LinShunkang on 2024/02/08 7 | */ 8 | public interface JvmGcMetricsV3Formatter extends MetricsFormatter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/JvmMemoryMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter; 2 | 3 | import cn.myperf4j.base.metric.JvmMemoryMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/21 7 | */ 8 | public interface JvmMemoryMetricsExporter extends MetricsExporter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/JvmThreadMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter; 2 | 3 | import cn.myperf4j.base.metric.JvmThreadMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/21 7 | */ 8 | public interface JvmThreadMetricsExporter extends MetricsExporter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/JvmClassMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter; 2 | 3 | import cn.myperf4j.base.metric.JvmClassMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/21 7 | */ 8 | public interface JvmClassMetricsFormatter extends MetricsFormatter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/-----.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 新功能建议 3 | about: 为 MyPerf4J 提供新功能的建议 4 | title: '' 5 | labels: enhancement 6 | assignees: '' 7 | 8 | --- 9 | 10 | **你想要的功能和某一个问题相关?请详细描述它。** 11 | 清晰和简要的描述你所遇到的问题。 12 | 13 | **描述你想要的解决方式** 14 | 清晰和简要描述你想要的解决方式。 15 | 16 | **描述你所构想的解决方式** 17 | 清晰和简要描述你思考过的可行方式。 18 | 19 | **其他相关内容** 20 | 在这里添加与该功能相关的其他上下文内容或者截图。 21 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/JvmMemoryMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter; 2 | 3 | import cn.myperf4j.base.metric.JvmMemoryMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/21 7 | */ 8 | public interface JvmMemoryMetricsFormatter extends MetricsFormatter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/JvmThreadMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter; 2 | 3 | import cn.myperf4j.base.metric.JvmThreadMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/21 7 | */ 8 | public interface JvmThreadMetricsFormatter extends MetricsFormatter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/JvmBufferPoolMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter; 2 | 3 | import cn.myperf4j.base.metric.JvmBufferPoolMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/21 7 | */ 8 | public interface JvmBufferPoolMetricsExporter extends MetricsExporter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/http/server/Dispatcher.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.http.server; 2 | 3 | import cn.myperf4j.base.http.HttpRequest; 4 | import cn.myperf4j.base.http.HttpResponse; 5 | 6 | /** 7 | * Created by LinShunkang on 2020/07/12 8 | */ 9 | public interface Dispatcher { 10 | 11 | HttpResponse dispatch(HttpRequest request); 12 | 13 | } 14 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/JvmCompilationMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter; 2 | 3 | import cn.myperf4j.base.metric.JvmCompilationMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2019/11/09 7 | */ 8 | public interface JvmCompilationMetricsExporter extends MetricsExporter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/JvmFileDescMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter; 2 | 3 | import cn.myperf4j.base.metric.JvmFileDescriptorMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2019/11/09 7 | */ 8 | public interface JvmFileDescMetricsExporter extends MetricsExporter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/JvmBufferPoolMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter; 2 | 3 | import cn.myperf4j.base.metric.JvmBufferPoolMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/21 7 | */ 8 | public interface JvmBufferPoolMetricsFormatter extends MetricsFormatter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/JvmCompilationMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter; 2 | 3 | import cn.myperf4j.base.metric.JvmCompilationMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2019/11/09 7 | */ 8 | public interface JvmCompilationMetricsFormatter extends MetricsFormatter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/JvmFileDescMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter; 2 | 3 | import cn.myperf4j.base.metric.JvmFileDescriptorMetrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2019/11/09 7 | */ 8 | public interface JvmFileDescMetricsFormatter extends MetricsFormatter { 9 | 10 | } 11 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/Scheduler.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base; 2 | 3 | /** 4 | * Created by LinShunkang on 2018/8/22 5 | */ 6 | public interface Scheduler { 7 | 8 | /** 9 | * @param lastTimeSliceStartTime: 上一个时间片的起始时间 10 | * @param millTimeSlice: 时间片大小 11 | */ 12 | void run(long lastTimeSliceStartTime, long millTimeSlice); 13 | 14 | String name(); 15 | } 16 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/dynamic/DaoInvocationHandler.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J.dynamic; 2 | 3 | import java.lang.reflect.InvocationHandler; 4 | import java.lang.reflect.Method; 5 | 6 | /** 7 | * Created by LinShunkang on 2019/02/06 8 | */ 9 | public class DaoInvocationHandler implements InvocationHandler { 10 | 11 | @Override 12 | public Object invoke(Object proxy, Method method, Object[] args) { 13 | return new Object(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug---.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug 报告 3 | about: 创建报告以帮助我们改进 MyPerf4J 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Bug 描述** 11 | 12 | 请清晰和简洁的描述你遇见的 bug,方便快速的定位和解决问题 13 | 14 | **运行环境** 15 | * MyPerf4J 版本号,例如,2.8.0、2.9.0 等 16 | * JDK 版本号,例如,JDK7、JDK8 等 17 | * 运行容器和框架,例如,Tomcat、PlainJava、Spring、SpringBoot 等 18 | * 操作系统,例如,Linux、MacOS、Windows 等 19 | 20 | **配置文件** 21 | ``` 22 | 请把你的配置文件内容完整的复制出来 23 | ``` 24 | 25 | **异常堆栈** 26 | ``` 27 | 请把程序的错误栈信息完整的复制出来 28 | ``` 29 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/ClassToTest.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J; 2 | 3 | import cn.myperf4j.base.util.concurrent.ThreadUtils; 4 | 5 | import java.util.concurrent.ThreadLocalRandom; 6 | import java.util.concurrent.TimeUnit; 7 | 8 | /** 9 | * Created by LinShunkang on 2018/10/18 10 | */ 11 | public class ClassToTest { 12 | 13 | public String getStr() { 14 | ThreadUtils.sleepQuietly(ThreadLocalRandom.current().nextInt(10), TimeUnit.MILLISECONDS); 15 | return "111"; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/collections/ArrayUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.collections; 2 | 3 | /** 4 | * Created by LinShunkang on 2020/05/16 5 | */ 6 | public final class ArrayUtils { 7 | 8 | public static boolean isEmpty(byte[] arr) { 9 | return arr == null || arr.length <= 0; 10 | } 11 | 12 | public static boolean isNotEmpty(byte[] arr) { 13 | return !isEmpty(arr); 14 | } 15 | 16 | private ArrayUtils() { 17 | //empty 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/----.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: 问题咨询 3 | about: 请按以下模板,详细和清晰描述你的疑问,方便快速帮助你解决问题 4 | title: '' 5 | labels: question 6 | assignees: '' 7 | 8 | --- 9 | 10 | **问题描述** 11 | 12 | 请清晰和简洁的描述你遇见的 问题 13 | 14 | **运行环境** 15 | * MyPerf4J 版本号,例如,2.8.0、2.9.0 等 16 | * JDK 版本号,例如,JDK7、JDK8 等 17 | * 运行容器和框架,例如,Tomcat、PlainJava、Spring、SpringBoot 等 18 | * 操作系统,例如,Linux、MacOS、Windows 等 19 | 20 | **JVM启动参数** 21 | ``` 22 | 请把你的JVM启动参数完整的复制出来 23 | ``` 24 | 25 | **配置文件** 26 | ``` 27 | 请把你的配置文件内容完整的复制出来 28 | ``` 29 | 30 | **异常堆栈** 31 | ``` 32 | 请把程序的错误栈信息完整的复制出来 33 | ``` 34 | -------------------------------------------------------------------------------- /MyPerf4J-Core/src/main/java/cn/myperf4j/core/AbstractMethodTagMaintainer.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.core; 2 | 3 | import cn.myperf4j.base.MethodTag; 4 | 5 | import java.lang.reflect.Method; 6 | 7 | /** 8 | * Created by LinShunkang on 2018/5/20 9 | */ 10 | public abstract class AbstractMethodTagMaintainer { 11 | 12 | public abstract int addMethodTag(MethodTag methodTag); 13 | 14 | public abstract int addMethodTag(Method method); 15 | 16 | public abstract MethodTag getMethodTag(int methodId); 17 | 18 | public abstract int getMethodTagCount(); 19 | } 20 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/main/java/cn/myperf4j/asm/PreMain.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.asm; 2 | 3 | import cn.myperf4j.asm.aop.ProfilingTransformer; 4 | 5 | import java.lang.instrument.Instrumentation; 6 | 7 | /** 8 | * Created by LinShunkang on 2018/4/25 9 | */ 10 | public final class PreMain { 11 | 12 | private PreMain() { 13 | //empty 14 | } 15 | 16 | public static void premain(String options, Instrumentation ins) { 17 | if (ASMBootstrap.getInstance().initial()) { 18 | ins.addTransformer(new ProfilingTransformer()); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/LoggerTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import cn.myperf4j.base.util.Logger; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Created by LinShunkang on 2018/10/17 8 | */ 9 | public class LoggerTest { 10 | 11 | @Test 12 | public void test() { 13 | Logger.setDebugEnable(true); 14 | Logger.debug("debug test"); 15 | Logger.info("info test"); 16 | Logger.warn("warn test"); 17 | Logger.error("error test"); 18 | // Logger.error("error test", new UnknownError()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/SetUtilsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import cn.myperf4j.base.util.collections.SetUtils; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | /** 8 | * Created by LinShunkang on 2019-01-01 9 | */ 10 | public class SetUtilsTest { 11 | 12 | @Test 13 | public void test() { 14 | Assert.assertTrue(SetUtils.of(1).contains(1)); 15 | Assert.assertTrue(SetUtils.of(1, 2, 3).contains(1)); 16 | Assert.assertTrue(SetUtils.of(1, 2, 3).contains(2)); 17 | Assert.assertTrue(SetUtils.of(1, 2, 3).contains(3)); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/dynamic/DaoInvocationHandlerV2.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J.dynamic; 2 | 3 | import cn.myperf4j.asm.aop.ProfilingAspect; 4 | 5 | import java.lang.reflect.InvocationHandler; 6 | import java.lang.reflect.Method; 7 | 8 | /** 9 | * Created by LinShunkang on 2019/02/06 10 | */ 11 | public class DaoInvocationHandlerV2 implements InvocationHandler { 12 | 13 | @Override 14 | public Object invoke(Object proxy, Method method, Object[] args) { 15 | long start = System.nanoTime(); 16 | Object o = new Object(); 17 | ProfilingAspect.profiling(start, method); 18 | return o; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/collections/SetUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.collections; 2 | 3 | import java.util.Arrays; 4 | import java.util.HashSet; 5 | import java.util.Set; 6 | 7 | /** 8 | * Created by LinShunkang on 2018-12-31 9 | */ 10 | public final class SetUtils { 11 | 12 | @SafeVarargs 13 | public static Set of(T... t) { 14 | return new HashSet<>(Arrays.asList(t)); 15 | } 16 | 17 | public static Set createHashSet(int size) { 18 | return new HashSet<>((int) (size / .75) + 1); 19 | } 20 | 21 | private SetUtils() { 22 | //empty 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/MetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter; 2 | 3 | import cn.myperf4j.base.metric.Metrics; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * Created by LinShunkang on 2018/8/21 9 | */ 10 | public interface MetricsFormatter { 11 | 12 | ThreadLocal SB_TL = new ThreadLocal() { 13 | @Override 14 | protected StringBuilder initialValue() { 15 | return new StringBuilder(32 * 1024); 16 | } 17 | }; 18 | 19 | String format(List metricsList, long startMillis, long stopMillis); 20 | 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/CompilationTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import org.junit.Test; 4 | 5 | import java.lang.management.CompilationMXBean; 6 | import java.lang.management.ManagementFactory; 7 | 8 | /** 9 | * Created by LinShunkang on 2019/09/22 10 | */ 11 | public class CompilationTest { 12 | 13 | @Test 14 | public void test() { 15 | CompilationMXBean mxBean = ManagementFactory.getCompilationMXBean(); 16 | System.out.println(mxBean.getName()); 17 | System.out.println(mxBean.getObjectName()); 18 | System.out.println(mxBean.getTotalCompilationTime() + "ms"); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/http/HttpMethodTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.http; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import static cn.myperf4j.base.http.HttpMethod.GET; 7 | import static cn.myperf4j.base.http.HttpMethod.HEAD; 8 | import static cn.myperf4j.base.http.HttpMethod.POST; 9 | 10 | /** 11 | * Created by LinShunkang on 2020/05/16 12 | */ 13 | public class HttpMethodTest { 14 | 15 | @Test 16 | public void testPermitsBody() { 17 | Assert.assertFalse(HEAD.isPermitsBody()); 18 | Assert.assertFalse(GET.isPermitsBody()); 19 | Assert.assertTrue(POST.isPermitsBody()); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/log/NullLogger.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.log; 2 | 3 | public class NullLogger implements ILogger { 4 | 5 | NullLogger() { 6 | //empty` 7 | } 8 | 9 | @Override 10 | public void log(String msg) { 11 | //discard 12 | } 13 | 14 | @Override 15 | public void logAndFlush(String msg) { 16 | //discard 17 | } 18 | 19 | @Override 20 | public void flushLog() { 21 | //ignore 22 | } 23 | 24 | @Override 25 | public void preCloseLog() { 26 | //ignore 27 | } 28 | 29 | @Override 30 | public void closeLog() { 31 | //ignore 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/DateFormatUtilsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import cn.myperf4j.base.util.text.DateFormatUtils; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | import java.text.SimpleDateFormat; 8 | import java.util.Date; 9 | 10 | /** 11 | * Created by LinShunkang on 2018/10/17 12 | */ 13 | public class DateFormatUtilsTest { 14 | 15 | @Test 16 | public void test() { 17 | SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 18 | long mills = System.currentTimeMillis(); 19 | Assert.assertEquals(DateFormatUtils.format(mills), format.format(new Date(mills))); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/log/StdoutLogger.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.log; 2 | 3 | /** 4 | * Created by LinShunkang on 2018/10/29 5 | */ 6 | public class StdoutLogger implements ILogger { 7 | 8 | @Override 9 | public void log(String msg) { 10 | System.out.println(msg); 11 | } 12 | 13 | @Override 14 | public void logAndFlush(String msg) { 15 | log(msg); 16 | } 17 | 18 | @Override 19 | public void flushLog() { 20 | //empty 21 | } 22 | 23 | @Override 24 | public void preCloseLog() { 25 | //empty 26 | } 27 | 28 | @Override 29 | public void closeLog() { 30 | //empty 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/io/IOUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.io; 2 | 3 | import cn.myperf4j.base.util.Logger; 4 | 5 | import java.io.Closeable; 6 | import java.io.IOException; 7 | 8 | /** 9 | * Created by LinShunkang on 2018/4/9 10 | */ 11 | public final class IOUtils { 12 | 13 | private IOUtils() { 14 | //empty 15 | } 16 | 17 | public static void closeQuietly(Closeable closeable) { 18 | try { 19 | if (closeable != null) { 20 | closeable.close(); 21 | } 22 | } catch (IOException e) { 23 | Logger.warn("IOUtils.closeQuietly(): " + e.getMessage()); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/util/net/IpUtilsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.net; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Created by LinShunkang on 2022/02/04 8 | */ 9 | public class IpUtilsTest { 10 | 11 | @Test 12 | public void testGetLocalhostName() { 13 | Assert.assertNotEquals("127.0.0.1", IpUtils.getLocalhostName()); 14 | Assert.assertNotEquals("localhost", IpUtils.getLocalhostName()); 15 | } 16 | 17 | @Test 18 | public void testGetLocalIp() { 19 | Assert.assertNotEquals("127.0.0.1", IpUtils.getLocalIp()); 20 | Assert.assertNotEquals("localhost", IpUtils.getLocalIp()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/config/ConfigKey.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.config; 2 | 3 | /** 4 | * Created by LinShunkang on 2020/05/24 5 | */ 6 | public final class ConfigKey { 7 | 8 | private final String key; 9 | 10 | private final String legacyKey; 11 | 12 | private ConfigKey(String key, String legacyKey) { 13 | this.key = key; 14 | this.legacyKey = legacyKey; 15 | } 16 | 17 | public String key() { 18 | return key; 19 | } 20 | 21 | public String legacyKey() { 22 | return legacyKey; 23 | } 24 | 25 | public static ConfigKey of(String key, String legacyKey) { 26 | return new ConfigKey(key, legacyKey); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/ProfilingParamsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import cn.myperf4j.base.config.ProfilingParams; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | /** 8 | * Created by LinShunkang on 2018/10/28 9 | */ 10 | public class ProfilingParamsTest { 11 | 12 | @Test 13 | public void test() { 14 | ProfilingParams params = ProfilingParams.of(1000, 10); 15 | Assert.assertEquals(params.mostTimeThreshold(), 1000); 16 | Assert.assertNotEquals(params.mostTimeThreshold(), -1000); 17 | 18 | Assert.assertEquals(params.outThresholdCount(), 10); 19 | Assert.assertNotEquals(params.outThresholdCount(), -10); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/util/Base64Test.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util; 2 | 3 | import cn.myperf4j.base.util.Base64.Encoder; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | import static java.nio.charset.StandardCharsets.UTF_8; 8 | 9 | /** 10 | * Created by LinShunkang on 2020/05/24 11 | */ 12 | public class Base64Test { 13 | 14 | private final Encoder encoder = Encoder.RFC4648; 15 | 16 | @Test 17 | public void test() { 18 | byte[] bytes = "Hello, Base64!".getBytes(UTF_8); 19 | Assert.assertEquals("SGVsbG8sIEJhc2U2NCE=", encoder.encodeToString(bytes)); 20 | Assert.assertArrayEquals("SGVsbG8sIEJhc2U2NCE=".getBytes(UTF_8), encoder.encode(bytes)); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /MyPerf4J-Benchmark/src/main/java/cn/myperf4j/bench/util/concurrent/SimpleRandom.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.bench.util.concurrent; 2 | 3 | import java.util.concurrent.atomic.AtomicLong; 4 | 5 | public final class SimpleRandom { 6 | 7 | private static final long multiplier = 0x5DEECE66DL; 8 | 9 | private static final long addend = 0xBL; 10 | 11 | private static final long mask = (1L << 48) - 1; 12 | 13 | static final AtomicLong seq = new AtomicLong(-715159705); 14 | 15 | private long seed; 16 | 17 | public SimpleRandom() { 18 | seed = System.nanoTime() + seq.getAndAdd(129); 19 | } 20 | 21 | public int next() { 22 | return (int) ((seed = (seed * multiplier + addend) & mask) >>> 17) & 0x7FFFFFFF; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | # - oraclejdk7 4 | # - openjdk7 5 | - openjdk8 6 | - openjdk9 7 | - openjdk10 8 | - openjdk11 9 | before_install: 10 | # - mvn install -Dmaven.javadoc.skip=true 11 | - "echo skipped" 12 | install: 13 | #- "echo skipped" 14 | - mvn install -DskipTests=true -Dmaven.javadoc.skip=true 15 | script: 16 | - "echo skipped" 17 | # - mvn clean install -Dmaven.javadoc.skip=true 18 | # - mvn deploy 19 | after_success: 20 | - mvn clean cobertura:cobertura coveralls:report 21 | - bash <(curl -s https://codecov.io/bash) 22 | notifications: 23 | email: 24 | recipients: 25 | - asdfg2385856@gmail.com 26 | #on_success: [always|never|change] default: change 27 | #on_failure: [always|never|change] default: always 28 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/util/NetUtilsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util; 2 | 3 | import cn.myperf4j.base.util.net.NetUtils; 4 | import com.sun.net.httpserver.HttpServer; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import java.io.IOException; 9 | import java.net.InetSocketAddress; 10 | 11 | /** 12 | * Created by LinShunkang on 2020/12/06 13 | */ 14 | public class NetUtilsTest { 15 | 16 | @Test 17 | public void test() throws IOException { 18 | final int port = 1234; 19 | final HttpServer server = HttpServer.create(new InetSocketAddress(port), 0); 20 | server.start(); 21 | Assert.assertFalse(NetUtils.isPortAvailable(port)); 22 | Assert.assertTrue(NetUtils.isPortAvailable(1235)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/text/DateFormatUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.text; 2 | 3 | import java.text.DateFormat; 4 | import java.text.SimpleDateFormat; 5 | import java.util.Date; 6 | 7 | /** 8 | * Created by LinShunkang on 2018/8/22 9 | */ 10 | public final class DateFormatUtils { 11 | 12 | private static final ThreadLocal DEFAULT_DATE_FORMAT = new ThreadLocal() { 13 | @Override 14 | protected DateFormat initialValue() { 15 | return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 16 | } 17 | }; 18 | 19 | public static String format(long millis) { 20 | return DEFAULT_DATE_FORMAT.get().format(new Date(millis)); 21 | } 22 | 23 | private DateFormatUtils() { 24 | //empty 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/constant/ClassLevels.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.constant; 2 | 3 | /** 4 | * Created by LinShunkang on 2019/04/27 5 | */ 6 | public interface ClassLevels { 7 | 8 | String CONTROLLER = "Controller"; 9 | 10 | String INTERCEPTOR = "Interceptor"; 11 | 12 | String PRODUCER = "Producer"; 13 | 14 | String CONSUMER = "Consumer"; 15 | 16 | String LISTENER = "Listener"; 17 | 18 | String FILTER = "Filter"; 19 | 20 | String HANDLER = "Handler"; 21 | 22 | String PROCESSOR = "Processor"; 23 | 24 | String DISPATCHER = "Dispatcher"; 25 | 26 | String API = "Api"; 27 | 28 | String SERVICE = "Service"; 29 | 30 | String CACHE = "Cache"; 31 | 32 | String DAO = "DAO"; 33 | 34 | String UTILS = "Utils"; 35 | 36 | String OTHERS = "Others"; 37 | } 38 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/MapUtilsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import cn.myperf4j.base.util.collections.MapUtils; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | import java.util.Map; 8 | import java.util.concurrent.ConcurrentHashMap; 9 | 10 | /** 11 | * Created by LinShunkang on 2018/10/17 12 | */ 13 | public class MapUtilsTest { 14 | 15 | @Test 16 | public void test() { 17 | Map hashMap = MapUtils.createHashMap(1); 18 | Assert.assertNotNull(hashMap); 19 | 20 | Map hashMap2 = MapUtils.createHashMap(10, 0.01F); 21 | Assert.assertNotNull(hashMap2); 22 | 23 | ConcurrentHashMap concHashMap = MapUtils.createConcHashMap(10, 0.1F); 24 | Assert.assertNotNull(concHashMap); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/discard/DiscardJvmGcMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.discard; 2 | 3 | import cn.myperf4j.base.metric.JvmGcMetrics; 4 | import cn.myperf4j.base.metric.exporter.JvmGcMetricsExporter; 5 | 6 | /** 7 | * Created by LinShunkang on 2018/8/21 8 | */ 9 | public class DiscardJvmGcMetricsExporter implements JvmGcMetricsExporter { 10 | 11 | @Override 12 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 13 | //empty 14 | } 15 | 16 | @Override 17 | public void process(JvmGcMetrics metrics, long processId, long startMillis, long stopMillis) { 18 | //empty 19 | } 20 | 21 | @Override 22 | public void afterProcess(long processId, long startMillis, long stopMillis) { 23 | //empty 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/discard/DiscardJvmGcMetricsV3Exporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.discard; 2 | 3 | import cn.myperf4j.base.metric.JvmGcMetricsV3; 4 | import cn.myperf4j.base.metric.exporter.JvmGcMetricsV3Exporter; 5 | 6 | /** 7 | * Created by LinShunkang on 2024/02/08 8 | */ 9 | public class DiscardJvmGcMetricsV3Exporter implements JvmGcMetricsV3Exporter { 10 | 11 | @Override 12 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 13 | //empty 14 | } 15 | 16 | @Override 17 | public void process(JvmGcMetricsV3 metrics, long processId, long startMillis, long stopMillis) { 18 | //empty 19 | } 20 | 21 | @Override 22 | public void afterProcess(long processId, long startMillis, long stopMillis) { 23 | //empty 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/discard/DiscardJvmClassMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.discard; 2 | 3 | import cn.myperf4j.base.metric.JvmClassMetrics; 4 | import cn.myperf4j.base.metric.exporter.JvmClassMetricsExporter; 5 | 6 | /** 7 | * Created by LinShunkang on 2018/8/21 8 | */ 9 | public class DiscardJvmClassMetricsExporter implements JvmClassMetricsExporter { 10 | 11 | @Override 12 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 13 | //empty 14 | } 15 | 16 | @Override 17 | public void process(JvmClassMetrics metrics, long processId, long startMillis, long stopMillis) { 18 | //empty 19 | } 20 | 21 | @Override 22 | public void afterProcess(long processId, long startMillis, long stopMillis) { 23 | //empty 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/discard/DiscardJvmMemoryMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.discard; 2 | 3 | import cn.myperf4j.base.metric.JvmMemoryMetrics; 4 | import cn.myperf4j.base.metric.exporter.JvmMemoryMetricsExporter; 5 | 6 | /** 7 | * Created by LinShunkang on 2018/8/21 8 | */ 9 | public class DiscardJvmMemoryMetricsExporter implements JvmMemoryMetricsExporter { 10 | 11 | @Override 12 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 13 | //empty 14 | } 15 | 16 | @Override 17 | public void process(JvmMemoryMetrics metrics, long processId, long startMillis, long stopMillis) { 18 | //empty 19 | } 20 | 21 | @Override 22 | public void afterProcess(long processId, long startMillis, long stopMillis) { 23 | //empty 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/discard/DiscardJvmThreadMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.discard; 2 | 3 | import cn.myperf4j.base.metric.JvmThreadMetrics; 4 | import cn.myperf4j.base.metric.exporter.JvmThreadMetricsExporter; 5 | 6 | /** 7 | * Created by LinShunkang on 2018/8/21 8 | */ 9 | public class DiscardJvmThreadMetricsExporter implements JvmThreadMetricsExporter { 10 | 11 | @Override 12 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 13 | //empty 14 | } 15 | 16 | @Override 17 | public void process(JvmThreadMetrics metrics, long processId, long startMillis, long stopMillis) { 18 | //empty 19 | } 20 | 21 | @Override 22 | public void afterProcess(long processId, long startMillis, long stopMillis) { 23 | //empty 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/discard/DiscardJvmBufferPoolMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.discard; 2 | 3 | import cn.myperf4j.base.metric.JvmBufferPoolMetrics; 4 | import cn.myperf4j.base.metric.exporter.JvmBufferPoolMetricsExporter; 5 | 6 | /** 7 | * Created by LinShunkang on 2018/8/25 8 | */ 9 | public class DiscardJvmBufferPoolMetricsExporter implements JvmBufferPoolMetricsExporter { 10 | 11 | @Override 12 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 13 | //empty 14 | } 15 | 16 | @Override 17 | public void process(JvmBufferPoolMetrics metrics, long processId, long startMillis, long stopMillis) { 18 | //empty 19 | } 20 | 21 | @Override 22 | public void afterProcess(long processId, long startMillis, long stopMillis) { 23 | //empty 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/config/ProfilingParams.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.config; 2 | 3 | /** 4 | * Created by LinShunkang on 2018/4/28 5 | */ 6 | public final class ProfilingParams { 7 | 8 | private final int mostTimeThreshold; //UNIT: ms 9 | 10 | private final int outThresholdCount; 11 | 12 | private ProfilingParams(int mostTimeThreshold, int outThresholdCount) { 13 | this.mostTimeThreshold = mostTimeThreshold; 14 | this.outThresholdCount = outThresholdCount; 15 | } 16 | 17 | public int mostTimeThreshold() { 18 | return mostTimeThreshold; 19 | } 20 | 21 | public int outThresholdCount() { 22 | return outThresholdCount; 23 | } 24 | 25 | public static ProfilingParams of(int mostTimeThreshold, int outThresholdCount) { 26 | return new ProfilingParams(mostTimeThreshold, outThresholdCount); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/discard/DiscardJvmFileDescMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.discard; 2 | 3 | import cn.myperf4j.base.metric.JvmFileDescriptorMetrics; 4 | import cn.myperf4j.base.metric.exporter.JvmFileDescMetricsExporter; 5 | 6 | /** 7 | * Created by LinShunkang on 2019/11/09 8 | */ 9 | public class DiscardJvmFileDescMetricsExporter implements JvmFileDescMetricsExporter { 10 | 11 | @Override 12 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 13 | //empty 14 | } 15 | 16 | @Override 17 | public void process(JvmFileDescriptorMetrics metrics, long processId, long startMillis, long stopMillis) { 18 | //empty 19 | } 20 | 21 | @Override 22 | public void afterProcess(long processId, long startMillis, long stopMillis) { 23 | //empty 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/discard/DiscardJvmCompilationMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.discard; 2 | 3 | import cn.myperf4j.base.metric.JvmCompilationMetrics; 4 | import cn.myperf4j.base.metric.exporter.JvmCompilationMetricsExporter; 5 | 6 | /** 7 | * Created by LinShunkang on 2019/11/09 8 | */ 9 | public class DiscardJvmCompilationMetricsExporter implements JvmCompilationMetricsExporter { 10 | 11 | @Override 12 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 13 | //empty 14 | } 15 | 16 | @Override 17 | public void process(JvmCompilationMetrics metrics, long processId, long startMillis, long stopMillis) { 18 | //empty 19 | } 20 | 21 | @Override 22 | public void afterProcess(long processId, long startMillis, long stopMillis) { 23 | //empty 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/collector/JvmClassCollector.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.collector; 2 | 3 | import cn.myperf4j.base.metric.JvmClassMetrics; 4 | 5 | import java.lang.management.ClassLoadingMXBean; 6 | import java.lang.management.ManagementFactory; 7 | 8 | /** 9 | * Created by LinShunkang on 2019/07/10 10 | */ 11 | public final class JvmClassCollector { 12 | 13 | private static final ClassLoadingMXBean CLASS_LOADING_MX_BEAN = ManagementFactory.getClassLoadingMXBean(); 14 | 15 | private JvmClassCollector() { 16 | //empty 17 | } 18 | 19 | public static JvmClassMetrics collectClassMetrics() { 20 | final ClassLoadingMXBean mxBean = CLASS_LOADING_MX_BEAN; 21 | return new JvmClassMetrics(mxBean.getTotalLoadedClassCount(), 22 | mxBean.getLoadedClassCount(), 23 | mxBean.getUnloadedClassCount()); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/AbstractLogJvmGcMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.log.ILogger; 5 | import cn.myperf4j.base.log.LoggerFactory; 6 | import cn.myperf4j.base.metric.exporter.JvmGcMetricsExporter; 7 | 8 | /** 9 | * Created by LinShunkang on 2018/8/25 10 | */ 11 | public abstract class AbstractLogJvmGcMetricsExporter implements JvmGcMetricsExporter { 12 | 13 | protected ILogger logger = LoggerFactory.getLogger(ProfilingConfig.metricsConfig().gcMetricsFile()); 14 | 15 | @Override 16 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 17 | //empty 18 | } 19 | 20 | @Override 21 | public void afterProcess(long processId, long startMillis, long stopMillis) { 22 | logger.flushLog(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/util/StrMatchUtilsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import static cn.myperf4j.base.util.StrMatchUtils.isMatch; 7 | 8 | /** 9 | * Created by LinShunkang on 2020/07/26 10 | */ 11 | public class StrMatchUtilsTest { 12 | 13 | @Test 14 | public void testIsMatch() { 15 | Assert.assertTrue(isMatch("cn.myperf4j.config.abc", "cn.myperf4j*abc")); 16 | Assert.assertTrue(isMatch("cn.myperf4j.config.abc", "*.myperf4j*abc")); 17 | Assert.assertTrue(isMatch("cn.myperf4j.config.abc", "*.myperf4j*a*c")); 18 | 19 | Assert.assertFalse(isMatch("cn.myperf4j.config.abc", "*.myperf4j*ac")); 20 | Assert.assertFalse(isMatch("cn.myperf4j.config.abc", "a*.myperf4j*ac")); 21 | Assert.assertFalse(isMatch("cn.myperf4j.config.abc", "a*.myperf4j.a*")); 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/AbstractLogJvmGcMetricsV3Exporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.log.ILogger; 5 | import cn.myperf4j.base.log.LoggerFactory; 6 | import cn.myperf4j.base.metric.exporter.JvmGcMetricsV3Exporter; 7 | 8 | /** 9 | * Created by LinShunkang on 2024/02/08 10 | */ 11 | public abstract class AbstractLogJvmGcMetricsV3Exporter implements JvmGcMetricsV3Exporter { 12 | 13 | protected ILogger logger = LoggerFactory.getLogger(ProfilingConfig.metricsConfig().gcMetricsFile()); 14 | 15 | @Override 16 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 17 | //empty 18 | } 19 | 20 | @Override 21 | public void afterProcess(long processId, long startMillis, long stopMillis) { 22 | logger.flushLog(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/AbstractLogMethodMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.log.ILogger; 5 | import cn.myperf4j.base.log.LoggerFactory; 6 | import cn.myperf4j.base.metric.exporter.MethodMetricsExporter; 7 | 8 | /** 9 | * Created by LinShunkang on 2018/8/25 10 | */ 11 | public abstract class AbstractLogMethodMetricsExporter implements MethodMetricsExporter { 12 | 13 | protected ILogger logger = LoggerFactory.getLogger(ProfilingConfig.metricsConfig().methodMetricsFile()); 14 | 15 | @Override 16 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 17 | //empty 18 | } 19 | 20 | @Override 21 | public void afterProcess(long processId, long startMillis, long stopMillis) { 22 | logger.flushLog(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/AbstractLogJvmClassMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.log.ILogger; 5 | import cn.myperf4j.base.log.LoggerFactory; 6 | import cn.myperf4j.base.metric.exporter.JvmClassMetricsExporter; 7 | 8 | /** 9 | * Created by LinShunkang on 2018/8/25 10 | */ 11 | public abstract class AbstractLogJvmClassMetricsExporter implements JvmClassMetricsExporter { 12 | 13 | protected ILogger logger = LoggerFactory.getLogger(ProfilingConfig.metricsConfig().classMetricsFile()); 14 | 15 | @Override 16 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 17 | //empty 18 | } 19 | 20 | @Override 21 | public void afterProcess(long processId, long startMillis, long stopMillis) { 22 | logger.flushLog(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/AbstractLogJvmMemoryMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.log.ILogger; 5 | import cn.myperf4j.base.log.LoggerFactory; 6 | import cn.myperf4j.base.metric.exporter.JvmMemoryMetricsExporter; 7 | 8 | /** 9 | * Created by LinShunkang on 2018/8/25 10 | */ 11 | public abstract class AbstractLogJvmMemoryMetricsExporter implements JvmMemoryMetricsExporter { 12 | 13 | protected ILogger logger = LoggerFactory.getLogger(ProfilingConfig.metricsConfig().memoryMetricsFile()); 14 | 15 | @Override 16 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 17 | //empty 18 | } 19 | 20 | @Override 21 | public void afterProcess(long processId, long startMillis, long stopMillis) { 22 | logger.flushLog(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/AbstractLogJvmThreadMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.log.ILogger; 5 | import cn.myperf4j.base.log.LoggerFactory; 6 | import cn.myperf4j.base.metric.exporter.JvmThreadMetricsExporter; 7 | 8 | /** 9 | * Created by LinShunkang on 2018/8/25 10 | */ 11 | public abstract class AbstractLogJvmThreadMetricsExporter implements JvmThreadMetricsExporter { 12 | 13 | protected ILogger logger = LoggerFactory.getLogger(ProfilingConfig.metricsConfig().threadMetricsFile()); 14 | 15 | @Override 16 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 17 | //empty 18 | } 19 | 20 | @Override 21 | public void afterProcess(long processId, long startMillis, long stopMillis) { 22 | logger.flushLog(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/AbstractLogJvmFileDescMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.log.ILogger; 5 | import cn.myperf4j.base.log.LoggerFactory; 6 | import cn.myperf4j.base.metric.exporter.JvmFileDescMetricsExporter; 7 | 8 | /** 9 | * Created by LinShunkang on 2019/11/09 10 | */ 11 | public abstract class AbstractLogJvmFileDescMetricsExporter implements JvmFileDescMetricsExporter { 12 | 13 | protected ILogger logger = LoggerFactory.getLogger(ProfilingConfig.metricsConfig().fileDescMetricsFile()); 14 | 15 | @Override 16 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 17 | //empty 18 | } 19 | 20 | @Override 21 | public void afterProcess(long processId, long startMillis, long stopMillis) { 22 | logger.flushLog(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/AbstractLogJvmBufferPoolMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.log.ILogger; 5 | import cn.myperf4j.base.log.LoggerFactory; 6 | import cn.myperf4j.base.metric.exporter.JvmBufferPoolMetricsExporter; 7 | 8 | /** 9 | * Created by LinShunkang on 2018/8/25 10 | */ 11 | public abstract class AbstractLogJvmBufferPoolMetricsExporter implements JvmBufferPoolMetricsExporter { 12 | 13 | protected ILogger logger = LoggerFactory.getLogger(ProfilingConfig.metricsConfig().bufferPoolMetricsFile()); 14 | 15 | @Override 16 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 17 | //empty 18 | } 19 | 20 | @Override 21 | public void afterProcess(long processId, long startMillis, long stopMillis) { 22 | logger.flushLog(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/JvmCompilationMetrics.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric; 2 | 3 | /** 4 | * Created by LinShunkang on 2019/11/03 5 | */ 6 | public class JvmCompilationMetrics extends Metrics { 7 | 8 | private static final long serialVersionUID = 4067660607211597333L; 9 | 10 | private final long time; //UNIT: ms 11 | 12 | private final long totalTime; //UNIT: ms 13 | 14 | public JvmCompilationMetrics(long time, long totalTime) { 15 | this.time = time; 16 | this.totalTime = totalTime; 17 | } 18 | 19 | public long getTime() { 20 | return time; 21 | } 22 | 23 | public long getTotalTime() { 24 | return totalTime; 25 | } 26 | 27 | @Override 28 | public String toString() { 29 | return "JvmCompilationMetrics{" + 30 | "time=" + time + 31 | ", time=" + time + 32 | "} " + super.toString(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/AbstractLogJvmCompilationMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.log.ILogger; 5 | import cn.myperf4j.base.log.LoggerFactory; 6 | import cn.myperf4j.base.metric.exporter.JvmCompilationMetricsExporter; 7 | 8 | /** 9 | * Created by LinShunkang on 2019/11/09 10 | */ 11 | public abstract class AbstractLogJvmCompilationMetricsExporter implements JvmCompilationMetricsExporter { 12 | 13 | protected ILogger logger = LoggerFactory.getLogger(ProfilingConfig.metricsConfig().compilationMetricsFile()); 14 | 15 | @Override 16 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 17 | //empty 18 | } 19 | 20 | @Override 21 | public void afterProcess(long processId, long startMillis, long stopMillis) { 22 | logger.flushLog(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/influxdb/InfluxLogJvmGcMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.influxdb; 2 | 3 | import cn.myperf4j.base.metric.JvmGcMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmGcMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.influxdb.InfluxJvmGcMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmGcMetricsExporter; 7 | 8 | import java.util.Collections; 9 | 10 | /** 11 | * Created by LinShunkang on 2018/8/25 12 | */ 13 | public class InfluxLogJvmGcMetricsExporter extends AbstractLogJvmGcMetricsExporter { 14 | 15 | private static final JvmGcMetricsFormatter METRICS_FORMATTER = new InfluxJvmGcMetricsFormatter(); 16 | 17 | @Override 18 | public void process(JvmGcMetrics metrics, long processId, long startMillis, long stopMillis) { 19 | logger.log(METRICS_FORMATTER.format(Collections.singletonList(metrics), startMillis, stopMillis)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/influxdb/InfluxLogMethodMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.influxdb; 2 | 3 | import cn.myperf4j.base.metric.MethodMetrics; 4 | import cn.myperf4j.base.metric.formatter.MethodMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.influxdb.InfluxMethodMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogMethodMetricsExporter; 7 | 8 | import java.util.Collections; 9 | 10 | /** 11 | * Created by LinShunkang on 2018/7/9 12 | */ 13 | public class InfluxLogMethodMetricsExporter extends AbstractLogMethodMetricsExporter { 14 | 15 | private static final MethodMetricsFormatter METRICS_FORMATTER = new InfluxMethodMetricsFormatter(); 16 | 17 | @Override 18 | public void process(MethodMetrics metrics, long processId, long startMillis, long stopMillis) { 19 | logger.log(METRICS_FORMATTER.format(Collections.singletonList(metrics), startMillis, stopMillis)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/collector/JvmBufferPoolCollector.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.collector; 2 | 3 | import cn.myperf4j.base.metric.JvmBufferPoolMetrics; 4 | 5 | import java.lang.management.BufferPoolMXBean; 6 | import java.lang.management.ManagementFactory; 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | /** 11 | * Created by LinShunkang on 2022/01/03 12 | */ 13 | public final class JvmBufferPoolCollector { 14 | 15 | private JvmBufferPoolCollector() { 16 | //empty 17 | } 18 | 19 | public static List collectBufferPoolMetrics() { 20 | final List pools = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class); 21 | final List result = new ArrayList<>(pools.size()); 22 | for (int i = 0, size = pools.size(); i < size; i++) { 23 | result.add(new JvmBufferPoolMetrics(pools.get(i))); 24 | } 25 | return result; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/influxdb/InfluxLogJvmGcMetricsV3Exporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.influxdb; 2 | 3 | import cn.myperf4j.base.metric.JvmGcMetricsV3; 4 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmGcMetricsV3Exporter; 5 | import cn.myperf4j.base.metric.formatter.JvmGcMetricsV3Formatter; 6 | import cn.myperf4j.base.metric.formatter.influxdb.InfluxJvmGcMetricsV3Formatter; 7 | 8 | import java.util.Collections; 9 | 10 | /** 11 | * Created by LinShunkang on 2024/02/08 12 | */ 13 | public class InfluxLogJvmGcMetricsV3Exporter extends AbstractLogJvmGcMetricsV3Exporter { 14 | 15 | private static final JvmGcMetricsV3Formatter METRICS_FORMATTER = new InfluxJvmGcMetricsV3Formatter(); 16 | 17 | @Override 18 | public void process(JvmGcMetricsV3 metrics, long processId, long startMillis, long stopMillis) { 19 | logger.log(METRICS_FORMATTER.format(Collections.singletonList(metrics), startMillis, stopMillis)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/test1/ExitingAdapter.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J.test1; 2 | 3 | import org.objectweb.asm.MethodVisitor; 4 | import org.objectweb.asm.Opcodes; 5 | import org.objectweb.asm.commons.AdviceAdapter; 6 | 7 | /** 8 | * Created by LinShunkang on 2018/4/23 9 | */ 10 | public class ExitingAdapter extends AdviceAdapter { 11 | 12 | private String name; 13 | 14 | public ExitingAdapter(MethodVisitor mv, int acc, String name, String desc) { 15 | super(Opcodes.ASM5, mv, acc, name, desc); 16 | this.name = name; 17 | } 18 | 19 | public void onMethodExit(int opcode) { 20 | mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;"); 21 | if (opcode == ATHROW) { 22 | mv.visitLdcInsn("Exiting on exception " + name); 23 | } else { 24 | mv.visitLdcInsn("Exiting " + name); 25 | } 26 | mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /MyPerf4J-Base/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | MyPerf4J 7 | MyPerf4J 8 | 3.6.0 9 | 10 | 4.0.0 11 | MyPerf4J-Base 12 | 13 | 14 | UTF-8 15 | 1.7 16 | 1.7 17 | 18 | 19 | 20 | 21 | junit 22 | junit 23 | test 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/influxdb/InfluxLogJvmClassMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.influxdb; 2 | 3 | import cn.myperf4j.base.metric.JvmClassMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmClassMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.influxdb.InfluxJvmClassMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmClassMetricsExporter; 7 | 8 | import java.util.Collections; 9 | 10 | /** 11 | * Created by LinShunkang on 2018/8/23 12 | */ 13 | public class InfluxLogJvmClassMetricsExporter extends AbstractLogJvmClassMetricsExporter { 14 | 15 | private static final JvmClassMetricsFormatter METRICS_FORMATTER = new InfluxJvmClassMetricsFormatter(); 16 | 17 | @Override 18 | public void process(JvmClassMetrics metrics, long processId, long startMillis, long stopMillis) { 19 | logger.log(METRICS_FORMATTER.format(Collections.singletonList(metrics), startMillis, stopMillis)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/influxdb/InfluxLogJvmMemoryMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.influxdb; 2 | 3 | import cn.myperf4j.base.metric.JvmMemoryMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmMemoryMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.influxdb.InfluxJvmMemoryMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmMemoryMetricsExporter; 7 | 8 | import java.util.Collections; 9 | 10 | /** 11 | * Created by LinShunkang on 2018/8/25 12 | */ 13 | public class InfluxLogJvmMemoryMetricsExporter extends AbstractLogJvmMemoryMetricsExporter { 14 | 15 | private static final JvmMemoryMetricsFormatter METRICS_FORMATTER = new InfluxJvmMemoryMetricsFormatter(); 16 | 17 | @Override 18 | public void process(JvmMemoryMetrics metrics, long processId, long startMillis, long stopMillis) { 19 | logger.log(METRICS_FORMATTER.format(Collections.singletonList(metrics), startMillis, stopMillis)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/influxdb/InfluxLogJvmThreadMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.influxdb; 2 | 3 | import cn.myperf4j.base.metric.JvmThreadMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmThreadMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.influxdb.InfluxJvmThreadMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmThreadMetricsExporter; 7 | 8 | import java.util.Collections; 9 | 10 | /** 11 | * Created by LinShunkang on 2018/8/25 12 | */ 13 | public class InfluxLogJvmThreadMetricsExporter extends AbstractLogJvmThreadMetricsExporter { 14 | 15 | private static final JvmThreadMetricsFormatter METRICS_FORMATTER = new InfluxJvmThreadMetricsFormatter(); 16 | 17 | @Override 18 | public void process(JvmThreadMetrics metrics, long processId, long startMillis, long stopMillis) { 19 | logger.log(METRICS_FORMATTER.format(Collections.singletonList(metrics), startMillis, stopMillis)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/net/NetUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.net; 2 | 3 | import java.io.IOException; 4 | import java.net.DatagramSocket; 5 | import java.net.ServerSocket; 6 | 7 | /** 8 | * Created by LinShunkang on 2020/12/06 9 | */ 10 | public final class NetUtils { 11 | 12 | public static final int MIN_PORT_NUMBER = 1; 13 | 14 | public static final int MAX_PORT_NUMBER = 65535; 15 | 16 | public static boolean isPortAvailable(int port) { 17 | if (port < MIN_PORT_NUMBER || port > MAX_PORT_NUMBER) { 18 | throw new IllegalArgumentException("Invalid port: " + port); 19 | } 20 | 21 | try (ServerSocket ss = new ServerSocket(port); DatagramSocket ds = new DatagramSocket(port)) { 22 | ss.setReuseAddress(true); 23 | ds.setReuseAddress(true); 24 | return true; 25 | } catch (IOException e) { 26 | return false; 27 | } 28 | } 29 | 30 | private NetUtils() { 31 | //empty 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/main/java/cn/myperf4j/asm/ASMRecorderMaintainer.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.asm; 2 | 3 | import cn.myperf4j.base.config.ProfilingParams; 4 | import cn.myperf4j.core.recorder.AbstractRecorderMaintainer; 5 | import cn.myperf4j.core.recorder.Recorders; 6 | 7 | /** 8 | * Created by LinShunkang on 2018/4/26 9 | */ 10 | public class ASMRecorderMaintainer extends AbstractRecorderMaintainer { 11 | 12 | private static final ASMRecorderMaintainer instance = new ASMRecorderMaintainer(); 13 | 14 | public static ASMRecorderMaintainer getInstance() { 15 | return instance; 16 | } 17 | 18 | @Override 19 | public boolean initOther() { 20 | return true; 21 | } 22 | 23 | @Override 24 | public void addRecorder(int methodTagId, ProfilingParams params) { 25 | for (int i = 0, size = recordersList.size(); i < size; ++i) { 26 | final Recorders recorders = recordersList.get(i); 27 | recorders.setRecorder(methodTagId, createRecorder(methodTagId, params)); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/influxdb/InfluxLogJvmFileDescMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.influxdb; 2 | 3 | import cn.myperf4j.base.metric.JvmFileDescriptorMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmFileDescMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.influxdb.InfluxJvmFileDescMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmFileDescMetricsExporter; 7 | 8 | import java.util.Collections; 9 | 10 | /** 11 | * Created by LinShunkang on 2019/11/09 12 | */ 13 | public class InfluxLogJvmFileDescMetricsExporter extends AbstractLogJvmFileDescMetricsExporter { 14 | 15 | private static final JvmFileDescMetricsFormatter METRICS_FORMATTER = new InfluxJvmFileDescMetricsFormatter(); 16 | 17 | @Override 18 | public void process(JvmFileDescriptorMetrics metrics, long processId, long startMillis, long stopMillis) { 19 | logger.log(METRICS_FORMATTER.format(Collections.singletonList(metrics), startMillis, stopMillis)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/influxdb/InfluxLogJvmBufferPoolMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.influxdb; 2 | 3 | import cn.myperf4j.base.metric.JvmBufferPoolMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmBufferPoolMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.influxdb.InfluxJvmBufferPoolMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmBufferPoolMetricsExporter; 7 | 8 | import java.util.Collections; 9 | 10 | /** 11 | * Created by LinShunkang on 2018/8/25 12 | */ 13 | public class InfluxLogJvmBufferPoolMetricsExporter extends AbstractLogJvmBufferPoolMetricsExporter { 14 | 15 | private static final JvmBufferPoolMetricsFormatter METRICS_FORMATTER = new InfluxJvmBufferPoolMetricsFormatter(); 16 | 17 | @Override 18 | public void process(JvmBufferPoolMetrics metrics, long processId, long startMillis, long stopMillis) { 19 | logger.log(METRICS_FORMATTER.format(Collections.singletonList(metrics), startMillis, stopMillis)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/influxdb/InfluxLogJvmCompilationMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.influxdb; 2 | 3 | import cn.myperf4j.base.metric.JvmCompilationMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmCompilationMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.influxdb.InfluxJvmCompilationMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmCompilationMetricsExporter; 7 | 8 | import java.util.Collections; 9 | 10 | /** 11 | * Created by LinShunkang on 2019/11/09 12 | */ 13 | public class InfluxLogJvmCompilationMetricsExporter extends AbstractLogJvmCompilationMetricsExporter { 14 | 15 | private static final JvmCompilationMetricsFormatter METRICS_FORMATTER = new InfluxJvmCompilationMetricsFormatter(); 16 | 17 | @Override 18 | public void process(JvmCompilationMetrics metrics, long processId, long startMillis, long stopMillis) { 19 | logger.log(METRICS_FORMATTER.format(Collections.singletonList(metrics), startMillis, stopMillis)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/http/HttpMethod.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.http; 2 | 3 | /** 4 | * Created by LinShunkang on 2020/05/15 5 | */ 6 | public enum HttpMethod { 7 | 8 | UNKNOWN("UNKNOWN", false), 9 | HEAD("HEAD", false), 10 | GET("GET", false), 11 | POST("POST", true), 12 | ; 13 | 14 | private final String name; 15 | 16 | private final boolean permitsBody; 17 | 18 | HttpMethod(String name, boolean permitsBody) { 19 | this.name = name; 20 | this.permitsBody = permitsBody; 21 | } 22 | 23 | public String getName() { 24 | return name; 25 | } 26 | 27 | public boolean isPermitsBody() { 28 | return permitsBody; 29 | } 30 | 31 | public static HttpMethod parse(String name) { 32 | switch (name) { 33 | case "HEAD": 34 | return HEAD; 35 | case "GET": 36 | return GET; 37 | case "POST": 38 | return POST; 39 | default: 40 | return UNKNOWN; 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/text/NumFormatUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.text; 2 | 3 | import java.text.DecimalFormat; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/7/9 7 | */ 8 | public final class NumFormatUtils { 9 | 10 | private static final ThreadLocal DECIMAL_FORMAT = new ThreadLocal() { 11 | @Override 12 | protected DecimalFormat initialValue() { 13 | return new DecimalFormat("0.00"); 14 | } 15 | }; 16 | 17 | private static final ThreadLocal PERCENT_FORMAT = new ThreadLocal() { 18 | @Override 19 | protected DecimalFormat initialValue() { 20 | return new DecimalFormat("0.00%"); 21 | } 22 | }; 23 | 24 | public static String doubleFormat(double num) { 25 | return DECIMAL_FORMAT.get().format(num); 26 | } 27 | 28 | public static String doublePercent(double num) { 29 | return PERCENT_FORMAT.get().format(num); 30 | } 31 | 32 | private NumFormatUtils() { 33 | //empty 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/influxdb/InfluxDbV1ClientTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.influxdb; 2 | 3 | import org.junit.Test; 4 | 5 | import java.util.concurrent.TimeUnit; 6 | 7 | /** 8 | * Created by LinShunkang on 2020/05/19 9 | */ 10 | public class InfluxDbV1ClientTest { 11 | 12 | private final InfluxDbV1Client influxDbV1Client = new InfluxDbV1Client.Builder() 13 | .host("127.0.0.1") 14 | .port(8086) 15 | .connectTimeout(100) 16 | .readTimeout(1000) 17 | .database("test_db_0") 18 | .username("admin") 19 | .password("admin123") 20 | .build(); 21 | 22 | @Test 23 | public void testWrite() throws InterruptedException { 24 | boolean write = influxDbV1Client.writeMetricsAsync( 25 | "cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000\n" + 26 | "cpu_load_short,host=server02,region=us-west value=0.96 1434055562000000000"); 27 | System.out.println(write); 28 | TimeUnit.SECONDS.sleep(3); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | ## What is the purpose of the change 2 | 3 | XXXXX 4 | 5 | ## Brief ChangeLog 6 | 7 | XXXXX 8 | 9 | ## Verifying this change 10 | 11 | XXXXX 12 | 13 | Follow this checklist to help us incorporate your contribution quickly and easily: 14 | 15 | - [x] Make sure there is a [GITHUB_issue](https://github.com/LinShunKang/MyPerf4J/issues) field for the change (usually before you start working on it). Trivial changes like typos do not require a GITHUB issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue. 16 | - [ ] Format the pull request title like `[MyPerf4J-XXX] Fix NullPointerException when host is null #XXX`. Each commit in the pull request should have a meaningful subject line and body. 17 | - [ ] Write a pull request description that is detailed enough to understand what the pull request does, how, and why. 18 | - [ ] Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. 19 | - [ ] Run `mvn clean package -Dmaven.test.skip=false` to make sure unit-test pass. -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/Version.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base; 2 | 3 | import cn.myperf4j.base.util.Logger; 4 | import cn.myperf4j.base.util.StrUtils; 5 | 6 | /** 7 | * Created by LinShunkang on 2020/08/09 8 | */ 9 | public final class Version { 10 | 11 | private static final String VERSION = getVersion(Version.class, ""); 12 | 13 | public static String getVersion() { 14 | return VERSION; 15 | } 16 | 17 | public static String getVersion(Class cls, String defaultVersion) { 18 | try { 19 | String version = cls.getPackage().getImplementationVersion(); 20 | if (StrUtils.isBlank(version)) { 21 | version = cls.getPackage().getSpecificationVersion(); 22 | } 23 | return StrUtils.isBlank(version) ? defaultVersion : version; 24 | } catch (Throwable e) { 25 | Logger.error("Version.getVersion(" + cls + ", " + defaultVersion + "): catch Exception ", e); 26 | return defaultVersion; 27 | } 28 | } 29 | 30 | private Version() { 31 | //empty 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/JvmClassMetrics.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric; 2 | 3 | /** 4 | * Created by LinShunkang on 2018/8/19 5 | */ 6 | public class JvmClassMetrics extends Metrics { 7 | 8 | private static final long serialVersionUID = 5189910445931453667L; 9 | 10 | private final long total; 11 | 12 | private final long loaded; 13 | 14 | private final long unloaded; 15 | 16 | public JvmClassMetrics(long total, long loaded, long unloaded) { 17 | this.total = total; 18 | this.loaded = loaded; 19 | this.unloaded = unloaded; 20 | } 21 | 22 | public long getTotal() { 23 | return total; 24 | } 25 | 26 | public long getLoaded() { 27 | return loaded; 28 | } 29 | 30 | public long getUnloaded() { 31 | return unloaded; 32 | } 33 | 34 | @Override 35 | public String toString() { 36 | return "JvmClassMetrics{" + 37 | "total=" + total + 38 | ", loaded=" + loaded + 39 | ", unloaded=" + unloaded + 40 | '}'; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/NumUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util; 2 | 3 | /** 4 | * Created by LinShunkang on 2019/11/03 5 | */ 6 | public final class NumUtils { 7 | 8 | private NumUtils() { 9 | //empty 10 | } 11 | 12 | public static double getPercent(double curNum, double maxNum) { 13 | if (curNum > 0L && maxNum > 0L) { 14 | return (100D * curNum) / maxNum; 15 | } 16 | return 0D; 17 | } 18 | 19 | public static int parseInt(String str, int defaultValue) { 20 | try { 21 | return Integer.parseInt(str); 22 | } catch (Exception e) { 23 | Logger.error("NumUtils.parseInt(" + str + ", " + defaultValue + ")", e); 24 | } 25 | return defaultValue; 26 | } 27 | 28 | public static long composeKv(int key, int value) { 29 | return ((long) value) << 32 | key; 30 | } 31 | 32 | public static int parseKey(long kv) { 33 | return (int) kv; 34 | } 35 | 36 | public static int parseValue(long kv) { 37 | return (int) (kv >> 32); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/http/QueryStringDecoderTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.http; 2 | 3 | import cn.myperf4j.base.http.server.QueryStringDecoder; 4 | import org.junit.Test; 5 | 6 | import java.nio.charset.StandardCharsets; 7 | import java.util.List; 8 | import java.util.Map; 9 | 10 | /** 11 | * Created by LinShunkang on 2020/07/12 12 | */ 13 | public class QueryStringDecoderTest { 14 | 15 | @Test 16 | public void testHasPath() { 17 | final String encodedURL = "/api/test?k1=v1&k2=v2&k3=v3"; 18 | QueryStringDecoder appDecoder = new QueryStringDecoder(encodedURL, StandardCharsets.UTF_8, true); 19 | Map> params = appDecoder.parameters(); 20 | System.out.println(params); 21 | } 22 | 23 | @Test 24 | public void testNoPath() { 25 | final String encodedURL = "k1=v1&k2=v2&k3=v3"; 26 | QueryStringDecoder appDecoder = new QueryStringDecoder(encodedURL, StandardCharsets.UTF_8, false); 27 | Map> params = appDecoder.parameters(); 28 | System.out.println(params); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/collector/JvmCompilationCollector.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.collector; 2 | 3 | import cn.myperf4j.base.metric.JvmCompilationMetrics; 4 | 5 | import java.lang.management.CompilationMXBean; 6 | import java.lang.management.ManagementFactory; 7 | 8 | /** 9 | * Created by LinShunkang on 2019/11/03 10 | */ 11 | public final class JvmCompilationCollector { 12 | 13 | private static volatile long lastTime; 14 | 15 | private static final CompilationMXBean COMPILATION_MX_BEAN = ManagementFactory.getCompilationMXBean(); 16 | 17 | private JvmCompilationCollector() { 18 | //empty 19 | } 20 | 21 | public static JvmCompilationMetrics collectCompilationMetrics() { 22 | final CompilationMXBean mxBean = COMPILATION_MX_BEAN; 23 | if (mxBean != null && mxBean.isCompilationTimeMonitoringSupported()) { 24 | final long totalTime = mxBean.getTotalCompilationTime(); 25 | return new JvmCompilationMetrics(totalTime - lastTime, lastTime = totalTime); 26 | } 27 | return new JvmCompilationMetrics(0L, 0L); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/DateUtilsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import cn.myperf4j.base.util.DateUtils; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | import java.util.Calendar; 8 | import java.util.Date; 9 | 10 | /** 11 | * Created by LinShunkang on 2018/10/28 12 | */ 13 | public class DateUtilsTest { 14 | 15 | @Test 16 | public void test() { 17 | Assert.assertTrue(DateUtils.isSameMinute(new Date(), new Date())); 18 | Assert.assertTrue(DateUtils.isSameHour(new Date(), new Date())); 19 | Assert.assertTrue(DateUtils.isSameDay(new Date(), new Date())); 20 | 21 | Calendar calendar1 = Calendar.getInstance(); 22 | calendar1.add(Calendar.MINUTE, 1); 23 | Assert.assertFalse(DateUtils.isSameMinute(new Date(), calendar1.getTime())); 24 | 25 | calendar1.add(Calendar.HOUR_OF_DAY, 1); 26 | Assert.assertFalse(DateUtils.isSameHour(new Date(), calendar1.getTime())); 27 | 28 | calendar1.add(Calendar.DATE, 1); 29 | Assert.assertFalse(DateUtils.isSameDay(new Date(), calendar1.getTime())); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/LineProtocolUtilsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import static cn.myperf4j.base.util.LineProtocolUtils.processTagOrField; 7 | 8 | /** 9 | * Created by LinShunkang on 2018/10/17 10 | */ 11 | public class LineProtocolUtilsTest { 12 | 13 | @Test 14 | public void test() { 15 | String expect = "method_metrics\\,AppName\\=TestApp\\,ClassName\\=TestClass\\,Method\\=TestClass.test\\ " + 16 | "RPS\\=1i\\,Avg\\=0.00\\,Min\\=0i\\,Max\\=0i\\,StdDev\\=0.00\\,Count\\=17i\\,TP50\\=0i\\,TP90\\=0i\\," + 17 | "TP95\\=0i\\,TP99\\=0i\\,TP999\\=0i\\,TP9999\\=0i\\,TP99999\\=0i\\,TP100\\=0i\\ 1539705590006000000"; 18 | Assert.assertEquals(expect, 19 | processTagOrField("method_metrics,AppName=TestApp,ClassName=TestClass,Method=TestClass.test " + 20 | "RPS=1i,Avg=0.00,Min=0i,Max=0i,StdDev=0.00,Count=17i,TP50=0i,TP90=0i," + 21 | "TP95=0i,TP99=0i,TP999=0i,TP9999=0i,TP99999=0i,TP100=0i 1539705590006000000")); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/MetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter; 2 | 3 | import cn.myperf4j.base.metric.Metrics; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/8/19 7 | *

8 | * 整体处理流程如下: 9 | * beforeProcess() -> process() -> process() -> *** -> process() -> afterProcess() 10 | * 即:每一轮统计均以beforeProcess()开始,以afterProcess()结束,中间会多次调用process() 11 | */ 12 | public interface MetricsExporter { 13 | 14 | /** 15 | * 在每一轮统计指标开始处理前调用 16 | * 17 | * @param processId : 本轮统计的唯一ID 18 | */ 19 | void beforeProcess(long processId, long startMillis, long stopMillis); 20 | 21 | /** 22 | * @param metrics : 本轮统计的某一个指标 23 | * @param processId : 本轮统计的唯一ID 24 | * @param startMillis : 本轮统计信息的起始时间,单位为ms 25 | * @param stopMillis : 本轮统计信息的终止时间,单位为ms 26 | */ 27 | void process(T metrics, long processId, long startMillis, long stopMillis); 28 | 29 | /** 30 | * 在每一轮统计指标开始处理前调用 31 | * 32 | * @param processId : 本轮统计的唯一ID 33 | */ 34 | void afterProcess(long processId, long startMillis, long stopMillis); 35 | } 36 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/collector/JvmFileDescCollector.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.collector; 2 | 3 | import cn.myperf4j.base.metric.JvmFileDescriptorMetrics; 4 | import com.sun.management.UnixOperatingSystemMXBean; 5 | 6 | import java.lang.management.ManagementFactory; 7 | import java.lang.management.OperatingSystemMXBean; 8 | 9 | /** 10 | * Created by LinShunkang on 2019/11/03 11 | */ 12 | public final class JvmFileDescCollector { 13 | 14 | private static final OperatingSystemMXBean SYSTEM_MX_BEAN = ManagementFactory.getOperatingSystemMXBean(); 15 | 16 | private JvmFileDescCollector() { 17 | //empty 18 | } 19 | 20 | public static JvmFileDescriptorMetrics collectFileDescMetrics() { 21 | if (SYSTEM_MX_BEAN instanceof UnixOperatingSystemMXBean) { 22 | final UnixOperatingSystemMXBean unixMXBean = (UnixOperatingSystemMXBean) SYSTEM_MX_BEAN; 23 | return new JvmFileDescriptorMetrics(unixMXBean.getOpenFileDescriptorCount(), 24 | unixMXBean.getMaxFileDescriptorCount()); 25 | } 26 | return new JvmFileDescriptorMetrics(0L, 0L); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/JvmFileDescriptorMetrics.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric; 2 | 3 | import cn.myperf4j.base.util.NumUtils; 4 | 5 | /** 6 | * Created by LinShunkang on 2019/11/03 7 | */ 8 | public class JvmFileDescriptorMetrics extends Metrics { 9 | 10 | private static final long serialVersionUID = -4979694928407737915L; 11 | 12 | private final long openCount; 13 | 14 | private final long maxCount; 15 | 16 | public JvmFileDescriptorMetrics(long openCount, long maxCount) { 17 | this.openCount = openCount; 18 | this.maxCount = maxCount; 19 | } 20 | 21 | public long getOpenCount() { 22 | return openCount; 23 | } 24 | 25 | public long getMaxCount() { 26 | return maxCount; 27 | } 28 | 29 | public double getOpenPercent() { 30 | return NumUtils.getPercent(openCount, maxCount); 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return "JvmFileDescriptorMetrics{" + 36 | "curCount=" + openCount + 37 | ", maxCount=" + maxCount + 38 | "} " + super.toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/influxdb/InfluxDbV2ClientTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.influxdb; 2 | 3 | import org.junit.After; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | /** 8 | * Created by LinShunkang on 2020/05/19 9 | */ 10 | public class InfluxDbV2ClientTest { 11 | 12 | private final InfluxDbV2Client influxDbClient = new InfluxDbV2Client.Builder() 13 | .orgName("localhost") 14 | .host("127.0.0.1") 15 | .port(8086) 16 | .connectTimeout(100) 17 | .readTimeout(1000) 18 | .database("MyPerf4J") 19 | .username("admin") 20 | .password("admin123") 21 | .build(); 22 | 23 | @Test 24 | public void testWrite() { 25 | Assert.assertTrue(influxDbClient.writeMetricsSync( 26 | "cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000\n" + 27 | "cpu_load_short,host=server02,region=us-west value=0.96 1434055562000000000")); 28 | } 29 | 30 | @After 31 | public void testClose() { 32 | Assert.assertTrue(influxDbClient.close()); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /MyPerf4J-Benchmark/src/main/java/cn/myperf4j/bench/profiling/ThroughputBench.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.bench.profiling; 2 | 3 | import org.openjdk.jmh.annotations.BenchmarkMode; 4 | import org.openjdk.jmh.annotations.Mode; 5 | import org.openjdk.jmh.annotations.OutputTimeUnit; 6 | import org.openjdk.jmh.runner.Runner; 7 | import org.openjdk.jmh.runner.RunnerException; 8 | import org.openjdk.jmh.runner.options.Options; 9 | import org.openjdk.jmh.runner.options.OptionsBuilder; 10 | 11 | import java.util.concurrent.TimeUnit; 12 | 13 | /** 14 | * Created by LinShunkang on 2019/08/31 15 | */ 16 | @BenchmarkMode({Mode.Throughput}) 17 | @OutputTimeUnit(TimeUnit.SECONDS) 18 | public class ThroughputBench extends AbstractBench { 19 | 20 | public static void main(String[] args) throws RunnerException { 21 | // 使用一个单独进程执行测试,执行5遍warmup,然后执行5遍测试 22 | Options opt = new OptionsBuilder() 23 | .include(ThroughputBench.class.getSimpleName()) 24 | .forks(1) 25 | .threads(1) 26 | .warmupIterations(3) 27 | .measurementIterations(5) 28 | .build(); 29 | new Runner(opt).run(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /MyPerf4J-Core/src/main/java/cn/myperf4j/core/recorder/Recorder.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.core.recorder; 2 | 3 | import cn.myperf4j.base.buffer.LongBuf; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/3/13 7 | */ 8 | public abstract class Recorder { 9 | 10 | private final int methodTagId; 11 | 12 | public Recorder(int methodTagId) { 13 | this.methodTagId = methodTagId; 14 | } 15 | 16 | public int getMethodTagId() { 17 | return methodTagId; 18 | } 19 | 20 | public abstract boolean hasRecord(); 21 | 22 | public abstract void recordTime(long startNanoTime, long endNanoTime); 23 | 24 | /** 25 | * 为了节省内存的使用,利用 LongBuf 承载返回结果 26 | * 27 | * @param longBuf : intBuf#capacity 至少为 diffCount! 28 | * 其中: 29 | * 第 0 位存 ((long)timeCost << 32) | count, 30 | * 第 1 位存 ((long)timeCost << 32) | count, 31 | * 以此类推 32 | * @return 总请求数 33 | */ 34 | public abstract long fillSortedRecords(LongBuf longBuf); 35 | 36 | /** 37 | * 获取有效的记录的个数 38 | */ 39 | public abstract int getDiffCount(); 40 | 41 | public abstract void resetRecord(); 42 | } 43 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/RollingFileWriterTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import cn.myperf4j.base.file.AutoRollingFileWriter; 4 | import cn.myperf4j.base.file.DailyRollingFileWriter; 5 | import cn.myperf4j.base.file.HourlyRollingFileWriter; 6 | import cn.myperf4j.base.file.MinutelyRollingFileWriter; 7 | import org.junit.Test; 8 | 9 | /** 10 | * Created by LinShunkang on 2018/10/17 11 | */ 12 | public class RollingFileWriterTest { 13 | 14 | @Test 15 | public void test() { 16 | AutoRollingFileWriter writer1 = new MinutelyRollingFileWriter("/tmp/test1.log", 1); 17 | test(writer1); 18 | 19 | AutoRollingFileWriter writer2 = new HourlyRollingFileWriter("/tmp/test2.log", 1); 20 | test(writer2); 21 | 22 | AutoRollingFileWriter writer3 = new DailyRollingFileWriter("/tmp/test3.log", 1); 23 | test(writer3); 24 | } 25 | 26 | private void test(AutoRollingFileWriter writer) { 27 | writer.write("111111"); 28 | writer.write("222222"); 29 | writer.write("333333"); 30 | writer.writeAndFlush("44444"); 31 | writer.preCloseFile(); 32 | writer.closeFile(true); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /MyPerf4J-Benchmark/src/main/java/cn/myperf4j/bench/profiling/TimeCostBench.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.bench.profiling; 2 | 3 | import org.openjdk.jmh.annotations.BenchmarkMode; 4 | import org.openjdk.jmh.annotations.Mode; 5 | import org.openjdk.jmh.annotations.OutputTimeUnit; 6 | import org.openjdk.jmh.runner.Runner; 7 | import org.openjdk.jmh.runner.RunnerException; 8 | import org.openjdk.jmh.runner.options.Options; 9 | import org.openjdk.jmh.runner.options.OptionsBuilder; 10 | 11 | import java.util.concurrent.TimeUnit; 12 | 13 | /** 14 | * Created by LinShunkang on 2019/08/31 15 | */ 16 | @BenchmarkMode({Mode.AverageTime, Mode.SampleTime}) 17 | @OutputTimeUnit(TimeUnit.NANOSECONDS) 18 | public class TimeCostBench extends AbstractBench { 19 | 20 | public static void main(String[] args) throws RunnerException { 21 | // 使用一个单独进程执行测试,执行5遍warmup,然后执行5遍测试 22 | Options opt = new OptionsBuilder() 23 | .include(TimeCostBench.class.getSimpleName()) 24 | .forks(1) 25 | .threads(1) 26 | .warmupIterations(3) 27 | .measurementIterations(5) 28 | .build(); 29 | new Runner(opt).run(); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/util/DateUtilsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.util.Date; 7 | 8 | import static cn.myperf4j.base.util.DateUtils.isSameDay; 9 | import static cn.myperf4j.base.util.DateUtils.isSameHour; 10 | import static cn.myperf4j.base.util.DateUtils.isSameMinute; 11 | 12 | /** 13 | * Created by LinShunkang on 2020/07/26 14 | */ 15 | public class DateUtilsTest { 16 | 17 | @Test 18 | public void testSameDay() { 19 | Assert.assertTrue(isSameDay(new Date(), new Date())); 20 | Assert.assertFalse(isSameDay(new Date(), new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000))); 21 | } 22 | 23 | @Test 24 | public void testSameMinute() { 25 | Assert.assertTrue(isSameMinute(new Date(), new Date())); 26 | Assert.assertFalse(isSameMinute(new Date(), new Date(System.currentTimeMillis() + 60 * 1000))); 27 | } 28 | 29 | @Test 30 | public void testSameHour() { 31 | Assert.assertTrue(isSameHour(new Date(), new Date())); 32 | Assert.assertFalse(isSameHour(new Date(), new Date(System.currentTimeMillis() + 60 * 60 * 1000))); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/concurrent/ExecutorManager.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.concurrent; 2 | 3 | import cn.myperf4j.base.util.Logger; 4 | 5 | import java.util.HashSet; 6 | import java.util.Set; 7 | import java.util.concurrent.ExecutorService; 8 | import java.util.concurrent.TimeUnit; 9 | 10 | /** 11 | * Created by LinShunkang on 2018/8/23 12 | *

13 | * 该类用于统一管理ExecutorService,便于统一关闭线程池 14 | */ 15 | public final class ExecutorManager { 16 | 17 | private static final Set executors = new HashSet<>(); 18 | 19 | private ExecutorManager() { 20 | //empty 21 | } 22 | 23 | public static void addExecutorService(ExecutorService executor) { 24 | executors.add(executor); 25 | } 26 | 27 | public static void stopAll(long timeout, TimeUnit unit) { 28 | for (ExecutorService executorService : executors) { 29 | try { 30 | executorService.shutdown(); 31 | executorService.awaitTermination(timeout, unit); 32 | } catch (InterruptedException e) { 33 | Logger.error("ExecutorManager.stopAll()", e); 34 | Thread.currentThread().interrupt(); 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /MyPerf4J-Core/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | MyPerf4J 7 | MyPerf4J 8 | 3.6.0 9 | 10 | 4.0.0 11 | 12 | MyPerf4J-Core 13 | 14 | MyPerf4J-Core 15 | 16 | 17 | UTF-8 18 | 1.7 19 | 1.7 20 | 21 | 22 | 23 | 24 | junit 25 | junit 26 | test 27 | 28 | 29 | 30 | MyPerf4J 31 | MyPerf4J-Base 32 | ${project.version} 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/http/HttpRespStatusTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.http; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import static cn.myperf4j.base.http.HttpRespStatus.BAD_REQUEST; 7 | import static cn.myperf4j.base.http.HttpRespStatus.CONTINUE; 8 | import static cn.myperf4j.base.http.HttpRespStatus.INTERNAL_SERVER_ERROR; 9 | import static cn.myperf4j.base.http.HttpRespStatus.MOVED_PERMANENTLY; 10 | import static cn.myperf4j.base.http.HttpRespStatus.MULTIPLE_CHOICES; 11 | import static cn.myperf4j.base.http.HttpRespStatus.OK; 12 | 13 | /** 14 | * Created by LinShunkang on 2020/05/16 15 | */ 16 | public class HttpRespStatusTest { 17 | 18 | @Test 19 | public void testValueOf() { 20 | Assert.assertEquals(CONTINUE, HttpRespStatus.valueOf(100)); 21 | Assert.assertEquals(OK, HttpRespStatus.valueOf(200)); 22 | Assert.assertEquals(MULTIPLE_CHOICES, HttpRespStatus.valueOf(300)); 23 | Assert.assertEquals(MOVED_PERMANENTLY, HttpRespStatus.valueOf(301)); 24 | Assert.assertEquals(BAD_REQUEST, HttpRespStatus.valueOf(400)); 25 | Assert.assertEquals(INTERNAL_SERVER_ERROR, HttpRespStatus.valueOf(500)); 26 | 27 | Assert.assertEquals(100, HttpRespStatus.valueOf(100).code()); 28 | Assert.assertEquals(200, HttpRespStatus.valueOf(200).code()); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/http/HttpHeadersTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.http; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.util.Arrays; 7 | import java.util.Collections; 8 | 9 | /** 10 | * Created by LinShunkang on 2020/05/16 11 | */ 12 | public class HttpHeadersTest { 13 | 14 | @Test 15 | public void testSet() { 16 | HttpHeaders headers = new HttpHeaders(2); 17 | headers.set("Connection", "close"); 18 | headers.set("Connection", "Keep-Alive"); 19 | headers.set("Accept-Encoding", "gzip, deflate"); 20 | 21 | Assert.assertEquals("Keep-Alive", headers.get("Connection")); 22 | Assert.assertEquals("gzip, deflate", headers.get("Accept-Encoding")); 23 | 24 | Assert.assertEquals(Collections.singletonList("Keep-Alive"), headers.getValues("Connection")); 25 | Assert.assertEquals(Collections.singletonList("gzip, deflate"), headers.getValues("Accept-Encoding")); 26 | } 27 | 28 | @Test 29 | public void testAdd() { 30 | HttpHeaders headers = new HttpHeaders(2); 31 | headers.set("Connection", "Keep-Alive"); 32 | headers.add("Connection", "close"); 33 | 34 | Assert.assertEquals("Keep-Alive", headers.get("Connection")); 35 | Assert.assertEquals(Arrays.asList("Keep-Alive", "close"), headers.getValues("Connection")); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/JvmGcMetricsV3.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric; 2 | 3 | /** 4 | * Created by LinShunkang on 2024/02/08 5 | */ 6 | public class JvmGcMetricsV3 extends Metrics { 7 | 8 | private static final long serialVersionUID = -233095689152915892L; 9 | 10 | private final String gcName; 11 | 12 | private final long gcCount; 13 | 14 | private final long gcTime; 15 | 16 | private final double avgGcTime; 17 | 18 | public JvmGcMetricsV3(String gcName, long gcCount, long gcTime) { 19 | this.gcName = gcName; 20 | this.gcCount = gcCount; 21 | this.gcTime = gcTime; 22 | this.avgGcTime = gcCount > 0L ? ((double) gcTime) / gcCount : 0D; 23 | } 24 | 25 | public String getGcName() { 26 | return gcName; 27 | } 28 | 29 | public long getGcCount() { 30 | return gcCount; 31 | } 32 | 33 | public long getGcTime() { 34 | return gcTime; 35 | } 36 | 37 | public double getAvgGcTime() { 38 | return avgGcTime; 39 | } 40 | 41 | @Override 42 | public String toString() { 43 | return "JvmGcMetricsV3{" + 44 | "gcName='" + gcName + '\'' + 45 | ", gcCount=" + gcCount + 46 | ", gcTime=" + gcTime + 47 | ", avgGcTime=" + avgGcTime + 48 | "} " + super.toString(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/MyPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import cn.myperf4j.base.config.MyProperties; 4 | import cn.myperf4j.base.constant.PropertyKeys; 5 | import cn.myperf4j.base.constant.PropertyKeys.Basic; 6 | import cn.myperf4j.base.constant.PropertyKeys.Filter; 7 | import cn.myperf4j.base.constant.PropertyKeys.Metrics; 8 | import org.junit.Assert; 9 | import org.junit.Test; 10 | 11 | /** 12 | * Created by LinShunkang on 2018/10/28 13 | */ 14 | public class MyPropertiesTest extends BaseTest { 15 | 16 | @Test 17 | public void test() { 18 | Assert.assertEquals(MyProperties.getStr(PropertyKeys.PRO_FILE_NAME), BaseTest.TEMP_FILE); 19 | Assert.assertEquals(MyProperties.getStr(Basic.APP_NAME), BaseTest.APP_NAME); 20 | Assert.assertEquals(MyProperties.getStr(Metrics.EXPORTER), BaseTest.METRICS_EXPORTER); 21 | Assert.assertEquals(MyProperties.getStr(Filter.PACKAGES_INCLUDE), BaseTest.INCLUDE_PACKAGES); 22 | 23 | MyProperties.setStr("key", "value"); 24 | Assert.assertEquals(MyProperties.getStr("key"), "value"); 25 | Assert.assertTrue(MyProperties.isSame("key", "value")); 26 | 27 | MyProperties.setStr("long", "1000"); 28 | Assert.assertEquals(MyProperties.getLong("long", 1), 1000); 29 | Assert.assertEquals(MyProperties.getLong("long", 1, 10000), 10000); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/test1/EnteringAdapter.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J.test1; 2 | 3 | import org.objectweb.asm.Label; 4 | import org.objectweb.asm.MethodVisitor; 5 | import org.objectweb.asm.Opcodes; 6 | import org.objectweb.asm.Type; 7 | import org.objectweb.asm.commons.AdviceAdapter; 8 | 9 | /** 10 | * Created by LinShunkang on 2018/4/23 11 | */ 12 | public class EnteringAdapter extends AdviceAdapter { 13 | 14 | private String name; 15 | private int timeVar; 16 | private Label timeVarStart = new Label(); 17 | private Label timeVarEnd = new Label(); 18 | 19 | public EnteringAdapter(MethodVisitor mv, int acc, String name, String desc) { 20 | super(Opcodes.ASM5, mv, acc, name, desc); 21 | this.name = name; 22 | } 23 | 24 | protected void onMethodEnter() { 25 | visitLabel(timeVarStart); 26 | int timeVar = newLocal(Type.getType("J")); 27 | visitLocalVariable("timeVar", "J", null, timeVarStart, timeVarEnd, timeVar); 28 | super.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;"); 29 | super.visitLdcInsn("Entering " + name); 30 | super.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); 31 | } 32 | 33 | public void visitMaxs(int stack, int locals) { 34 | visitLabel(timeVarEnd); 35 | super.visitMaxs(stack, locals); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/StrUtilsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.util.Arrays; 7 | 8 | import static cn.myperf4j.base.util.StrUtils.isBlank; 9 | import static cn.myperf4j.base.util.StrUtils.isEmpty; 10 | import static cn.myperf4j.base.util.StrUtils.splitAsList; 11 | 12 | /** 13 | * Created by LinShunkang on 2019/05/12 14 | */ 15 | public class StrUtilsTest { 16 | 17 | @Test 18 | public void testBlank() { 19 | Assert.assertTrue(isBlank(" ")); 20 | Assert.assertTrue(isBlank("\t")); 21 | Assert.assertTrue(isBlank("\n")); 22 | Assert.assertTrue(isBlank("")); 23 | Assert.assertTrue(isBlank(null)); 24 | Assert.assertFalse(isBlank("a")); 25 | } 26 | 27 | @Test 28 | public void testEmpty() { 29 | Assert.assertTrue(isEmpty("")); 30 | Assert.assertTrue(isEmpty(null)); 31 | Assert.assertFalse(isEmpty("a")); 32 | } 33 | 34 | @Test 35 | public void testSplitAsList() { 36 | Assert.assertEquals(Arrays.asList("A", "B", "C"), splitAsList("A,B,C", ',')); 37 | Assert.assertEquals(Arrays.asList("A", "B", "C"), splitAsList("A,B,C,", ',')); 38 | Assert.assertEquals(Arrays.asList("A", "B", "C"), splitAsList(",A,B,C,", ',')); 39 | Assert.assertEquals(Arrays.asList("A", "B", "C"), splitAsList(",,,,A,B,C,,,,", ',')); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/test1/Test.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J.test1; 2 | 3 | import org.objectweb.asm.ClassReader; 4 | import org.objectweb.asm.ClassWriter; 5 | 6 | import java.io.File; 7 | import java.io.FileOutputStream; 8 | import java.io.IOException; 9 | 10 | /** 11 | * Created by LinShunkang on 2018/4/23 12 | */ 13 | public final class Test { 14 | 15 | private Test() { 16 | //empty 17 | } 18 | 19 | public static void main(String[] args) throws IOException { 20 | test1(); 21 | // runNewFoo(); 22 | } 23 | 24 | private static void test1() throws IOException { 25 | ClassReader cr = new ClassReader(Foo.class.getName()); 26 | ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); 27 | MyClassAdapter cv = new MyClassAdapter(cw, Foo.class); 28 | cr.accept(cv, ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); 29 | 30 | byte[] bytes = cw.toByteArray(); 31 | rewriteClass(bytes); 32 | } 33 | 34 | private static void rewriteClass(byte[] toByte) throws IOException { 35 | File tofile = new File("/Users/thinker/WorkSpace/personal/MyPerf4J/MyPerf4J-ASM/target/test-classes" + 36 | "/MyPerf4J/Foo.class"); 37 | FileOutputStream fout = new FileOutputStream(tofile); 38 | fout.write(toByte); 39 | fout.close(); 40 | } 41 | 42 | private static void runNewFoo() { 43 | Foo.test1(); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/file/MinutelyRollingFileWriter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.file; 2 | 3 | import cn.myperf4j.base.util.DateUtils; 4 | 5 | import java.text.SimpleDateFormat; 6 | import java.util.Calendar; 7 | import java.util.Date; 8 | 9 | /** 10 | * Created by LinShunkang on 2018/9/7 11 | */ 12 | public class MinutelyRollingFileWriter extends AutoRollingFileWriter { 13 | 14 | private static final ThreadLocal FILE_DATE_FORMAT = new ThreadLocal() { 15 | @Override 16 | protected SimpleDateFormat initialValue() { 17 | return new SimpleDateFormat("'.'yyyy-MM-dd-HH-mm"); 18 | } 19 | }; 20 | 21 | public MinutelyRollingFileWriter(String fileName, int reserveFileCount) { 22 | super(fileName, reserveFileCount); 23 | } 24 | 25 | @Override 26 | String formatDateFileName(String fileName, Date date) { 27 | return fileName + FILE_DATE_FORMAT.get().format(date); 28 | } 29 | 30 | @Override 31 | Calendar computeEpochCal(Date now, int epochOffset) { 32 | final Calendar cal = Calendar.getInstance(); 33 | cal.setTime(now); 34 | cal.set(Calendar.SECOND, 0); 35 | cal.set(Calendar.MILLISECOND, 0); 36 | cal.add(Calendar.MINUTE, epochOffset); 37 | return cal; 38 | } 39 | 40 | @Override 41 | boolean isSameEpoch(Date date1, Date date2) { 42 | return DateUtils.isSameMinute(date1, date2); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/JvmBufferPoolMetrics.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric; 2 | 3 | import java.lang.management.BufferPoolMXBean; 4 | 5 | /** 6 | * Created by LinShunkang on 2018/11/1 7 | */ 8 | public class JvmBufferPoolMetrics extends Metrics { 9 | 10 | private static final long serialVersionUID = 1308517280962399791L; 11 | 12 | private final String name; 13 | 14 | private final long count; 15 | 16 | private final long memoryUsed; //KB 17 | 18 | private final long memoryCapacity; //KB 19 | 20 | public JvmBufferPoolMetrics(BufferPoolMXBean mxBean) { 21 | this.name = mxBean.getName(); 22 | this.count = mxBean.getCount(); 23 | this.memoryUsed = mxBean.getMemoryUsed() >> 10; 24 | this.memoryCapacity = mxBean.getTotalCapacity() >> 10; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public long getCount() { 32 | return count; 33 | } 34 | 35 | public long getMemoryUsed() { 36 | return memoryUsed; 37 | } 38 | 39 | public long getMemoryCapacity() { 40 | return memoryCapacity; 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "JvmBufferPoolMetrics{" + 46 | "name='" + name + '\'' + 47 | ", count=" + count + 48 | ", memoryUsed=" + memoryUsed + 49 | ", memoryCapacity=" + memoryCapacity + 50 | '}'; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/collections/ListUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.collections; 2 | 3 | import java.util.ArrayList; 4 | import java.util.List; 5 | 6 | /** 7 | * Created by LinShunkang on 2020/05/16 8 | */ 9 | public final class ListUtils { 10 | 11 | public static boolean isEmpty(List list) { 12 | return list == null || list.isEmpty(); 13 | } 14 | 15 | public static boolean isNotEmpty(List list) { 16 | return !isEmpty(list); 17 | } 18 | 19 | public static List> partition(List list, int size) { 20 | if (size <= 0) { 21 | throw new IllegalArgumentException(size + " <= 0"); 22 | } 23 | 24 | if (list == null || list.size() == 0) { 25 | return new ArrayList<>(0); 26 | } 27 | 28 | final List> result = new ArrayList<>(list.size() / size + 1); 29 | for (int fromIndex = 0; fromIndex < list.size(); fromIndex += size) { 30 | result.add(getSubList(list, fromIndex, size)); 31 | } 32 | return result; 33 | } 34 | 35 | public static List getSubList(List list, int fromIndex, int limit) { 36 | if (limit <= 0) { 37 | throw new IllegalArgumentException(limit + " <= 0"); 38 | } 39 | 40 | final int toIndex = Math.min(list.size(), fromIndex + limit); 41 | return list.subList(fromIndex, toIndex); 42 | } 43 | 44 | private ListUtils() { 45 | //empty 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/file/HourlyRollingFileWriter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.file; 2 | 3 | import cn.myperf4j.base.util.DateUtils; 4 | 5 | import java.text.SimpleDateFormat; 6 | import java.util.Calendar; 7 | import java.util.Date; 8 | 9 | /** 10 | * Created by LinShunkang on 2018/9/7 11 | */ 12 | public class HourlyRollingFileWriter extends AutoRollingFileWriter { 13 | 14 | private static final ThreadLocal FILE_DATE_FORMAT = new ThreadLocal() { 15 | @Override 16 | protected SimpleDateFormat initialValue() { 17 | return new SimpleDateFormat("'.'yyyy-MM-dd-HH"); 18 | } 19 | }; 20 | 21 | public HourlyRollingFileWriter(String fileName, int reserveFileCount) { 22 | super(fileName, reserveFileCount); 23 | } 24 | 25 | @Override 26 | Calendar computeEpochCal(Date now, int epochOffset) { 27 | final Calendar cal = Calendar.getInstance(); 28 | cal.setTime(now); 29 | cal.set(Calendar.MINUTE, 0); 30 | cal.set(Calendar.SECOND, 0); 31 | cal.set(Calendar.MILLISECOND, 0); 32 | cal.add(Calendar.HOUR_OF_DAY, epochOffset); 33 | return cal; 34 | } 35 | 36 | @Override 37 | String formatDateFileName(String fileName, Date date) { 38 | return fileName + FILE_DATE_FORMAT.get().format(date); 39 | } 40 | 41 | @Override 42 | boolean isSameEpoch(Date date1, Date date2) { 43 | return DateUtils.isSameHour(date1, date2); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /MyPerf4J-Core/src/test/java/cn/myperf4j/core/SystemPropertiesTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.core; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import java.io.File; 7 | 8 | /** 9 | * Created by LinShunkang on 2019/07/28 10 | */ 11 | public class SystemPropertiesTest { 12 | 13 | @Test 14 | public void test() { 15 | System.out.println(System.getProperty("os.name")); 16 | System.out.println(System.getProperty("file.separator")); 17 | System.out.println(System.getProperty("path.separator")); 18 | System.out.println(System.getProperty("line.separator")); 19 | } 20 | 21 | @Test 22 | public void testParseConfigFileDir() { 23 | // windows下os.name的返回值是以 Windows 开头的 24 | final String osName = System.getProperty("os.name"); 25 | Assert.assertFalse(osName.startsWith("windows")); 26 | 27 | final boolean isWindowsOS = osName.toLowerCase().startsWith("windows"); 28 | if (isWindowsOS) { 29 | final String configFilePath = "C:\\path\\to\\MyPerf4J.properties"; 30 | final int idx = configFilePath.lastIndexOf(File.separatorChar); 31 | Assert.assertEquals(configFilePath.substring(0, idx + 1), "C:\\path\\to\\"); 32 | } else { 33 | final String configFilePath = "/path/to/MyPerf4J.properties"; 34 | final int idx = configFilePath.lastIndexOf(File.separatorChar); 35 | Assert.assertEquals(configFilePath.substring(0, idx + 1), "/path/to/"); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/LineProtocolUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util; 2 | 3 | import java.util.Map; 4 | import java.util.concurrent.ConcurrentHashMap; 5 | import java.util.regex.Pattern; 6 | 7 | /** 8 | * Created by LinShunkang on 2018/9/6 9 | */ 10 | public final class LineProtocolUtils { 11 | 12 | private static final Pattern SPACE_PATTERN = Pattern.compile(" "); 13 | 14 | private static final Pattern COMMA_PATTERN = Pattern.compile(","); 15 | 16 | private static final Pattern EQUAL_SIGN_PATTERN = Pattern.compile("="); 17 | 18 | private static final Map methodNameMap = new ConcurrentHashMap<>(1024); 19 | 20 | private LineProtocolUtils() { 21 | //empty 22 | } 23 | 24 | /** 25 | * 用于把tagOrField里的 ',' ' ' '=' 转义为符合LineProtocol的格式 26 | * 27 | * @param tagOrField : tag key、tag value、field key 28 | * @return: 符合LineProtocol格式的文本 29 | */ 30 | public static String processTagOrField(String tagOrField) { 31 | String lineProtocol = methodNameMap.get(tagOrField); 32 | if (lineProtocol != null) { 33 | return lineProtocol; 34 | } 35 | 36 | lineProtocol = SPACE_PATTERN.matcher(tagOrField).replaceAll("\\\\ "); 37 | lineProtocol = COMMA_PATTERN.matcher(lineProtocol).replaceAll("\\\\,"); 38 | lineProtocol = EQUAL_SIGN_PATTERN.matcher(lineProtocol).replaceAll("\\\\="); 39 | methodNameMap.put(tagOrField, lineProtocol); 40 | return lineProtocol; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/NumFormatUtilsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import static cn.myperf4j.base.util.text.NumFormatUtils.doubleFormat; 7 | import static cn.myperf4j.base.util.text.NumFormatUtils.doublePercent; 8 | 9 | /** 10 | * Created by LinShunkang on 2018/10/17 11 | */ 12 | public class NumFormatUtilsTest { 13 | 14 | @Test 15 | public void testDoubleFormat() { 16 | Assert.assertEquals("10011.22", doubleFormat(10011.22222D)); 17 | Assert.assertEquals("10011.22", doubleFormat(10011.22D)); 18 | Assert.assertEquals("1.22", doubleFormat(1.2222D)); 19 | Assert.assertEquals("1.20", doubleFormat(1.2D)); 20 | Assert.assertEquals("1.00", doubleFormat(1D)); 21 | Assert.assertEquals("0.00", doubleFormat(0D)); 22 | Assert.assertEquals("-1.00", doubleFormat(-1D)); 23 | Assert.assertEquals("-1.10", doubleFormat(-1.1D)); 24 | } 25 | 26 | @Test 27 | public void testDoublePercent() { 28 | Assert.assertEquals("0.00%", doublePercent(0.0D)); 29 | Assert.assertEquals("100.00%", doublePercent(1.0D)); 30 | Assert.assertEquals("-100.00%", doublePercent(-1.0D)); 31 | 32 | Assert.assertEquals("20.00%", doublePercent(0.2000D)); 33 | Assert.assertEquals("20.01%", doublePercent(0.2001D)); 34 | Assert.assertEquals("20.00%", doublePercent(0.20D)); 35 | Assert.assertEquals("120.00%", doublePercent(1.20D)); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/ClassLevelMapping.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import cn.myperf4j.base.config.LevelMappingFilter; 4 | import org.junit.Assert; 5 | import org.junit.Before; 6 | import org.junit.Test; 7 | 8 | import java.util.Arrays; 9 | import java.util.Collections; 10 | 11 | /** 12 | * Created by LinShunkang on 2019/05/12 13 | */ 14 | public class ClassLevelMapping { 15 | 16 | @Before 17 | public void init() { 18 | LevelMappingFilter.putLevelMapping("Controller", Collections.singletonList("*Controller")); 19 | LevelMappingFilter.putLevelMapping("Api", Collections.singletonList("*Api*")); 20 | LevelMappingFilter.putLevelMapping("Service", Arrays.asList("*Service", "*ServiceImpl")); 21 | } 22 | 23 | @Test 24 | public void test() { 25 | Assert.assertEquals(LevelMappingFilter.getClassLevel("com.google.UserController"), "Controller"); 26 | Assert.assertEquals(LevelMappingFilter.getClassLevel("com.google.UserApi"), "Api"); 27 | Assert.assertEquals(LevelMappingFilter.getClassLevel("com.google.UserApiImpl"), "Api"); 28 | Assert.assertEquals(LevelMappingFilter.getClassLevel("com.google.UserApiService"), "Api"); 29 | Assert.assertEquals(LevelMappingFilter.getClassLevel("com.google.UserApiServiceImpl"), "Api"); 30 | Assert.assertEquals(LevelMappingFilter.getClassLevel("com.google.UserService"), "Service"); 31 | Assert.assertEquals(LevelMappingFilter.getClassLevel("com.google.UserServiceImpl"), "Service"); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/file/DailyRollingFileWriter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.file; 2 | 3 | import cn.myperf4j.base.util.DateUtils; 4 | 5 | import java.text.SimpleDateFormat; 6 | import java.util.Calendar; 7 | import java.util.Date; 8 | 9 | /** 10 | * Created by LinShunkang on 2018/9/7 11 | */ 12 | public class DailyRollingFileWriter extends AutoRollingFileWriter { 13 | 14 | private static final ThreadLocal FILE_DATE_FORMAT = new ThreadLocal() { 15 | @Override 16 | protected SimpleDateFormat initialValue() { 17 | return new SimpleDateFormat("'.'yyyy-MM-dd"); 18 | } 19 | }; 20 | 21 | public DailyRollingFileWriter(String fileName, int reserveFileCount) { 22 | super(fileName, reserveFileCount); 23 | } 24 | 25 | @Override 26 | Calendar computeEpochCal(Date now, int epochOffset) { 27 | final Calendar cal = Calendar.getInstance(); 28 | cal.setTime(now); 29 | cal.set(Calendar.HOUR_OF_DAY, 0); 30 | cal.set(Calendar.MINUTE, 0); 31 | cal.set(Calendar.SECOND, 0); 32 | cal.set(Calendar.MILLISECOND, 0); 33 | cal.add(Calendar.DATE, epochOffset); 34 | return cal; 35 | } 36 | 37 | @Override 38 | String formatDateFileName(String fileName, Date date) { 39 | return fileName + FILE_DATE_FORMAT.get().format(date); 40 | } 41 | 42 | @Override 43 | boolean isSameEpoch(Date date1, Date date2) { 44 | return DateUtils.isSameDay(date1, date2); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/test1/FinallyAdapter.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J.test1; 2 | 3 | import org.objectweb.asm.Label; 4 | import org.objectweb.asm.MethodVisitor; 5 | import org.objectweb.asm.Opcodes; 6 | import org.objectweb.asm.commons.AdviceAdapter; 7 | 8 | /** 9 | * Created by LinShunkang on 2018/4/23 10 | */ 11 | public class FinallyAdapter extends AdviceAdapter { 12 | 13 | private String name; 14 | 15 | private Label startFinally = new Label(); 16 | 17 | public FinallyAdapter(MethodVisitor mv, int acc, String name, String desc) { 18 | super(Opcodes.ASM5, mv, acc, name, desc); 19 | this.name = name; 20 | } 21 | 22 | public void visitCode() { 23 | super.visitCode(); 24 | mv.visitLabel(startFinally); 25 | } 26 | 27 | public void visitMaxs(int maxStack, int maxLocals) { 28 | Label endFinally = new Label(); 29 | mv.visitTryCatchBlock(startFinally, endFinally, endFinally, null); 30 | mv.visitLabel(endFinally); 31 | onFinally(ATHROW); 32 | mv.visitInsn(ATHROW); 33 | mv.visitMaxs(maxStack, maxLocals); 34 | } 35 | 36 | protected void onMethodExit(int opcode) { 37 | if (opcode != ATHROW) { 38 | onFinally(opcode); 39 | } 40 | } 41 | 42 | private void onFinally(int opcode) { 43 | mv.visitFieldInsn(GETSTATIC, "java/lang/System", "err", "Ljava/io/PrintStream;"); 44 | mv.visitLdcInsn("Exiting " + name); 45 | mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /MyPerf4J-Benchmark/src/main/java/cn/myperf4j/bench/EnvUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.bench; 2 | 3 | import cn.myperf4j.asm.ASMBootstrap; 4 | import cn.myperf4j.base.constant.PropertyKeys; 5 | import cn.myperf4j.base.constant.PropertyValues.Metrics; 6 | import cn.myperf4j.base.file.AutoRollingFileWriter; 7 | import cn.myperf4j.base.file.MinutelyRollingFileWriter; 8 | 9 | /** 10 | * Created by LinShunkang on 2019/08/31 11 | */ 12 | public final class EnvUtils { 13 | 14 | private EnvUtils() { 15 | //empty 16 | } 17 | 18 | public static synchronized boolean initASMBootstrap(long milliTimeSlice) { 19 | initProperties(milliTimeSlice); 20 | return ASMBootstrap.getInstance().initial(); 21 | } 22 | 23 | private static void initProperties(long milliTimeSlice) { 24 | String propertiesFile = "/tmp/MyPerf4J.properties"; 25 | buildPropertiesFile(propertiesFile, milliTimeSlice); 26 | System.setProperty(PropertyKeys.PRO_FILE_NAME, propertiesFile); 27 | } 28 | 29 | private static void buildPropertiesFile(String propertiesFile, long milliTimeSlice) { 30 | AutoRollingFileWriter writer = new MinutelyRollingFileWriter(propertiesFile, 1); 31 | writer.write("app_name=MyPerf4JTest\n"); 32 | writer.write("metrics.exporter=" + Metrics.EXPORTER_LOG_STDOUT + "\n"); 33 | writer.write("filter.packages.include=MyPerf4J\n"); 34 | writer.write("metrics.time_slice.method=" + milliTimeSlice + "\n"); 35 | writer.write("metrics.time_slice.jvm=" + milliTimeSlice + "\n"); 36 | writer.closeFile(true); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2018, LinShunkang 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/main/java/cn/myperf4j/asm/aop/ProfilingDynamicMethodVisitor.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.asm.aop; 2 | 3 | import org.objectweb.asm.MethodVisitor; 4 | import org.objectweb.asm.Type; 5 | import org.objectweb.asm.commons.AdviceAdapter; 6 | 7 | import static org.objectweb.asm.Type.LONG_TYPE; 8 | 9 | /** 10 | * Created by LinShunkang on 2018/4/15 11 | */ 12 | public class ProfilingDynamicMethodVisitor extends AdviceAdapter { 13 | 14 | private static final String PROFILING_ASPECT_INNER_NAME = Type.getInternalName(ProfilingAspect.class); 15 | 16 | private int startTimeIdentifier; 17 | 18 | public ProfilingDynamicMethodVisitor(int access, 19 | String name, 20 | String desc, 21 | MethodVisitor mv) { 22 | super(ASM9, mv, access, name, desc); 23 | } 24 | 25 | @Override 26 | protected void onMethodEnter() { 27 | mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "nanoTime", "()J", false); 28 | startTimeIdentifier = newLocal(LONG_TYPE); 29 | mv.visitVarInsn(LSTORE, startTimeIdentifier); 30 | } 31 | 32 | @Override 33 | protected void onMethodExit(int opcode) { 34 | if (IRETURN <= opcode && opcode <= RETURN || opcode == ATHROW) { 35 | mv.visitVarInsn(LLOAD, startTimeIdentifier); 36 | mv.visitVarInsn(ALOAD, 2); 37 | mv.visitMethodInsn(INVOKESTATIC, PROFILING_ASPECT_INNER_NAME, "profiling", 38 | "(JLjava/lang/reflect/Method;)V", false); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/buffer/LongBuffTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.buffer; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | /** 7 | * Created by LinShunkang on 2022/08/14 8 | */ 9 | public class LongBuffTest { 10 | 11 | @Test 12 | public void testLongBuf() { 13 | try (LongBuf longBuf = new LongBuf(100)) { 14 | Assert.assertEquals(longBuf.capacity(), 100); 15 | 16 | longBuf.write(1L); 17 | longBuf.write(2L); 18 | Assert.assertEquals(longBuf.writerIndex(), 2L); 19 | 20 | longBuf.write(3); 21 | Assert.assertEquals(longBuf.writerIndex(), 3); 22 | 23 | Assert.assertEquals(longBuf.getLong(0), 1L); 24 | Assert.assertEquals(longBuf.getLong(1), 2L); 25 | Assert.assertEquals(longBuf.getLong(2), 3L); 26 | 27 | Assert.assertEquals(longBuf.writerIndex(), 3); 28 | } 29 | } 30 | 31 | @Test 32 | public void testLongBufPool() { 33 | final LongBufPool pool = LongBufPool.getInstance(); 34 | try (LongBuf longBuf = pool.acquire(1024)) { 35 | Assert.assertEquals(longBuf.pool(), pool); 36 | longBuf.write(1); 37 | Assert.assertEquals(longBuf.writerIndex(), 1); 38 | 39 | longBuf.reset(); 40 | Assert.assertEquals(longBuf.writerIndex(), 0); 41 | } 42 | 43 | try (LongBuf intBuf2 = pool.acquire(100 * 1024 + 1)) { 44 | Assert.assertNotEquals(intBuf2.pool(), pool); 45 | Assert.assertNull(intBuf2.pool()); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /MyPerf4J-Benchmark/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | MyPerf4J 7 | MyPerf4J 8 | 3.6.0 9 | 10 | 4.0.0 11 | 12 | MyPerf4J-Benchmark 13 | 14 | MyPerf4J-Benchmark 15 | 16 | 17 | UTF-8 18 | 1.7 19 | 1.7 20 | 21 | 22 | 23 | 24 | junit 25 | junit 26 | test 27 | 28 | 29 | 30 | MyPerf4J 31 | MyPerf4J-ASM 32 | ${project.version} 33 | 34 | 35 | 36 | org.openjdk.jmh 37 | jmh-core 38 | 39 | 40 | 41 | org.openjdk.jmh 42 | jmh-generator-annprocess 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/dynamic/Test.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J.dynamic; 2 | 3 | import org.objectweb.asm.ClassReader; 4 | import org.objectweb.asm.ClassVisitor; 5 | import org.objectweb.asm.ClassWriter; 6 | 7 | import java.io.File; 8 | import java.io.FileOutputStream; 9 | import java.io.IOException; 10 | 11 | 12 | /** 13 | * Created by LinShunkang on 2018/4/23 14 | */ 15 | public final class Test { 16 | 17 | private Test() { 18 | //empty 19 | } 20 | 21 | public static void main(String[] args) throws Throwable { 22 | rewriteClass(); 23 | // runNewHandler(); 24 | } 25 | 26 | private static void rewriteClass() throws IOException { 27 | ClassReader cr = new ClassReader(DaoInvocationHandler.class.getName()); 28 | ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES); 29 | ClassVisitor cv = new DynamicClassAdapter(cw); 30 | cr.accept(cv, ClassReader.EXPAND_FRAMES); 31 | 32 | byte[] bytes = cw.toByteArray(); 33 | rewriteClass(bytes); 34 | } 35 | 36 | private static void rewriteClass(byte[] toByte) throws IOException { 37 | File tofile = new File("/Users/linshunkang/WorkSpace/personal/MyPerf4J/MyPerf4J-ASM/target" + 38 | "/test-classes/MyPerf4J/dynamic/DaoInvocationHandler.class"); 39 | FileOutputStream fout = new FileOutputStream(tofile); 40 | fout.write(toByte); 41 | fout.close(); 42 | } 43 | 44 | private static void runNewHandler() { 45 | DaoInvocationHandler handler = new DaoInvocationHandler(); 46 | handler.invoke(null, null, new Object[0]); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /MyPerf4J-Benchmark/src/main/java/cn/myperf4j/bench/profiling/AbstractBench.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.bench.profiling; 2 | 3 | import cn.myperf4j.asm.ASMRecorderMaintainer; 4 | import cn.myperf4j.asm.aop.ProfilingAspect; 5 | import cn.myperf4j.base.config.ProfilingParams; 6 | import cn.myperf4j.bench.EnvUtils; 7 | import org.openjdk.jmh.annotations.Benchmark; 8 | import org.openjdk.jmh.annotations.CompilerControl; 9 | import org.openjdk.jmh.annotations.Scope; 10 | import org.openjdk.jmh.annotations.Setup; 11 | import org.openjdk.jmh.annotations.State; 12 | 13 | /** 14 | * Created by LinShunkang on 2019/08/31 15 | */ 16 | @State(Scope.Thread) 17 | public class AbstractBench { 18 | 19 | @Setup 20 | public void setup() { 21 | boolean initASMBootstrap = EnvUtils.initASMBootstrap(60 * 1000); 22 | System.out.println("initASMBootstrap=" + initASMBootstrap); 23 | 24 | ASMRecorderMaintainer recorderMaintainer = ASMRecorderMaintainer.getInstance(); 25 | recorderMaintainer.addRecorder(0, ProfilingParams.of(1000, 16)); 26 | 27 | ProfilingAspect.setRunning(true); 28 | ProfilingAspect.setRecorderMaintainer(recorderMaintainer); 29 | } 30 | 31 | @Benchmark 32 | public int baseline() { 33 | return emptyMethod(); 34 | } 35 | 36 | @Benchmark 37 | public int profiling() { 38 | long start = System.nanoTime(); 39 | int result = emptyMethod(); 40 | ProfilingAspect.profiling(start, 0); 41 | return result; 42 | } 43 | 44 | @CompilerControl(CompilerControl.Mode.DONT_INLINE) 45 | public int emptyMethod() { 46 | return 1000; 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/standard/StdJvmCompilationMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter.standard; 2 | 3 | import cn.myperf4j.base.metric.JvmCompilationMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmCompilationMetricsFormatter; 5 | import cn.myperf4j.base.util.text.DateFormatUtils; 6 | 7 | import java.util.List; 8 | 9 | import static cn.myperf4j.base.util.SysProperties.LINE_SEPARATOR; 10 | 11 | /** 12 | * Created by LinShunkang on 2018/8/21 13 | */ 14 | public final class StdJvmCompilationMetricsFormatter implements JvmCompilationMetricsFormatter { 15 | 16 | private static final String TITLE_FORMAT = "%-16s%16s%n"; 17 | 18 | private static final String DATA_FORMAT = "%-16d%16d%n"; 19 | 20 | @Override 21 | public String format(List metricsList, long startMillis, long stopMillis) { 22 | final StringBuilder sb = new StringBuilder((metricsList.size() + 2) * (16 + 24)); 23 | sb.append("MyPerf4J JVM Compilation Metrics [").append(DateFormatUtils.format(startMillis)).append(", ") 24 | .append(DateFormatUtils.format(stopMillis)).append(']').append(LINE_SEPARATOR); 25 | sb.append(String.format(TITLE_FORMAT, "Time(ms)", "TotalTime(ms)")); 26 | if (metricsList.isEmpty()) { 27 | return sb.toString(); 28 | } 29 | 30 | for (int i = 0; i < metricsList.size(); ++i) { 31 | final JvmCompilationMetrics metrics = metricsList.get(i); 32 | sb.append(String.format(DATA_FORMAT, metrics.getTime(), metrics.getTotalTime())); 33 | } 34 | return sb.toString(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/StrMatchUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util; 2 | 3 | /** 4 | * Created by LinShunkang on 2019/05/04 5 | */ 6 | public final class StrMatchUtils { 7 | 8 | private StrMatchUtils() { 9 | //empty 10 | } 11 | 12 | /** 13 | * 该方法用于判断 str 是否满足 exp 表达式 14 | * 注意:目前只处理 '*' 作为模糊匹配 15 | *

16 | * 参考链接如下: 17 | * 1、https://www.cnblogs.com/daleyzou/p/9535134.html 18 | * 2、https://shmilyaw-hotmail-com.iteye.com/blog/2154716 19 | */ 20 | public static boolean isMatch(String str, String exp) { 21 | int strIndex = 0; 22 | int expIndex = 0; 23 | int starIndex = -1; //记录上一个 '*' 的位置 24 | while (strIndex < str.length()) { 25 | final char pkgChar = str.charAt(strIndex); 26 | final char expChar = expIndex < exp.length() ? exp.charAt(expIndex) : '\0'; 27 | if (pkgChar == expChar) { //字符相等 28 | strIndex++; 29 | expIndex++; 30 | } else if (expChar == '*') { //遇到'*', 记录'*'的位置,并记录 expIndex 和 match 31 | starIndex = expIndex; 32 | expIndex++; 33 | } else if (starIndex != -1) { //不是上述两种情况,无法匹配,因此回溯 34 | expIndex = starIndex + 1; 35 | strIndex++; 36 | } else { //其他情况, 直接返回false 37 | return false; 38 | } 39 | } 40 | 41 | //检测 exp 尾部是否全部都为 '*' 42 | while (expIndex < exp.length() && exp.charAt(expIndex) == '*') { 43 | expIndex++; 44 | } 45 | 46 | //若 exp 尾部全部为 '*',说明匹配 47 | return expIndex == exp.length(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/http/HttpResponse.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.http; 2 | 3 | import java.util.Arrays; 4 | 5 | import static java.nio.charset.StandardCharsets.UTF_8; 6 | 7 | /** 8 | * Created by LinShunkang on 2020/05/15 9 | */ 10 | public final class HttpResponse { 11 | 12 | private final HttpRespStatus status; 13 | 14 | private final HttpHeaders headers; 15 | 16 | private final byte[] body; 17 | 18 | private String bodyString; 19 | 20 | public HttpResponse(HttpRespStatus status, HttpHeaders headers, String bodyString) { 21 | this.status = status; 22 | this.headers = headers; 23 | this.body = bodyString.getBytes(UTF_8); 24 | this.bodyString = bodyString; 25 | } 26 | 27 | public HttpResponse(HttpRespStatus status, HttpHeaders headers, byte[] body) { 28 | this.status = status; 29 | this.headers = headers; 30 | this.body = body; 31 | } 32 | 33 | public HttpRespStatus getStatus() { 34 | return status; 35 | } 36 | 37 | public HttpHeaders getHeaders() { 38 | return headers; 39 | } 40 | 41 | public byte[] getBody() { 42 | return body; 43 | } 44 | 45 | public String getBodyString() { 46 | if (bodyString == null) { 47 | bodyString = new String(body, 0, body.length, UTF_8); 48 | } 49 | return bodyString; 50 | } 51 | 52 | @Override 53 | public String toString() { 54 | return "HttpResponse{" + 55 | "status=" + status + 56 | ", headers=" + headers + 57 | ", body=" + Arrays.toString(body) + 58 | '}'; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/main/java/cn/myperf4j/asm/ASMBootstrap.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.asm; 2 | 3 | import cn.myperf4j.asm.aop.ProfilingAspect; 4 | import cn.myperf4j.base.config.ProfilingConfig; 5 | import cn.myperf4j.base.config.RecorderConfig; 6 | import cn.myperf4j.base.util.Logger; 7 | import cn.myperf4j.core.AbstractBootstrap; 8 | import cn.myperf4j.core.recorder.AbstractRecorderMaintainer; 9 | 10 | /** 11 | * Created by LinShunkang on 2018/4/19 12 | */ 13 | public final class ASMBootstrap extends AbstractBootstrap { 14 | 15 | private static final ASMBootstrap instance = new ASMBootstrap(); 16 | 17 | private ASMBootstrap() { 18 | //empty 19 | } 20 | 21 | public static ASMBootstrap getInstance() { 22 | return instance; 23 | } 24 | 25 | @Override 26 | public AbstractRecorderMaintainer doInitRecorderMaintainer() { 27 | final RecorderConfig recorderConf = ProfilingConfig.recorderConfig(); 28 | final ASMRecorderMaintainer maintainer = ASMRecorderMaintainer.getInstance(); 29 | if (maintainer.initial(methodMetricsExporter, recorderConf.accurateMode(), recorderConf.backupCount())) { 30 | return maintainer; 31 | } 32 | return null; 33 | } 34 | 35 | @Override 36 | public boolean initOther() { 37 | return initProfilerAspect(); 38 | } 39 | 40 | private boolean initProfilerAspect() { 41 | try { 42 | ProfilingAspect.setRecorderMaintainer((ASMRecorderMaintainer) maintainer); 43 | ProfilingAspect.setRunning(true); 44 | return true; 45 | } catch (Exception e) { 46 | Logger.error("ASMBootstrap.initProfilerAspect()", e); 47 | } 48 | return false; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/http/HttpStatusClassTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.http; 2 | 3 | import org.junit.Assert; 4 | import org.junit.Test; 5 | 6 | import static cn.myperf4j.base.http.HttpStatusClass.CLIENT_ERROR; 7 | import static cn.myperf4j.base.http.HttpStatusClass.INFORMATIONAL; 8 | import static cn.myperf4j.base.http.HttpStatusClass.REDIRECTION; 9 | import static cn.myperf4j.base.http.HttpStatusClass.SERVER_ERROR; 10 | import static cn.myperf4j.base.http.HttpStatusClass.SUCCESS; 11 | import static cn.myperf4j.base.http.HttpStatusClass.UNKNOWN; 12 | 13 | /** 14 | * Created by LinShunkang on 2020/05/16 15 | */ 16 | public class HttpStatusClassTest { 17 | 18 | @Test 19 | public void testValueOf() { 20 | for (int i = -100; i < 100; i++) { 21 | Assert.assertEquals(UNKNOWN, HttpStatusClass.valueOf(i)); 22 | } 23 | 24 | for (int i = 100; i < 200; i++) { 25 | Assert.assertEquals(INFORMATIONAL, HttpStatusClass.valueOf(i)); 26 | } 27 | 28 | for (int i = 200; i < 300; i++) { 29 | Assert.assertEquals(SUCCESS, HttpStatusClass.valueOf(i)); 30 | } 31 | 32 | for (int i = 300; i < 400; i++) { 33 | Assert.assertEquals(REDIRECTION, HttpStatusClass.valueOf(i)); 34 | } 35 | 36 | for (int i = 400; i < 500; i++) { 37 | Assert.assertEquals(CLIENT_ERROR, HttpStatusClass.valueOf(i)); 38 | } 39 | 40 | for (int i = 500; i < 600; i++) { 41 | Assert.assertEquals(SERVER_ERROR, HttpStatusClass.valueOf(i)); 42 | } 43 | 44 | for (int i = 600; i < 1024; i++) { 45 | Assert.assertEquals(UNKNOWN, HttpStatusClass.valueOf(i)); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/test1/MyClassAdapter.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J.test1; 2 | 3 | import org.objectweb.asm.ClassVisitor; 4 | import org.objectweb.asm.MethodVisitor; 5 | import org.objectweb.asm.Opcodes; 6 | 7 | /** 8 | * Created by LinShunkang on 2018/4/23 9 | */ 10 | public class MyClassAdapter extends ClassVisitor implements Opcodes { 11 | 12 | private Class analysisClass; 13 | 14 | private boolean isInterface; 15 | 16 | public MyClassAdapter(final ClassVisitor cv, Class clazz) { 17 | super(ASM5, cv); 18 | this.analysisClass = clazz; 19 | } 20 | 21 | @Override 22 | public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { 23 | super.visit(version, access, name, signature, superName, interfaces); 24 | this.isInterface = (access & ACC_INTERFACE) != 0; 25 | } 26 | 27 | @Override 28 | public MethodVisitor visitMethod(int access, 29 | String name, 30 | String desc, 31 | String signature, 32 | String[] exceptions) { 33 | MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions); //先得到原始的方法 34 | if (isInterface || mv == null || "".equals(name) || "".equals(name)) { 35 | return mv; 36 | } 37 | 38 | // return new EnteringAdapter(mv, access, name, desc); //访问需要修改的方法 39 | // return new ExitingAdapter(mv, access, name, desc); //访问需要修改的方法 40 | 41 | MethodVisitor enteringMV = new EnteringAdapter(mv, access, name, desc); //访问需要修改的方法 42 | return new FinallyAdapter(enteringMV, access, name, desc); //访问需要修改的方法 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/log/LoggerFactory.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.log; 2 | 3 | import cn.myperf4j.base.config.MetricsConfig; 4 | import cn.myperf4j.base.config.ProfilingConfig; 5 | import cn.myperf4j.base.constant.PropertyValues.Metrics; 6 | 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | public final class LoggerFactory { 11 | 12 | private static final MetricsConfig METRICS_CONFIG = ProfilingConfig.metricsConfig(); 13 | 14 | private static final Map LOGGER_MAP = new HashMap<>(); 15 | 16 | static { 17 | Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { 18 | @Override 19 | public void run() { 20 | for (ILogger writer : LOGGER_MAP.values()) { 21 | writer.preCloseLog(); 22 | } 23 | 24 | for (ILogger writer : LOGGER_MAP.values()) { 25 | writer.closeLog(); 26 | } 27 | } 28 | })); 29 | } 30 | 31 | private LoggerFactory() { 32 | //empty 33 | } 34 | 35 | public static synchronized ILogger getLogger(String logFile) { 36 | logFile = logFile.trim(); 37 | 38 | if (logFile.equalsIgnoreCase(Metrics.NULL_FILE)) { 39 | return new NullLogger(); 40 | } else if (logFile.equalsIgnoreCase(Metrics.STDOUT_FILE)) { 41 | return new StdoutLogger(); 42 | } 43 | 44 | ILogger logger = LOGGER_MAP.get(logFile); 45 | if (logger != null) { 46 | return logger; 47 | } 48 | 49 | logger = new AutoRollingLogger(logFile, METRICS_CONFIG.logRollingTimeUnit(), METRICS_CONFIG.logReserveCount()); 50 | LOGGER_MAP.put(logFile, logger); 51 | return logger; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/standard/StdJvmClassMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter.standard; 2 | 3 | import cn.myperf4j.base.metric.JvmClassMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmClassMetricsFormatter; 5 | import cn.myperf4j.base.util.text.DateFormatUtils; 6 | 7 | import java.util.List; 8 | 9 | import static cn.myperf4j.base.util.SysProperties.LINE_SEPARATOR; 10 | 11 | /** 12 | * Created by LinShunkang on 2018/8/21 13 | */ 14 | public final class StdJvmClassMetricsFormatter implements JvmClassMetricsFormatter { 15 | 16 | private static final String TITLE_FORMAT = "%-10s%10s%10s%n"; 17 | 18 | private static final String DATA_FORMAT = "%-10d%10d%10d%n"; 19 | 20 | @Override 21 | public String format(List metricsList, long startMillis, long stopMillis) { 22 | final StringBuilder sb = new StringBuilder((metricsList.size() + 2) * (12 * 3 + 64)); 23 | sb.append("MyPerf4J JVM Class Metrics [").append(DateFormatUtils.format(startMillis)).append(", ") 24 | .append(DateFormatUtils.format(stopMillis)).append(']').append(LINE_SEPARATOR); 25 | sb.append(String.format(TITLE_FORMAT, "Total", "Loaded", "Unloaded")); 26 | if (metricsList.isEmpty()) { 27 | return sb.toString(); 28 | } 29 | 30 | for (int i = 0; i < metricsList.size(); ++i) { 31 | final JvmClassMetrics metrics = metricsList.get(i); 32 | sb.append( 33 | String.format(DATA_FORMAT, 34 | metrics.getTotal(), 35 | metrics.getLoaded(), 36 | metrics.getUnloaded() 37 | ) 38 | ); 39 | } 40 | return sb.toString(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/constant/PropertyValues.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.constant; 2 | 3 | /** 4 | * Created by LinShunkang on 2018/4/27 5 | */ 6 | public interface PropertyValues { 7 | 8 | String DEFAULT_PRO_FILE = "/data/MyPerf4J/MyPerf4J.properties"; 9 | 10 | interface LegacyValues { 11 | 12 | int METRICS_PROCESS_TYPE_STDOUT = 0; 13 | 14 | int METRICS_PROCESS_TYPE_LOGGER = 1; 15 | 16 | int METRICS_PROCESS_TYPE_INFLUX_DB = 2; 17 | } 18 | 19 | interface Metrics { 20 | 21 | String EXPORTER_LOG_STDOUT = "log.stdout"; 22 | 23 | String EXPORTER_LOG_STANDARD = "log.standard"; 24 | 25 | String EXPORTER_LOG_INFLUX_DB = "log.influxdb"; 26 | 27 | String EXPORTER_HTTP_INFLUX_DB = "http.influxdb"; 28 | 29 | String STDOUT_METRICS_FILE = "STDOUT"; 30 | 31 | String DEFAULT_METRICS_FILE = "/data/logs/MyPerf4J/metrics.log"; 32 | 33 | String NULL_FILE = "NULL"; 34 | 35 | String STDOUT_FILE = "STDOUT"; 36 | 37 | long DEFAULT_TIME_SLICE = 60 * 1000L; 38 | 39 | long MIN_TIME_SLICE = 1000L; 40 | 41 | long MAX_TIME_SLICE = 10 * 60 * 1000L; 42 | 43 | int DEFAULT_LOG_RESERVE_COUNT = 7; 44 | 45 | String LOG_ROLLING_MINUTELY = "MINUTELY"; 46 | 47 | String LOG_ROLLING_HOURLY = "HOURLY"; 48 | 49 | String LOG_ROLLING_DAILY = "DAILY"; 50 | } 51 | 52 | interface Recorder { 53 | 54 | String MODE_ACCURATE = "ACCURATE"; 55 | 56 | String MODE_ROUGH = "ROUGH"; 57 | 58 | int MIN_BACKUP_RECORDERS_COUNT = 1; 59 | 60 | int MAX_BACKUP_RECORDERS_COUNT = 8; 61 | } 62 | 63 | interface Separator { 64 | 65 | char ELE = ';'; 66 | 67 | char ELE_KV = ':'; 68 | 69 | char ARR_ELE = ','; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/collections/MapUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.collections; 2 | 3 | import java.util.HashMap; 4 | import java.util.LinkedHashMap; 5 | import java.util.Map; 6 | import java.util.concurrent.ConcurrentHashMap; 7 | 8 | /** 9 | * Created by LinShunkang on 2018/3/11 10 | */ 11 | public final class MapUtils { 12 | 13 | private static final float DEFAULT_LOAD_FACTOR = 0.75f; 14 | 15 | public static Map of(K k, V v) { 16 | Map map = createHashMap(1); 17 | map.put(k, v); 18 | return map; 19 | } 20 | 21 | public static Map createHashMap(int keyNum) { 22 | return new HashMap<>(getFitCapacity(keyNum)); 23 | } 24 | 25 | public static Map createHashMap(int keyNum, float loadFactor) { 26 | return new HashMap<>(getFitCapacity(keyNum, loadFactor)); 27 | } 28 | 29 | public static ConcurrentHashMap createConcHashMap(int keyNum, float loadFactor) { 30 | return new ConcurrentHashMap<>(getFitCapacity(keyNum, loadFactor)); 31 | } 32 | 33 | public static Map createLinkedHashMap(int keyNum) { 34 | return new LinkedHashMap<>(getFitCapacity(keyNum)); 35 | } 36 | 37 | public static int getFitCapacity(int keyNum) { 38 | return getFitCapacity(keyNum, DEFAULT_LOAD_FACTOR); 39 | } 40 | 41 | public static int getFitCapacity(int keyNum, float loadFactor) { 42 | return (int) (keyNum / loadFactor) + 1; 43 | } 44 | 45 | public static boolean isEmpty(Map map) { 46 | return map == null || map.isEmpty(); 47 | } 48 | 49 | public static boolean isNotEmpty(Map map) { 50 | return !isEmpty(map); 51 | } 52 | 53 | private MapUtils() { 54 | //empty 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/influxdb/InfluxDbClientFactory.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.influxdb; 2 | 3 | import cn.myperf4j.base.config.InfluxDbConfig; 4 | import cn.myperf4j.base.config.ProfilingConfig; 5 | 6 | /** 7 | * Created by LinShunkang on 2020/05/18 8 | */ 9 | public final class InfluxDbClientFactory { 10 | 11 | private static final InfluxDbConfig CONFIG = ProfilingConfig.influxDBConfig(); 12 | 13 | private static final InfluxDbClient CLIENT = generateClient(); 14 | 15 | private static InfluxDbClient generateClient() { 16 | final String version = CONFIG.version(); 17 | if (version.startsWith("2.")) { 18 | return new InfluxDbV2Client.Builder() 19 | .host(CONFIG.host()) 20 | .port(CONFIG.port()) 21 | .orgName(CONFIG.orgName()) 22 | .database(CONFIG.database()) 23 | .username(CONFIG.username()) 24 | .password(CONFIG.password()) 25 | .connectTimeout(CONFIG.connectTimeout()) 26 | .readTimeout(CONFIG.readTimeout()) 27 | .build(); 28 | } else { 29 | return new InfluxDbV1Client.Builder() 30 | .host(CONFIG.host()) 31 | .port(CONFIG.port()) 32 | .database(CONFIG.database()) 33 | .username(CONFIG.username()) 34 | .password(CONFIG.password()) 35 | .connectTimeout(CONFIG.connectTimeout()) 36 | .readTimeout(CONFIG.readTimeout()) 37 | .build(); 38 | } 39 | } 40 | 41 | public static InfluxDbClient getClient() { 42 | return CLIENT; 43 | } 44 | 45 | private InfluxDbClientFactory() { 46 | //empty 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/influxdb/InfluxJvmCompilationMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter.influxdb; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.metric.JvmCompilationMetrics; 5 | import cn.myperf4j.base.metric.formatter.JvmCompilationMetricsFormatter; 6 | 7 | import java.util.List; 8 | 9 | import static cn.myperf4j.base.util.net.IpUtils.getLocalhostName; 10 | import static cn.myperf4j.base.util.LineProtocolUtils.processTagOrField; 11 | 12 | /** 13 | * Created by LinShunkang on 2020/5/17 14 | */ 15 | public final class InfluxJvmCompilationMetricsFormatter implements JvmCompilationMetricsFormatter { 16 | 17 | @Override 18 | public String format(List metricsList, long startMillis, long stopMillis) { 19 | final StringBuilder sb = SB_TL.get(); 20 | try { 21 | final long startNanos = startMillis * 1000 * 1000L; 22 | for (int i = 0, size = metricsList.size(); i < size; ++i) { 23 | appendLineProtocol(metricsList.get(i), startNanos, sb); 24 | } 25 | return sb.substring(0, Math.max(0, sb.length() - 1)); 26 | } finally { 27 | sb.setLength(0); 28 | } 29 | } 30 | 31 | private void appendLineProtocol(JvmCompilationMetrics metrics, long startNanos, StringBuilder sb) { 32 | sb.append("jvm_compilation_metrics") 33 | .append(",AppName=").append(ProfilingConfig.basicConfig().appName()) 34 | .append(",host=").append(processTagOrField(getLocalhostName())) 35 | .append(" Time=").append(metrics.getTime()).append('i') 36 | .append(",TotalTime=").append(metrics.getTotalTime()).append('i') 37 | .append(' ').append(startNanos).append('\n'); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/influxdb/InfluxJvmFileDescMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter.influxdb; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.metric.JvmFileDescriptorMetrics; 5 | import cn.myperf4j.base.metric.formatter.JvmFileDescMetricsFormatter; 6 | 7 | import java.util.List; 8 | 9 | import static cn.myperf4j.base.util.net.IpUtils.getLocalhostName; 10 | import static cn.myperf4j.base.util.LineProtocolUtils.processTagOrField; 11 | 12 | /** 13 | * Created by LinShunkang on 2020/5/17 14 | */ 15 | public final class InfluxJvmFileDescMetricsFormatter implements JvmFileDescMetricsFormatter { 16 | 17 | @Override 18 | public String format(List metricsList, long startMillis, long stopMillis) { 19 | final StringBuilder sb = SB_TL.get(); 20 | try { 21 | final long startNanos = startMillis * 1000 * 1000L; 22 | for (int i = 0, size = metricsList.size(); i < size; ++i) { 23 | appendLineProtocol(metricsList.get(i), startNanos, sb); 24 | } 25 | return sb.substring(0, Math.max(0, sb.length() - 1)); 26 | } finally { 27 | sb.setLength(0); 28 | } 29 | } 30 | 31 | private void appendLineProtocol(JvmFileDescriptorMetrics metrics, long startNanos, StringBuilder sb) { 32 | sb.append("jvm_file_descriptor_metrics") 33 | .append(",AppName=").append(ProfilingConfig.basicConfig().appName()) 34 | .append(",host=").append(processTagOrField(getLocalhostName())) 35 | .append(" OpenCount=").append(metrics.getOpenCount()).append('i') 36 | .append(",OpenPercent=").append(metrics.getOpenPercent()) 37 | .append(' ').append(startNanos).append('\n'); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/standard/StdJvmGcMetricsV3Formatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter.standard; 2 | 3 | import cn.myperf4j.base.metric.JvmGcMetricsV3; 4 | import cn.myperf4j.base.metric.formatter.JvmGcMetricsV3Formatter; 5 | import cn.myperf4j.base.util.text.DateFormatUtils; 6 | 7 | import java.util.List; 8 | 9 | import static cn.myperf4j.base.util.SysProperties.LINE_SEPARATOR; 10 | 11 | /** 12 | * Created by LinShunkang on 2024/02/08 13 | */ 14 | public class StdJvmGcMetricsV3Formatter implements JvmGcMetricsV3Formatter { 15 | 16 | private static final String TITLE_FORMAT = "%-20s%15s%15s%15s%n"; 17 | 18 | private static final String DATA_FORMAT = "%-20s%15d%15d%15.2f%n"; 19 | 20 | @Override 21 | public String format(List metricsList, long startMillis, long stopMillis) { 22 | final StringBuilder sb = new StringBuilder((metricsList.size() + 2) * (9 * 3 + 64)); 23 | sb.append("MyPerf4J JVM GC MetricsV3 [").append(DateFormatUtils.format(startMillis)).append(", ") 24 | .append(DateFormatUtils.format(stopMillis)).append(']').append(LINE_SEPARATOR); 25 | sb.append(String.format(TITLE_FORMAT, "GcName", "GcCount", "GcTime", "AvgGcTime")); 26 | if (metricsList.isEmpty()) { 27 | return sb.toString(); 28 | } 29 | 30 | for (int i = 0; i < metricsList.size(); ++i) { 31 | final JvmGcMetricsV3 metrics = metricsList.get(i); 32 | final String gcName = metrics.getGcName(); 33 | final long gcCount = metrics.getGcCount(); 34 | final long gcTime = metrics.getGcTime(); 35 | final double avgGcTime = metrics.getAvgGcTime(); 36 | sb.append(String.format(DATA_FORMAT, gcName, gcCount, gcTime, avgGcTime)); 37 | } 38 | return sb.toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/dynamic/DynamicMethodVisitor.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J.dynamic; 2 | 3 | import cn.myperf4j.asm.aop.ProfilingAspect; 4 | import org.objectweb.asm.MethodVisitor; 5 | import org.objectweb.asm.Opcodes; 6 | import org.objectweb.asm.Type; 7 | import org.objectweb.asm.commons.AdviceAdapter; 8 | 9 | /** 10 | * Created by LinShunkang on 2018/4/15 11 | */ 12 | public class DynamicMethodVisitor extends AdviceAdapter { 13 | 14 | private static final String PROFILING_ASPECT_INNER_NAME = Type.getInternalName(ProfilingAspect.class); 15 | 16 | private int startTimeIdentifier; 17 | 18 | private String name; 19 | 20 | private String desc; 21 | 22 | public DynamicMethodVisitor(int access, 23 | String name, 24 | String desc, 25 | MethodVisitor mv) { 26 | super(ASM5, mv, access, name, desc); 27 | this.name = name; 28 | this.desc = desc; 29 | } 30 | 31 | @Override 32 | protected void onMethodEnter() { 33 | if (profiling()) { 34 | mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "nanoTime", "()J", false); 35 | startTimeIdentifier = newLocal(Type.LONG_TYPE); 36 | mv.visitVarInsn(LSTORE, startTimeIdentifier); 37 | } 38 | } 39 | 40 | @Override 41 | protected void onMethodExit(int opcode) { 42 | if (profiling() && (IRETURN <= opcode && opcode <= RETURN || opcode == ATHROW)) { 43 | mv.visitVarInsn(LLOAD, startTimeIdentifier); 44 | mv.visitVarInsn(Opcodes.ALOAD, 2); 45 | mv.visitMethodInsn(INVOKESTATIC, PROFILING_ASPECT_INNER_NAME, 46 | "profiling", "(JLjava/lang/reflect/Method;)V", false); 47 | } 48 | } 49 | 50 | private boolean profiling() { 51 | return true; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/influxdb/InfluxJvmClassMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter.influxdb; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.metric.JvmClassMetrics; 5 | import cn.myperf4j.base.metric.formatter.JvmClassMetricsFormatter; 6 | 7 | import java.util.List; 8 | 9 | import static cn.myperf4j.base.util.net.IpUtils.getLocalhostName; 10 | import static cn.myperf4j.base.util.LineProtocolUtils.processTagOrField; 11 | 12 | /** 13 | * Created by LinShunkang on 2020/5/17 14 | */ 15 | public final class InfluxJvmClassMetricsFormatter implements JvmClassMetricsFormatter { 16 | 17 | @Override 18 | public String format(List metricsList, long startMillis, long stopMillis) { 19 | final StringBuilder sb = SB_TL.get(); 20 | try { 21 | final long startNanos = startMillis * 1000 * 1000L; 22 | for (int i = 0, size = metricsList.size(); i < size; ++i) { 23 | appendLineProtocol(metricsList.get(i), startNanos, sb); 24 | } 25 | return sb.substring(0, Math.max(0, sb.length() - 1)); 26 | } finally { 27 | sb.setLength(0); 28 | } 29 | } 30 | 31 | private void appendLineProtocol(JvmClassMetrics metrics, long startNanos, StringBuilder sb) { 32 | sb.append("jvm_class_metrics") 33 | .append(",AppName=").append(ProfilingConfig.basicConfig().appName()) 34 | .append(",host=").append(processTagOrField(getLocalhostName())) 35 | .append(" Total=").append(metrics.getTotal()).append('i') 36 | .append(",Loaded=").append(metrics.getLoaded()).append('i') 37 | .append(",Unloaded=").append(metrics.getUnloaded()).append('i') 38 | .append(' ').append(startNanos).append('\n'); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/standard/StdJvmFileDescMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter.standard; 2 | 3 | import cn.myperf4j.base.metric.JvmFileDescriptorMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmFileDescMetricsFormatter; 5 | import cn.myperf4j.base.util.text.DateFormatUtils; 6 | 7 | import java.util.List; 8 | 9 | import static cn.myperf4j.base.util.SysProperties.LINE_SEPARATOR; 10 | 11 | /** 12 | * Created by LinShunkang on 2018/8/21 13 | */ 14 | public final class StdJvmFileDescMetricsFormatter implements JvmFileDescMetricsFormatter { 15 | 16 | private static final String TITLE_FORMAT = "%-14s%14s%14s%n"; 17 | 18 | private static final String DATA_FORMAT = "%-14d%14.2f%14s%n"; 19 | 20 | @Override 21 | public String format(List metricsList, long startMillis, long stopMillis) { 22 | final StringBuilder sb = new StringBuilder((metricsList.size() + 2) * (12 * 2 + 24)); 23 | sb.append("MyPerf4J JVM FileDescriptor Metrics [").append(DateFormatUtils.format(startMillis)).append(", ") 24 | .append(DateFormatUtils.format(stopMillis)).append(']').append(LINE_SEPARATOR); 25 | sb.append(String.format(TITLE_FORMAT, "OpenCount", "OpenPercent", "MaxPercent")); 26 | if (metricsList.isEmpty()) { 27 | return sb.toString(); 28 | } 29 | 30 | for (int i = 0; i < metricsList.size(); ++i) { 31 | final JvmFileDescriptorMetrics metrics = metricsList.get(i); 32 | sb.append( 33 | String.format(DATA_FORMAT, 34 | metrics.getOpenCount(), 35 | metrics.getOpenPercent(), 36 | metrics.getMaxCount() 37 | ) 38 | ); 39 | } 40 | return sb.toString(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /MyPerf4J-Core/src/main/java/cn/myperf4j/core/MethodMetricsInfo.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.core; 2 | 3 | /** 4 | * Created by LinShunkang on 2019/07/23 5 | */ 6 | public class MethodMetricsInfo { 7 | 8 | private long tp95Sum; 9 | 10 | private long tp99Sum; 11 | 12 | private long tp999Sum; 13 | 14 | private long tp9999Sum; 15 | 16 | private int count; 17 | 18 | public MethodMetricsInfo(int tp95, int tp99, int tp999, int tp9999) { 19 | this.tp95Sum = tp95; 20 | this.tp99Sum = tp99; 21 | this.tp999Sum = tp999; 22 | this.tp9999Sum = tp9999; 23 | this.count = isValid(tp95, tp99, tp999, tp9999) ? 1 : 0; 24 | } 25 | 26 | private boolean isValid(int tp95, int tp99, int tp999, int tp9999) { 27 | return tp95 >= 0 && tp99 >= 0 && tp999 >= 0 && tp9999 >= 0; 28 | } 29 | 30 | public void add(int tp95, int tp99, int tp999, int tp9999) { 31 | if (isValid(tp95, tp99, tp999, tp9999)) { 32 | tp95Sum += tp95; 33 | tp99Sum += tp99; 34 | tp999Sum += tp999; 35 | tp9999Sum += tp9999; 36 | count++; 37 | } 38 | } 39 | 40 | public long getTp95Sum() { 41 | return tp95Sum; 42 | } 43 | 44 | public long getTp99Sum() { 45 | return tp99Sum; 46 | } 47 | 48 | public long getTp999Sum() { 49 | return tp999Sum; 50 | } 51 | 52 | public long getTp9999Sum() { 53 | return tp9999Sum; 54 | } 55 | 56 | public int getCount() { 57 | return count; 58 | } 59 | 60 | @Override 61 | public String toString() { 62 | return "MethodMetricsInfo{" + 63 | "tp95Sum=" + tp95Sum + 64 | ", tp99Sum=" + tp99Sum + 65 | ", tp999Sum=" + tp999Sum + 66 | ", tp9999Sum=" + tp9999Sum + 67 | ", count=" + count + 68 | '}'; 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/log/AutoRollingLogger.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.log; 2 | 3 | import cn.myperf4j.base.constant.PropertyValues.Metrics; 4 | import cn.myperf4j.base.file.AutoRollingFileWriter; 5 | import cn.myperf4j.base.file.DailyRollingFileWriter; 6 | import cn.myperf4j.base.file.HourlyRollingFileWriter; 7 | import cn.myperf4j.base.file.MinutelyRollingFileWriter; 8 | 9 | import static cn.myperf4j.base.util.SysProperties.LINE_SEPARATOR; 10 | 11 | public class AutoRollingLogger implements ILogger { 12 | 13 | private final AutoRollingFileWriter writer; 14 | 15 | AutoRollingLogger(String logFile, String rollingTimeUnit, int reserveFileCount) { 16 | switch (rollingTimeUnit.toUpperCase()) { 17 | case Metrics.LOG_ROLLING_HOURLY: 18 | this.writer = new HourlyRollingFileWriter(logFile, reserveFileCount); 19 | break; 20 | case Metrics.LOG_ROLLING_MINUTELY: 21 | this.writer = new MinutelyRollingFileWriter(logFile, reserveFileCount); 22 | break; 23 | default: 24 | this.writer = new DailyRollingFileWriter(logFile, reserveFileCount); 25 | } 26 | } 27 | 28 | @Override 29 | public void log(String msg) { 30 | writer.write(msg + LINE_SEPARATOR); 31 | } 32 | 33 | @Override 34 | public void logAndFlush(String msg) { 35 | writer.writeAndFlush(msg + LINE_SEPARATOR); 36 | } 37 | 38 | @Override 39 | public void flushLog() { 40 | writer.flush(); 41 | } 42 | 43 | @Override 44 | public void preCloseLog() { 45 | writer.preCloseFile(); 46 | } 47 | 48 | @Override 49 | public void closeLog() { 50 | writer.closeFile(true); 51 | } 52 | 53 | @Override 54 | protected void finalize() throws Throwable { 55 | super.finalize(); 56 | writer.closeFile(true); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/standard/StdJvmBufferPoolMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter.standard; 2 | 3 | import cn.myperf4j.base.metric.JvmBufferPoolMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmBufferPoolMetricsFormatter; 5 | import cn.myperf4j.base.util.text.DateFormatUtils; 6 | 7 | import java.util.List; 8 | 9 | import static cn.myperf4j.base.util.SysProperties.LINE_SEPARATOR; 10 | 11 | /** 12 | * Created by LinShunkang on 2018/8/21 13 | */ 14 | public class StdJvmBufferPoolMetricsFormatter implements JvmBufferPoolMetricsFormatter { 15 | 16 | private static final String TITLE_FORMAT = "%-32s%19s%19s%19s%n"; 17 | 18 | private static final String DATA_FORMAT = "%-32s%19d%19d%19d%n"; 19 | 20 | @Override 21 | public String format(List metricsList, long startMillis, long stopMillis) { 22 | final StringBuilder sb = new StringBuilder((metricsList.size() + 2) * (3 * 19 + 64)); 23 | sb.append("MyPerf4J JVM BufferPool Metrics [").append(DateFormatUtils.format(startMillis)).append(", ") 24 | .append(DateFormatUtils.format(stopMillis)).append(']').append(LINE_SEPARATOR); 25 | sb.append(String.format(TITLE_FORMAT, "Name", "Count", "MemoryUsed", "MemoryCapacity")); 26 | if (metricsList.isEmpty()) { 27 | return sb.toString(); 28 | } 29 | 30 | for (int i = 0; i < metricsList.size(); ++i) { 31 | final JvmBufferPoolMetrics metrics = metricsList.get(i); 32 | sb.append( 33 | String.format(DATA_FORMAT, 34 | metrics.getName(), 35 | metrics.getCount(), 36 | metrics.getMemoryUsed(), 37 | metrics.getMemoryCapacity() 38 | ) 39 | ); 40 | } 41 | return sb.toString(); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/http/HttpStatusClass.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.http; 2 | 3 | /** 4 | * Created by LinShunkang on 2020/05/16 5 | */ 6 | public enum HttpStatusClass { 7 | 8 | INFORMATIONAL(100, 200, "Informational"), 9 | 10 | SUCCESS(200, 300, "Success"), 11 | 12 | REDIRECTION(300, 400, "Redirection"), 13 | 14 | CLIENT_ERROR(400, 500, "Client Error"), 15 | 16 | SERVER_ERROR(500, 600, "Server Error"), 17 | 18 | UNKNOWN(0, 0, "Unknown Status") { 19 | @Override 20 | public boolean contains(int code) { 21 | return code < 100 || code >= 600; 22 | } 23 | }; 24 | 25 | private final int min; 26 | 27 | private final int max; 28 | 29 | private final String defaultPhrase; 30 | 31 | HttpStatusClass(int min, int max, String defaultPhrase) { 32 | this.min = min; 33 | this.max = max; 34 | this.defaultPhrase = defaultPhrase; 35 | } 36 | 37 | public int getMin() { 38 | return min; 39 | } 40 | 41 | public int getMax() { 42 | return max; 43 | } 44 | 45 | public String getDefaultPhrase() { 46 | return defaultPhrase; 47 | } 48 | 49 | public boolean contains(int code) { 50 | return code >= min && code < max; 51 | } 52 | 53 | /** 54 | * Returns the class of the specified HTTP status code. 55 | */ 56 | public static HttpStatusClass valueOf(int code) { 57 | if (INFORMATIONAL.contains(code)) { 58 | return INFORMATIONAL; 59 | } 60 | if (SUCCESS.contains(code)) { 61 | return SUCCESS; 62 | } 63 | if (REDIRECTION.contains(code)) { 64 | return REDIRECTION; 65 | } 66 | if (CLIENT_ERROR.contains(code)) { 67 | return CLIENT_ERROR; 68 | } 69 | if (SERVER_ERROR.contains(code)) { 70 | return SERVER_ERROR; 71 | } 72 | return UNKNOWN; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/influxdb/InfluxJvmBufferPoolMetricsFormatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter.influxdb; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.metric.JvmBufferPoolMetrics; 5 | import cn.myperf4j.base.metric.formatter.JvmBufferPoolMetricsFormatter; 6 | 7 | import java.util.List; 8 | 9 | import static cn.myperf4j.base.util.net.IpUtils.getLocalhostName; 10 | import static cn.myperf4j.base.util.LineProtocolUtils.processTagOrField; 11 | 12 | /** 13 | * Created by LinShunkang on 2020/5/17 14 | */ 15 | public class InfluxJvmBufferPoolMetricsFormatter implements JvmBufferPoolMetricsFormatter { 16 | 17 | @Override 18 | public String format(List metricsList, long startMillis, long stopMillis) { 19 | final StringBuilder sb = SB_TL.get(); 20 | try { 21 | final long startNanos = startMillis * 1000 * 1000L; 22 | for (int i = 0, size = metricsList.size(); i < size; ++i) { 23 | appendLineProtocol(metricsList.get(i), startNanos, sb); 24 | } 25 | return sb.substring(0, Math.max(0, sb.length() - 1)); 26 | } finally { 27 | sb.setLength(0); 28 | } 29 | } 30 | 31 | private void appendLineProtocol(JvmBufferPoolMetrics metrics, long startNanos, StringBuilder sb) { 32 | sb.append("jvm_buffer_pool_metrics_v2") 33 | .append(",AppName=").append(ProfilingConfig.basicConfig().appName()) 34 | .append(",PoolName=").append(processTagOrField(metrics.getName())) 35 | .append(",host=").append(processTagOrField(getLocalhostName())) 36 | .append(" Count=").append(metrics.getCount()).append('i') 37 | .append(",MemoryUsed=").append(metrics.getMemoryUsed()).append('i') 38 | .append(",MemoryCapacity=").append(metrics.getMemoryCapacity()).append('i') 39 | .append(' ').append(startNanos).append('\n'); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/concurrent/ThreadUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.concurrent; 2 | 3 | import java.util.concurrent.ThreadFactory; 4 | import java.util.concurrent.TimeUnit; 5 | import java.util.concurrent.atomic.AtomicInteger; 6 | 7 | /** 8 | * Created by LinShunkang on 2018/3/16 9 | */ 10 | public final class ThreadUtils { 11 | 12 | private ThreadUtils() { 13 | //empty 14 | } 15 | 16 | public static ThreadFactory newThreadFactory(final String prefix) { 17 | return new ThreadFactory() { 18 | final AtomicInteger threadId = new AtomicInteger(0); 19 | 20 | @Override 21 | public Thread newThread(Runnable r) { 22 | return new Thread(r, prefix + threadId.getAndIncrement()); 23 | } 24 | }; 25 | } 26 | 27 | public static void sleepQuietly(long time, TimeUnit timeUnit) { 28 | try { 29 | timeUnit.sleep(time); 30 | } catch (Exception e) { 31 | //empty 32 | } 33 | } 34 | 35 | public static ThreadGroup getSystemThreadGroup() { 36 | ThreadGroup threadGroup = Thread.currentThread().getThreadGroup(); 37 | while (threadGroup.getParent() != null) { 38 | threadGroup = threadGroup.getParent(); 39 | } 40 | return threadGroup; 41 | } 42 | 43 | public static Thread[] findThreads(final ThreadGroup group, final boolean recurse) { 44 | if (group == null) { 45 | throw new IllegalArgumentException("The group must not be null"); 46 | } 47 | 48 | int count = group.activeCount(); 49 | Thread[] threads; 50 | do { 51 | threads = new Thread[count + (count / 2) + 1]; //slightly grow the array size 52 | count = group.enumerate(threads, recurse); 53 | //return value of enumerate() must be strictly less than the array size according to javadoc 54 | } while (count >= threads.length); 55 | return threads; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/ExpUtilsTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import cn.myperf4j.base.util.PkgExpUtils; 4 | import cn.myperf4j.base.util.StrMatchUtils; 5 | import org.junit.Assert; 6 | import org.junit.Test; 7 | 8 | import java.util.Arrays; 9 | 10 | /** 11 | * Created by LinShunkang on 2019-01-01 12 | */ 13 | public class ExpUtilsTest { 14 | 15 | @Test 16 | public void testParse() { 17 | Assert.assertTrue(PkgExpUtils.parse("cn.myperf4j.util.[Logger,DateUtils]") 18 | .containsAll(Arrays.asList("cn.myperf4j.util.Logger;cn.myperf4j.util.DateUtils".split(";")))); 19 | 20 | Assert.assertTrue(PkgExpUtils.parse("cn.myperf4j.util.[Logger,DateUtils].abc") 21 | .containsAll(Arrays.asList("cn.myperf4j.util.Logger.abc;cn.myperf4j.util.DateUtils.abc".split(";")))); 22 | 23 | Assert.assertTrue(PkgExpUtils.parse("cn.myperf4j.util.[Logger,DateUtils].abc.[e,f]") 24 | .containsAll(Arrays.asList(("cn.myperf4j.util.Logger.abc.e;cn.myperf4j.util.DateUtils.abc.f;" + 25 | "cn.myperf4j.util.Logger.abc.e;cn.myperf4j.util.DateUtils.abc.f").split(";")))); 26 | } 27 | 28 | @Test 29 | public void testMatch() { 30 | Assert.assertTrue(StrMatchUtils.isMatch("abc", "abc")); 31 | Assert.assertTrue(StrMatchUtils.isMatch("abc", "a*")); 32 | Assert.assertTrue(StrMatchUtils.isMatch("abc", "*abc")); 33 | Assert.assertTrue(StrMatchUtils.isMatch("abc", "a*bc")); 34 | Assert.assertTrue(StrMatchUtils.isMatch("abc", "a*c")); 35 | Assert.assertTrue(StrMatchUtils.isMatch("abc", "ab*")); 36 | Assert.assertTrue(StrMatchUtils.isMatch("abc", "ab**")); 37 | Assert.assertTrue(StrMatchUtils.isMatch("abcdef", "ab****c*")); 38 | Assert.assertTrue(StrMatchUtils.isMatch("abcdef", "ab*c*f")); 39 | 40 | Assert.assertFalse(StrMatchUtils.isMatch("abcdef", "ab*c*eg")); 41 | Assert.assertFalse(StrMatchUtils.isMatch("abcdef", "abcdefg")); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/PkgExpUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util; 2 | 3 | import cn.myperf4j.base.util.collections.SetUtils; 4 | 5 | import java.util.List; 6 | import java.util.Set; 7 | 8 | import static cn.myperf4j.base.constant.PropertyValues.Separator.ARR_ELE; 9 | 10 | /** 11 | * Created by LinShunkang on 2018-12-31 12 | * 该类用于解析 MyPerf4J 自定义的包路径表达式解析 13 | * 规则: 14 | * 1、可以用 [] 代表集合的概念,集合 [e1,e2,e3] 中包含多个元素,每个元素由英文逗号分隔,元素可以是 package 和 class。但 [] 不可嵌套出现。 15 | * 例如: 16 | * a、cn.myperf4j.util.[Logger,DateUtils] -> cn.myperf4j.util.Logger;cn.myperf4j.util.DateUtils 17 | * b、cn.myperf4j.metric.[formatter,processor] -> cn.myperf4j.metric.formatter;cn.myperf4j.metric.processor 18 | * c、cn.myperf4j.metric.[formatter,MethodMetrics] -> cn.myperf4j.metric.formatter;cn.myperf4j.metric.MethodMetrics 19 | *

20 | * 2、可以用 * 代表贪心匹配,可以匹配多个字符。 21 | */ 22 | public final class PkgExpUtils { 23 | 24 | private PkgExpUtils() { 25 | //empty 26 | } 27 | 28 | public static Set parse(String expStr) { 29 | final int leftIdx = expStr.indexOf('['); 30 | if (leftIdx < 0) { 31 | return SetUtils.of(expStr); 32 | } 33 | 34 | final int rightIdx = expStr.indexOf(']', leftIdx); 35 | if (rightIdx < 0) { 36 | throw new IllegalArgumentException("PkgExpUtils.parse(\"" + expStr + "\"): '[' always paired with ']'"); 37 | } 38 | 39 | final String prefixStr = expStr.substring(0, leftIdx); 40 | final String suffixStr = rightIdx + 1 < expStr.length() ? expStr.substring(rightIdx + 1) : ""; 41 | final String elementsStr = expStr.substring(leftIdx + 1, rightIdx); 42 | final List elements = StrUtils.splitAsList(elementsStr, ARR_ELE); 43 | final Set result = SetUtils.createHashSet(elements.size() << 2); 44 | for (String element : elements) { 45 | result.addAll(parse(prefixStr.concat(element).concat(suffixStr))); 46 | } 47 | return result; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/DateUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util; 2 | 3 | import java.util.Calendar; 4 | import java.util.Date; 5 | 6 | import static java.util.Calendar.DAY_OF_MONTH; 7 | import static java.util.Calendar.HOUR_OF_DAY; 8 | import static java.util.Calendar.MINUTE; 9 | import static java.util.Calendar.MONTH; 10 | import static java.util.Calendar.YEAR; 11 | 12 | public final class DateUtils { 13 | 14 | private DateUtils() { 15 | //empty 16 | } 17 | 18 | public static boolean isSameDay(Date date1, Date date2) { 19 | final Calendar cal1 = Calendar.getInstance(); 20 | final Calendar cal2 = Calendar.getInstance(); 21 | cal1.setTime(date1); 22 | cal2.setTime(date2); 23 | return cal1.get(YEAR) == cal2.get(YEAR) 24 | && cal1.get(MONTH) == cal2.get(MONTH) 25 | && cal1.get(DAY_OF_MONTH) == cal2.get(DAY_OF_MONTH); 26 | } 27 | 28 | public static boolean isSameHour(Date date1, Date date2) { 29 | final Calendar cal1 = Calendar.getInstance(); 30 | final Calendar cal2 = Calendar.getInstance(); 31 | cal1.setTime(date1); 32 | cal2.setTime(date2); 33 | return cal1.get(YEAR) == cal2.get(YEAR) 34 | && cal1.get(MONTH) == cal2.get(MONTH) 35 | && cal1.get(DAY_OF_MONTH) == cal2.get(DAY_OF_MONTH) 36 | && cal1.get(HOUR_OF_DAY) == cal2.get(HOUR_OF_DAY); 37 | } 38 | 39 | public static boolean isSameMinute(Date date1, Date date2) { 40 | final Calendar cal1 = Calendar.getInstance(); 41 | final Calendar cal2 = Calendar.getInstance(); 42 | cal1.setTime(date1); 43 | cal2.setTime(date2); 44 | return cal1.get(YEAR) == cal2.get(YEAR) 45 | && cal1.get(MONTH) == cal2.get(MONTH) 46 | && cal1.get(DAY_OF_MONTH) == cal2.get(DAY_OF_MONTH) 47 | && cal1.get(HOUR_OF_DAY) == cal2.get(HOUR_OF_DAY) 48 | && cal1.get(MINUTE) == cal2.get(MINUTE); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/formatter/influxdb/InfluxJvmGcMetricsV3Formatter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.formatter.influxdb; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import cn.myperf4j.base.metric.JvmGcMetricsV3; 5 | import cn.myperf4j.base.metric.formatter.JvmGcMetricsV3Formatter; 6 | 7 | import java.util.List; 8 | 9 | import static cn.myperf4j.base.util.LineProtocolUtils.processTagOrField; 10 | import static cn.myperf4j.base.util.net.IpUtils.getLocalhostName; 11 | import static cn.myperf4j.base.util.text.NumFormatUtils.doubleFormat; 12 | 13 | /** 14 | * Created by LinShunkang on 2024/02/08 15 | */ 16 | public class InfluxJvmGcMetricsV3Formatter implements JvmGcMetricsV3Formatter { 17 | 18 | @Override 19 | public String format(List metricsList, long startMillis, long stopMillis) { 20 | final StringBuilder sb = SB_TL.get(); 21 | try { 22 | final long startNanos = startMillis * 1000 * 1000L; 23 | for (int i = 0, size = metricsList.size(); i < size; ++i) { 24 | appendLineProtocol(metricsList.get(i), startNanos, sb); 25 | } 26 | return sb.substring(0, Math.max(0, sb.length() - 1)); 27 | } finally { 28 | sb.setLength(0); 29 | } 30 | } 31 | 32 | private void appendLineProtocol(JvmGcMetricsV3 metrics, long startNanos, StringBuilder sb) { 33 | sb.append("jvm_gc_metrics_v3") 34 | .append(",AppName=").append(ProfilingConfig.basicConfig().appName()) 35 | .append(",host=").append(processTagOrField(getLocalhostName())) 36 | .append(",GcName=").append(processTagOrField(metrics.getGcName())) 37 | .append(" GcCount=").append(metrics.getGcCount()).append('i') 38 | .append(",GcTime=").append(metrics.getGcTime()).append('i') 39 | .append(",AvgGcTime=").append(doubleFormat(metrics.getAvgGcTime())) 40 | .append(' ').append(startNanos).append('\n'); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /MyPerf4J-Core/src/test/java/cn/myperf4j/core/MethodMetricsBenchmark.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.core; 2 | 3 | import cn.myperf4j.base.MethodTag; 4 | import cn.myperf4j.base.metric.MethodMetrics; 5 | import cn.myperf4j.core.recorder.Recorder; 6 | import cn.myperf4j.core.recorder.Recorders; 7 | import cn.myperf4j.core.recorder.RoughRecorder; 8 | 9 | import java.util.concurrent.atomic.AtomicReferenceArray; 10 | 11 | import static cn.myperf4j.core.MethodMetricsCalculator.calMetrics; 12 | 13 | /** 14 | * Created by LinShunkang on 2019/06/22 15 | */ 16 | public final class MethodMetricsBenchmark { 17 | 18 | private MethodMetricsBenchmark() { 19 | //empty 20 | } 21 | 22 | public static void main(String[] args) { 23 | Recorders recorders = new Recorders(new AtomicReferenceArray(10)); 24 | MethodTagMaintainer methodTagMaintainer = MethodTagMaintainer.getInstance(); 25 | 26 | int methodId1 = methodTagMaintainer.addMethodTag(MethodTag.getGeneralInstance("", "Test", "Api", "m1", "")); 27 | // recorders.setRecorder(methodId1, AccurateRecorder.getInstance(0, 9000, 50)); 28 | recorders.setRecorder(methodId1, RoughRecorder.getInstance(0, 10000)); 29 | 30 | Recorder recorder = recorders.getRecorder(methodId1); 31 | recorders.setStartTime(System.currentTimeMillis()); 32 | long start = System.nanoTime(); 33 | for (long i = 0; i < 1000; ++i) { 34 | recorder.recordTime(start, start + i * 1000 * 1000); 35 | } 36 | recorders.setStopTime(System.currentTimeMillis()); 37 | 38 | MethodTag methodTag = methodTagMaintainer.getMethodTag(methodId1); 39 | 40 | long tmp = 0L; 41 | start = System.nanoTime(); 42 | for (int i = 0; i < 1000000; ++i) { 43 | MethodMetrics metrics = calMetrics(recorder, methodTag, recorders.getStartTime(), recorders.getStopTime()); 44 | tmp += metrics.getRPS(); 45 | } 46 | System.out.println("tmp=" + tmp + ", totalCost=" + (System.nanoTime() - start) / 1000_000 + "ms"); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /MyPerf4J-Benchmark/src/main/java/cn/myperf4j/bench/RecorderBenchmark.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.bench; 2 | 3 | import cn.myperf4j.core.recorder.AccurateRecorder; 4 | import cn.myperf4j.core.recorder.Recorder; 5 | import cn.myperf4j.core.recorder.RoughRecorder; 6 | import org.openjdk.jmh.annotations.Benchmark; 7 | import org.openjdk.jmh.annotations.BenchmarkMode; 8 | import org.openjdk.jmh.annotations.Mode; 9 | import org.openjdk.jmh.annotations.OutputTimeUnit; 10 | import org.openjdk.jmh.annotations.Scope; 11 | import org.openjdk.jmh.annotations.Setup; 12 | import org.openjdk.jmh.annotations.State; 13 | import org.openjdk.jmh.runner.Runner; 14 | import org.openjdk.jmh.runner.RunnerException; 15 | import org.openjdk.jmh.runner.options.Options; 16 | import org.openjdk.jmh.runner.options.OptionsBuilder; 17 | 18 | import java.util.concurrent.TimeUnit; 19 | 20 | /** 21 | * Created by LinShunkang on 2019/10/19 22 | */ 23 | @BenchmarkMode({Mode.Throughput}) 24 | @OutputTimeUnit(TimeUnit.MICROSECONDS) 25 | @State(Scope.Thread) 26 | public class RecorderBenchmark { 27 | 28 | private Recorder roughRecorder; 29 | 30 | private Recorder accurateRecorder; 31 | 32 | @Setup 33 | public void setup() { 34 | roughRecorder = RoughRecorder.getInstance(0, 1024); 35 | accurateRecorder = AccurateRecorder.getInstance(1, 1024, 64); 36 | } 37 | 38 | @Benchmark 39 | public void roughRecorderBench() { 40 | roughRecorder.recordTime(0L, 1000000000L); 41 | } 42 | 43 | @Benchmark 44 | public void accurateRecorderBench() { 45 | accurateRecorder.recordTime(0L, 1000000000L); 46 | } 47 | 48 | public static void main(String[] args) throws RunnerException { 49 | // 使用一个单独进程执行测试,执行3遍warmup,然后执行5遍测试 50 | Options opt = new OptionsBuilder() 51 | .include(RecorderBenchmark.class.getSimpleName()) 52 | .forks(2) 53 | .threads(8) 54 | .warmupIterations(3) 55 | .measurementIterations(5) 56 | .build(); 57 | new Runner(opt).run(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /MyPerf4J-Core/src/test/java/cn/myperf4j/core/BaseTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.core; 2 | 3 | import cn.myperf4j.base.config.MyProperties; 4 | import cn.myperf4j.base.constant.PropertyKeys; 5 | import cn.myperf4j.base.constant.PropertyValues; 6 | import cn.myperf4j.base.constant.PropertyValues.Metrics; 7 | import cn.myperf4j.base.file.AutoRollingFileWriter; 8 | import cn.myperf4j.base.file.MinutelyRollingFileWriter; 9 | import cn.myperf4j.base.util.io.IOUtils; 10 | import cn.myperf4j.base.util.Logger; 11 | import org.junit.BeforeClass; 12 | 13 | import java.io.File; 14 | import java.io.FileInputStream; 15 | import java.io.IOException; 16 | import java.io.InputStream; 17 | import java.util.Properties; 18 | 19 | /** 20 | * Created by LinShunkang on 2020/05/31 21 | */ 22 | public abstract class BaseTest { 23 | 24 | @BeforeClass 25 | public static void doInit() { 26 | String propertiesFile = "/tmp/MyPerf4J.properties"; 27 | System.setProperty(PropertyKeys.PRO_FILE_NAME, propertiesFile); 28 | AutoRollingFileWriter writer = new MinutelyRollingFileWriter(propertiesFile, 1); 29 | writer.write("app_name=Test\n"); 30 | writer.write("metrics.exporter=" + Metrics.EXPORTER_LOG_INFLUX_DB + "\n"); 31 | writer.write("filter.packages.include=MyPerf4J\n"); 32 | writer.write("metrics.time_slice.method=1000\n"); 33 | writer.write("metrics.time_slice.jvm=1000\n"); 34 | writer.closeFile(true); 35 | 36 | new File(propertiesFile).deleteOnExit(); 37 | } 38 | 39 | protected static void initProperties() { 40 | InputStream in = null; 41 | try { 42 | in = new FileInputStream(System.getProperty(PropertyKeys.PRO_FILE_NAME, PropertyValues.DEFAULT_PRO_FILE)); 43 | 44 | Properties properties = new Properties(); 45 | properties.load(in); 46 | MyProperties.initial(properties); 47 | } catch (IOException e) { 48 | Logger.error("BaseTest.initProperties()", e); 49 | } finally { 50 | IOUtils.closeQuietly(in); 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/io/InputStreamUtils.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.io; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | 7 | /** 8 | * Created by LinShunkang on 2020/05/16 9 | */ 10 | public final class InputStreamUtils { 11 | 12 | private static final ThreadLocal OP_TL = new ThreadLocal() { 13 | @Override 14 | protected ByteArrayOutputStream initialValue() { 15 | return new ByteArrayOutputStream(4096); 16 | } 17 | }; 18 | 19 | private static final ThreadLocal BYTES_TL = new ThreadLocal() { 20 | @Override 21 | protected byte[] initialValue() { 22 | return new byte[1024]; 23 | } 24 | }; 25 | 26 | private InputStreamUtils() { 27 | //empty 28 | } 29 | 30 | public static String toString(InputStream inputStream) throws IOException { 31 | if (inputStream == null) { 32 | return null; 33 | } 34 | 35 | final ByteArrayOutputStream result = OP_TL.get(); 36 | final byte[] buffer = BYTES_TL.get(); 37 | try { 38 | int length; 39 | while ((length = inputStream.read(buffer)) != -1) { 40 | result.write(buffer, 0, length); 41 | } 42 | return result.toString("UTF-8"); 43 | } finally { 44 | result.reset(); 45 | } 46 | } 47 | 48 | public static byte[] toBytes(InputStream inputStream) throws IOException { 49 | if (inputStream == null) { 50 | return null; 51 | } 52 | 53 | final ByteArrayOutputStream result = OP_TL.get(); 54 | final byte[] buffer = BYTES_TL.get(); 55 | try { 56 | int length; 57 | while ((length = inputStream.read(buffer)) != -1) { 58 | result.write(buffer, 0, length); 59 | } 60 | return result.toByteArray(); 61 | } finally { 62 | result.reset(); 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/test/java/cn/myperf4j/base/test/ILoggerTest.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.test; 2 | 3 | import cn.myperf4j.base.config.MetricsConfig; 4 | import cn.myperf4j.base.config.ProfilingConfig; 5 | import cn.myperf4j.base.constant.PropertyValues.Metrics; 6 | import cn.myperf4j.base.log.ILogger; 7 | import cn.myperf4j.base.log.LoggerFactory; 8 | import org.junit.Assert; 9 | import org.junit.Test; 10 | 11 | public class ILoggerTest { 12 | 13 | @Test 14 | public void test() { 15 | ProfilingConfig.metricsConfig(MetricsConfig.loadMetricsConfig()); 16 | ProfilingConfig.metricsConfig().logRollingTimeUnit(Metrics.LOG_ROLLING_MINUTELY); 17 | ProfilingConfig.metricsConfig().logReserveCount(Metrics.DEFAULT_LOG_RESERVE_COUNT); 18 | 19 | test(LoggerFactory.getLogger("/tmp/testLogger.log")); 20 | test(LoggerFactory.getLogger(Metrics.NULL_FILE)); 21 | } 22 | 23 | private void test(ILogger logger) { 24 | logger.log("111111111"); 25 | logger.log("222222222"); 26 | logger.log("333333333"); 27 | logger.log("444444444"); 28 | logger.flushLog(); 29 | 30 | logger.logAndFlush("555555"); 31 | 32 | logger.preCloseLog(); 33 | logger.closeLog(); 34 | } 35 | 36 | public static void main(String[] args) { 37 | for (int i = 0; i < 100; ++i) { 38 | final String file = "/tmp/testLogger.log"; 39 | new Thread(new Runnable() { 40 | @Override 41 | public void run() { 42 | writeFile(file); 43 | } 44 | }).start(); 45 | } 46 | } 47 | 48 | private static void writeFile(String file) { 49 | ILogger logger = LoggerFactory.getLogger(file); 50 | Assert.assertNotNull(logger); 51 | 52 | logger.log("111111111"); 53 | logger.log("222222222"); 54 | logger.log("333333333"); 55 | logger.log("444444444"); 56 | 57 | for (int i = 0; i < 1000000; ++i) { 58 | logger.log(Thread.currentThread().getName() + ": " + i); 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/config/ProfilingConfig.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.config; 2 | 3 | /** 4 | * Created by LinShunkang on 2018/5/12 5 | */ 6 | public final class ProfilingConfig { 7 | 8 | private static BasicConfig BASIC_CONFIG; 9 | 10 | private static HttpServerConfig HTTP_SERVER_CONFIG; 11 | 12 | private static MetricsConfig METRICS_CONFIG; 13 | 14 | private static FilterConfig FILTER_CONFIG; 15 | 16 | private static InfluxDbConfig INFLUX_DB_CONFIG; 17 | 18 | private static RecorderConfig RECORDER_CONFIG; 19 | 20 | private ProfilingConfig() { 21 | //empty 22 | } 23 | 24 | public static BasicConfig basicConfig() { 25 | return BASIC_CONFIG; 26 | } 27 | 28 | public static void basicConfig(BasicConfig basicConfig) { 29 | BASIC_CONFIG = basicConfig; 30 | } 31 | 32 | public static HttpServerConfig httpServerConfig() { 33 | return HTTP_SERVER_CONFIG; 34 | } 35 | 36 | public static void httpServerConfig(HttpServerConfig httpServerConfig) { 37 | HTTP_SERVER_CONFIG = httpServerConfig; 38 | } 39 | 40 | public static InfluxDbConfig influxDBConfig() { 41 | return INFLUX_DB_CONFIG; 42 | } 43 | 44 | public static void influxDBConfig(InfluxDbConfig influxDBConfig) { 45 | ProfilingConfig.INFLUX_DB_CONFIG = influxDBConfig; 46 | } 47 | 48 | public static MetricsConfig metricsConfig() { 49 | return METRICS_CONFIG; 50 | } 51 | 52 | public static void metricsConfig(MetricsConfig metricsConfig) { 53 | ProfilingConfig.METRICS_CONFIG = metricsConfig; 54 | } 55 | 56 | public static FilterConfig filterConfig() { 57 | return FILTER_CONFIG; 58 | } 59 | 60 | public static void filterConfig(FilterConfig filterConfig) { 61 | ProfilingConfig.FILTER_CONFIG = filterConfig; 62 | } 63 | 64 | public static RecorderConfig recorderConfig() { 65 | return RECORDER_CONFIG; 66 | } 67 | 68 | public static void recorderConfig(RecorderConfig recorderConfig) { 69 | RECORDER_CONFIG = recorderConfig; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/dynamic/DynamicClassAdapter.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J.dynamic; 2 | 3 | import cn.myperf4j.base.config.ProfilingConfig; 4 | import org.objectweb.asm.ClassVisitor; 5 | import org.objectweb.asm.MethodVisitor; 6 | 7 | import static org.objectweb.asm.Opcodes.*; 8 | 9 | /** 10 | * Created by LinShunkang on 2018/4/15 11 | */ 12 | public class DynamicClassAdapter extends ClassVisitor { 13 | 14 | public DynamicClassAdapter(final ClassVisitor cv) { 15 | super(ASM5, cv); 16 | } 17 | 18 | @Override 19 | public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { 20 | super.visit(version, access, name, signature, superName, interfaces); 21 | } 22 | 23 | @Override 24 | public MethodVisitor visitMethod(int access, 25 | String name, 26 | String desc, 27 | String signature, 28 | String[] exceptions) { 29 | if (!isNeedVisit(access, name)) { 30 | return super.visitMethod(access, name, desc, signature, exceptions); 31 | } 32 | 33 | MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions); 34 | if (mv == null) { 35 | return null; 36 | } 37 | return new DynamicMethodVisitor(access, name, desc, mv); 38 | } 39 | 40 | private boolean isNeedVisit(int access, String name) { 41 | //不对私有方法进行注入 42 | if ((access & ACC_PRIVATE) != 0 && ProfilingConfig.filterConfig().excludePrivateMethod()) { 43 | return false; 44 | } 45 | 46 | //不对抽象方法、native方法、桥接方法、合成方法进行注入 47 | if ((access & ACC_ABSTRACT) != 0 48 | || (access & ACC_NATIVE) != 0 49 | || (access & ACC_BRIDGE) != 0 50 | || (access & ACC_SYNTHETIC) != 0) { 51 | return false; 52 | } 53 | 54 | if ("".equals(name) || "".equals(name)) { 55 | return false; 56 | } 57 | 58 | return true; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/util/concurrent/IntHashCounter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.util.concurrent; 2 | 3 | import cn.myperf4j.base.buffer.LongBuf; 4 | 5 | /** 6 | * Created by LinShunkang on 2022/03/19 7 | */ 8 | public interface IntHashCounter { 9 | 10 | /** 11 | * Get the specified value of the specified key. 12 | * 13 | * @param key the key 14 | * @return the specified value of the specified key, or 0 if this map contains no mapping for the key 15 | */ 16 | int get(int key); 17 | 18 | /** 19 | * Atomically increments the specified value of the specified key by one. 20 | * 21 | * @param key the key 22 | * @return the previous value. Negative return indicates that update failure. 23 | */ 24 | int getAndIncrement(int key); 25 | 26 | /** 27 | * Atomically adds the given value of the specified key to the specified value. 28 | * 29 | * @param key the key 30 | * @return the previous value. Negative return indicates that update failure. 31 | */ 32 | int getAndAdd(int key, int delta); 33 | 34 | /** 35 | * Atomically increments the specified value of the specified key by one. 36 | * 37 | * @param key the key 38 | * @return the updated value 39 | */ 40 | int incrementAndGet(int key); 41 | 42 | /** 43 | * Atomically adds the given value of the specified key to the specified value. 44 | * 45 | * @param key the key 46 | * @return the updated value. Non-positive number return indicates that update failure. 47 | */ 48 | int addAndGet(int key, int delta); 49 | 50 | /** 51 | * Returns the number of key-value mappings in this map. 52 | * 53 | * @return the number of key-value mappings in this map 54 | */ 55 | int size(); 56 | 57 | /** 58 | * Removes all the mappings from this map (optional operation). 59 | */ 60 | void reset(); 61 | 62 | /** 63 | * Write sorted key-value into longBuf, order by key. 64 | * 65 | * @param longBuf the LongBuf 66 | * @return the total values write into longBuf 67 | */ 68 | long fillSortedKvs(LongBuf longBuf); 69 | } 70 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/http/HttpHeaders.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.http; 2 | 3 | import cn.myperf4j.base.util.collections.MapUtils; 4 | 5 | import java.util.ArrayList; 6 | import java.util.List; 7 | import java.util.Map; 8 | 9 | import static java.nio.charset.StandardCharsets.UTF_8; 10 | 11 | /** 12 | * Created by LinShunkang on 2020/05/15 13 | */ 14 | public final class HttpHeaders { 15 | 16 | private final Map> headers; 17 | 18 | public HttpHeaders(int size) { 19 | this.headers = MapUtils.createHashMap(size); 20 | } 21 | 22 | public HttpHeaders(Map> headers) { 23 | this.headers = headers; 24 | } 25 | 26 | public String get(String name) { 27 | List values = headers.get(name); 28 | if (values == null || values.isEmpty()) { 29 | return null; 30 | } 31 | return values.get(0); 32 | } 33 | 34 | public List getValues(String name) { 35 | return headers.get(name); 36 | } 37 | 38 | public void set(String name, String value) { 39 | List values = headers.get(name); 40 | if (values == null) { 41 | values = new ArrayList<>(1); 42 | headers.put(name, values); 43 | } else { 44 | values.clear(); 45 | } 46 | values.add(value); 47 | } 48 | 49 | public void add(String name, String value) { 50 | List values = headers.get(name); 51 | if (values == null) { 52 | values = new ArrayList<>(1); 53 | headers.put(name, values); 54 | } 55 | values.add(value); 56 | } 57 | 58 | public List names() { 59 | return new ArrayList<>(headers.keySet()); 60 | } 61 | 62 | public Map> headers() { 63 | return headers; 64 | } 65 | 66 | public static HttpHeaders defaultHeaders() { 67 | HttpHeaders headers = new HttpHeaders(6); 68 | headers.set("User-Agent", "MyPerf4J"); 69 | headers.set("Connection", "Keep-Alive"); 70 | headers.set("Charset", UTF_8.name()); 71 | return headers; 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/standard/StdLogJvmGcMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.standard; 2 | 3 | import cn.myperf4j.base.metric.JvmGcMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmGcMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.standard.StdJvmGcMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmGcMetricsExporter; 7 | import cn.myperf4j.base.util.Logger; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | import java.util.concurrent.ConcurrentHashMap; 12 | import java.util.concurrent.ConcurrentMap; 13 | 14 | /** 15 | * Created by LinShunkang on 2018/8/25 16 | */ 17 | public class StdLogJvmGcMetricsExporter extends AbstractLogJvmGcMetricsExporter { 18 | 19 | private static final JvmGcMetricsFormatter METRICS_FORMATTER = new StdJvmGcMetricsFormatter(); 20 | 21 | private final ConcurrentMap> metricsMap = new ConcurrentHashMap<>(8); 22 | 23 | @Override 24 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 25 | metricsMap.put(processId, new ArrayList(1)); 26 | } 27 | 28 | @Override 29 | public void process(JvmGcMetrics metrics, long processId, long startMillis, long stopMillis) { 30 | List metricsList = metricsMap.get(processId); 31 | if (metricsList != null) { 32 | metricsList.add(metrics); 33 | } else { 34 | Logger.error("StdLogJvmGcMetricsExporter.process(" + processId + ", " + startMillis + ", " 35 | + stopMillis + "): metricsList is null!!!"); 36 | } 37 | } 38 | 39 | @Override 40 | public void afterProcess(long processId, long startMillis, long stopMillis) { 41 | List metricsList = metricsMap.remove(processId); 42 | if (metricsList != null) { 43 | logger.logAndFlush(METRICS_FORMATTER.format(metricsList, startMillis, stopMillis)); 44 | } else { 45 | Logger.error("StdLogJvmGcMetricsExporter.afterProcess(" + processId + ", " + startMillis + ", " 46 | + stopMillis + "): metricsList is null!!!"); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/standard/StdLogMethodMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.standard; 2 | 3 | import cn.myperf4j.base.metric.MethodMetrics; 4 | import cn.myperf4j.base.metric.formatter.standard.StdMethodMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.MethodMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogMethodMetricsExporter; 7 | import cn.myperf4j.base.util.Logger; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | import java.util.concurrent.ConcurrentHashMap; 12 | import java.util.concurrent.ConcurrentMap; 13 | 14 | /** 15 | * Created by LinShunkang on 2018/7/11 16 | */ 17 | public class StdLogMethodMetricsExporter extends AbstractLogMethodMetricsExporter { 18 | 19 | private static final MethodMetricsFormatter METRICS_FORMATTER = new StdMethodMetricsFormatter(); 20 | 21 | private final ConcurrentMap> metricsMap = new ConcurrentHashMap<>(8); 22 | 23 | @Override 24 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 25 | metricsMap.put(processId, new ArrayList(64)); 26 | } 27 | 28 | @Override 29 | public void process(MethodMetrics metrics, long processId, long startMillis, long stopMillis) { 30 | List metricsList = metricsMap.get(processId); 31 | if (metricsList != null) { 32 | metricsList.add(metrics); 33 | } else { 34 | Logger.error("StdLogMethodMetricsExporter.process(" + processId + ", " + startMillis + ", " 35 | + stopMillis + "): metricsList is null!!!"); 36 | } 37 | } 38 | 39 | @Override 40 | public void afterProcess(long processId, long startMillis, long stopMillis) { 41 | List metricsList = metricsMap.remove(processId); 42 | if (metricsList != null) { 43 | logger.logAndFlush(METRICS_FORMATTER.format(metricsList, startMillis, stopMillis)); 44 | } else { 45 | Logger.error("StdLogMethodMetricsExporter.afterProcess(" + processId + ", " + startMillis + ", " 46 | + stopMillis + "): metricsList is null!!!"); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /MyPerf4J-ASM/src/test/java/MyPerf4J/TypeDestUtilsTest.java: -------------------------------------------------------------------------------- 1 | package MyPerf4J; 2 | 3 | import cn.myperf4j.base.util.TypeDescUtils; 4 | import org.junit.Assert; 5 | import org.junit.Test; 6 | 7 | import java.lang.reflect.Method; 8 | 9 | import static cn.myperf4j.base.util.TypeDescUtils.getMethodParamsDesc; 10 | import static cn.myperf4j.base.util.TypeDescUtils.getSimpleClassName; 11 | 12 | /** 13 | * Created by LinShunkang on 2018/10/19 14 | */ 15 | public class TypeDestUtilsTest { 16 | 17 | @Test 18 | public void test() { 19 | Assert.assertEquals(getMethodParamsDesc("()V"), "()"); 20 | Assert.assertEquals(getMethodParamsDesc("(IF)V"), "(int, float)"); 21 | Assert.assertEquals(getMethodParamsDesc("(ILjava/lang/Object;F)V"), "(int, Object, float)"); 22 | Assert.assertEquals(getMethodParamsDesc("(Ljava/lang/Object;)I"), "(Object)"); 23 | Assert.assertEquals(getMethodParamsDesc("(ILjava/lang/String;)[I"), "(int, String)"); 24 | Assert.assertEquals(getMethodParamsDesc("(ILjava/lang/Map;)[I"), "(int, Map)"); 25 | Assert.assertEquals(getMethodParamsDesc("([I)Ljava/lang/Object;"), "(int[])"); 26 | Assert.assertEquals(getMethodParamsDesc( 27 | "([ILjava/lang/Object;[Ljava/lang/Object;[Ljava/lang/String;)Ljava/lang/Object;"), 28 | "(int[], Object, Object[], String[])"); 29 | Assert.assertEquals(getMethodParamsDesc( 30 | "([[ILjava/lang/Object;[[[Ljava/lang/Object;[[[[[Ljava/lang/String;)Ljava/lang/Object;"), 31 | "(int[][], Object, Object[][][], String[][][][][])"); 32 | } 33 | 34 | @Test 35 | public void testMethod() throws NoSuchMethodException { 36 | Method method0 = TypeDestUtilsTest.class.getDeclaredMethod("testMethod"); 37 | Assert.assertEquals(getMethodParamsDesc(method0), "()"); 38 | 39 | Method method1 = TypeDescUtils.class.getDeclaredMethod("getMethodParamsDesc", Method.class); 40 | Assert.assertEquals(getMethodParamsDesc(method1), "(Method)"); 41 | } 42 | 43 | @Test 44 | public void testSimpleClassName() { 45 | Assert.assertEquals(getSimpleClassName("java/lang/String"), "String"); 46 | Assert.assertEquals(getSimpleClassName("java/lang/Object"), "Object"); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/standard/StdLogJvmGcMetricsV3Exporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.standard; 2 | 3 | import cn.myperf4j.base.metric.JvmGcMetricsV3; 4 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmGcMetricsV3Exporter; 5 | import cn.myperf4j.base.metric.formatter.JvmGcMetricsV3Formatter; 6 | import cn.myperf4j.base.metric.formatter.standard.StdJvmGcMetricsV3Formatter; 7 | import cn.myperf4j.base.util.Logger; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | import java.util.concurrent.ConcurrentHashMap; 12 | import java.util.concurrent.ConcurrentMap; 13 | 14 | /** 15 | * Created by LinShunkang on 2024/02/08 16 | */ 17 | public class StdLogJvmGcMetricsV3Exporter extends AbstractLogJvmGcMetricsV3Exporter { 18 | 19 | private static final JvmGcMetricsV3Formatter METRICS_FORMATTER = new StdJvmGcMetricsV3Formatter(); 20 | 21 | private final ConcurrentMap> metricsMap = new ConcurrentHashMap<>(8); 22 | 23 | @Override 24 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 25 | metricsMap.put(processId, new ArrayList(1)); 26 | } 27 | 28 | @Override 29 | public void process(JvmGcMetricsV3 metrics, long processId, long startMillis, long stopMillis) { 30 | final List metricsList = metricsMap.get(processId); 31 | if (metricsList != null) { 32 | metricsList.add(metrics); 33 | } else { 34 | Logger.error("StdLogJvmGcMetricsV3Exporter.process(" + processId + ", " + startMillis + ", " 35 | + stopMillis + "): metricsList is null!!!"); 36 | } 37 | } 38 | 39 | @Override 40 | public void afterProcess(long processId, long startMillis, long stopMillis) { 41 | final List metricsList = metricsMap.remove(processId); 42 | if (metricsList != null) { 43 | logger.logAndFlush(METRICS_FORMATTER.format(metricsList, startMillis, stopMillis)); 44 | } else { 45 | Logger.error("StdLogJvmGcMetricsV3Exporter.afterProcess(" + processId + ", " + startMillis + ", " 46 | + stopMillis + "): metricsList is null!!!"); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/standard/StdLogJvmClassMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.standard; 2 | 3 | import cn.myperf4j.base.metric.JvmClassMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmClassMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.standard.StdJvmClassMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmClassMetricsExporter; 7 | import cn.myperf4j.base.util.Logger; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | import java.util.concurrent.ConcurrentHashMap; 12 | import java.util.concurrent.ConcurrentMap; 13 | 14 | /** 15 | * Created by LinShunkang on 2018/8/25 16 | */ 17 | public class StdLogJvmClassMetricsExporter extends AbstractLogJvmClassMetricsExporter { 18 | 19 | private static final JvmClassMetricsFormatter METRICS_FORMATTER = new StdJvmClassMetricsFormatter(); 20 | 21 | private final ConcurrentMap> metricsMap = new ConcurrentHashMap<>(8); 22 | 23 | @Override 24 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 25 | metricsMap.put(processId, new ArrayList(1)); 26 | } 27 | 28 | @Override 29 | public void process(JvmClassMetrics metrics, long processId, long startMillis, long stopMillis) { 30 | List metricsList = metricsMap.get(processId); 31 | if (metricsList != null) { 32 | metricsList.add(metrics); 33 | } else { 34 | Logger.error("StdLogJvmClassMetricsExporter.process(" + processId + ", " + startMillis + ", " 35 | + stopMillis + "): metricsList is null!!!"); 36 | } 37 | } 38 | 39 | @Override 40 | public void afterProcess(long processId, long startMillis, long stopMillis) { 41 | List metricsList = metricsMap.remove(processId); 42 | if (metricsList != null) { 43 | logger.logAndFlush(METRICS_FORMATTER.format(metricsList, startMillis, stopMillis)); 44 | } else { 45 | Logger.error("StdLogJvmClassMetricsExporter.afterProcess(" + processId + ", " + startMillis + ", " 46 | + stopMillis + "): metricsList is null!!!"); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/standard/StdLogJvmMemoryMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.standard; 2 | 3 | import cn.myperf4j.base.metric.JvmMemoryMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmMemoryMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.standard.StdJvmMemoryMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmMemoryMetricsExporter; 7 | import cn.myperf4j.base.util.Logger; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | import java.util.concurrent.ConcurrentHashMap; 12 | import java.util.concurrent.ConcurrentMap; 13 | 14 | /** 15 | * Created by LinShunkang on 2018/8/25 16 | */ 17 | public class StdLogJvmMemoryMetricsExporter extends AbstractLogJvmMemoryMetricsExporter { 18 | 19 | private static final JvmMemoryMetricsFormatter METRICS_FORMATTER = new StdJvmMemoryMetricsFormatter(); 20 | 21 | private final ConcurrentMap> metricsMap = new ConcurrentHashMap<>(8); 22 | 23 | @Override 24 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 25 | metricsMap.put(processId, new ArrayList(2)); 26 | } 27 | 28 | @Override 29 | public void process(JvmMemoryMetrics metrics, long processId, long startMillis, long stopMillis) { 30 | List metricsList = metricsMap.get(processId); 31 | if (metricsList != null) { 32 | metricsList.add(metrics); 33 | } else { 34 | Logger.error("StdLogJvmMemoryMetricsExporter.process(" + processId + ", " + startMillis + ", " 35 | + stopMillis + "): metricsList is null!!!"); 36 | } 37 | } 38 | 39 | @Override 40 | public void afterProcess(long processId, long startMillis, long stopMillis) { 41 | List metricsList = metricsMap.remove(processId); 42 | if (metricsList != null) { 43 | logger.logAndFlush(METRICS_FORMATTER.format(metricsList, startMillis, stopMillis)); 44 | } else { 45 | Logger.error("StdLogJvmMemoryMetricsExporter.afterProcess(" + processId + ", " + startMillis + ", " 46 | + stopMillis + "): metricsList is null!!!"); 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /MyPerf4J-Base/src/main/java/cn/myperf4j/base/metric/exporter/log/standard/StdLogJvmThreadMetricsExporter.java: -------------------------------------------------------------------------------- 1 | package cn.myperf4j.base.metric.exporter.log.standard; 2 | 3 | import cn.myperf4j.base.metric.JvmThreadMetrics; 4 | import cn.myperf4j.base.metric.formatter.JvmThreadMetricsFormatter; 5 | import cn.myperf4j.base.metric.formatter.standard.StdJvmThreadMetricsFormatter; 6 | import cn.myperf4j.base.metric.exporter.log.AbstractLogJvmThreadMetricsExporter; 7 | import cn.myperf4j.base.util.Logger; 8 | 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | import java.util.concurrent.ConcurrentHashMap; 12 | import java.util.concurrent.ConcurrentMap; 13 | 14 | /** 15 | * Created by LinShunkang on 2018/8/25 16 | */ 17 | public class StdLogJvmThreadMetricsExporter extends AbstractLogJvmThreadMetricsExporter { 18 | 19 | private static final JvmThreadMetricsFormatter METRICS_FORMATTER = new StdJvmThreadMetricsFormatter(); 20 | 21 | private final ConcurrentMap> metricsMap = new ConcurrentHashMap<>(8); 22 | 23 | @Override 24 | public void beforeProcess(long processId, long startMillis, long stopMillis) { 25 | metricsMap.put(processId, new ArrayList(1)); 26 | } 27 | 28 | @Override 29 | public void process(JvmThreadMetrics metrics, long processId, long startMillis, long stopMillis) { 30 | List metricsList = metricsMap.get(processId); 31 | if (metricsList != null) { 32 | metricsList.add(metrics); 33 | } else { 34 | Logger.error("StdLogJvmThreadMetricsExporter.process(" + processId + ", " + startMillis + ", " 35 | + stopMillis + "): metricsList is null!!!"); 36 | } 37 | } 38 | 39 | @Override 40 | public void afterProcess(long processId, long startMillis, long stopMillis) { 41 | List metricsList = metricsMap.remove(processId); 42 | if (metricsList != null) { 43 | logger.logAndFlush(METRICS_FORMATTER.format(metricsList, startMillis, stopMillis)); 44 | } else { 45 | Logger.error("StdLogJvmThreadMetricsExporter.afterProcess(" + processId + ", " + startMillis + ", " 46 | + stopMillis + "): metricsList is null!!!"); 47 | } 48 | } 49 | } 50 | --------------------------------------------------------------------------------