@callstack/nitro-performance-analytics 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/Package.swift +107 -0
- package/README.md +72 -2
- package/android/build.gradle +3 -0
- package/android/src/main/java/com/margelo/nitro/callstack/analytics/HybridPerformanceAnalytics.kt +37 -0
- package/android/src/main/java/com/margelo/nitro/callstack/analytics/HybridThreadBenchmark.kt +33 -0
- package/ios/HybridPerformanceAnalytics.swift +19 -0
- package/ios/HybridThreadBenchmark.swift +22 -0
- package/nitro.json +7 -0
- package/package.json +34 -4
- package/src/PerformanceAnalytics.nitro.ts +18 -0
- package/src/ThreadBenchmark.nitro.ts +26 -0
- package/src/index.ts +50 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hanno J. Gödecke
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+
SOFTWARE.
|
package/Package.swift
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// swift-tools-version: 6.4
|
|
2
|
+
|
|
3
|
+
import Foundation
|
|
4
|
+
import PackageDescription
|
|
5
|
+
|
|
6
|
+
let name = "PerformanceAnalyticsNitro"
|
|
7
|
+
let generated = "nitrogen/generated"
|
|
8
|
+
let nitroDependencies: [Target.Dependency] = [
|
|
9
|
+
.product(name: "NitroModules", package: "nitro-core")
|
|
10
|
+
]
|
|
11
|
+
let cxxSettings: [CXXSetting] = [
|
|
12
|
+
.define("DEBUG", .when(configuration: .debug)),
|
|
13
|
+
.define("NDEBUG", .when(configuration: .release)),
|
|
14
|
+
.disableWarning("nullability-extension"),
|
|
15
|
+
]
|
|
16
|
+
let swiftSources = ["ios", "\(generated)/apple/swift"]
|
|
17
|
+
let bridgeSources = ["c++", "\(name)-Swift-Cxx-Bridge.cpp", "\(name)Autolinking.cpp"]
|
|
18
|
+
let cxxSources = ["\(generated)/shared/c++"]
|
|
19
|
+
|
|
20
|
+
// Bare-host integration. Nitrogen generates the bindings; this manifest owns the targets.
|
|
21
|
+
let package = Package(
|
|
22
|
+
name: name,
|
|
23
|
+
platforms: [.iOS(.v16), .macOS(.v13)],
|
|
24
|
+
products: [
|
|
25
|
+
.library(name: name, targets: ["\(name)Cxx", name, "\(name)SwiftBridge"])
|
|
26
|
+
],
|
|
27
|
+
dependencies: [
|
|
28
|
+
.package(path: "../nitro-core"),
|
|
29
|
+
.package(url: "https://github.com/callstackincubator/performance-analytics.git",
|
|
30
|
+
exact: "0.2.1")
|
|
31
|
+
],
|
|
32
|
+
targets: [
|
|
33
|
+
.target(
|
|
34
|
+
name: "\(name)Cxx",
|
|
35
|
+
dependencies: nitroDependencies,
|
|
36
|
+
path: ".",
|
|
37
|
+
exclude: excludes(in: ".", keeping: cxxSources),
|
|
38
|
+
sources: cxxSources,
|
|
39
|
+
publicHeadersPath: "\(generated)/shared/c++/include",
|
|
40
|
+
cxxSettings: cxxSettings
|
|
41
|
+
),
|
|
42
|
+
.target(
|
|
43
|
+
name: "\(name)AppleCxx",
|
|
44
|
+
dependencies: [.target(name: "\(name)Cxx")] + nitroDependencies,
|
|
45
|
+
path: "\(generated)/apple/\(name)AppleCxx",
|
|
46
|
+
cxxSettings: cxxSettings
|
|
47
|
+
),
|
|
48
|
+
.target(
|
|
49
|
+
name: name,
|
|
50
|
+
dependencies: [
|
|
51
|
+
.target(name: "\(name)AppleCxx"),
|
|
52
|
+
.product(name: "CallstackPerformanceAnalytics", package: "performance-analytics"),
|
|
53
|
+
] + nitroDependencies,
|
|
54
|
+
path: ".",
|
|
55
|
+
exclude: excludes(in: ".", keeping: swiftSources),
|
|
56
|
+
sources: swiftSources,
|
|
57
|
+
cxxSettings: cxxSettings,
|
|
58
|
+
swiftSettings: [
|
|
59
|
+
// UIKit implementations run synchronously on the host's main thread.
|
|
60
|
+
.swiftLanguageMode(.v5),
|
|
61
|
+
.unsafeFlags(["-strict-concurrency=minimal"]),
|
|
62
|
+
.enableUpcomingFeature("InternalImportsByDefault"),
|
|
63
|
+
.enableUpcomingFeature("MemberImportVisibility"),
|
|
64
|
+
.interoperabilityMode(.Cxx),
|
|
65
|
+
.unsafeFlags(["-Xfrontend", "-clang-header-expose-decls=has-expose-attr-or-stdlib"]),
|
|
66
|
+
.unsafeFlags([
|
|
67
|
+
"-Xfrontend", "-clang-header-expose-module",
|
|
68
|
+
"-Xfrontend", "NitroModules=NitroModules-Swift.h",
|
|
69
|
+
]),
|
|
70
|
+
]
|
|
71
|
+
),
|
|
72
|
+
.target(
|
|
73
|
+
name: "\(name)SwiftBridge",
|
|
74
|
+
dependencies: [.target(name: name), .target(name: "\(name)AppleCxx")] + nitroDependencies,
|
|
75
|
+
path: "\(generated)/apple",
|
|
76
|
+
exclude: excludes(in: "\(generated)/apple", keeping: bridgeSources) + ["c++/include"],
|
|
77
|
+
sources: bridgeSources,
|
|
78
|
+
publicHeadersPath: "c++/include",
|
|
79
|
+
cxxSettings: cxxSettings + [
|
|
80
|
+
.unsafeFlags(["-I${GENERATED_MODULEMAP_DIR}", "-fno-modules"])
|
|
81
|
+
]
|
|
82
|
+
),
|
|
83
|
+
],
|
|
84
|
+
cxxLanguageStandard: .cxx20
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
func excludes(in path: String, keeping sources: [String]) -> [String] {
|
|
88
|
+
var parents: Set<String> = [""]
|
|
89
|
+
for source in sources {
|
|
90
|
+
var components = source.split(separator: "/").dropLast()
|
|
91
|
+
while !components.isEmpty {
|
|
92
|
+
parents.insert(components.joined(separator: "/"))
|
|
93
|
+
components = components.dropLast()
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
let root = URL(fileURLWithPath: Context.packageDirectory).appendingPathComponent(path)
|
|
97
|
+
return parents.sorted().flatMap { parent in
|
|
98
|
+
let entries =
|
|
99
|
+
(try? FileManager.default.contentsOfDirectory(
|
|
100
|
+
atPath: root.appendingPathComponent(parent).path)) ?? []
|
|
101
|
+
return entries.sorted().compactMap { entry in
|
|
102
|
+
let entryPath = parent.isEmpty ? entry : "\(parent)/\(entry)"
|
|
103
|
+
return entry.hasPrefix(".") || sources.contains(entryPath) || parents.contains(entryPath)
|
|
104
|
+
? nil : entryPath
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
package/README.md
CHANGED
|
@@ -1,3 +1,73 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Callstack PerformanceAnalytics for Nitro UI
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Add `@callstack/nitro-performance-analytics` as a direct app dependency.
|
|
4
|
+
From this monorepo, use `workspace:*`. Nitro UI CLI and renderer
|
|
5
|
+
`0.3.0` resolve and register the Swift and Kotlin implementations
|
|
6
|
+
when you run `nitro-ui prepare` and rebuild your app.
|
|
7
|
+
|
|
8
|
+
```ts
|
|
9
|
+
import { PerformanceAnalytics } from '@callstack/nitro-performance-analytics';
|
|
10
|
+
|
|
11
|
+
PerformanceAnalytics.init({
|
|
12
|
+
host: 'https://perf.example.com',
|
|
13
|
+
apiKey: 'ps_your_project_ingest_key',
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
PerformanceAnalytics.flushNow();
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Call `init` before mounting your root to include startup in the recording.
|
|
20
|
+
The default sampling interval is 10 seconds. Use `sampleIntervalMs`,
|
|
21
|
+
`enableBackgroundSampling` (default false), `autoInjectFCP` (default true)
|
|
22
|
+
and `enableTouchTracking` (default true) to configure native collection.
|
|
23
|
+
`flushNow` persists current samples and requests upload of completed sessions.
|
|
24
|
+
|
|
25
|
+
## iOS
|
|
26
|
+
|
|
27
|
+
The module links the native SDK through Swift Package Manager. Import
|
|
28
|
+
`CallstackPerformanceAnalytics` in your app delegate. In
|
|
29
|
+
`application(_:didFinishLaunchingWithOptions:)`, register background uploads
|
|
30
|
+
and subscribe to lifecycle events before the first activation.
|
|
31
|
+
|
|
32
|
+
```swift
|
|
33
|
+
CallstackPerformanceAnalytics.preRegisterBackgroundTaskIfNeeded()
|
|
34
|
+
CallstackPerformanceAnalytics.shared.startAutomaticLifecycleTracking()
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
In Info.plist, add
|
|
38
|
+
`com.margeloanalyticssdk.upload` to `BGTaskSchedulerPermittedIdentifiers`
|
|
39
|
+
and `fetch` to `UIBackgroundModes`.
|
|
40
|
+
|
|
41
|
+
## Android
|
|
42
|
+
|
|
43
|
+
Include `mavenCentral()` in the app's Gradle dependency repositories.
|
|
44
|
+
The adapter brings in `com.callstack:performance-analytics-android:0.2.1`
|
|
45
|
+
and its C++ dependency. The SDK's manifest provider installs process lifecycle
|
|
46
|
+
tracking. See the [Gradle setup](https://github.com/callstackincubator/performance-analytics#android-with-gradle).
|
|
47
|
+
|
|
48
|
+
## Thread benchmarks on Android and iOS
|
|
49
|
+
|
|
50
|
+
Construct a window on the Hermes thread immediately before the work. End it
|
|
51
|
+
on the same thread and pass the returned object to your CI harness.
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { ThreadBenchmark } from '@callstack/nitro-performance-analytics';
|
|
55
|
+
|
|
56
|
+
const window = new ThreadBenchmark('read-items');
|
|
57
|
+
try {
|
|
58
|
+
readItems();
|
|
59
|
+
} finally {
|
|
60
|
+
const result = window.end();
|
|
61
|
+
reportToCI(result);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
The constructor and `end()` are synchronous. A window spanning an `await`
|
|
66
|
+
also counts other work that runs on that thread before it resumes. JavaScript
|
|
67
|
+
represents counts and nanoseconds exactly up to `Number.MAX_SAFE_INTEGER`.
|
|
68
|
+
On iOS, the window returns both clocks and the thread ID with
|
|
69
|
+
`countersAvailable: false`. The example’s **Run thread benchmark** action
|
|
70
|
+
displays the result on either platform.
|
|
71
|
+
|
|
72
|
+
[Measurement fields and platform support](../../README.md#thread-benchmarks) ·
|
|
73
|
+
[Runnable Release harness](../../apps/example-nitro-ui/benchmarks/README.md)
|
package/android/src/main/java/com/margelo/nitro/callstack/analytics/HybridPerformanceAnalytics.kt
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
package com.margelo.nitro.callstack.analytics
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import com.callstack.performanceanalytics.AnalyticsNativeConfig
|
|
5
|
+
import com.callstack.performanceanalytics.CallstackPerformanceAnalytics
|
|
6
|
+
import com.margelo.nitroui.runtime.NitroActivity
|
|
7
|
+
import com.margelo.nitroui.runtime.NitroActivityInitializer
|
|
8
|
+
|
|
9
|
+
class HybridPerformanceAnalytics : HybridPerformanceAnalyticsSpec() {
|
|
10
|
+
override fun setup(host: String, apiKey: String, sampleIntervalMs: Double,
|
|
11
|
+
enableBackgroundSampling: Boolean, autoInjectFCP: Boolean, enableTouchTracking: Boolean) {
|
|
12
|
+
val interval = sampleIntervalMs.toLong()
|
|
13
|
+
val config = AnalyticsNativeConfig(
|
|
14
|
+
host = host,
|
|
15
|
+
apiKey = apiKey,
|
|
16
|
+
sampleIntervalMs = interval,
|
|
17
|
+
enableBackgroundSampling = enableBackgroundSampling,
|
|
18
|
+
autoInjectFCP = autoInjectFCP,
|
|
19
|
+
enableTouchTracking = enableTouchTracking
|
|
20
|
+
)
|
|
21
|
+
CallstackPerformanceAnalytics.setup(context, config)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
override fun flushNow() {
|
|
25
|
+
CallstackPerformanceAnalytics.flushNow(context)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
companion object : HybridPerformanceAnalyticsSpec.Companion, NitroActivityInitializer {
|
|
29
|
+
private lateinit var context: Context
|
|
30
|
+
|
|
31
|
+
override fun onCreate(activity: NitroActivity) {
|
|
32
|
+
context = activity.applicationContext
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
override fun constructor() = HybridPerformanceAnalytics()
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
package com.margelo.nitro.callstack.analytics
|
|
2
|
+
|
|
3
|
+
import com.callstack.performanceanalytics.ThreadBenchmark
|
|
4
|
+
|
|
5
|
+
class HybridThreadBenchmark(name: String) : HybridThreadBenchmarkSpec() {
|
|
6
|
+
private val window = ThreadBenchmark(name)
|
|
7
|
+
|
|
8
|
+
override fun end(): ThreadBenchmarkResult {
|
|
9
|
+
val result = window.end()
|
|
10
|
+
val threadId = result.threadId.toDouble()
|
|
11
|
+
val instructions = result.instructions?.toDouble()
|
|
12
|
+
val cycles = result.cycles?.toDouble()
|
|
13
|
+
val enabledNs = result.timeEnabledNs?.toDouble()
|
|
14
|
+
val runningNs = result.timeRunningNs?.toDouble()
|
|
15
|
+
val errno = result.counterErrno?.toDouble()
|
|
16
|
+
return ThreadBenchmarkResult(
|
|
17
|
+
name = result.name,
|
|
18
|
+
threadId = threadId,
|
|
19
|
+
elapsedMs = result.elapsedMs,
|
|
20
|
+
threadCpuMs = result.threadCpuMs,
|
|
21
|
+
countersAvailable = result.countersAvailable,
|
|
22
|
+
instructions = instructions,
|
|
23
|
+
cycles = cycles,
|
|
24
|
+
timeEnabledNs = enabledNs,
|
|
25
|
+
timeRunningNs = runningNs,
|
|
26
|
+
counterErrno = errno
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
companion object : HybridThreadBenchmarkSpec.Companion {
|
|
31
|
+
override fun constructor(name: String) = HybridThreadBenchmark(name)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
internal import CallstackPerformanceAnalytics
|
|
2
|
+
internal import Foundation
|
|
3
|
+
internal import NitroModules
|
|
4
|
+
|
|
5
|
+
final class HybridPerformanceAnalytics: HybridPerformanceAnalyticsSpec {
|
|
6
|
+
private let sdk = CallstackPerformanceAnalytics.shared
|
|
7
|
+
|
|
8
|
+
func setup(host: String, apiKey: String, sampleIntervalMs: Double, enableBackgroundSampling: Bool,
|
|
9
|
+
autoInjectFCP: Bool, enableTouchTracking: Bool) throws {
|
|
10
|
+
guard let url = URL(string: host) else { throw URLError(.badURL) }
|
|
11
|
+
sdk.setup(host: url, apiKey: apiKey, sampleIntervalMs: sampleIntervalMs,
|
|
12
|
+
enableBackgroundSampling: enableBackgroundSampling, autoInjectFCP: autoInjectFCP,
|
|
13
|
+
enableTouchTracking: enableTouchTracking)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
func flushNow() throws {
|
|
17
|
+
sdk.flushNow()
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
internal import class CallstackPerformanceAnalytics.ThreadBenchmark
|
|
2
|
+
internal import NitroModules
|
|
3
|
+
|
|
4
|
+
final class HybridThreadBenchmark: HybridThreadBenchmarkSpec {
|
|
5
|
+
private let window: ThreadBenchmark
|
|
6
|
+
|
|
7
|
+
init(name: String) throws {
|
|
8
|
+
window = try ThreadBenchmark(name: name)
|
|
9
|
+
try super.init()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
func end() throws -> ThreadBenchmarkResult {
|
|
13
|
+
let result = try window.end()
|
|
14
|
+
let tid = Double(result.threadId)
|
|
15
|
+
let errno = Double(result.counterErrno)
|
|
16
|
+
return ThreadBenchmarkResult(
|
|
17
|
+
name: result.name, threadId: tid, elapsedMs: result.elapsedMs,
|
|
18
|
+
threadCpuMs: result.threadCpuMs, countersAvailable: result.countersAvailable,
|
|
19
|
+
instructions: nil, cycles: nil, timeEnabledNs: nil, timeRunningNs: nil,
|
|
20
|
+
counterErrno: errno)
|
|
21
|
+
}
|
|
22
|
+
}
|
package/nitro.json
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"environment": "bare",
|
|
3
|
+
"cxxNamespace": ["callstack", "analytics"],
|
|
4
|
+
"apple": { "moduleName": "PerformanceAnalyticsNitro" },
|
|
5
|
+
"android": { "namespace": ["callstack", "analytics"], "cxxLibName": "PerformanceAnalyticsNitro" },
|
|
6
|
+
"ignorePaths": ["**/node_modules"]
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@callstack/nitro-performance-analytics",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Callstack PerformanceAnalytics SDK for Nitro UI",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/callstackincubator/performance-analytics.git",
|
|
9
|
+
"directory": "packages/nitro-sdk"
|
|
10
|
+
},
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
},
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./src/index.ts"
|
|
17
|
+
},
|
|
18
|
+
"imports": {
|
|
19
|
+
"#nitro/*": "./nitrogen/generated/shared/javascript/*.cjs"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"src",
|
|
23
|
+
"ios",
|
|
24
|
+
"android",
|
|
25
|
+
"nitro.json",
|
|
26
|
+
"Package.swift"
|
|
27
|
+
],
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"nitro-core": "1.0.0-alpha.nightly.137.gd4793c40b8b8"
|
|
30
|
+
},
|
|
31
|
+
"nitroUI": {
|
|
32
|
+
"android": {
|
|
33
|
+
"activityInitializer": "com.margelo.nitro.callstack.analytics.HybridPerformanceAnalytics"
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Android, iOS, type HybridObject } from 'nitro-core';
|
|
2
|
+
|
|
3
|
+
export * from '#nitro/PerformanceAnalytics';
|
|
4
|
+
|
|
5
|
+
@iOS({ language: 'swift', implementation: { className: 'HybridPerformanceAnalytics' } })
|
|
6
|
+
@Android({ language: 'kotlin', implementation: { className: 'HybridPerformanceAnalytics' } })
|
|
7
|
+
export declare class PerformanceAnalytics extends HybridObject {
|
|
8
|
+
public constructor();
|
|
9
|
+
setup(
|
|
10
|
+
host: string,
|
|
11
|
+
apiKey: string,
|
|
12
|
+
sampleIntervalMs: number,
|
|
13
|
+
enableBackgroundSampling: boolean,
|
|
14
|
+
autoInjectFCP: boolean,
|
|
15
|
+
enableTouchTracking: boolean
|
|
16
|
+
): void;
|
|
17
|
+
flushNow(): void;
|
|
18
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Android, iOS, type HybridObject } from 'nitro-core';
|
|
2
|
+
|
|
3
|
+
export * from '#nitro/ThreadBenchmark';
|
|
4
|
+
|
|
5
|
+
export interface ThreadBenchmarkResult {
|
|
6
|
+
name: string;
|
|
7
|
+
threadId: number;
|
|
8
|
+
elapsedMs: number;
|
|
9
|
+
threadCpuMs: number;
|
|
10
|
+
countersAvailable: boolean;
|
|
11
|
+
instructions?: number;
|
|
12
|
+
cycles?: number;
|
|
13
|
+
timeEnabledNs?: number;
|
|
14
|
+
timeRunningNs?: number;
|
|
15
|
+
counterErrno?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
@iOS({ language: 'swift', implementation: { className: 'HybridThreadBenchmark' } })
|
|
19
|
+
@Android({
|
|
20
|
+
language: 'kotlin',
|
|
21
|
+
implementation: { className: 'HybridThreadBenchmark' },
|
|
22
|
+
})
|
|
23
|
+
export declare class ThreadBenchmark extends HybridObject {
|
|
24
|
+
public constructor(name: string);
|
|
25
|
+
end(): ThreadBenchmarkResult;
|
|
26
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { PerformanceAnalytics as NativePerformanceAnalytics } from './PerformanceAnalytics.nitro';
|
|
2
|
+
|
|
3
|
+
export interface AnalyticsConfig {
|
|
4
|
+
host: string;
|
|
5
|
+
apiKey: string;
|
|
6
|
+
sampleIntervalMs?: number;
|
|
7
|
+
enableBackgroundSampling?: boolean;
|
|
8
|
+
autoInjectFCP?: boolean;
|
|
9
|
+
enableTouchTracking?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const native = new NativePerformanceAnalytics();
|
|
13
|
+
// The native timer represents its interval in signed 64-bit nanoseconds.
|
|
14
|
+
const maxIntervalMs = Math.floor(2 ** 63 / 1_000_000);
|
|
15
|
+
|
|
16
|
+
export const PerformanceAnalytics = {
|
|
17
|
+
init(config: AnalyticsConfig): void {
|
|
18
|
+
const host = new URL(config.host);
|
|
19
|
+
if (host.protocol !== 'https:' && host.protocol !== 'http:') {
|
|
20
|
+
throw new Error('The SDK host must use HTTP or HTTPS.');
|
|
21
|
+
}
|
|
22
|
+
if (host.username || host.password || host.search || host.hash) {
|
|
23
|
+
throw new Error(
|
|
24
|
+
'The SDK host must not contain credentials, a query, or a fragment.'
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
const apiKey = config.apiKey.trim();
|
|
28
|
+
if (apiKey.length === 0) {
|
|
29
|
+
throw new Error('The SDK apiKey must be a project ingest key.');
|
|
30
|
+
}
|
|
31
|
+
const interval = config.sampleIntervalMs ?? 10_000;
|
|
32
|
+
if (!Number.isFinite(interval) || interval > maxIntervalMs) {
|
|
33
|
+
throw new Error('sampleIntervalMs exceeds the native timer range.');
|
|
34
|
+
}
|
|
35
|
+
native.setup(
|
|
36
|
+
host.href,
|
|
37
|
+
apiKey,
|
|
38
|
+
interval,
|
|
39
|
+
config.enableBackgroundSampling ?? false,
|
|
40
|
+
config.autoInjectFCP ?? true,
|
|
41
|
+
config.enableTouchTracking ?? true
|
|
42
|
+
);
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
flushNow(): void {
|
|
46
|
+
native.flushNow();
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export { ThreadBenchmark, type ThreadBenchmarkResult } from './ThreadBenchmark.nitro';
|