Skip to content

Commit 0e48b0d

Browse files
Add heap implementation for AsyncTimeout (#1658)
* Add heap implementation for AsyncTimeout * Add AsyncTimeout benchmark tests * Some test fixes * linting * Don't sample the clock too much * Spotless * working heap * benchmark test improvements, async timeout code cleanup * Visual test * more tests, clean up heap code * Spotless * apiDump * Fix a bunch of nitpicks * No new public API --------- Co-authored-by: Jesse Wilson <[email protected]>
1 parent d9e59b6 commit 0e48b0d

File tree

3 files changed

+495
-63
lines changed

3 files changed

+495
-63
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright (C) 2025 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.squareup.okio.benchmarks;
17+
18+
import java.util.ArrayList;
19+
import java.util.Collections;
20+
import java.util.List;
21+
import java.util.Random;
22+
import java.util.concurrent.TimeUnit;
23+
import okio.AsyncTimeout;
24+
import okio.PriorityQueue;
25+
import org.openjdk.jmh.annotations.Benchmark;
26+
import org.openjdk.jmh.annotations.BenchmarkMode;
27+
import org.openjdk.jmh.annotations.Fork;
28+
import org.openjdk.jmh.annotations.Measurement;
29+
import org.openjdk.jmh.annotations.Mode;
30+
import org.openjdk.jmh.annotations.OutputTimeUnit;
31+
import org.openjdk.jmh.annotations.Param;
32+
import org.openjdk.jmh.annotations.Scope;
33+
import org.openjdk.jmh.annotations.Setup;
34+
import org.openjdk.jmh.annotations.State;
35+
import org.openjdk.jmh.annotations.Warmup;
36+
37+
@Fork(1)
38+
@Warmup(iterations = 2, time = 2)
39+
@Measurement(iterations = 3, time = 2)
40+
@State(Scope.Benchmark)
41+
@BenchmarkMode(Mode.AverageTime)
42+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
43+
public class AsyncTimeoutBenchmark {
44+
45+
@Param({ "1", "10", "100", "1000" })
46+
int queueSize;
47+
48+
private List<AsyncTimeout> asyncTimeouts;
49+
private PriorityQueue heap = new PriorityQueue();
50+
private int next = 0;
51+
52+
@Setup
53+
public void setup() {
54+
heap = new PriorityQueue();
55+
asyncTimeouts = new ArrayList<>(queueSize);
56+
for (int i = 0; i < queueSize; i++) {
57+
AsyncTimeout timeout = new AsyncTimeout() {
58+
@Override protected void timedOut() {
59+
// No-op
60+
}
61+
};
62+
timeout.timeout(i, TimeUnit.SECONDS);
63+
asyncTimeouts.add(timeout);
64+
}
65+
Collections.shuffle(asyncTimeouts, new Random(0));
66+
for (int i = 0; i < queueSize; i++) {
67+
heap.add(asyncTimeouts.get(i));
68+
}
69+
}
70+
71+
@Benchmark
72+
public void enterExit() {
73+
AsyncTimeout timeout = asyncTimeouts.get(next++);
74+
heap.remove(timeout);
75+
heap.add(timeout);
76+
next = next % queueSize;
77+
}
78+
}

0 commit comments

Comments
 (0)