Skip to content

Commit 3ab83ce

Browse files
authored
Merge pull request #3284 from djspiewak/feature/specialize-deferred
2 parents 07e7050 + a045092 commit 3ab83ce

26 files changed

Lines changed: 209 additions & 38 deletions

benchmarks/src/main/scala/cats/effect/benchmarks/AsyncBenchmark.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import java.util.concurrent.TimeUnit
3232
*
3333
* Or to run the benchmark from within sbt:
3434
*
35-
* jmh:run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.AsyncBenchmark
35+
* Jmh / run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.AsyncBenchmark
3636
*
3737
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that
3838
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but

benchmarks/src/main/scala/cats/effect/benchmarks/AttemptBenchmark.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import java.util.concurrent.TimeUnit
3232
*
3333
* Or to run the benchmark from within sbt:
3434
*
35-
* jmh:run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.AttemptBenchmark
35+
* Jmh / run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.AttemptBenchmark
3636
*
3737
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that
3838
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but

benchmarks/src/main/scala/cats/effect/benchmarks/BothBenchmark.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import java.util.concurrent.TimeUnit
3232
*
3333
* Or to run the benchmark from within sbt:
3434
*
35-
* jmh:run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.BothBenchmark
35+
* Jmh / run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.BothBenchmark
3636
*
3737
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that
3838
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but

benchmarks/src/main/scala/cats/effect/benchmarks/DeepBindBenchmark.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import java.util.concurrent.TimeUnit
3232
*
3333
* Or to run the benchmark from within sbt:
3434
*
35-
* jmh:run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.DeepBindBenchmark
35+
* Jmh / run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.DeepBindBenchmark
3636
*
3737
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that
3838
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2020-2022 Typelevel
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+
17+
package cats.effect.benchmarks
18+
19+
import cats.effect._
20+
import cats.effect.unsafe.implicits.global
21+
import cats.syntax.all._
22+
23+
import org.openjdk.jmh.annotations._
24+
25+
import java.util.concurrent.TimeUnit
26+
27+
/**
28+
* To do comparative benchmarks between versions:
29+
*
30+
* benchmarks/run-benchmark DeferredBenchmark
31+
*
32+
* This will generate results in `benchmarks/results`.
33+
*
34+
* Or to run the benchmark from within sbt:
35+
*
36+
* Jmh / run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.DeferredBenchmark
37+
*
38+
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that
39+
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but
40+
* more is better.
41+
*/
42+
@State(Scope.Thread)
43+
@BenchmarkMode(Array(Mode.Throughput))
44+
@OutputTimeUnit(TimeUnit.SECONDS)
45+
class DeferredBenchmark {
46+
47+
@Param(Array("10", "100", "1000"))
48+
var count: Int = _
49+
50+
@Benchmark
51+
def get(): Unit = {
52+
IO.deferred[Unit]
53+
.flatMap(d => d.complete(()) *> d.get.replicateA_(count))
54+
.replicateA_(1000)
55+
.unsafeRunSync()
56+
}
57+
58+
@Benchmark
59+
def complete(): Unit = {
60+
IO.deferred[Unit]
61+
.flatMap { d => d.get.parReplicateA_(count) &> d.complete(()) }
62+
.replicateA_(1000)
63+
.unsafeRunSync()
64+
}
65+
66+
@Benchmark
67+
def cancel(): Unit = {
68+
IO.deferred[Unit]
69+
.flatMap { d => d.get.start.replicateA(count).flatMap(_.traverse(_.cancel)) }
70+
.replicateA_(1000)
71+
.unsafeRunSync()
72+
}
73+
}

benchmarks/src/main/scala/cats/effect/benchmarks/DispatcherBenchmark.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import java.util.concurrent.TimeUnit
3434
*
3535
* Or to run the benchmark from within sbt:
3636
*
37-
* jmh:run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.DispatcherBenchmark
37+
* Jmh / run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.DispatcherBenchmark
3838
*
3939
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that
4040
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but

benchmarks/src/main/scala/cats/effect/benchmarks/HandleErrorBenchmark.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import java.util.concurrent.TimeUnit
3232
*
3333
* Or to run the benchmark from within sbt:
3434
*
35-
* jmh:run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.HandleErrorBenchmark
35+
* Jmh / run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.HandleErrorBenchmark
3636
*
3737
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that
3838
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but

benchmarks/src/main/scala/cats/effect/benchmarks/MapCallsBenchmark.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import java.util.concurrent.TimeUnit
3232
*
3333
* Or to run the benchmark from within sbt:
3434
*
35-
* jmh:run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.MapCallsBenchmark
35+
* Jmh / run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.MapCallsBenchmark
3636
*
3737
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that
3838
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but

benchmarks/src/main/scala/cats/effect/benchmarks/MapStreamBenchmark.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import java.util.concurrent.TimeUnit
3232
*
3333
* Or to run the benchmark from within sbt:
3434
*
35-
* jmh:run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.MapStreamBenchmark
35+
* Jmh / run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.MapStreamBenchmark
3636
*
3737
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that
3838
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but

benchmarks/src/main/scala/cats/effect/benchmarks/ParallelBenchmark.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import java.util.concurrent.TimeUnit
3434
*
3535
* Or to run the benchmark from within sbt:
3636
*
37-
* jmh:run -i 10 -wi 10 -f 2 -t 4 cats.effect.benchmarks.ParallelBenchmark
37+
* Jmh / run -i 10 -wi 10 -f 2 -t 4 cats.effect.benchmarks.ParallelBenchmark
3838
*
3939
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "4 thread". Please note that
4040
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but

0 commit comments

Comments
 (0)