Skip to content

Commit

Permalink
feat(cli): show how long cdk deploy steps take (#18230)
Browse files Browse the repository at this point in the history
Keeps track of how long `cdk deploy` steps take. Times synthesis time along with actual deploy time.

Result is something like this:

![Screen Shot 2021-12-30 at 2 32 24 PM](https://user-images.githubusercontent.com/36202692/147782736-bc68d41f-4a9a-4cc9-b623-b7a27e44c10e.png)

No tests because all I'm adding is print statements.
Closes #18213.
 
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
kaizencc authored Jan 4, 2022
1 parent 3d478ca commit 82fa742
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions packages/aws-cdk/lib/cdk-toolkit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ export class CdkToolkit {
return this.watch(options);
}

const startSynthTime = new Date().getTime();
const stacks = await this.selectStacksForDeploy(options.selector, options.exclusively, options.cacheCloudAssembly);
const elapsedSynthTime = new Date().getTime() - startSynthTime;
print('\n✨ Synthesis time: %ss\n', formatTime(elapsedSynthTime));

const requireApproval = options.requireApproval ?? RequireApproval.Broadening;

Expand Down Expand Up @@ -184,12 +187,14 @@ export class CdkToolkit {
}

print('%s: deploying...', colors.bold(stack.displayName));
const startDeployTime = new Date().getTime();

let tags = options.tags;
if (!tags || tags.length === 0) {
tags = tagsForStack(stack);
}

let elapsedDeployTime = 0;
try {
const result = await this.props.cloudFormation.deployStack({
stack,
Expand All @@ -216,9 +221,11 @@ export class CdkToolkit {
: ' ✅ %s';

success('\n' + message, stack.displayName);
elapsedDeployTime = new Date().getTime() - startDeployTime;
print('\n✨ Deployment time: %ss\n', formatTime(elapsedDeployTime));

if (Object.keys(result.outputs).length > 0) {
print('\nOutputs:');
print('Outputs:');

stackOutputs[stack.stackName] = result.outputs;
}
Expand All @@ -228,7 +235,7 @@ export class CdkToolkit {
print('%s.%s = %s', colors.cyan(stack.id), colors.cyan(name), colors.underline(colors.cyan(value)));
}

print('\nStack ARN:');
print('Stack ARN:');

data(result.stackArn);
} catch (e) {
Expand All @@ -246,6 +253,7 @@ export class CdkToolkit {
});
}
}
print('\n✨ Total time: %ss\n', formatTime(elapsedSynthTime + elapsedDeployTime));
}
}

Expand Down Expand Up @@ -847,3 +855,27 @@ export interface Tag {
readonly Key: string;
readonly Value: string;
}

/**
* Formats time in milliseconds (which we get from 'Date.getTime()')
* to a human-readable time; returns time in seconds rounded to 2
* decimal places.
*/
function formatTime(num: number): number {
return roundPercentage(millisecondsToSeconds(num));
}

/**
* Rounds a decimal number to two decimal points.
* The function is useful for fractions that need to be outputted as percentages.
*/
function roundPercentage(num: number): number {
return Math.round(100 * num) / 100;
}

/**
* Given a time in miliseconds, return an equivalent amount in seconds.
*/
function millisecondsToSeconds(num: number): number {
return num / 1000;
}

0 comments on commit 82fa742

Please sign in to comment.