我正在使用JMH基准测试工具和hs_gc hotspot profiler.
我遇到的问题是我不确定输出计数器值是什么意思,并且无法在网上找到参考.
有人能指出我的规范方向或解释这些价值观吗?
我在下面粘贴了一个示例输出:
HS(GC) | difference: {
sun.gc.collector.0.invocations=16,
sun.gc.collector.0.lastEntryTime=37106821,
sun.gc.collector.0.lastExitTime=37109336,
sun.gc.collector.0.time=1528884,
sun.gc.collector.1.invocations=6,
sun.gc.collector.1.lastEntryTime=34419368,
sun.gc.collector.1.lastExitTime=35532892,
sun.gc.collector.1.time=6721387,
sun.gc.generation.0.space.0.used=872712984,
sun.gc.generation.1.space.0.used=5721334504,
sun.gc.generation.2.space.0.used=4848,
sun.gc.policy.avgBaseFootprint=5312,
sun.gc.policy.avgMajorIntervalTime=-667,
sun.gc.policy.avgMajorPauseTime=-41,
sun.gc.policy.avgMinorIntervalTime=-557,
sun.gc.policy.avgMinorPauseTime=1,
sun.gc.policy.avgOldLive=88064,
sun.gc.policy.avgPromotedAvg=421184,
sun.gc.policy.avgPromotedDev=364832,
sun.gc.policy.avgPromotedPaddedAvg=1515648,
sun.gc.policy.avgSurvivedAvg=1056640,
sun.gc.policy.avgSurvivedDev=-189440,
sun.gc.policy.avgSurvivedPaddedAvg=488320,
sun.gc.policy.avgYoungLive=2708352,
sun.gc.policy.liveAtLastFullGc=5721334504,
sun.gc.policy.liveSpace=2801664,
sun.gc.policy.majorGcCost=5,
sun.gc.policy.majorPauseYoungSlope=-3,
sun.gc.policy.minorGcCost=2,
sun.gc.policy.minorPauseTime=3,
sun.gc.policy.minorPauseYoungSlope=1,
sun.gc.policy.mutatorCost=-7,
sun.gc.policy.oldEdenSize=11534336,
sun.gc.policy.promoted=4584928,
sun.gc.policy.survived=6194624,
sun.gc.tlab.alloc=12852845,
sun.gc.tlab.fills=-15,
sun.gc.tlab.gcWaste=-100979,
sun.gc.tlab.maxFills=-22,
sun.gc.tlab.maxGcWaste=-100859,
sun.gc.tlab.maxSlowAlloc=-5,
sun.gc.tlab.maxSlowWaste=8826,
sun.gc.tlab.slowAlloc=-5,
sun.gc.tlab.slowWaste=8849}
Run Code Online (Sandbox Code Playgroud) 我是Java初学者.
今天,我练习了如何在java中复制文件并尝试按照本教程
http://www.journaldev.com/861/4-ways-to-copy-file-in-java
完成本教程后,我运行了JMH使用57MB txt文件检查性能的基准测试.
nioFiles和NIOChannel之间存在性能差距,比我预期的要大.
Benchmark Mode Cnt Score Error Units
CompressTest.fileCopyUsingNIOChannelClass thrpt 10 22.465 ± 2.996 ops/s
CompressTest.fileCopyWithNIOFiles thrpt 10 0.843 ± 0.488 ops/s
Run Code Online (Sandbox Code Playgroud)
这是我用过的代码
@Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 10, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
@Fork(1)
@State(Scope.Benchmark)
public class CompressTest
{
final static Path source = Paths.get("c:/temp/system.out.lambda.txt");
final static Path target = Paths.get("c:/temp/copied.lambda.txt");
public static void main(String[] args) throws RunnerException, IOException {
Main.main(args);
}
@Benchmark
public static void fileCopyWithNIOFiles() throws …Run Code Online (Sandbox Code Playgroud) 我正在使用JMH,但发现有些难以理解的地方:我有一种方法带有注释,@Benchmark并且设置了measurementIterations(3)。该方法被调用3次,但是在每次迭代调用中,函数都会运行相当大且随机的次数。
我的问题是:这个数字是完全随机的吗?有没有一种方法可以控制它并确定该函数应在一次迭代中运行多少次?设置measurementIterations每种功能随机运行几次的重要性是什么?
我正在运行jmh基准测试,但每个试验中的调用都是串行发生的.如何让调用同时运行?
以下是我的代码摘要:
@State(Scope.Benchmark)
public class FooBenchmark {
@Param({"123456"})
public int barId;
@Setup
public void setup() {
}
@Benchmark
public void run(Blackhole hole) {
System.out.println("A"); // for proof that it's serial (see below)
...
System.out.println("B"); // for proof that it's serial (see below)
}
}
Run Code Online (Sandbox Code Playgroud)
这将打印A然后打印B.将永远不会给出两个连续的A或B.
我目前的设置:
public void launchBenchmark() throws Exception {
Options opt = new OptionsBuilder()
.include(this.getClass().getName())
.mode(Mode.Throughput) //Calculate number of operations in a time unit.
.mode(Mode.AverageTime) //Calculate an average running time per operation
.timeUnit(TimeUnit.MILLISECONDS)
.warmupIterations(1)
.measurementIterations(30)
.threads(Runtime.getRuntime().availableProcessors())
.forks(1)
.shouldFailOnError(true)
.shouldDoGC(true)
.build();
new Runner(opt).run();
}
Run Code Online (Sandbox Code Playgroud)
如何知道/控制(如果可能)每个基准测试执行的操作数量?
设置暖启时间和测量迭代时间是否重要?
谢谢.
我正在使用 Java Measurement Harness (JMH) 来对一些例程进行基准测试。我有兴趣获得每次运行的最大堆大小。JMH 的 GC Profiler 为我提供了诸如分配率和流失率之类的信息,但我正在寻找在测试运行期间获得的最大堆。这能做到吗?
我正在尝试衡量特定方法的性能。当直接调用该方法时,我运行基准测试得很好,但是当该方法使用带有自定义执行器的完整未来时,一切都崩溃了。我已经实现了使用可完成的未来的方法,以便在该方法花费太长时间时强制超时。
@Benchmark
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Threads(value = 5)
@Warmup(iterations = 20)
@Measurement(iterations = 50, timeUnit = TimeUnit.MILLISECONDS)
public String very_big_query(TestState testState) throws Exception {
return testState.transpiler.process(testState.veryBigQuery);
}
Run Code Online (Sandbox Code Playgroud)
@State(Scope.Thread)
public static class TestState {
String veryBigQuery;
Transpiler transpiler;
@Setup(Level.Trial)
public void doSetupTrial() throws Exception {
veryBigQuery = "(";
for(int i = 0; i < 99; i++) {
veryBigQuery += String.format("java_%s OR ", i);
}
veryBigQuery += "java_100) AND (";
for(int i = 100; i < 199; i++) {
veryBigQuery += String.format("java_%s OR …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
public class Playground {
private static final int MAX = 100_000_000;
public static void main(String... args) {
execute(() -> {});
execute(() -> {});
execute(() -> {});
execute(() -> {});
}
public static void execute(Runnable task) {
Stopwatch stopwatch = Stopwatch.createStarted();
for (int i = 0; i < MAX; i++) {
task.run();
}
System.out.println(stopwatch);
}
}
Run Code Online (Sandbox Code Playgroud)
目前,我在 Temurin 17 上的 Intel MBP 上打印以下内容:
3.675 ms
1.948 ms
216.9 ms
243.3 ms
Run Code Online (Sandbox Code Playgroud)
请注意,第三次(以及任何后续)执行时速度减慢了 100*。现在,显然,这不是用Java 编写基准测试的方法。循环代码不执行任何操作,因此我希望它能够消除所有重复。另外,我无法使用 JMH 重复这种效果,这告诉我原因很棘手且脆弱。
那么,为什么会发生这种情况呢?为什么会突然出现如此灾难性的减速,幕后到底发生了什么?一个假设是 …
jmh ×10
java ×8
benchmarking ×2
performance ×2
concurrency ×1
copy ×1
heap-memory ×1
java-8 ×1
jit ×1
nio ×1