Skip to content
This repository was archived by the owner on Jul 30, 2024. It is now read-only.

[perf_hooks] Add new perf_hooks module #257

Merged
merged 1 commit into from
May 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -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
}