Skip to content

Instantly share code, notes, and snippets.

@seanf
Created October 17, 2022 22:58
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 seanf/f9ec82e9a7f55c9a0ae42679143c90ee to your computer and use it in GitHub Desktop.
Save seanf/f9ec82e9a7f55c9a0ae42679143c90ee to your computer and use it in GitHub Desktop.
Log the slowest steps in a Jenkins build
// Paste this JavaScript into the JS console of your Jenkins build page
// eg https://jenkins.example.com/jenkins/job/MyJob/main/lastSuccessfulBuild/
// to see the slowest (non-paused) steps in your pipeline.
//
// Warning: this may not work for large jobs, or parallel steps.
// Ref: https://issues.jenkins.io/browse/JENKINS-52394
//
// This requires https://plugins.jenkins.io/pipeline-rest-api/
// (for https://github.com/jenkinsci/pipeline-stage-view-plugin/blob/master/rest-api/README.md#pipeline-rest-api-plugin)
const NUMBER_OF_CULPRITS = 10;
// https://stackoverflow.com/a/9763769/14379
function msToTime(s) {
// Pad to 2 or 3 digits, default is 2
var pad = (n, z = 2) => ('00' + n).slice(-z);
return pad(s/3.6e6|0) + ':' + pad((s%3.6e6)/6e4 | 0) + ':' + pad((s%6e4)/1000|0) + '.' + pad(s%1000, 3);
}
const sortBy = (key) => {
return (a, b) => (a[key] > b[key]) ? 1 : ((b[key] > a[key]) ? -1 : 0);
};
const sortByDesc = (key) => {
return (a, b) => (a[key] > b[key]) ? -1 : ((b[key] > a[key]) ? 1 : 0);
}
const jsonUrl = document.location+'/wfapi/describe';
const response = await fetch(jsonUrl);
const json = await response.json();
const stages = json.stages.filter(it => it.pauseDurationMillis == 0).concat().sort(sortByDesc('durationMillis'));
stages.slice(0, NUMBER_OF_CULPRITS)
.forEach(it => console.log(`${it.name}(#${it.id}): ${msToTime(it.durationMillis)}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment