Skip to content

Commit 82fa742

Browse files
authored
feat(cli): show how long cdk deploy steps take (#18230)
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*
1 parent 3d478ca commit 82fa742

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

packages/aws-cdk/lib/cdk-toolkit.ts

+34-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ export class CdkToolkit {
117117
return this.watch(options);
118118
}
119119

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

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

@@ -184,12 +187,14 @@ export class CdkToolkit {
184187
}
185188

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

188192
let tags = options.tags;
189193
if (!tags || tags.length === 0) {
190194
tags = tagsForStack(stack);
191195
}
192196

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

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

220227
if (Object.keys(result.outputs).length > 0) {
221-
print('\nOutputs:');
228+
print('Outputs:');
222229

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

231-
print('\nStack ARN:');
238+
print('Stack ARN:');
232239

233240
data(result.stackArn);
234241
} catch (e) {
@@ -246,6 +253,7 @@ export class CdkToolkit {
246253
});
247254
}
248255
}
256+
print('\n✨ Total time: %ss\n', formatTime(elapsedSynthTime + elapsedDeployTime));
249257
}
250258
}
251259

@@ -847,3 +855,27 @@ export interface Tag {
847855
readonly Key: string;
848856
readonly Value: string;
849857
}
858+
859+
/**
860+
* Formats time in milliseconds (which we get from 'Date.getTime()')
861+
* to a human-readable time; returns time in seconds rounded to 2
862+
* decimal places.
863+
*/
864+
function formatTime(num: number): number {
865+
return roundPercentage(millisecondsToSeconds(num));
866+
}
867+
868+
/**
869+
* Rounds a decimal number to two decimal points.
870+
* The function is useful for fractions that need to be outputted as percentages.
871+
*/
872+
function roundPercentage(num: number): number {
873+
return Math.round(100 * num) / 100;
874+
}
875+
876+
/**
877+
* Given a time in miliseconds, return an equivalent amount in seconds.
878+
*/
879+
function millisecondsToSeconds(num: number): number {
880+
return num / 1000;
881+
}

0 commit comments

Comments
 (0)