اختبارات معيارية دقيقة (Microbenchmarks) مع JMH
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
measure small code paths without common traps.
الموضوعات المخطط لها
- JMH (Java Microbenchmark Harness) handles warm-up, forking, and measurement iterations.
- Traps: dead-code elimination (use a
Blackholeor return values), constant folding, a loop the JIT optimizes away, shared state, unrepresentative data. - Run several forks and report variance, not one number.
- Microbenchmarks answer narrow questions; confirm that the code path matters in the whole system.
تفاصيل المخطط المبدئي
عرض عملي
@Benchmark
public int parseTotal(Fixture f, Blackhole bh) {
return f.parser.total(f.line); // input from @State, result consumed
}Use @Fork, @Warmup, @Measurement with values suited to the case; verify JMH version before recording.
اختبر فهمك
Why return or consume the result in a benchmark?
أظهر الإجابة
Otherwise the JIT may remove the computation as dead code.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.