-
Notifications
You must be signed in to change notification settings - Fork 28
chore: Added a few files to run perf tests #366
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bizob2828
wants to merge
2
commits into
main
Choose a base branch
from
perf-test-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,59 @@ | ||
| /* | ||
| * Copyright 2026 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| const fs = require('node:fs/promises') | ||
| const path = require('path') | ||
| const express = require('express') | ||
| const app = express() | ||
| const PORT = 3001 | ||
| const monitorDir = path.join(__dirname, 'monitor-data') | ||
|
|
||
| app.use(express.static(path.join(__dirname, 'public'))) | ||
|
|
||
| // List all CSV files in the directory | ||
| app.get('/csv-list', async (req, res) => { | ||
| const contents = await fs.readdir(monitorDir) | ||
| const files = contents.filter((file) => file.endsWith('.csv')) | ||
| res.json(files) | ||
| }) | ||
|
|
||
| // Fetch CSV data for a given file | ||
| app.get('/data', async (req, res) => { | ||
| const file = req.query.file | ||
| if (!file || !file.endsWith('.csv')) { | ||
| res.status(400).send('Invalid CSV file') | ||
| return | ||
| } | ||
|
|
||
| let csvPath | ||
| try { | ||
| csvPath = await fs.realpath(path.resolve(monitorDir, file)) | ||
|
|
||
| } catch (err) { | ||
| res.status(404).send(`Failed to find file ${err.message}`) | ||
| return | ||
| } | ||
|
|
||
| const csv = await fs.readFile(csvPath, 'utf8') | ||
|
|
||
| const lines = csv.trim().split('\n') | ||
| const headers = lines.shift().split(',') | ||
| // formats the data into a collection: | ||
| // [ | ||
| // { timestamp, cpu_user_ms, cpu_system_ms, etc } | ||
| // ] | ||
| const data = lines.map((line) => { | ||
| const values = line.split(',') | ||
| const obj = {} | ||
| headers.forEach((h, i) => { | ||
| obj[h] = values[i] | ||
| }) | ||
| return obj | ||
| }) | ||
| res.json({ headers, data }) | ||
| }) | ||
|
|
||
|
|
||
| app.listen(PORT, () => { | ||
| // eslint-disable-next-line no-console | ||
| console.log(`CSV visualizer running at http://localhost:${PORT}`) | ||
| }) | ||
This file contains hidden or 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 hidden or 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 hidden or 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,101 @@ | ||
| <!DOCTYPE html> | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thank you claude, this is good enough for a "front-end" |
||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="UTF-8"> | ||
| <title>V8 Monitor CSV Visualization</title> | ||
| <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | ||
| <style> | ||
| body { font-family: sans-serif; margin: 2em; } | ||
| #controls { margin-bottom: 1em; } | ||
| canvas { max-width: 100%; } | ||
| </style> | ||
| </head> | ||
| <body> | ||
| <h1>V8 Monitor CSV Visualization</h1> | ||
| <div id="controls"> | ||
| <label for="csvSelect">CSV File:</label> | ||
| <select id="csvSelect"></select> | ||
| </div> | ||
| <div id="charts"></div> | ||
| <script> | ||
| async function fetchCsvList() { | ||
| const res = await fetch('/csv-list'); | ||
| return await res.json(); | ||
| } | ||
|
|
||
| async function fetchData(file) { | ||
| const res = await fetch(`/data?file=${encodeURIComponent(file)}`); | ||
| return await res.json(); | ||
| } | ||
|
|
||
| function renderAllCharts(headers, data) { | ||
| const chartsDiv = document.getElementById('charts'); | ||
| chartsDiv.innerHTML = ''; | ||
| const timestamps = data.map(row => row.timestamp); | ||
| headers.forEach(metric => { | ||
| if (metric === 'timestamp') return; | ||
| const values = data.map(row => parseFloat(row[metric])); | ||
| const container = document.createElement('div'); | ||
| container.style.marginBottom = '2em'; | ||
| const label = document.createElement('h3'); | ||
| label.textContent = metric; | ||
| container.appendChild(label); | ||
| const canvas = document.createElement('canvas'); | ||
| canvas.height = 80; | ||
| container.appendChild(canvas); | ||
| chartsDiv.appendChild(container); | ||
| const ctx = canvas.getContext('2d'); | ||
| // Use distinct colors for percent metrics | ||
| let color = 'blue'; | ||
| let yLabel = metric; | ||
| if (metric.endsWith('Percent')) { | ||
| if (metric === 'heapUsedPercent') color = 'green'; | ||
| if (metric === 'cpuUserPercent') color = 'orange'; | ||
| if (metric === 'cpuSystemPercent') color = 'red'; | ||
| yLabel = metric + ' (%)'; | ||
| } | ||
| new Chart(ctx, { | ||
| type: 'line', | ||
| data: { | ||
| labels: timestamps, | ||
| datasets: [{ | ||
| label: metric, | ||
| data: values, | ||
| borderColor: color, | ||
| fill: false, | ||
| }], | ||
| }, | ||
| options: { | ||
| scales: { | ||
| x: { display: true, title: { display: true, text: 'Timestamp' } }, | ||
| y: { display: true, title: { display: true, text: yLabel } }, | ||
| }, | ||
| }, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function main() { | ||
| const csvList = await fetchCsvList(); | ||
| const csvSelect = document.getElementById('csvSelect'); | ||
| csvList.forEach(file => { | ||
| const option = document.createElement('option'); | ||
| option.value = file; | ||
| option.textContent = file; | ||
| csvSelect.appendChild(option); | ||
| }); | ||
| async function loadAndRender(file) { | ||
| const { headers, data } = await fetchData(file); | ||
| renderAllCharts(headers, data); | ||
| } | ||
| csvSelect.addEventListener('change', () => { | ||
| loadAndRender(csvSelect.value); | ||
| }); | ||
| if (csvList.length > 0) { | ||
| loadAndRender(csvList[0]); | ||
| } | ||
| } | ||
| main(); | ||
| </script> | ||
| </body> | ||
| </html> | ||
This file contains hidden or 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,35 @@ | ||
| #!/bin/bash | ||
| RUN_TIME=1 | ||
| commands=("start:perf:no-agent" "start:perf:agent-no-profiling" "start:perf:agent-profiling") | ||
|
|
||
| echo "Removing leftover monitoring files from past runs" | ||
| rm monitor-data/* | ||
|
|
||
| for cmd in "${commands[@]}"; do | ||
| # Start the app in the background | ||
| echo "Starting app with $cmd" | ||
| npm run "$cmd" & | ||
| APP_PID=$! | ||
|
|
||
| # Wait for the server to start (adjust as needed) | ||
| sleep 10 | ||
|
|
||
| echo "Injecting traffic for $RUN_TIME minutes" | ||
| hey -z "$RUN_TIME"m http://localhost:3000/named-route | ||
|
|
||
| # After hey completes, stop the app | ||
| kill $APP_PID | ||
| # Determine output filename | ||
| case "$cmd" in | ||
| "start:perf:no-agent") out="perf-data-no-agent.csv" ;; | ||
| "start:perf:agent-no-profiling") out="perf-data-agent-no-profiling.csv" ;; | ||
| "start:perf:agent-profiling") out="perf-data-agent-profiling.csv" ;; | ||
| esac | ||
|
|
||
| echo "moving file to $out" | ||
| # Find the most recent CSV file in monitor-data and rename it | ||
| # Using /bin/ls because my shell alias ls to some other cli tool | ||
| CSV_FILE=$(/bin/ls -t monitor-data/*.csv | head -n 1) | ||
| mv "$CSV_FILE" monitor-data/"$out" | ||
| echo "done, onto the next" | ||
| done |
This file contains hidden or 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,115 @@ | ||
| /* | ||
| * Copyright 2026 New Relic Corporation. All rights reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| const fs = require('node:fs/promises') | ||
| const path = require('node:path') | ||
|
|
||
| const SAMPLE_INTERVAL = 1000 // ms | ||
| const WARMUP_DURATION = 5000 // ms (default warmup) | ||
| const OUTPUT_FILE = `v8-monitor-${Date.now()}.csv` | ||
| const MONITORING_DIR = path.join(__dirname, 'monitor-data') | ||
| const FILE_PATH = path.join(MONITORING_DIR, OUTPUT_FILE) | ||
|
|
||
| function getCpuUsage(prevCpu) { | ||
| // Get CPU usage delta from process | ||
| const usage = process.cpuUsage(prevCpu) | ||
| const formatted = { | ||
| user: usage.user / 1000, | ||
| system: usage.system / 1000, | ||
| } | ||
| return { | ||
| raw: usage, | ||
| formatted, | ||
| } | ||
| } | ||
|
|
||
| function getMemoryUsage() { | ||
| const mem = process.memoryUsage() | ||
| return { | ||
| rss: mem.rss, | ||
| heapTotal: mem.heapTotal, | ||
| heapUsed: mem.heapUsed, | ||
| } | ||
| } | ||
|
|
||
| async function writeHeader() { | ||
| try { | ||
| await fs.mkdir(MONITORING_DIR) | ||
| } catch {} | ||
|
|
||
| const header = [ | ||
| 'timestamp', | ||
| 'cpu_user_ms', | ||
| 'cpu_system_ms', | ||
| 'cpuUserPercent', | ||
| 'cpuSystemPercent', | ||
| 'rss', | ||
| 'heapTotal', | ||
| 'heapUsed', | ||
| 'heapUsedPercent', | ||
| ].join(',') + '\n' | ||
|
|
||
| return fs.writeFile(FILE_PATH, header) | ||
| } | ||
|
|
||
| async function writeSample(data) { | ||
| const heapUsedPercent = data.memory.heapTotal > 0 ? (data.memory.heapUsed / data.memory.heapTotal) * 100 : 0 | ||
| // cpu.user and cpu.system are now percent per core | ||
| const cpuUserPercent = data.cpu.user | ||
| const cpuSystemPercent = data.cpu.system | ||
| const line = [ | ||
| data.timestamp, | ||
| data.cpu.user, | ||
| data.cpu.system, | ||
| cpuUserPercent, | ||
| cpuSystemPercent, | ||
| data.memory.rss, | ||
| data.memory.heapTotal, | ||
| data.memory.heapUsed, | ||
| heapUsedPercent, | ||
| ].join(',') + '\n' | ||
| return fs.appendFile(FILE_PATH, line) | ||
| } | ||
|
|
||
| let running = false | ||
| async function main() { | ||
| // eslint-disable-next-line no-console | ||
| console.log(`Warming up for ${WARMUP_DURATION} ms...`) | ||
| await writeHeader() | ||
| setTimeout(() => { | ||
| running = true | ||
| const prevCpu = process.cpuUsage() | ||
| process.on('SIGINT', () => { | ||
| running = false | ||
| // eslint-disable-next-line no-console | ||
| console.log('Stopped monitoring.') | ||
| }) | ||
|
|
||
| // eslint-disable-next-line no-console | ||
| console.log('Starting sampling') | ||
| sample({ prevCpu }) | ||
| }, WARMUP_DURATION) | ||
| } | ||
|
|
||
| async function sample({ prevCpu }) { | ||
| const now = new Date().toISOString() | ||
| // Calculate CPU delta since last sample | ||
| const cpuUsage = getCpuUsage(prevCpu) | ||
| // Update prevCpu to the current snapshot for next interval | ||
| prevCpu = process.cpuUsage() | ||
| const memory = getMemoryUsage() | ||
| // CPU percent per core | ||
| const intervalMicros = SAMPLE_INTERVAL * 1000 | ||
| const cpu = { | ||
| user: (cpuUsage.raw.user / intervalMicros) * 100, | ||
| system: (cpuUsage.raw.system / intervalMicros) * 100, | ||
| } | ||
| await writeSample({ timestamp: now, cpu, memory }) | ||
| if (running) { | ||
| setTimeout(sample.bind(null, { prevCpu }), SAMPLE_INTERVAL) | ||
| } | ||
| } | ||
|
|
||
| main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.