Skip to content

Commit

Permalink
A fix the result output to README.md file.
Browse files Browse the repository at this point in the history
  • Loading branch information
daveads committed Jul 22, 2024
1 parent 42f461f commit e2cec2a
Showing 1 changed file with 84 additions and 40 deletions.
124 changes: 84 additions & 40 deletions analyze.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
Expand All @@ -16,6 +15,7 @@ function extractMetric(file, metric) {
}

function average(values) {
if (values.length === 0) return 0;
const sum = values.reduce((a, b) => parseFloat(a) + parseFloat(b), 0);
return sum / values.length;
}
Expand All @@ -42,10 +42,12 @@ servers.forEach((server, idx) => {
const latencyVals = [];
for (let j = 0; j < 3; j++) {
const fileIdx = startIdx + j;
const reqSec = extractMetric(resultFiles[fileIdx], "Requests/sec");
const latency = extractMetric(resultFiles[fileIdx], "Latency");
if (reqSec !== null) reqSecVals.push(reqSec);
if (latency !== null) latencyVals.push(latency);
if (fileIdx < resultFiles.length) {
const reqSec = extractMetric(resultFiles[fileIdx], "Requests/sec");
const latency = extractMetric(resultFiles[fileIdx], "Latency");
if (reqSec !== null) reqSecVals.push(reqSec);
if (latency !== null) latencyVals.push(latency);
}
}
avgReqSecs[server] = average(reqSecVals);
avgLatencies[server] = average(latencyVals);
Expand All @@ -54,25 +56,43 @@ servers.forEach((server, idx) => {
const reqSecData = "/tmp/reqSec.dat";
const latencyData = "/tmp/latency.dat";

fs.writeFileSync(reqSecData, "Server Value\n" + servers.map(server => `${server} ${avgReqSecs[server]}`).join('\n'));
fs.writeFileSync(latencyData, "Server Value\n" + servers.map(server => `${server} ${avgLatencies[server]}`).join('\n'));
try {
fs.writeFileSync(reqSecData, "Server Value\n" + servers.map(server => `${server} ${avgReqSecs[server]}`).join('\n'));
fs.writeFileSync(latencyData, "Server Value\n" + servers.map(server => `${server} ${avgLatencies[server]}`).join('\n'));
} catch (error) {
console.error(`Error writing data files: ${error.message}`);
}

let whichBench = 1;
if (resultFiles[0].startsWith("bench2")) {
whichBench = 2;
} else if (resultFiles[0].startsWith("bench3")) {
whichBench = 3;
if (resultFiles.length > 0) {
if (resultFiles[0].startsWith("bench2")) {
whichBench = 2;
} else if (resultFiles[0].startsWith("bench3")) {
whichBench = 3;
}
}

const reqSecHistogramFile = `req_sec_histogram${whichBench}.png`;
const latencyHistogramFile = `latency_histogram${whichBench}.png`;

function getMaxValue(data) {
return Math.max(...data.split('\n').slice(1).map(line => parseFloat(line.split(' ')[1])));
try {
return Math.max(...data.split('\n').slice(1).map(line => parseFloat(line.split(' ')[1])));
} catch (error) {
console.error(`Error getting max value: ${error.message}`);
return 0;
}
}

const reqSecMax = getMaxValue(fs.readFileSync(reqSecData, 'utf-8')) * 1.2;
const latencyMax = getMaxValue(fs.readFileSync(latencyData, 'utf-8')) * 1.2;
let reqSecMax, latencyMax;
try {
reqSecMax = getMaxValue(fs.readFileSync(reqSecData, 'utf-8')) * 1.2;
latencyMax = getMaxValue(fs.readFileSync(latencyData, 'utf-8')) * 1.2;
} catch (error) {
console.error(`Error reading data files: ${error.message}`);
reqSecMax = 0;
latencyMax = 0;
}

const gnuplotScript = `
set term pngcairo size 1280,720 enhanced font 'Courier,12'
Expand All @@ -94,19 +114,26 @@ plot '${latencyData}' using 2:xtic(1) title 'Latency'
`;

const gnuplotScriptFile = '/tmp/gnuplot_script.gp';
fs.writeFileSync(gnuplotScriptFile, gnuplotScript);
try {
fs.writeFileSync(gnuplotScriptFile, gnuplotScript);
} catch (error) {
console.error(`Error writing gnuplot script: ${error.message}`);
}

try {
execSync(`gnuplot ${gnuplotScriptFile}`, { stdio: 'inherit' });
console.log('Gnuplot executed successfully');
} catch (error) {
console.error('Error executing gnuplot:', error.message);
process.exit(1);
}

const assetsDir = path.join(__dirname, "assets");
if (!fs.existsSync(assetsDir)) {
fs.mkdirSync(assetsDir);
try {
fs.mkdirSync(assetsDir);
} catch (error) {
console.error(`Error creating assets directory: ${error.message}`);
}
}

function moveFile(source, destination) {
Expand Down Expand Up @@ -138,11 +165,15 @@ const lastServerReqSecs = avgReqSecs[lastServer];

const resultsFile = "results.md";

if (!fs.existsSync(resultsFile) || fs.readFileSync(resultsFile, 'utf8').trim() === '') {
fs.writeFileSync(resultsFile, `<!-- PERFORMANCE_RESULTS_START -->
try {
if (!fs.existsSync(resultsFile) || fs.readFileSync(resultsFile, 'utf8').trim() === '') {
fs.writeFileSync(resultsFile, `<!-- PERFORMANCE_RESULTS_START -->
| Query | Server | Requests/sec | Latency (ms) | Relative |
|-------:|--------:|--------------:|--------------:|---------:|`);
}
} catch (error) {
console.error(`Error initializing results file: ${error.message}`);
}

let resultsTable = "";
Expand All @@ -169,30 +200,43 @@ sortedServers.forEach((server) => {
resultsTable += `\n|| [${formattedServerNames[server]}] | \`${formattedReqSecs}\` | \`${formattedLatencies}\` | \`${relativePerformance}x\` |`;
});

fs.appendFileSync(resultsFile, resultsTable);
try {
fs.appendFileSync(resultsFile, resultsTable);
} catch (error) {
console.error(`Error appending to results file: ${error.message}`);
}

if (whichBench === 3) {
fs.appendFileSync(resultsFile, "\n\n<!-- PERFORMANCE_RESULTS_END -->");

const finalResults = fs
.readFileSync(resultsFile, "utf-8")
.replace(/(\r\n|\n|\r)/gm, "\\n");

const readmePath = "README.md";
let readmeContent = fs.readFileSync(readmePath, "utf-8");
const performanceResultsRegex =
/<!-- PERFORMANCE_RESULTS_START -->[\s\S]*<!-- PERFORMANCE_RESULTS_END -->/;
if (performanceResultsRegex.test(readmeContent)) {
readmeContent = readmeContent.replace(
performanceResultsRegex,
finalResults
);
} else {
readmeContent += `\n${finalResults}`;
try {
fs.appendFileSync(resultsFile, "\n\n<!-- PERFORMANCE_RESULTS_END -->");

const finalResults = fs
.readFileSync(resultsFile, "utf-8")
.replace(/\\/g, ''); // Remove backslashes

const readmePath = "README.md";
let readmeContent = fs.readFileSync(readmePath, "utf-8");
const performanceResultsRegex =
/<!-- PERFORMANCE_RESULTS_START -->[\s\S]*<!-- PERFORMANCE_RESULTS_END -->/;
if (performanceResultsRegex.test(readmeContent)) {
readmeContent = readmeContent.replace(
performanceResultsRegex,
finalResults
);
} else {
readmeContent += `\n${finalResults}`;
}
fs.writeFileSync(readmePath, readmeContent);
console.log("README.md updated successfully");
} catch (error) {
console.error(`Error updating README: ${error.message}`);
}
fs.writeFileSync(readmePath, readmeContent);
}

resultFiles.forEach((file) => {
fs.unlinkSync(file);
});
try {
fs.unlinkSync(file);
} catch (error) {
console.error(`Error deleting file ${file}: ${error.message}`);
}
});

1 comment on commit e2cec2a

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Query Server Requests/sec Latency (ms) Relative
1 { posts { id userId title user { id name email }}}
[Tailcall] 29,179.11 3.41 109.48x
[Hasura] 4,910.47 20.34 18.42x
[async-graphql] 1,811.70 55.16 6.80x
[Caliban] 1,520.78 65.50 5.71x
[GraphQL JIT] 1,343.20 74.14 5.04x
[Gqlgen] 758.44 130.78 2.85x
[Netflix DGS] 356.70 173.25 1.34x
[Apollo GraphQL] 266.53 369.32 1.00x
2 { posts { title }}
[Tailcall] 59,157.51 1.68 42.98x
[async-graphql] 9,221.97 10.99 6.70x
[Caliban] 9,104.00 11.33 6.61x
[Hasura] 6,073.04 16.54 4.41x
[Gqlgen] 2,163.21 47.93 1.57x
[Apollo GraphQL] 1,757.48 56.81 1.28x
[Netflix DGS] 1,586.96 70.13 1.15x
[GraphQL JIT] 1,376.33 72.54 1.00x
3 { greet }
[Caliban] 67,685.19 1.06 12.97x
[Tailcall] 59,947.79 1.68 11.48x
[async-graphql] 47,440.29 2.19 9.09x
[Gqlgen] 47,354.57 5.01 9.07x
[Netflix DGS] 8,190.13 14.70 1.57x
[Apollo GraphQL] 7,963.28 12.69 1.53x
[Hasura] 6,499.69 15.56 1.25x
[GraphQL JIT] 5,220.34 19.13 1.00x

Please sign in to comment.