Skip to content

Instantly share code, notes, and snippets.

@KazChe
Last active May 21, 2024 01:55
Show Gist options
  • Save KazChe/8608d68a31a1a90a5656eb1b844e465c to your computer and use it in GitHub Desktop.
Save KazChe/8608d68a31a1a90a5656eb1b844e465c to your computer and use it in GitHub Desktop.
parse various section of a HAR file. uses nodejs
const fs = require('fs');
function readHarFile(filePath) {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) {
console.error('Error reading the file:', err);
return;
}
const harData = JSON.parse(data);
analyzeHarData(harData);
});
}
function analyzeHarData(harData) {
const entries = harData.log.entries;
const totalWaitTime = entries.reduce((acc, entry) => acc + entry.timings.wait, 0);
let totalGetWaitTime = 0, totalPostWaitTime = 0;
let startTimes = [];
let postCallsInfo = [];
let requestTimes = [{endpoint: '', time: 0, method: '', payloadSize: 0}]
entries.forEach(entry => {
if (entry.request.method === 'GET') {
totalGetWaitTime += entry.timings.wait;
postCallsInfo.push(extractPOSTDataFactory(entry));
requestTimes.push({endpoint: entry.request.url, time: entry.time});
} else if (entry.request.method === 'POST') {
totalPostWaitTime += entry.timings.wait;
postCallsInfo.push(extractPOSTDataFactory(entry));
requestTimes.push({endpoint: entry.request.url, time: entry.time, method: entry.request.method, payloadSize: entry.request.postData.text.length});
}
startTimes.push(entry.startedDateTime);
});
let start = new Date(startTimes.shift(0));
let end = new Date(startTimes.pop());
let harDurationMilliseconds = end - start;
let harDurationMinutes = harDurationMilliseconds / 60000;
let metaData = {
"metaData": {
"startTime": start,
"endTime": end,
"duration": harDurationMinutes + ' minutes',
"totalWaitTime": totalWaitTime.toFixed(3) + ' ms',
"totalGetWaitTime": totalGetWaitTime.toFixed(3) + ' ms',
"totalPostWaitTime": totalPostWaitTime.toFixed(3) + ' ms',
}
}
postCallsInfo.push(metaData);
let topFourTimes = {"top4": requestTimes.sort((a, b) => b.time - a.time).slice(0, 5)};
postCallsInfo.push(topFourTimes);
fs.writeFile('postCallsInfo.json', JSON.stringify(postCallsInfo, null, 2), (err) => {
if (err) {
console.error('Error writing the file:', err);
return;
}
console.log('File has been written successfully');
});
}
function extractPOSTDataFactory(harEntry) {
return {
"url": harEntry.request.url,
"method": harEntry.request.method,
"postData": harEntry.request.postData ? harEntry.request.postData.text : 'Not a POST request',
"serverIPAddress": harEntry.serverIPAddress,
"startedDateTime": harEntry.startedDateTime,
"time": harEntry.time,
"timings": {
"blocked": harEntry.timings.blocked,
"dns": harEntry.timings.dns,
"ssl": harEntry.timings.ssl,
"connect": harEntry.timings.connect,
"send": harEntry.timings.send,
"wait": harEntry.timings.wait,
"receive": harEntry.timings.receive,
"_blocked_queueing": harEntry.timings._blocked_queueing
}
}
}
// Replace 'path_to_har_file.har' with the path to your HAR file
readHarFile('path_to_har_file.har');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment