Skip to content

Commit 567b161

Browse files
committed
Make build robust for profile-independant running.
1 parent 93c2b67 commit 567b161

9 files changed

Lines changed: 251 additions & 23 deletions

File tree

‎byte-buddy-benchmark/pom.xml‎

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<properties>
2626
<benchmark.mainClass>net.bytebuddy.benchmark.runner.BenchmarkRunner</benchmark.mainClass>
2727
<japicmp.skip>true</japicmp.skip>
28+
<surefire.arguments>-Dnet.bytebuddy.safe=false</surefire.arguments>
2829
</properties>
2930

3031
<dependencies>
@@ -125,6 +126,19 @@
125126
</build>
126127

127128
<profiles>
129+
<!-- On JDK 9 and later, reflective ClassLoader.defineClass calls (used by cglib and javassist) require an
130+
explicit 'add-opens' for java.base/java.lang. The flag itself is not recognized on JDK 8, so it must be
131+
applied conditionally. -->
132+
<profile>
133+
<id>benchmark-modular-args</id>
134+
<activation>
135+
<activeByDefault>false</activeByDefault>
136+
<jdk>[9,)</jdk>
137+
</activation>
138+
<properties>
139+
<surefire.arguments>-Dnet.bytebuddy.safe=false --add-opens java.base/java.lang=ALL-UNNAMED</surefire.arguments>
140+
</properties>
141+
</profile>
128142
<profile>
129143
<id>extras</id>
130144
<activation>
@@ -191,26 +205,6 @@
191205
</plugins>
192206
</build>
193207
</profile>
194-
<!-- Do not run tests on JDK 11 or later as the benchmark currently requires sun.misc.Unsafe::defineClass. -->
195-
<profile>
196-
<id>java11-tests</id>
197-
<activation>
198-
<activeByDefault>false</activeByDefault>
199-
<jdk>[11,)</jdk>
200-
</activation>
201-
<build>
202-
<plugins>
203-
<plugin>
204-
<groupId>org.apache.maven.plugins</groupId>
205-
<artifactId>maven-surefire-plugin</artifactId>
206-
<version>${version.plugin.surefire}</version>
207-
<configuration>
208-
<skipTests>true</skipTests>
209-
</configuration>
210-
</plugin>
211-
</plugins>
212-
</build>
213-
</profile>
214208
</profiles>
215209

216210
</project>

‎byte-buddy-benchmark/src/main/java/net/bytebuddy/benchmark/StubInvocationBenchmark.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ public void setUp() throws Exception {
131131
ClassByImplementationBenchmark classByImplementationBenchmark = new ClassByImplementationBenchmark();
132132
baselineInstance = classByImplementationBenchmark.baseline();
133133
byteBuddyInstance = classByImplementationBenchmark.benchmarkByteBuddy();
134+
jdkProxyInstance = classByImplementationBenchmark.benchmarkJdkProxy();
134135
cglibInstance = classByImplementationBenchmark.benchmarkCglib();
135136
javassistInstance = classByImplementationBenchmark.benchmarkJavassist();
136-
jdkProxyInstance = classByImplementationBenchmark.benchmarkJdkProxy();
137137
}
138138

