-
Notifications
You must be signed in to change notification settings - Fork 530
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'series/3.x' into chrome
- Loading branch information
Showing
6 changed files
with
224 additions
and
4 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
benchmarks/src/main/scala/cats/effect/benchmarks/ParallelBenchmark.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2020-2021 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect.benchmarks | ||
|
||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits.global | ||
import cats.implicits.{catsSyntaxParallelTraverse1, toTraverseOps} | ||
|
||
import org.openjdk.jmh.annotations._ | ||
import org.openjdk.jmh.infra.Blackhole | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* To do comparative benchmarks between versions: | ||
* | ||
* benchmarks/run-benchmark ParallelBenchmark | ||
* | ||
* This will generate results in `benchmarks/results`. | ||
* | ||
* Or to run the benchmark from within sbt: | ||
* | ||
* jmh:run -i 10 -wi 10 -f 2 -t 4 cats.effect.benchmarks.ParallelBenchmark | ||
* | ||
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "4 thread". | ||
* Please note that benchmarks should be usually executed at least in | ||
* 10 iterations (as a rule of thumb), but more is better. | ||
*/ | ||
@State(Scope.Thread) | ||
@BenchmarkMode(Array(Mode.Throughput)) | ||
@OutputTimeUnit(TimeUnit.SECONDS) | ||
class ParallelBenchmark { | ||
|
||
@Param(Array("100", "1000", "10000")) | ||
var size: Int = _ | ||
|
||
@Param(Array("100", "1000", "10000", "100000", "1000000")) | ||
var cpuTokens: Long = _ | ||
|
||
@Benchmark | ||
def parTraverse(): Unit = | ||
1.to(size).toList.parTraverse(_ => IO(Blackhole.consumeCPU(cpuTokens))).void.unsafeRunSync() | ||
|
||
@Benchmark | ||
def traverse(): Unit = | ||
1.to(size).toList.traverse(_ => IO(Blackhole.consumeCPU(cpuTokens))).void.unsafeRunSync() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
webworker-tests/src/main/scala/cats/effect/IOSpecRunner.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2020-2021 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect | ||
|
||
import org.scalajs.dom.webworkers.DedicatedWorkerGlobalScope | ||
import org.specs2.control.ExecuteActions | ||
import org.specs2.reporter.BufferedLineLogger | ||
import org.specs2.runner.ClassRunner | ||
import org.specs2.runner.Runner | ||
import org.specs2.specification.core.Env | ||
|
||
import scala.scalajs.js | ||
|
||
object IOSpecRunner extends IOApp.Simple with ClassRunner { | ||
|
||
def postMessage(msg: js.Any): Unit = DedicatedWorkerGlobalScope.self.postMessage(msg) | ||
|
||
override def run: IO[Unit] = IO.fromFuture { | ||
IO { | ||
val spec = new IOSpec | ||
val env = Env(lineLogger = new BufferedLineLogger { | ||
override def infoLine(msg: String): Unit = postMessage(s"[info] $msg") | ||
override def failureLine(msg: String): Unit = postMessage(s"[error] $msg") | ||
override def errorLine(msg: String): Unit = postMessage(s"[error] $msg") | ||
override def warnLine(msg: String): Unit = postMessage(s"[warn] $msg") | ||
}) | ||
val loader = new ClassLoader() {} | ||
val action = for { | ||
printers <- createPrinters(env.arguments, loader).toAction | ||
stats <- Runner.runSpecStructure(spec.structure(env), env, loader, printers) | ||
// TODO I have no idea how to suspend effects in this | ||
_ = postMessage(stats.toString) | ||
_ = postMessage(stats.isSuccess) | ||
} yield () | ||
ExecuteActions.runActionFuture(action)(env.executionEnv) | ||
} | ||
} | ||
|
||
} |
56 changes: 56 additions & 0 deletions
56
webworker-tests/src/test/scala/cats/effect/WebWorkerIOSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2020-2021 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package cats.effect | ||
|
||
import org.scalajs.dom.webworkers.Worker | ||
import org.scalajs.dom.window | ||
|
||
import scala.concurrent.duration._ | ||
import scala.util.Try | ||
|
||
class WebWorkerIOSpec extends BaseSpec { | ||
|
||
override def executionTimeout = 5.minutes // This is to run the _entire_ IOSpec | ||
|
||
def scalaVersion = if (BuildInfo.scalaVersion.startsWith("2")) | ||
BuildInfo.scalaVersion.split("\\.").init.mkString(".") | ||
else | ||
BuildInfo.scalaVersion | ||
|
||
def targetDir = s"${BuildInfo.baseDirectory}/target/scala-${scalaVersion}" | ||
|
||
Try(window).toOption.foreach { _ => | ||
"io on webworker" should { | ||
"pass the spec" in real { | ||
for { | ||
worker <- IO( | ||
new Worker(s"file://${targetDir}/cats-effect-webworker-tests-fastopt/main.js")) | ||
success <- IO.async_[Boolean] { cb => | ||
worker.onmessage = { event => | ||
event.data match { | ||
case log: String => println(log) | ||
case success: Boolean => cb(Right(success)) | ||
case _ => () | ||
} | ||
} | ||
} | ||
} yield success mustEqual true | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright 2020-2021 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package java.io | ||
|
||
// hack hack buildinfo hack | ||
class File(path: String) { | ||
override def toString() = path | ||
} |