7903368: JMH: GC profiler misreports allocation and churn rates · openjdk/jmh@e7b1218 (original) (raw)

``

1

`+

/*

`

``

2

`+

`

``

3

`+

`

``

4

`+

`

``

5

`+

`

``

6

`+

`

``

7

`+

`

``

8

`+

`

``

9

`+

`

``

10

`+

`

``

11

`+

`

``

12

`+

`

``

13

`+

`

``

14

`+

`

``

15

`+

`

``

16

`+

`

``

17

`+

`

``

18

`+

`

``

19

`+

`

``

20

`+

`

``

21

`+

`

``

22

`+

`

``

23

`+

`

``

24

`+

*/

`

``

25

`+

package org.openjdk.jmh.it.profilers;

`

``

26

+

``

27

`+

import org.junit.Assert;

`

``

28

`+

import org.junit.Test;

`

``

29

`+

import org.openjdk.jmh.annotations.*;

`

``

30

`+

import org.openjdk.jmh.it.Fixtures;

`

``

31

`+

import org.openjdk.jmh.profile.GCProfiler;

`

``

32

`+

import org.openjdk.jmh.results.Result;

`

``

33

`+

import org.openjdk.jmh.results.RunResult;

`

``

34

`+

import org.openjdk.jmh.runner.Runner;

`

``

35

`+

import org.openjdk.jmh.runner.RunnerException;

`

``

36

`+

import org.openjdk.jmh.runner.options.Options;

`

``

37

`+

import org.openjdk.jmh.runner.options.OptionsBuilder;

`

``

38

+

``

39

`+

import java.util.Collection;

`

``

40

`+

import java.util.Map;

`

``

41

`+

import java.util.concurrent.TimeUnit;

`

``

42

+

``

43

`+

@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)

`

``

44

`+

@Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)

`

``

45

`+

@Fork(1)

`

``

46

`+

@BenchmarkMode(Mode.Throughput)

`

``

47

`+

@OutputTimeUnit(TimeUnit.SECONDS)

`

``

48

`+

public class GCProfilerAllocRateTest {

`

``

49

+

``

50

`+

@Benchmark

`

``

51

`+

public Object allocate() {

`

``

52

`+

return new byte[1000];

`

``

53

`+

}

`

``

54

+

``

55

`+

@Test

`

``

56

`+

public void test() throws RunnerException {

`

``

57

`+

Options opts = new OptionsBuilder()

`

``

58

`+

.include(Fixtures.getTestMask(this.getClass()))

`

``

59

`+

.addProfiler(GCProfiler.class)

`

``

60

`+

.build();

`

``

61

+

``

62

`+

RunResult rr = new Runner(opts).runSingle();

`

``

63

+

``

64

`+

double opsPerSec = rr.getPrimaryResult().getScore();

`

``

65

+

``

66

`+

Map<String, Result> sr = rr.getSecondaryResults();

`

``

67

`+

double allocRateMB = sr.get("·gc.alloc.rate").getScore();

`

``

68

`+

double allocRateNormB = sr.get("·gc.alloc.rate.norm").getScore();

`

``

69

`+

double allocRatePrimaryMB = opsPerSec * allocRateNormB / 1024 / 1024;

`

``

70

+

``

71

`+

// Allow 20% slack

`

``

72

`+

if (Math.abs(1 - allocRatePrimaryMB / allocRateMB) > 0.2) {

`

``

73

`+

Assert.fail("Allocation rates disagree. " +

`

``

74

`+

"Reported by profiler: " + allocRateMB +

`

``

75

`+

", computed from primary score: " + allocRatePrimaryMB);

`

``

76

`+

}

`

``

77

`+

}

`

``

78

`+

}

`