-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: export api function instead of running as cli (#12)
* feature: export api function instead of running as cli * test
- Loading branch information
Showing
7 changed files
with
78 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
interface Options { | ||
readonly workerPath: string | ||
readonly playwrightPath: string | ||
readonly threshold: number | ||
} | ||
|
||
export const measureMemory: (options: Options) => Promise<void> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
import * as Main from './parts/Main/Main.ts' | ||
|
||
Main.main() | ||
export * from './parts/Main/Main.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1 @@ | ||
import * as MeasureMemory from '../MeasureMemory/MeasureMemory.ts' | ||
import { parseArgs } from '../ParseArgs/ParseArgs.ts' | ||
import { root } from '../Root/Root.ts' | ||
|
||
export const main = async () => { | ||
const options = parseArgs() | ||
await MeasureMemory.measureMemory({ | ||
headless: options.headless, | ||
port: options.port, | ||
remoteDebuggingPort: '9222', | ||
workerPath: 'TODO', | ||
playwrightPath: 'TODO', | ||
root, | ||
threshold: 400_000, | ||
}) | ||
} | ||
export * from '../MeasureMemory/MeasureMemory.ts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,24 @@ | ||
import { MemoryLimitExceededError } from '../Errors/Errors.ts' | ||
import { getMemoryUsageWs } from '../GetMemoryUsageWs/GetMemoryUsageWs.ts' | ||
import { launchBrowser } from '../LaunchBrowser/LaunchBrowser.ts' | ||
import { startServer } from '../Server/Server.ts' | ||
import { waitForWorkerReady } from '../WaitForWorkerReady/WaitForWorkerReady.ts' | ||
import * as MeasureMemoryInternal from '../MeasureMemoryInternal/MeasureMemoryInternal.ts' | ||
import { parseArgs } from '../ParseArgs/ParseArgs.ts' | ||
import { root } from '../Root/Root.ts' | ||
|
||
export const measureMemory = async ({ | ||
workerPath, | ||
port, | ||
headless, | ||
remoteDebuggingPort, | ||
root, | ||
playwrightPath, | ||
threshold, | ||
}: { | ||
workerPath: string | ||
port: number | ||
headless: boolean | ||
remoteDebuggingPort: string | ||
root: string | ||
playwrightPath: string | ||
threshold: number | ||
}) => { | ||
if (process.platform === 'win32') { | ||
// not supported | ||
return | ||
} | ||
|
||
const server = await startServer(port, workerPath, root) | ||
|
||
const { page, browser } = await launchBrowser( | ||
headless, | ||
remoteDebuggingPort, | ||
const options = parseArgs() | ||
await MeasureMemoryInternal.measureMemoryInternal({ | ||
headless: options.headless, | ||
port: options.port, | ||
remoteDebuggingPort: '9222', | ||
workerPath, | ||
playwrightPath, | ||
) | ||
|
||
try { | ||
await page.goto(`http://localhost:${port}`) | ||
await waitForWorkerReady(page) | ||
|
||
const memoryUsage = await getMemoryUsageWs(remoteDebuggingPort) | ||
console.log('[memory] Worker Memory Usage:', memoryUsage) | ||
if (memoryUsage.usedSize >= threshold) { | ||
throw new MemoryLimitExceededError(threshold, memoryUsage.usedSize) | ||
} | ||
} catch (error) { | ||
console.error('[memory] Measurement failed:', error) | ||
process.exit(1) | ||
} finally { | ||
server.close() | ||
await browser.close() | ||
} | ||
root, | ||
threshold, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { MemoryLimitExceededError } from '../Errors/Errors.ts' | ||
import { getMemoryUsageWs } from '../GetMemoryUsageWs/GetMemoryUsageWs.ts' | ||
import { launchBrowser } from '../LaunchBrowser/LaunchBrowser.ts' | ||
import { startServer } from '../Server/Server.ts' | ||
import { waitForWorkerReady } from '../WaitForWorkerReady/WaitForWorkerReady.ts' | ||
|
||
export const measureMemoryInternal = async ({ | ||
workerPath, | ||
port, | ||
headless, | ||
remoteDebuggingPort, | ||
root, | ||
playwrightPath, | ||
threshold, | ||
}: { | ||
workerPath: string | ||
port: number | ||
headless: boolean | ||
remoteDebuggingPort: string | ||
root: string | ||
playwrightPath: string | ||
threshold: number | ||
}) => { | ||
if (process.platform === 'win32') { | ||
// not supported | ||
return | ||
} | ||
|
||
const server = await startServer(port, workerPath, root) | ||
|
||
const { page, browser } = await launchBrowser( | ||
headless, | ||
remoteDebuggingPort, | ||
playwrightPath | ||
) | ||
|
||
try { | ||
await page.goto(`http://localhost:${port}`) | ||
await waitForWorkerReady(page) | ||
|
||
const memoryUsage = await getMemoryUsageWs(remoteDebuggingPort) | ||
console.log('[memory] Worker Memory Usage:', memoryUsage) | ||
if (memoryUsage.usedSize >= threshold) { | ||
throw new MemoryLimitExceededError(threshold, memoryUsage.usedSize) | ||
} | ||
} catch (error) { | ||
console.error('[memory] Measurement failed:', error) | ||
process.exit(1) | ||
} finally { | ||
server.close() | ||
await browser.close() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters