@@ -2,12 +2,13 @@ package org.jetbrains.gradle.benchmarks.js
2
2
3
3
import kotlinx.cli.*
4
4
import org.jetbrains.gradle.benchmarks.*
5
+ import kotlin.js.*
5
6
import kotlin.math.*
6
7
7
8
external fun require (module : String ): dynamic
8
9
private val benchmarkJs: dynamic = require(" benchmark" )
9
- private val fs = require(" fs" );
10
- private val process = require(" process" );
10
+ private val fs = require(" fs" )
11
+ private val process = require(" process" )
11
12
12
13
class Suite (val title : String , @Suppress(" UNUSED_PARAMETER" ) dummy_args : Array <out String >) {
13
14
private val args = RunnerCommandLine ().also { it.parse((process[" argv" ] as Array <String >).drop(2 )) }
@@ -34,12 +35,27 @@ class Suite(val title: String, @Suppress("UNUSED_PARAMETER") dummy_args: Array<o
34
35
}
35
36
else -> throw UnsupportedOperationException (" Format ${args.traceFormat} is not supported." )
36
37
}
37
- suite.run ()
38
- fs.writeFile(args.reportFile, results.toJson()) { err -> if (err) throw err }
38
+
39
+ suite.run ().on(" complete" ) {
40
+ fs.writeFile(args.reportFile, results.toJson()) { err -> if (err != null ) throw err }
41
+ }
42
+ }
43
+
44
+ fun add (name : String , function : () -> Promise <* >, setup : () -> Unit , teardown : () -> Unit ) {
45
+ suite.add(name) { deferred: Promise <Unit > ->
46
+ // Mind asDynamic: this is **not** a regular promise
47
+ function().then { (deferred.asDynamic()).resolve() }
48
+ }
49
+
50
+ configureBenchmark(setup, teardown, isAsynchronous = true )
39
51
}
40
52
41
53
fun add (name : String , function : () -> Any? , setup : () -> Unit , teardown : () -> Unit ) {
42
54
suite.add(name, function)
55
+ configureBenchmark(setup, teardown, isAsynchronous = false )
56
+ }
57
+
58
+ private fun configureBenchmark (setup : () -> Unit , teardown : () -> Unit , isAsynchronous : Boolean ) {
43
59
val benchmark = suite[suite.length - 1 ] // take back last added benchmark and subscribe to events
44
60
45
61
// TODO: Configure properly
@@ -49,6 +65,8 @@ class Suite(val title: String, @Suppress("UNUSED_PARAMETER") dummy_args: Array<o
49
65
50
66
benchmark.options.minTime = args.iterationTime / 1000.0
51
67
benchmark.options.maxTime = args.iterationTime * args.iterations / 1000.0
68
+ benchmark.options.async = isAsynchronous
69
+ benchmark.options.defer = isAsynchronous
52
70
53
71
benchmark.on(" start" ) { event ->
54
72
val benchmarkFQN = event.target.name
@@ -87,7 +105,6 @@ class Suite(val title: String, @Suppress("UNUSED_PARAMETER") dummy_args: Array<o
87
105
else -> throw UnsupportedOperationException (" Format ${args.traceFormat} is not supported." )
88
106
}
89
107
90
-
91
108
results.add(result)
92
109
}
93
110
}
0 commit comments