@@ -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,26 @@ 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
+ }
39
42
}
40
43
41
- fun add (name : String , function : () -> Any? , setup : () -> Unit , teardown : () -> Unit ) {
42
- suite.add(name, function)
44
+ fun add (name : String , function : () -> Any? , setup : () -> Unit , teardown : () -> Unit , isAsynchronous : Boolean = false) {
45
+ /*
46
+ * `isAsynchronous` is set to true iff benchmark return type is `Promise`.
47
+ * In that case, special treatment by benchmark.js is required
48
+ */
49
+ if (isAsynchronous) {
50
+ suite.add(name) { deferred: Promise <Unit > ->
51
+ // Mind asDynamic: this is **not** a regular promise
52
+ (function() as Promise <* >).then { (deferred.asDynamic()).resolve() }
53
+ }
54
+ } else {
55
+ suite.add(name, function)
56
+ }
57
+
43
58
val benchmark = suite[suite.length - 1 ] // take back last added benchmark and subscribe to events
44
59
45
60
// TODO: Configure properly
@@ -49,6 +64,8 @@ class Suite(val title: String, @Suppress("UNUSED_PARAMETER") dummy_args: Array<o
49
64
50
65
benchmark.options.minTime = args.iterationTime / 1000.0
51
66
benchmark.options.maxTime = args.iterationTime * args.iterations / 1000.0
67
+ benchmark.options.async = isAsynchronous
68
+ benchmark.options.defer = isAsynchronous
52
69
53
70
benchmark.on(" start" ) { event ->
54
71
val benchmarkFQN = event.target.name
@@ -87,7 +104,6 @@ class Suite(val title: String, @Suppress("UNUSED_PARAMETER") dummy_args: Array<o
87
104
else -> throw UnsupportedOperationException (" Format ${args.traceFormat} is not supported." )
88
105
}
89
106
90
-
91
107
results.add(result)
92
108
}
93
109
}
0 commit comments