139139
/**

‎byte-buddy-benchmark/src/test/java/net/bytebuddy/benchmark/ClassByExtensionBenchmarkTest.java‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
package net.bytebuddy.benchmark;
22

33
import net.bytebuddy.benchmark.specimen.ExampleClass;
4+
import net.bytebuddy.test.utility.JavaVersionRule;
5+
import net.bytebuddy.test.utility.UnsafeAccessRule;
46
import org.hamcrest.CoreMatchers;
57
import org.junit.Before;
8+
import org.junit.Rule;
69
import org.junit.Test;
10+
import org.junit.rules.MethodRule;
711

812
import static org.hamcrest.CoreMatchers.is;
913
import static org.hamcrest.CoreMatchers.not;
1014
import static org.hamcrest.MatcherAssert.assertThat;
1115

1216
public class ClassByExtensionBenchmarkTest {
1317

18+
@Rule
19+
public MethodRule javaVersionRule = new JavaVersionRule();
20+
21+
@Rule
22+
public MethodRule unsafeAccessRule = new UnsafeAccessRule();
23+
1424
private static final boolean BOOLEAN_VALUE = true;
1525

1626
private static final byte BYTE_VALUE = 42;
@@ -75,6 +85,7 @@ public void testBaseline() throws Exception {
7585
}
7686

7787
@Test
88+
@UnsafeAccessRule.Enforce
7889
public void testByteBuddyWithProxiesClassCreation() throws Exception {
7990
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddyWithProxy();
8091
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -84,6 +95,7 @@ public void testByteBuddyWithProxiesClassCreation() throws Exception {
8495
}
8596

8697
@Test
98+
@UnsafeAccessRule.Enforce
8799
public void testByteBuddyWithProxiesClassCreationCached() throws Exception {
88100
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddyWithProxyAndReusedDelegator();
89101
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -93,6 +105,7 @@ public void testByteBuddyWithProxiesClassCreationCached() throws Exception {
93105
}
94106

95107
@Test
108+
@UnsafeAccessRule.Enforce
96109
public void testByteBuddyWithProxiesClassCreationWithTypePool() throws Exception {
97110
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddyWithProxyWithTypePool();
98111
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -102,6 +115,7 @@ public void testByteBuddyWithProxiesClassCreationWithTypePool() throws Exception
102115
}
103116

104117
@Test
118+
@UnsafeAccessRule.Enforce
105119
public void testByteBuddyWithProxiesClassCreationCachedWithTypePool() throws Exception {
106120
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddyWithProxyAndReusedDelegatorWithTypePool();
107121
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -111,6 +125,7 @@ public void testByteBuddyWithProxiesClassCreationCachedWithTypePool() throws Exc
111125
}
112126

113127
@Test
128+
@UnsafeAccessRule.Enforce
114129
public void testByteBuddyWithAccessorClassCreation() throws Exception {
115130
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddyWithAccessor();
116131
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -120,6 +135,7 @@ public void testByteBuddyWithAccessorClassCreation() throws Exception {
120135
}
121136

122137
@Test
138+
@UnsafeAccessRule.Enforce
123139
public void testByteBuddyWithAccessorClassCreationCached() throws Exception {
124140
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddyWithAccessorAndReusedDelegator();
125141
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -129,6 +145,7 @@ public void testByteBuddyWithAccessorClassCreationCached() throws Exception {
129145
}
130146

131147
@Test
148+
@UnsafeAccessRule.Enforce
132149
public void testByteBuddyWithAccessorClassCreationWithTypePool() throws Exception {
133150
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddyWithAccessorWithTypePool();
134151
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -138,6 +155,7 @@ public void testByteBuddyWithAccessorClassCreationWithTypePool() throws Exceptio
138155
}
139156

140157
@Test
158+
@UnsafeAccessRule.Enforce
141159
public void testByteBuddyWithAccessorClassCreationCachedWithTypePool() throws Exception {
142160
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddyWithAccessorAndReusedDelegatorWithTypePool();
143161
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -147,6 +165,7 @@ public void testByteBuddyWithAccessorClassCreationCachedWithTypePool() throws Ex
147165
}
148166

149167
@Test
168+
@UnsafeAccessRule.Enforce
150169
public void testByteBuddyWithPrefixClassCreation() throws Exception {
151170
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddyWithPrefix();
152171
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -156,6 +175,7 @@ public void testByteBuddyWithPrefixClassCreation() throws Exception {
156175
}
157176

158177
@Test
178+
@UnsafeAccessRule.Enforce
159179
public void testByteBuddyWithPrefixClassCreationCached() throws Exception {
160180
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddyWithPrefixAndReusedDelegator();
161181
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -165,6 +185,7 @@ public void testByteBuddyWithPrefixClassCreationCached() throws Exception {
165185
}
166186

167187
@Test
188+
@UnsafeAccessRule.Enforce
168189
public void testByteBuddyWithPrefixClassCreationWithTypePool() throws Exception {
169190
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddyWithPrefixWithTypePool();
170191
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -174,6 +195,7 @@ public void testByteBuddyWithPrefixClassCreationWithTypePool() throws Exception
174195
}
175196

176197
@Test
198+
@UnsafeAccessRule.Enforce
177199
public void testByteBuddyWithPrefixClassCreationCachedWithTypePool() throws Exception {
178200
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddyWithPrefixAndReusedDelegatorWithTypePool();
179201
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -183,6 +205,7 @@ public void testByteBuddyWithPrefixClassCreationCachedWithTypePool() throws Exce
183205
}
184206

185207
@Test
208+
@UnsafeAccessRule.Enforce
186209
public void testByteBuddySpecializedClassCreation() throws Exception {
187210
ExampleClass instance = classByExtensionBenchmark.benchmarkByteBuddySpecialized();
188211
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -192,6 +215,8 @@ public void testByteBuddySpecializedClassCreation() throws Exception {
192215
}
193216

194217
@Test
218+
@JavaVersionRule.Enforce(atMost = 10)
219+
@UnsafeAccessRule.Enforce
195220
public void testCglibClassCreation() throws Exception {
196221
ExampleClass instance = classByExtensionBenchmark.benchmarkCglib();
197222
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));
@@ -201,6 +226,8 @@ public void testCglibClassCreation() throws Exception {
201226
}
202227

203228
@Test
229+
@JavaVersionRule.Enforce(atMost = 10)
230+
@UnsafeAccessRule.Enforce
204231
public void testJavassistClassCreation() throws Exception {
205232
ExampleClass instance = classByExtensionBenchmark.benchmarkJavassist();
206233
assertThat(instance.getClass(), not(CoreMatchers.<Class<?>>is(ClassByExtensionBenchmark.BASE_CLASS)));

‎byte-buddy-benchmark/src/test/java/net/bytebuddy/benchmark/ClassByImplementationBenchmarkTest.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package net.bytebuddy.benchmark;
22

33
import net.bytebuddy.benchmark.specimen.ExampleInterface;
4+
import net.bytebuddy.test.utility.JavaVersionRule;
5+
import net.bytebuddy.test.utility.UnsafeAccessRule;
46
import org.hamcrest.CoreMatchers;
57
import org.junit.Before;
8+
import org.junit.Rule;
69
import org.junit.Test;
10+
import org.junit.rules.MethodRule;
711

812
import java.lang.reflect.Proxy;
913
import java.util.Arrays;
@@ -15,6 +19,12 @@
1519

1620
public class ClassByImplementationBenchmarkTest {
1721

22+
@Rule
23+
public MethodRule javaVersionRule = new JavaVersionRule();
24+
25+
@Rule
26+
public MethodRule unsafeAccessRule = new UnsafeAccessRule();
27+
1828
private static final boolean BOOLEAN_VALUE = true;
1929

2030
private static final byte BYTE_VALUE = 42;
@@ -72,6 +82,7 @@ public void setUp() throws Exception {
7282
}
7383

7484
@Test
85+
@UnsafeAccessRule.Enforce
7586
public void testBaseline() throws Exception {
7687
ExampleInterface instance = classByImplementationBenchmark.baseline();
7788
assertThat(Arrays.asList(instance.getClass().getInterfaces()), hasItem(ClassByImplementationBenchmark.BASE_CLASS));
@@ -81,6 +92,7 @@ public void testBaseline() throws Exception {
8192
}
8293

8394
@Test
95+
@UnsafeAccessRule.Enforce
8496
public void testByteBuddyClassCreation() throws Exception {
8597
ExampleInterface instance = classByImplementationBenchmark.benchmarkByteBuddy();
8698
assertThat(Arrays.asList(instance.getClass().getInterfaces()), hasItem(ClassByImplementationBenchmark.BASE_CLASS));
@@ -90,6 +102,7 @@ public void testByteBuddyClassCreation() throws Exception {
90102
}
91103

92104
@Test
105+
@UnsafeAccessRule.Enforce
93106
public void testByteBuddyClassCreationWithTypePool() throws Exception {
94107
ExampleInterface instance = classByImplementationBenchmark.benchmarkByteBuddyWithTypePool();
95108
assertThat(Arrays.asList(instance.getClass().getInterfaces()), hasItem(ClassByImplementationBenchmark.BASE_CLASS));
@@ -99,6 +112,8 @@ public void testByteBuddyClassCreationWithTypePool() throws Exception {
99112
}
100113

101114
@Test
115+
@JavaVersionRule.Enforce(atMost = 10)
116+
@UnsafeAccessRule.Enforce
102117
public void testCglibClassCreation() throws Exception {
103118
ExampleInterface instance = classByImplementationBenchmark.benchmarkCglib();
104119
assertThat(Arrays.asList(instance.getClass().getInterfaces()), hasItem(ClassByImplementationBenchmark.BASE_CLASS));
@@ -108,6 +123,8 @@ public void testCglibClassCreation() throws Exception {
108123
}
109124

110125
@Test
126+
@JavaVersionRule.Enforce(atMost = 10)
127+
@UnsafeAccessRule.Enforce
111128
public void testJavassistClassCreation() throws Exception {
112129
ExampleInterface instance = classByImplementationBenchmark.benchmarkJavassist();
113130
assertThat(Arrays.asList(instance.getClass().getInterfaces()), hasItem(ClassByImplementationBenchmark.BASE_CLASS));
@@ -117,6 +134,7 @@ public void testJavassistClassCreation() throws Exception {
117134
}
118135

119136
@Test
137+
@UnsafeAccessRule.Enforce
120138
public void testJdkProxyClassCreation() throws Exception {
121139
ExampleInterface instance = classByImplementationBenchmark.benchmarkJdkProxy();
122140
assertThat(Arrays.asList(instance.getClass().getInterfaces()), hasItem(ClassByImplementationBenchmark.BASE_CLASS));

‎byte-buddy-benchmark/src/test/java/net/bytebuddy/benchmark/StubInvocationBenchmarkTest.java‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,61 @@
11
package net.bytebuddy.benchmark;
22

3+
import net.bytebuddy.test.utility.JavaVersionRule;
4+
import net.bytebuddy.test.utility.UnsafeAccessRule;
35
import org.junit.Before;
6+
import org.junit.Rule;
47
import org.junit.Test;
8+
import org.junit.rules.MethodRule;
59

610
public class StubInvocationBenchmarkTest extends AbstractBlackHoleTest {
711

12+
@Rule
13+
public MethodRule javaVersionRule = new JavaVersionRule();
14+
15+
@Rule
16+
public MethodRule unsafeAccessRule = new UnsafeAccessRule();
17+
818
private StubInvocationBenchmark stubInvocationBenchmark;
919

1020
@Before
1121
public void setUp() throws Exception {
1222
stubInvocationBenchmark = new StubInvocationBenchmark();
13-
stubInvocationBenchmark.setUp();
23+
try {
24+
stubInvocationBenchmark.setUp();
25+
} catch (Throwable ignored) {
26+
// cglib and javassist initialization may fail on JDKs where reflective or cross-package
27+
// class definition is denied; the affected tests are skipped via JavaVersionRule.
28+
}
1429
}
1530

1631
@Test
32+
@UnsafeAccessRule.Enforce
1733
public void testBaseline() throws Exception {
1834
stubInvocationBenchmark.baseline(blackHole);
1935
}
2036

2137
@Test
38+
@UnsafeAccessRule.Enforce
2239
public void testByteBuddyBenchmark() throws Exception {
2340
stubInvocationBenchmark.benchmarkByteBuddy(blackHole);
2441
}
2542

2643
@Test
44+
@JavaVersionRule.Enforce(atMost = 10)
45+
@UnsafeAccessRule.Enforce
2746
public void testCglibBenchmark() throws Exception {
2847
stubInvocationBenchmark.benchmarkCglib(blackHole);
2948
}
3049

3150
@Test
51+
@JavaVersionRule.Enforce(atMost = 10)
52+
@UnsafeAccessRule.Enforce
3253
public void testJavassistBenchmark() throws Exception {
3354
stubInvocationBenchmark.benchmarkJavassist(blackHole);
3455
}
3556

3657
@Test
58+
@UnsafeAccessRule.Enforce
3759
public void testJdkProxyBenchmark() throws Exception {
3860
stubInvocationBenchmark.benchmarkJdkProxy(blackHole);
3961
}

0 commit comments

Comments
 (0)