Skip to content

Commit c18005c

Browse files
committed
chore: benchmark
1 parent d682bc0 commit c18005c

File tree

6 files changed

+73
-14
lines changed

6 files changed

+73
-14
lines changed

.env

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,2 @@
11
API_BASE_URL=http://localhost:3000
2-
DEBUG=true
3-
4-
# s3
5-
STORAGE_DRIVER=s3
6-
STORAGE_S3_BUCKET=test
7-
AWS_ENDPOINT_URL=http://localhost:9000
8-
AWS_ACCESS_KEY_ID=access_key
9-
AWS_SECRET_ACCESS_KEY=secret_key
10-
11-
# postgres
12-
DB_DRIVER=postgres
13-
DB_POSTGRES_URL=postgres://postgres:postgres@localhost:5432/postgres
2+
DEBUG=true

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ data/
1111
.idea
1212
.DS_Store
1313
tests/temp/
14-
actions-runner/
14+
actions-runner/
15+
benchmark.csv

benchmark.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { randomBytes } from 'node:crypto'
2+
import fs from 'node:fs/promises'
3+
import path from 'node:path'
4+
import { restoreCache, saveCache } from '@actions/cache'
5+
import pLimit from 'p-limit'
6+
7+
const TEMP_DIR = './benchmark'
8+
9+
Object.assign(process.env, {
10+
RUNNER_TEMP: path.join(TEMP_DIR, 'runner_temp'),
11+
ACTIONS_RESULTS_URL: 'http://localhost:3000/',
12+
ACTIONS_CACHE_URL: 'http://localhost:3000/',
13+
ACTIONS_CACHE_SERVICE_V2: 'true',
14+
ACTIONS_RUNTIME_TOKEN: 'mock-runtime',
15+
})
16+
17+
const CONCURRENCY = 20
18+
const FILE_SIZE_MB = 500
19+
const TOTAL_REQUESTS = 10
20+
21+
const MOCK_BUFFER = randomBytes(1024 * 1024 * FILE_SIZE_MB)
22+
23+
async function run(id: number) {
24+
const testFilePath = path.join(TEMP_DIR, `test-file-${id}.bin`)
25+
await fs.writeFile(testFilePath, MOCK_BUFFER)
26+
27+
await saveCache([testFilePath], `key`)
28+
await restoreCache([testFilePath], `key`)
29+
}
30+
31+
await fs.mkdir(TEMP_DIR, { recursive: true })
32+
33+
const limiter = pLimit(CONCURRENCY)
34+
35+
const start = Date.now()
36+
await Promise.all(Array.from({ length: TOTAL_REQUESTS }, (_, i) => limiter(() => run(i))))
37+
const duration = Date.now() - start
38+
39+
console.log(`Completed ${TOTAL_REQUESTS} requests in ${duration}ms`)
40+
41+
await fs.rm(TEMP_DIR, { recursive: true, force: true })

lib/schemas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export const envBaseSchema = type({
6161
'DISABLE_CLEANUP_JOBS?': 'boolean',
6262
'DEBUG?': 'boolean',
6363
'ENABLE_DIRECT_DOWNLOADS': 'boolean = false',
64+
'BENCHMARK': 'boolean = false',
6465
})
6566

6667
export const envSchema = envBaseSchema.and(envStorageDriverSchema).and(envDbDriverSchema)

plugins/benchmark.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import cluster from 'node:cluster'
2+
import fs from 'node:fs/promises'
3+
import { env } from '~/lib/env'
4+
import { logger } from '~/lib/logger'
5+
6+
export default defineNitroPlugin(async () => {
7+
if (!env.BENCHMARK || cluster.isPrimary) return
8+
9+
const logFile = 'benchmark.csv'
10+
await fs.writeFile(logFile, 'time,rss_mb,heap_total_mb,heap_used_mb,external_mb\n')
11+
12+
logger.info('Starting benchmark logging to', logFile)
13+
14+
setInterval(async () => {
15+
const mem = process.memoryUsage()
16+
await fs.appendFile(
17+
logFile,
18+
`${Date.now()},${toMB(mem.rss)},${toMB(mem.heapTotal)},${toMB(mem.heapUsed)},${toMB(mem.external)}\n`,
19+
)
20+
21+
if (globalThis.gc) globalThis.gc()
22+
}, 500)
23+
})
24+
25+
function toMB(bytes: number) {
26+
return (bytes / 1024 / 1024).toFixed(2)
27+
}

tests/setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const TESTING_ENV_BASE = {
3131
ACTIONS_CACHE_URL: 'http://localhost:3000/',
3232
} satisfies Omit<
3333
typeof envBaseSchema.infer,
34-
'CACHE_CLEANUP_OLDER_THAN_DAYS' | 'ENABLE_DIRECT_DOWNLOADS'
34+
'CACHE_CLEANUP_OLDER_THAN_DAYS' | 'ENABLE_DIRECT_DOWNLOADS' | 'BENCHMARK'
3535
> &
3636
Record<string, string>
3737

0 commit comments

Comments
 (0)