Skip to content

Commit

Permalink
feature: export api function instead of running as cli (#12)
Browse files Browse the repository at this point in the history
* feature: export api function instead of running as cli

* test
  • Loading branch information
levivilet authored Dec 19, 2024
1 parent 9cf58ca commit 1129872
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 62 deletions.
4 changes: 3 additions & 1 deletion scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const getGitTagFromGit = async () => {
['describe', '--exact-match', '--tags'],
{
reject: false,
},
}
)
if (exitCode) {
if (
Expand Down Expand Up @@ -72,8 +72,10 @@ delete packageJson.prettier
delete packageJson.jest
packageJson.version = version
packageJson.main = 'dist/index.js'
packageJson.types = 'dist/index.d.js'

await writeJson(join(dist, 'package.json'), packageJson)

await cp(join(root, 'README.md'), join(dist, 'README.md'))
await cp(join(root, 'LICENSE'), join(dist, 'LICENSE'))
await cp(join(root, 'src', 'main.d.ts'), join(dist, 'dist/index.d.ts'))
7 changes: 7 additions & 0 deletions src/main.d.ts
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>
4 changes: 1 addition & 3 deletions src/main.ts
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'
17 changes: 1 addition & 16 deletions src/parts/Main/Main.ts
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'
53 changes: 12 additions & 41 deletions src/parts/MeasureMemory/MeasureMemory.ts
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,
})
}
53 changes: 53 additions & 0 deletions src/parts/MeasureMemoryInternal/MeasureMemoryInternal.ts
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()
}
}
2 changes: 1 addition & 1 deletion test/Main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { expect, test } from '@jest/globals'
import * as Main from '../src/parts/Main/Main.ts'

test('main', () => {
expect(typeof Main.main).toBe('function')
expect(typeof Main.measureMemory).toBe('function')
})

0 comments on commit 1129872

Please sign in to comment.