Skip to content

Commit 3baa147

Browse files
javachemeta-codesync[bot]
authored andcommitted
Benchmark native internal JavaScript (#60484)
Summary: Pull Request resolved: #60484 Add Promise-chain, Promise.all fan-out, and serial async/await benchmarks with microtask controls and untimed completion checks. Measure complete batches rather than enqueueing work without draining it. Changelog: [Internal] Differential Revision: D135653667
1 parent 44238d2 commit 3baa147

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

‎packages/react-native/src/private/renderer/runtimescheduler/__tests__/RuntimeScheduler-benchmark-itest.js‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
1313

14+
import type {TestOptions} from '@react-native/fantom/src/Benchmark';
15+
1416
import NativeFantomTestSpecificMethods from '../../../testing/fantom/specs/NativeFantomTestSpecificMethods';
1517
import * as Fantom from '@react-native/fantom';
1618

@@ -60,3 +62,93 @@ Fantom.unstable_benchmark
6062
Fantom.runTask(noop);
6163
}
6264
});
65+
66+
for (const count of [100, 1000]) {
67+
let completed = 0;
68+
let resolvedValues: Array<number> = [];
69+
const expectedValues = Array.from({length: count}, (_, i) => i + 1);
70+
const verifyCompleted = () => {
71+
expect(completed).toBe(count);
72+
};
73+
const options: TestOptions = {
74+
beforeAll: () => {
75+
expect(global.HermesInternal?.hasPromise?.()).toBe(true);
76+
},
77+
beforeEach: () => {
78+
completed = 0;
79+
},
80+
afterEach: verifyCompleted,
81+
};
82+
83+
Fantom.unstable_benchmark
84+
.suite(`Promise microtasks (${count} operations)`, {
85+
minTestExecutionTimeMs: 1000,
86+
minWarmupIterations: 100,
87+
})
88+
.test(
89+
'Promise.then chain',
90+
() => {
91+
Fantom.runTask(() => {
92+
let promise = Promise.resolve(0);
93+
for (let i = 0; i < count; i++) {
94+
promise = promise.then(value => value + 1);
95+
}
96+
void promise.then(value => {
97+
completed = value;
98+
});
99+
});
100+
},
101+
options,
102+
)
103+
.test(
104+
'Promise.all fan-out',
105+
() => {
106+
Fantom.runTask(() => {
107+
const promises = [];
108+
for (let i = 0; i < count; i++) {
109+
promises.push(Promise.resolve(i).then(value => value + 1));
110+
}
111+
void Promise.all(promises).then(values => {
112+
resolvedValues = values;
113+
completed = values.length;
114+
});
115+
});
116+
},
117+
{
118+
...options,
119+
afterEach: () => {
120+
verifyCompleted();
121+
expect(resolvedValues).toEqual(expectedValues);
122+
},
123+
},
124+
)
125+
.test(
126+
'serial async/await',
127+
() => {
128+
Fantom.runTask(() => {
129+
async function run() {
130+
let value = 0;
131+
for (let i = 0; i < count; i++) {
132+
value += await Promise.resolve(1);
133+
}
134+
completed = value;
135+
}
136+
void run();
137+
});
138+
},
139+
options,
140+
)
141+
.test(
142+
'queueMicrotask control',
143+
() => {
144+
Fantom.runTask(() => {
145+
for (let i = 0; i < count; i++) {
146+
queueMicrotask(() => {
147+
completed++;
148+
});
149+
}
150+
});
151+
},
152+
options,
153+
);
154+
}

0 commit comments

Comments
 (0)