-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestThreads.scala
60 lines (53 loc) · 1.82 KB
/
TestThreads.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.perikov.threadtest
import java.util.concurrent.ThreadFactory
def testNative(numThreads: Int)(using threadFactory: ThreadFactory): Unit =
import java.net.ServerSocket
val acceptor = threadFactory.newThread { () =>
println("Started acceptor")
val sock = ServerSocket(Constants.listenPort)
while true do
var cnt = 0L
while true do
cnt += 1
val con = sock.accept()
println(s"Accepted connection $cnt")
Server(con, s"Server $cnt")
}
(1 to numThreads).foreach: n =>
Client(s"Client $n")
Thread.sleep(Constants.clientStartDelayMillis)
acceptor.join()
end testNative
/** @see
* [yellow, red and
* others](https://stackoverflow.com/questions/25309748/what-is-thread-stack-size-option-xss-given-to-jvm-why-does-it-have-a-limit-of)
*
* @see
* [Stack zones explained](https://pangin.pro/posts/stack-overflow-handling)
*/
object Main:
val nativeTH: ThreadFactory = Thread.ofPlatform().start(_)
val loomTH: ThreadFactory = Thread.ofVirtual().start(_)
def main(args: Array[String]): Unit =
import scala.util.control.NonFatal
val methods: Map[String, Int => Unit] = Map(
"fs2" -> (n => fs2async.testFS2Unsafe(n)),
"io" -> (n => io.testIOUnsafe(n)),
"ionio" -> (n => ionio.testIONIOUnsafe(n)),
"native" -> (n => testNative(n)(using nativeTH)),
"loom" -> (n => testNative(n)(using loomTH))
)
val (numThreads, methodName, runner) =
try
val nt = args(0).toInt
val mn = args(1)
(nt, mn, methods(mn))
catch
case NonFatal(_) =>
scala.sys.error(
s"Usage: prog <num threads> <${methods.keys.mkString(" | ")}>"
)
println(
s"Will measure thread performance for $numThreads threads, method: $methodName\n\n\n"
)
runner(numThreads)