2424 * <http://www.apache.org/>.
2525 *
2626 */
27- package org .apache .hc .core5 .pool ;
27+ package org .apache .hc .core5 .benchmark ;
2828
2929import java .io .IOException ;
3030import java .util .concurrent .CountDownLatch ;
3434
3535import org .apache .hc .core5 .io .CloseMode ;
3636import org .apache .hc .core5 .io .ModalCloseable ;
37+ import org .apache .hc .core5 .pool .DisposalCallback ;
38+ import org .apache .hc .core5 .pool .ManagedConnPool ;
39+ import org .apache .hc .core5 .pool .PoolEntry ;
40+ import org .apache .hc .core5 .pool .PoolReusePolicy ;
41+ import org .apache .hc .core5 .pool .RouteSegmentedConnPool ;
42+ import org .apache .hc .core5 .pool .StrictConnPool ;
3743import org .apache .hc .core5 .util .TimeValue ;
3844import org .apache .hc .core5 .util .Timeout ;
3945import org .openjdk .jmh .annotations .Benchmark ;
@@ -77,20 +83,19 @@ static ManagedConnPool<Integer, DummyConnection> createPool(
7783 final int maxPerRoute ,
7884 final int maxTotal ) {
7985
80- final DisposalCallback <DummyConnection > disposal =
81- (connection , closeMode ) -> connection .close (closeMode );
86+ final DisposalCallback <DummyConnection > disposal = DummyConnection ::close ;
8287
8388 switch (type ) {
8489 case STRICT :
85- return new StrictConnPool <Integer , DummyConnection >(
90+ return new StrictConnPool <>(
8691 maxPerRoute ,
8792 maxTotal ,
8893 TTL ,
8994 PoolReusePolicy .LIFO ,
9095 disposal ,
9196 null );
9297 case OFFLOCK :
93- return new RouteSegmentedConnPool <Integer , DummyConnection >(
98+ return new RouteSegmentedConnPool <>(
9499 maxPerRoute ,
95100 maxTotal ,
96101 TTL ,
@@ -155,12 +160,12 @@ public void setup() throws Exception {
155160
156161 for (int i = 0 ; i < MAX_TOTAL ; i ++) {
157162 final PoolEntry <Integer , DummyConnection > entry =
158- lease (pool , Integer . valueOf ( i ) );
163+ lease (pool , i );
159164 makeReusable (entry );
160165 pool .release (entry , true );
161166 }
162167
163- route = Integer . valueOf ( 0 ) ;
168+ route = 0 ;
164169 }
165170
166171 @ TearDown (Level .Trial )
@@ -172,8 +177,8 @@ public void tearDown() throws IOException {
172177 @ State (Scope .Thread )
173178 public static class BacklogFairnessState {
174179
175- private static final Integer SLOW_ROUTE = Integer . valueOf ( 0 ) ;
176- private static final Integer COLD_ROUTE = Integer . valueOf ( 1 ) ;
180+ private static final Integer SLOW_ROUTE = 0 ;
181+ private static final Integer COLD_ROUTE = 1 ;
177182
178183 @ Param ({"STRICT" , "OFFLOCK" })
179184 public PoolType poolType ;
@@ -185,48 +190,76 @@ public static class BacklogFairnessState {
185190 Future <PoolEntry <Integer , DummyConnection >> sameRouteWaiter ;
186191 PoolEntry <Integer , DummyConnection > coldEntry ;
187192
188- @ Setup (Level .Invocation )
189- public void setup () throws Exception {
193+ @ Setup (Level .Trial )
194+ public void setupPool () throws Exception {
190195 pool = createPool (poolType , 2 , 2 );
191196
192- // Warm the disposal path outside the measured region. OFFLOCK starts
193- // its disposer lazily on the first graceful discard; without this,
194- // every sample would include worker-thread startup because this
195- // state creates a fresh pool for every invocation.
197+ // Start the OFFLOCK disposal machinery once per trial, not once per
198+ // measured invocation.
196199 final CountDownLatch disposed = new CountDownLatch (1 );
197- final PoolEntry <Integer , DummyConnection > warmup =
198- lease (pool , Integer .valueOf (-1 ));
200+ final PoolEntry <Integer , DummyConnection > warmup = lease (pool , -1 );
199201 warmup .assignConnection (new DummyConnection (disposed ));
200202 pool .release (warmup , false );
201203 if (!disposed .await (1 , TimeUnit .SECONDS )) {
202204 throw new IllegalStateException ("Disposer warmup timed out" );
203205 }
206+ }
204207
208+ @ Setup (Level .Invocation )
209+ public void setupInvocation () throws Exception {
205210 slow1 = lease (pool , SLOW_ROUTE );
206211 slow2 = lease (pool , SLOW_ROUTE );
207212
208- // Queue another route first, then keep the route that owns all
209- // allocated slots permanently backlogged.
210213 coldWaiter = pool .lease (COLD_ROUTE , null , LEASE_TIMEOUT , null );
211214 sameRouteWaiter = pool .lease (SLOW_ROUTE , null , LEASE_TIMEOUT , null );
212215
213216 makeReusable (slow1 );
217+ coldEntry = null ;
214218 }
215219
216220 @ TearDown (Level .Invocation )
217- public void tearDown () throws IOException {
221+ public void tearDownInvocation () {
218222 if (sameRouteWaiter != null ) {
219223 sameRouteWaiter .cancel (true );
224+ sameRouteWaiter = null ;
220225 }
221- if (coldWaiter != null && !coldWaiter .isDone ()) {
222- coldWaiter .cancel (true );
226+
227+ if (coldWaiter != null ) {
228+ if (coldWaiter .isDone ()) {
229+ try {
230+ coldEntry = coldWaiter .get ();
231+ } catch (final Exception ignore ) {
232+ }
233+ } else {
234+ coldWaiter .cancel (true );
235+ }
236+ coldWaiter = null ;
223237 }
238+
239+ if (coldEntry != null ) {
240+ pool .release (coldEntry , false );
241+ coldEntry = null ;
242+ }
243+ coldWaiter = null ;
244+
245+ if (slow1 != null ) {
246+ pool .release (slow1 , false );
247+ slow1 = null ;
248+ }
249+
224250 if (slow2 != null ) {
225251 pool .release (slow2 , false );
252+ slow2 = null ;
226253 }
254+
227255 if (coldEntry != null ) {
228256 pool .release (coldEntry , false );
257+ coldEntry = null ;
229258 }
259+ }
260+
261+ @ TearDown (Level .Trial )
262+ public void tearDownPool () throws IOException {
230263 pool .close (CloseMode .IMMEDIATE );
231264 }
232265 }
@@ -247,7 +280,7 @@ public void setup() throws Exception {
247280
248281 routes = new Integer [ROUTE_COUNT ];
249282 for (int i = 0 ; i < ROUTE_COUNT ; i ++) {
250- routes [i ] = Integer . valueOf ( i ) ;
283+ routes [i ] = i ;
251284 }
252285
253286 // Fill the global pool entirely with reusable idle entries.
@@ -303,6 +336,10 @@ public void coldRouteProgressUnderSameRouteBacklog(
303336 state .pool .release (state .slow1 , true );
304337 state .slow1 = null ;
305338
339+ if (!state .coldWaiter .isDone ()) {
340+ throw new IllegalStateException ("Cold waiter was not completed inline" );
341+ }
342+
306343 state .coldEntry = state .coldWaiter .get (1 , TimeUnit .SECONDS );
307344 blackhole .consume (state .coldEntry );
308345 }
0 commit comments