From 17c7d0063906d68e9570319710af7d96fc24c5b5 Mon Sep 17 00:00:00 2001 From: exoego Date: Sun, 31 May 2020 22:58:33 +0900 Subject: [PATCH] Add perf_hooks module --- .../nodejs/perf_hooks/PerfHooksTest.scala | 11 +++ .../perf_hooks/PerformanceObserverTest.scala | 24 ++++++ .../nodejs/perf_hooks/PerfHooksTest.scala | 11 +++ .../scalajs/nodejs/perf_hooks/PerfHooks.scala | 21 +++++ .../nodejs/perf_hooks/Performance.scala | 85 +++++++++++++++++++ .../perf_hooks/PerformanceObserver.scala | 59 +++++++++++++ 6 files changed, 211 insertions(+) create mode 100644 app/nodejs-v10/src/test/scala/io/scalajs/nodejs/perf_hooks/PerfHooksTest.scala create mode 100644 app/nodejs-v10/src/test/scala/io/scalajs/nodejs/perf_hooks/PerformanceObserverTest.scala create mode 100644 app/nodejs-v12/src/test/scala/io/scalajs/nodejs/perf_hooks/PerfHooksTest.scala create mode 100644 app/nodejs-v14/src/main/scala/io/scalajs/nodejs/perf_hooks/PerfHooks.scala create mode 100644 app/nodejs-v14/src/main/scala/io/scalajs/nodejs/perf_hooks/Performance.scala create mode 100644 app/nodejs-v14/src/main/scala/io/scalajs/nodejs/perf_hooks/PerformanceObserver.scala diff --git a/app/nodejs-v10/src/test/scala/io/scalajs/nodejs/perf_hooks/PerfHooksTest.scala b/app/nodejs-v10/src/test/scala/io/scalajs/nodejs/perf_hooks/PerfHooksTest.scala new file mode 100644 index 000000000..07b523f4f --- /dev/null +++ b/app/nodejs-v10/src/test/scala/io/scalajs/nodejs/perf_hooks/PerfHooksTest.scala @@ -0,0 +1,11 @@ +package io.scalajs.nodejs.perf_hooks + +import org.scalatest.funspec.AnyFunSpec + +class PerfHooksTest extends AnyFunSpec { + describe("PerfHook") { + it("constants") { + assert(PerfHooks.constants.NODE_PERFORMANCE_GC_MAJOR === 2) + } + } +} diff --git a/app/nodejs-v10/src/test/scala/io/scalajs/nodejs/perf_hooks/PerformanceObserverTest.scala b/app/nodejs-v10/src/test/scala/io/scalajs/nodejs/perf_hooks/PerformanceObserverTest.scala new file mode 100644 index 000000000..10d59eb9d --- /dev/null +++ b/app/nodejs-v10/src/test/scala/io/scalajs/nodejs/perf_hooks/PerformanceObserverTest.scala @@ -0,0 +1,24 @@ +package io.scalajs.nodejs.perf_hooks + +import org.scalatest.funspec.AnyFunSpec +import scala.scalajs.js + +class PerformanceObserverTest extends AnyFunSpec { + describe("PerformanceObserver") { + it("callback should be when new entry added to the performance timeline") { + var called = false + val instance = new PerformanceObserver((_, observer) => { + called = true + observer.disconnect() + }) + instance.observe( + ObserveOptions( + entryTypes = js.Array("mark") + ) + ) + Performance.mark("test") + + assert(called) + } + } +} diff --git a/app/nodejs-v12/src/test/scala/io/scalajs/nodejs/perf_hooks/PerfHooksTest.scala b/app/nodejs-v12/src/test/scala/io/scalajs/nodejs/perf_hooks/PerfHooksTest.scala new file mode 100644 index 000000000..b4486fc82 --- /dev/null +++ b/app/nodejs-v12/src/test/scala/io/scalajs/nodejs/perf_hooks/PerfHooksTest.scala @@ -0,0 +1,11 @@ +package io.scalajs.nodejs.perf_hooks + +import org.scalatest.funspec.AnyFunSpec + +class PerfHooksTest extends AnyFunSpec { + describe("PerfHook") { + it("monitorEventLoopDelay") { + assert(PerfHooks.monitorEventLoopDelay().exceeds === 0) + } + } +} diff --git a/app/nodejs-v14/src/main/scala/io/scalajs/nodejs/perf_hooks/PerfHooks.scala b/app/nodejs-v14/src/main/scala/io/scalajs/nodejs/perf_hooks/PerfHooks.scala new file mode 100644 index 000000000..365038486 --- /dev/null +++ b/app/nodejs-v14/src/main/scala/io/scalajs/nodejs/perf_hooks/PerfHooks.scala @@ -0,0 +1,21 @@ +package io.scalajs.nodejs +package perf_hooks + +import com.thoughtworks.enableIf + +import scala.scalajs.js +import scala.scalajs.js.annotation.JSImport + +@js.native +trait PerfHooks extends js.Object { + def constants: Constants = js.native + + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12) def monitorEventLoopDelay(): Histogram = js.native + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12) def monitorEventLoopDelay( + options: MonitorEventLoopDelayOptions + ): Histogram = js.native +} + +@js.native +@JSImport("perf_hooks", JSImport.Namespace) +object PerfHooks extends PerfHooks diff --git a/app/nodejs-v14/src/main/scala/io/scalajs/nodejs/perf_hooks/Performance.scala b/app/nodejs-v14/src/main/scala/io/scalajs/nodejs/perf_hooks/Performance.scala new file mode 100644 index 000000000..22c6f0977 --- /dev/null +++ b/app/nodejs-v14/src/main/scala/io/scalajs/nodejs/perf_hooks/Performance.scala @@ -0,0 +1,85 @@ +package io.scalajs.nodejs +package perf_hooks + +import _root_.net.exoego.scalajs.types.util.Factory +import com.thoughtworks.enableIf + +import scala.scalajs.js +import scala.scalajs.js.annotation.JSImport + +@js.native +@JSImport("perf_hooks", "Performance") +class Performance extends js.Object { + def clearMarks(): Unit = js.native + def clearMarks(name: String): Unit = js.native + + def mark(): Unit = js.native + def mark(name: String): Unit = js.native + + def measure(name: String, statMark: String, endMark: String): Unit = js.native + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14) + def measure(name: String, statMark: String): Unit = js.native + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14) + def measure(name: String): Unit = js.native + + def nodeTiming: PerformanceNodeTiming = js.native + + def now(): Double = js.native + + def timeOrigin: Double = js.native + + def timerify[T <: js.Function](fn: T): T = js.native +} + +@js.native +@JSImport("perf_hooks", "performance") +object Performance extends Performance + +@Factory +trait PerformanceEntry extends js.Object { + def duration: Double + def name: String + def startTime: Double + def entryType: String + def kind: js.UndefOr[Int] + + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14) + def flags: js.UndefOr[Int] +} + +@Factory +trait PerformanceNodeTiming extends PerformanceEntry { + def bootstrapComplete: Double + def environment: Double + def loopExit: Double + def loopStart: Double + def nodeStart: Double + def v8Start: Double +} + +@js.native +@JSImport("perf_hooks", "constants") +object Constants extends Constants + +@js.native +trait Constants extends js.Object { + val NODE_PERFORMANCE_GC_MAJOR: Int = js.native + val NODE_PERFORMANCE_GC_MINOR: Int = js.native + val NODE_PERFORMANCE_GC_INCREMENTAL: Int = js.native + val NODE_PERFORMANCE_GC_WEAKCB: Int = js.native + + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14) val NODE_PERFORMANCE_GC_FLAGS_NO: Int = js.native + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14) val NODE_PERFORMANCE_GC_FLAGS_CONSTRUCT_RETAINED + : Int = js.native + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14) val NODE_PERFORMANCE_GC_FLAGS_FORCED: Int = + js.native + @enableIf( + io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14 + ) val NODE_PERFORMANCE_GC_FLAGS_SYNCHRONOUS_PHANTOM_PROCESSING: Int = js.native + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14) val NODE_PERFORMANCE_GC_FLAGS_ALL_AVAILABLE_GARBAGE + : Int = js.native + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14) val NODE_PERFORMANCE_GC_FLAGS_ALL_EXTERNAL_MEMORY + : Int = js.native + @enableIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs14) val NODE_PERFORMANCE_GC_FLAGS_SCHEDULE_IDLE: Int = + js.native +} diff --git a/app/nodejs-v14/src/main/scala/io/scalajs/nodejs/perf_hooks/PerformanceObserver.scala b/app/nodejs-v14/src/main/scala/io/scalajs/nodejs/perf_hooks/PerformanceObserver.scala new file mode 100644 index 000000000..43f593ee8 --- /dev/null +++ b/app/nodejs-v14/src/main/scala/io/scalajs/nodejs/perf_hooks/PerformanceObserver.scala @@ -0,0 +1,59 @@ +package io.scalajs.nodejs +package perf_hooks + +import com.thoughtworks.enableMembersIf +import _root_.net.exoego.scalajs.types.util.Factory + +import scala.scalajs.js +import scala.scalajs.js.annotation.JSImport + +@js.native +@JSImport("perf_hooks", "PerformanceObserver") +class PerformanceObserver(callback: js.Function2[PerformanceObserverEntryList, PerformanceObserver, Any]) + extends js.Object { + def disconnect(): Unit = js.native + + def observe(options: ObserveOptions): Unit = js.native +} + +@Factory +trait ObserveOptions extends js.Object { + var entryTypes: js.Array[String] + var buffered: js.UndefOr[Boolean] = js.undefined +} + +@js.native +trait PerformanceObserverEntryList extends js.Object { + def getEntries(): js.Array[PerformanceEntry] = js.native + + def getEntriesByName(name: String): js.Array[PerformanceEntry] = js.native + def getEntriesByName(name: String, `type`: String): js.Array[PerformanceEntry] = js.native + + def getEntriesByType(`type`: String): js.Array[PerformanceEntry] = js.native + + def monitorEventLoopDelay(): Histogram = js.native + def monitorEventLoopDelay(options: MonitorEventLoopDelayOptions): Histogram = js.native +} + +@enableMembersIf(io.scalajs.nodejs.internal.CompilerSwitches.gteNodeJs12) +@js.native +trait Histogram extends js.Object { + def disble(): Boolean = js.native + def enable(): Boolean = js.native + def exceeds: Double = js.native + def max: Double = js.native + def mean: Double = js.native + def min: Double = js.native + def percentile(value: Double): Double = js.native + def stddev: Double = js.native + + // TODO: Return js.Map + def percentiles: js.Object = js.native + + def reset(): Unit = js.native +} + +@Factory +trait MonitorEventLoopDelayOptions extends js.Object { + var resolution: js.UndefOr[Double] = js.undefined +}