Skip to content

Instantly share code, notes, and snippets.

@pwfcurry
Last active October 3, 2022 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pwfcurry/b47c88896e50d3a3d74a03ccd92ac54b to your computer and use it in GitHub Desktop.
Save pwfcurry/b47c88896e50d3a3d74a03ccd92ac54b to your computer and use it in GitHub Desktop.
Find slow appium commands
// Usage:
// node ./slow-appium-commands.js <log file>
//
// If your logs contain ANSI colour tags (ie downloaded from semaphore), first strip with
// sed -r "s/\x1B\[(([0-9]+)(;[0-9]+)*)?[m,K,H,f,J]//g" <log file>
const fs = require("fs");
// any webdriver commands which took longer than this will be logged
const THRESHOLD = 5000; // ms
if (process.argv.length !== 3) {
console.log("expected path to log file");
process.exit(-1);
}
const path = process.argv[2];
let logs = fs.readFileSync(path, { encoding: "utf8" }).split("\n");
let previousTimestamp = undefined;
let previousLine = undefined;
const searching = logs
.map((line, index) => {
return { index, match: line.indexOf("Connected: http") !== -1 };
})
.filter((result) => result.match);
// ignore everything up to the last occurrence of a particular string, which should roughly correspond to the app loaded & the interesting part of the test starting
const startIndex = searching[searching.length - 1].index;
logs = logs.slice(startIndex);
logs.forEach((line, i) => {
const lineNumber = startIndex + i;
if (line.indexOf("webdriver") === -1) {
// only interested in lines logged by the webdriver
return;
}
const split = line.trim().split(" ");
const timestamp = new Date(split[0]);
if (previousTimestamp) {
const ms = timestamp.valueOf();
const duration = ms - previousTimestamp.valueOf();
// check for particularly large time gaps between logs
if (duration > THRESHOLD) {
console.log(`==== Slow… line ${lineNumber}: took ${duration}ms\n`);
console.log(previousLine);
// log the next command invoked… this may have printed some formatted json, so is a little tricky. print up to next occurrence of webdriver, if found
const remainingLogs = logs.slice(i).map((line, index) => ({ line, index }));
const next = remainingLogs
.slice(1) // skip the first line (it will contain 'webdriver')
.find((line) => line.line.indexOf("webdriver") !== -1);
const endIndex = next?.index ?? 1;
console.log(
remainingLogs
.slice(0, endIndex)
.map((line) => line.line)
.join("\n")
);
console.log("\n\n");
}
}
previousTimestamp = timestamp;
previousLine = line;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment