From 97c9d992a32fc7da6b0af0049f2b7db654b8715a Mon Sep 17 00:00:00 2001 From: Henry Allen Date: Wed, 30 Oct 2024 10:47:09 -0400 Subject: [PATCH 01/11] Init package.json --- package-lock.json | 9 +++++++ .../package.json | 24 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 packages/react-native-benchmarking-library/package.json diff --git a/package-lock.json b/package-lock.json index e5be8be..7754469 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15586,6 +15586,10 @@ } } }, + "node_modules/react-native-benchmarking-library": { + "resolved": "packages/react-native-benchmarking-library", + "link": true + }, "node_modules/react-native-get-random-values": { "version": "1.11.0", "resolved": "https://registry.npmjs.org/react-native-get-random-values/-/react-native-get-random-values-1.11.0.tgz", @@ -19093,6 +19097,11 @@ "packages/perf_hooks": { "version": "0.0.1" }, + "packages/react-native-benchmarking-library": { + "version": "1.0.0", + "license": "ISC", + "devDependencies": {} + }, "packages/react-native-release-profiler": { "version": "0.0.1" } diff --git a/packages/react-native-benchmarking-library/package.json b/packages/react-native-benchmarking-library/package.json new file mode 100644 index 0000000..f88d29a --- /dev/null +++ b/packages/react-native-benchmarking-library/package.json @@ -0,0 +1,24 @@ +{ + "name": "react-native-benchmarking-library", + "version": "1.0.0", + "description": "Benchmarking utilities for React Native libraries and applications", + "main": "index.js", + "devDependencies": {}, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/rebeckerspecialties/benchmarking-test-app.git" + }, + "keywords": [ + "Benchmarking", + "React", + "Native" + ], + "author": "", + "bugs": { + "url": "https://github.com/rebeckerspecialties/benchmarking-test-app/issues" + }, + "homepage": "https://github.com/rebeckerspecialties/benchmarking-test-app#readme" +} From 825464c83891b7ca8af7355c74781aa4fe3624f2 Mon Sep 17 00:00:00 2001 From: Henry Allen Date: Wed, 30 Oct 2024 10:58:05 -0400 Subject: [PATCH 02/11] Update setup --- package.json | 3 +- .../example/App.js | 1 + .../example/babel.config.js | 16 ++++++++++ .../example/metro.config.js | 18 +++++++++++ .../example/tsconfig.json | 6 ++++ .../package.json | 31 ++++++------------- .../src/index.tsx | 3 ++ 7 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 packages/react-native-benchmarking-library/example/App.js create mode 100644 packages/react-native-benchmarking-library/example/babel.config.js create mode 100644 packages/react-native-benchmarking-library/example/metro.config.js create mode 100644 packages/react-native-benchmarking-library/example/tsconfig.json create mode 100644 packages/react-native-benchmarking-library/src/index.tsx diff --git a/package.json b/package.json index 4748073..00a6c94 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "react-native": "^0.75.0", "react-native-permissions": "^3.10.1", "react-native-wgpu": "^0.1.8", - "three": "^0.168.0" + "three": "^0.168.0", + "react-native-benchmarking-library": "file:./packages/react-native-benchmarking-library" }, "devDependencies": { "@babel/core": "^7.20.0", diff --git a/packages/react-native-benchmarking-library/example/App.js b/packages/react-native-benchmarking-library/example/App.js new file mode 100644 index 0000000..834a527 --- /dev/null +++ b/packages/react-native-benchmarking-library/example/App.js @@ -0,0 +1 @@ +export { default } from './src/App'; diff --git a/packages/react-native-benchmarking-library/example/babel.config.js b/packages/react-native-benchmarking-library/example/babel.config.js new file mode 100644 index 0000000..7a437af --- /dev/null +++ b/packages/react-native-benchmarking-library/example/babel.config.js @@ -0,0 +1,16 @@ +const path = require('path'); +const { getConfig } = require('react-native-builder-bob/babel-config'); +const pkg = require('../package.json'); + +const root = path.resolve(__dirname, '..'); + +module.exports = function (api) { + api.cache(true); + + return getConfig( + { + presets: ['babel-preset-expo'], + }, + { root, pkg } + ); +}; diff --git a/packages/react-native-benchmarking-library/example/metro.config.js b/packages/react-native-benchmarking-library/example/metro.config.js new file mode 100644 index 0000000..ccb291e --- /dev/null +++ b/packages/react-native-benchmarking-library/example/metro.config.js @@ -0,0 +1,18 @@ +const path = require('path'); +const { getDefaultConfig } = require('@expo/metro-config'); +const { getConfig } = require('react-native-builder-bob/metro-config'); +const pkg = require('../package.json'); + +const root = path.resolve(__dirname, '..'); + +/** + * Metro configuration + * https://facebook.github.io/metro/docs/configuration + * + * @type {import('metro-config').MetroConfig} + */ +module.exports = getConfig(getDefaultConfig(__dirname), { + root, + pkg, + project: __dirname, +}); diff --git a/packages/react-native-benchmarking-library/example/tsconfig.json b/packages/react-native-benchmarking-library/example/tsconfig.json new file mode 100644 index 0000000..facc3ec --- /dev/null +++ b/packages/react-native-benchmarking-library/example/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../tsconfig", + "compilerOptions": { + // Avoid expo-cli auto-generating a tsconfig + } +} diff --git a/packages/react-native-benchmarking-library/package.json b/packages/react-native-benchmarking-library/package.json index f88d29a..dae06d7 100644 --- a/packages/react-native-benchmarking-library/package.json +++ b/packages/react-native-benchmarking-library/package.json @@ -1,24 +1,13 @@ { "name": "react-native-benchmarking-library", - "version": "1.0.0", - "description": "Benchmarking utilities for React Native libraries and applications", - "main": "index.js", - "devDependencies": {}, - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/rebeckerspecialties/benchmarking-test-app.git" - }, - "keywords": [ - "Benchmarking", - "React", - "Native" - ], - "author": "", - "bugs": { - "url": "https://github.com/rebeckerspecialties/benchmarking-test-app/issues" - }, - "homepage": "https://github.com/rebeckerspecialties/benchmarking-test-app#readme" + "version": "0.0.0", + "description": "Utilities for benchmarking JavaScript libraries in React Native applications", + "main": "src/index", + "author": " <> ()", + "license": "UNLICENSED", + "homepage": "#readme", + "create-react-native-library": { + "type": "library", + "version": "0.42.2" + } } diff --git a/packages/react-native-benchmarking-library/src/index.tsx b/packages/react-native-benchmarking-library/src/index.tsx new file mode 100644 index 0000000..9e42cf3 --- /dev/null +++ b/packages/react-native-benchmarking-library/src/index.tsx @@ -0,0 +1,3 @@ +export function multiply(a: number, b: number): Promise { + return Promise.resolve(a * b); +} From cfe5dad51ae14acff686a794e4eeb4f1813798d1 Mon Sep 17 00:00:00 2001 From: Henry Allen Date: Wed, 30 Oct 2024 11:14:29 -0400 Subject: [PATCH 03/11] Move files to library --- .../src/Benchmark.tsx | 60 ++++++++++++++++ .../src/GraphicsBenchmark.tsx | 68 +++++++++++++++++++ .../src/JavaScriptEngineVersion.tsx | 11 +++ .../src/WebGpuBenchmark.tsx | 68 +++++++++++++++++++ .../src/index.tsx | 7 +- .../src/types.ts | 3 + .../tsconfig.json | 12 ++++ 7 files changed, 226 insertions(+), 3 deletions(-) create mode 100644 packages/react-native-benchmarking-library/src/Benchmark.tsx create mode 100644 packages/react-native-benchmarking-library/src/GraphicsBenchmark.tsx create mode 100644 packages/react-native-benchmarking-library/src/JavaScriptEngineVersion.tsx create mode 100644 packages/react-native-benchmarking-library/src/WebGpuBenchmark.tsx create mode 100644 packages/react-native-benchmarking-library/src/types.ts create mode 100644 packages/react-native-benchmarking-library/tsconfig.json diff --git a/packages/react-native-benchmarking-library/src/Benchmark.tsx b/packages/react-native-benchmarking-library/src/Benchmark.tsx new file mode 100644 index 0000000..e6a31c4 --- /dev/null +++ b/packages/react-native-benchmarking-library/src/Benchmark.tsx @@ -0,0 +1,60 @@ +import { useCallback, useState } from "react"; +import { Button, Text } from "react-native"; +import { startProfiling, stopProfiling } from "react-native-release-profiler"; + +export interface BenchmarkProps { + name: string; + run: () => Promise; + flamegraphEnabled: boolean; +} + +export const Benchmark: React.FC = ({ + name, + run, + flamegraphEnabled, +}) => { + const [benchmarkRunTime, setBenchmarkRunTime] = useState(null); + const [error, setError] = useState(); + const [running, setRunning] = useState(false); + const [profileLocation, setProfileLocation] = useState(""); + + const onBeginBenchmark = useCallback(async () => { + setRunning(true); + if (flamegraphEnabled) startProfiling(); + const startTime = Date.now(); + await run().catch((err) => { + setError(err); + }); + const timeDelta = Date.now() - startTime; + + if (flamegraphEnabled) { + const profileLocation = await stopProfiling().catch((err) => { + return `${err}`; + }); + setProfileLocation(profileLocation); + console.log(profileLocation); + } + setRunning(false); + setBenchmarkRunTime(timeDelta); + }, [run, flamegraphEnabled]); + + if (error) { + return {`${error}`}; + } + + if (running) { + return Running benchmark, please wait; + } + + if (benchmarkRunTime) { + const completedLabel = `${name}Completed`; + return ( + <> + {benchmarkRunTime} + {profileLocation} + + ); + } + + return