Skip to content

Commit a4a93e7

Browse files
committed
Fix benchmark
1 parent be3ac73 commit a4a93e7

File tree

3 files changed

+39
-29
lines changed

3 files changed

+39
-29
lines changed

example/globals.d.ts

+8
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ declare var _LABEL: string | undefined;
1212
* Sets the global console object
1313
*/
1414
declare var _setGlobalConsole: (console: any) => void;
15+
16+
declare var nativeLoggingHook: (message: string, severity: number) => void;
17+
18+
declare var _log: (message: string) => void;
19+
20+
declare var performance: {
21+
now: () => number;
22+
};

example/src/App.tsx

+28-28
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import {
1111
} from 'react-native';
1212
import { spawnThread } from 'react-native-multithreading';
1313
import 'react-native-reanimated';
14-
import { runOnJS, runOnUI } from 'react-native-reanimated';
1514

1615
// calculates the fibonacci number - that can be optimized really good so it's really really fast.
1716
const fibonacci = (num: number): number => {
@@ -55,8 +54,11 @@ const benchmark = () => {
5554
};
5655
};
5756

57+
const BENCHMARK_TIMES = 5;
58+
5859
export default function App() {
59-
const [isBenchmarking, setIsBenchmarking] = React.useState(false);
60+
const [isBenchmarkingCustom, setIsBenchmarkingCustom] = React.useState(false);
61+
const [isBenchmarkingReact, setIsBenchmarkingReact] = React.useState(false);
6062
const [isRunning, setIsRunning] = React.useState(false);
6163
const [input, setInput] = React.useState('5');
6264
const [result, setResult] = React.useState<number | undefined>();
@@ -91,44 +93,45 @@ export default function App() {
9193

9294
const runBenchmark = React.useCallback(
9395
async (thread: 'react' | 'custom' | 'ui') => {
94-
setIsBenchmarking(true);
96+
console.log(
97+
'Starting Benchmark - Please see native console (Xcode Logs/Android Logcat) for output!'
98+
);
9599
switch (thread) {
96100
case 'react': {
97-
for (let i = 0; i < 5; i++) {
101+
setIsBenchmarkingReact(true);
102+
global.nativeLoggingHook(
103+
`REACT_THREAD: Begin blocking React-JS Thread...`,
104+
1
105+
);
106+
for (let i = 0; i < BENCHMARK_TIMES; i++) {
98107
const r = benchmark();
99108
global.nativeLoggingHook(
100-
`REACT: Run #${i}: ${r.result} (took ${r.duration}ms)`,
109+
`REACT_THREAD: Run #${i}: ${r.result} (took ${r.duration}ms)`,
101110
1
102111
);
103112
}
104-
setIsBenchmarking(false);
113+
global.nativeLoggingHook(
114+
`REACT_THREAD: React-JS Thread unblocked!`,
115+
1
116+
);
117+
setIsBenchmarkingReact(false);
105118
break;
106119
}
107120
case 'custom': {
121+
setIsBenchmarkingCustom(true);
108122
await spawnThread(() => {
109123
'worklet';
110-
for (let i = 0; i < 5; i++) {
124+
_log(`CUSTOM_THREAD: Begin blocking Custom Thread...`);
125+
for (let i = 0; i < BENCHMARK_TIMES; i++) {
111126
const r = benchmark();
112127
// can't use console.log because that just dispatches to React-JS thread, which might be blocked.
113-
global._log(
114-
`CUSTOM: Run #${i}: ${r.result} (took ${r.duration}ms)`
128+
_log(
129+
`CUSTOM_THREAD: Run #${i}: ${r.result} (took ${r.duration}ms)`
115130
);
116131
}
132+
_log(`CUSTOM_THREAD: Custom Thread unblocked!`);
117133
});
118-
setIsBenchmarking(false);
119-
break;
120-
}
121-
case 'ui': {
122-
// couldn't manage to get this working, some weird undefined errors
123-
// runOnUI(() => {
124-
// 'worklet';
125-
// for (let i = 0; i < 5; i++) {
126-
// const r = benchmark();
127-
// // can't use console.log because that just dispatches to React-JS thread, which might be blocked.
128-
// global._log(`UI: Run #${i}: ${r.result} (took ${r.duration}ms)`);
129-
// }
130-
// runOnJS(setIsBenchmarking)(false);
131-
// })();
134+
setIsBenchmarkingCustom(false);
132135
break;
133136
}
134137
}
@@ -156,15 +159,12 @@ export default function App() {
156159
title="Run heavy calculation on React-JS Thread"
157160
onPress={() => runBenchmark('react')}
158161
/>
162+
{isBenchmarkingReact && <ActivityIndicator />}
159163
<Button
160164
title="Run heavy calculation on separate Thread"
161165
onPress={() => runBenchmark('custom')}
162166
/>
163-
<Button
164-
title="Run heavy calculation on REA UI Thread"
165-
onPress={() => runBenchmark('ui')}
166-
/>
167-
{isBenchmarking && <ActivityIndicator />}
167+
{isBenchmarkingCustom && <ActivityIndicator />}
168168
</View>
169169
</View>
170170
);

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,9 @@
9999
"root": true,
100100
"globals": {
101101
"_setGlobalConsole": true,
102-
"_LABEL": true
102+
"_LABEL": true,
103+
"_log": true,
104+
"performance": true
103105
},
104106
"extends": [
105107
"@react-native-community",

0 commit comments

Comments
 (0)