Skip to content

Commit 40db84e

Browse files
committed
feat: profiling setup
1 parent 2d0c3ff commit 40db84e

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

apps/new/src/plugin-demos/properties.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Button, EventData, Page, Switch, View, getViewById, Observable, Label, PropertyChangeData } from '@nativescript/core';
2+
import { jamieProfiler } from '@nativescript/core/profiling/jamie';
23

34
export function navigatingTo(args: EventData) {
45
const page = <Page>args.object;
@@ -47,11 +48,22 @@ export class DemoModel extends Observable {
4748

4849
this.target.addEventListener(Observable.propertyChangeEvent, onPropertyChange, null);
4950

51+
jamieProfiler.flush();
52+
53+
console.log('BEGIN PROFILE');
5054
const time = profile(() => {
5155
for (let i = 0; i < 1000000; i++) {
5256
this.target.setProperty(propName, i);
5357
}
5458
});
59+
console.log('END PROFILE');
60+
61+
console.log(
62+
jamieProfiler
63+
.report(jamieProfiler.flush())
64+
.map(([key, value]) => `${key}: ${value} ms`)
65+
.join('\n')
66+
);
5567

5668
this.target.removeEventListener(Observable.propertyChangeEvent, onPropertyChange, null);
5769

apps/old/src/plugin-demos/properties.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
import { Button, EventData, Page, Switch, View, getViewById, Observable, Label, PropertyChangeData } from '@nativescript/core';
22

3+
class Profiler {
4+
private map: Record<string, number> = {};
5+
6+
profile<T>(key: string, action: () => T) {
7+
const start = global.isIOS ? (global as any).performance.now() : __time();
8+
const returnValue = action();
9+
const stop = global.isIOS ? (global as any).performance.now() : __time();
10+
const period = stop - start;
11+
12+
this.map[key] = (this.map[key] || 0) + period;
13+
14+
// console.log(`[PROFILE] ${key}: ${stop - start} ms`);
15+
return returnValue;
16+
}
17+
18+
flush() {
19+
const map = this.map;
20+
this.map = {};
21+
return map;
22+
}
23+
24+
get(key: string) {
25+
return this.map[key];
26+
}
27+
28+
report(map: Record<string, number> = this.map) {
29+
return Object.entries(map).sort(([, valueA], [, valueB]) => {
30+
return sortDescending(valueA, valueB);
31+
});
32+
}
33+
}
34+
35+
function sortDescending(a: number, b: number): 1 | 0 | -1 {
36+
return a < b ? 1 : a > b ? -1 : 0;
37+
}
38+
39+
const jamieProfiler = new Profiler();
40+
341
export function navigatingTo(args: EventData) {
442
const page = <Page>args.object;
543

@@ -47,11 +85,22 @@ export class DemoModel extends Observable {
4785

4886
this.target.addEventListener(Observable.propertyChangeEvent, onPropertyChange, null);
4987

88+
jamieProfiler.flush();
89+
90+
console.log('BEGIN PROFILE');
5091
const time = profile(() => {
5192
for (let i = 0; i < 1000000; i++) {
5293
this.target.setProperty(propName, i);
5394
}
5495
});
96+
console.log('END PROFILE');
97+
98+
console.log(
99+
jamieProfiler
100+
.report(jamieProfiler.flush())
101+
.map(([key, value]) => `${key}: ${value} ms`)
102+
.join('\n')
103+
);
55104

56105
this.target.removeEventListener(Observable.propertyChangeEvent, onPropertyChange, null);
57106

packages/core/profiling/jamie.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Profiler {
2+
private map: Record<string, number> = {};
3+
4+
profile<T>(key: string, action: () => T) {
5+
const start = global.isIOS ? (global as any).performance.now() : __time();
6+
const returnValue = action();
7+
const stop = global.isIOS ? (global as any).performance.now() : __time();
8+
const period = stop - start;
9+
10+
this.map[key] = (this.map[key] || 0) + period;
11+
12+
// console.log(`[PROFILE] ${key}: ${stop - start} ms`);
13+
return returnValue;
14+
}
15+
16+
flush() {
17+
const map = this.map;
18+
this.map = {};
19+
return map;
20+
}
21+
22+
get(key: string) {
23+
return this.map[key];
24+
}
25+
26+
report(map: Record<string, number> = this.map) {
27+
return Object.entries(map).sort(([, valueA], [, valueB]) => {
28+
return sortDescending(valueA, valueB);
29+
});
30+
}
31+
}
32+
33+
function sortDescending(a: number, b: number): 1 | 0 | -1 {
34+
return a < b ? 1 : a > b ? -1 : 0;
35+
}
36+
37+
export const jamieProfiler = new Profiler();

0 commit comments

Comments
 (0)