forked from alexcasalboni/aws-lambda-power-tuning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
598 lines (520 loc) · 18.8 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
'use strict';
const AWS = require('aws-sdk');
const url = require('url');
// local reference to this module
const utils = module.exports;
// cost of 6+N state transitions (AWS Step Functions)
module.exports.stepFunctionsCost = (nPower) => +(this.stepFunctionsBaseCost() * (6 + nPower)).toFixed(5);
module.exports.stepFunctionsBaseCost = () => {
const prices = JSON.parse(process.env.sfCosts);
// assume the AWS_REGION variable is set for this function
return this.baseCostForRegion(prices, process.env.AWS_REGION);
};
module.exports.lambdaBaseCost = (region, architecture) => {
const prices = JSON.parse(process.env.baseCosts);
const priceMap = prices[architecture];
if (!priceMap){
throw new Error('Unsupported architecture: ' + architecture);
}
return this.baseCostForRegion(priceMap, region);
};
module.exports.allPowerValues = () => {
const increment = 64;
const powerValues = [];
for (let value = 128; value <= 3008; value += increment) {
powerValues.push(value);
}
return powerValues;
};
/**
* Check whether a Lambda Alias exists or not, and return its data.
*/
module.exports.getLambdaAlias = (lambdaARN, alias) => {
console.log('Checking alias ', alias);
const params = {
FunctionName: lambdaARN,
Name: alias,
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.getAlias(params).promise();
};
/**
* Return true if alias exist, false if it doesn't.
*/
module.exports.verifyAliasExistance = async(lambdaARN, alias) => {
try {
await utils.getLambdaAlias(lambdaARN, alias);
return true;
} catch (error) {
if (error.code === 'ResourceNotFoundException') {
// OK, the alias isn't supposed to exist
console.log('OK, even if missing alias ');
return false;
} else {
console.log('error during alias check:');
throw error; // a real error :)
}
}
};
/**
* Update power, publish new version, and create/update alias.
*/
module.exports.createPowerConfiguration = async(lambdaARN, value, alias) => {
try {
await utils.setLambdaPower(lambdaARN, value);
// wait for functoin update to complete
await utils.waitForFunctionUpdate(lambdaARN);
const {Version} = await utils.publishLambdaVersion(lambdaARN);
const aliasExists = await utils.verifyAliasExistance(lambdaARN, alias);
if (aliasExists) {
await utils.updateLambdaAlias(lambdaARN, alias, Version);
} else {
await utils.createLambdaAlias(lambdaARN, alias, Version);
}
} catch (error) {
if (error.message && error.message.includes('Alias already exists')) {
// shouldn't happen, but nothing we can do in that case
console.log('OK, even if: ', error);
} else {
console.log('error during config creation for value ' + value);
throw error; // a real error :)
}
}
};
/**
* Wait for the function's LastUpdateStatus to become Successful.
* Documentation: https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#functionUpdated-waiter
* Why is this needed? https://aws.amazon.com/blogs/compute/coming-soon-expansion-of-aws-lambda-states-to-all-functions/
*/
module.exports.waitForFunctionUpdate = async(lambdaARN) => {
console.log('Waiting for update to complete');
const params = {
FunctionName: lambdaARN,
$waiter: { // override delay (5s by default)
delay: 0.5,
},
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.waitFor('functionUpdated', params).promise();
};
module.exports.waitForAliasActive = async(lambdaARN, alias) => {
console.log(`Waiting for alias ${alias} to be active`);
const params = {
FunctionName: lambdaARN,
Qualifier: alias,
$waiter: {
// https://aws.amazon.com/blogs/developer/waiters-in-modular-aws-sdk-for-javascript/
// "In v2, there is no direct way to provide maximum wait time for a waiter.
// You need to configure delay and maxAttempts to indirectly suggest the maximum time you want the waiter to run for."
// 10s * 90 is ~15 minutes (max invocation time)
delay: 10,
maxAttempts: 90,
},
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.waitFor('functionActive', params).promise();
};
/**
* Retrieve a given Lambda Function's memory size (always $LATEST version)
*/
module.exports.getLambdaPower = async(lambdaARN) => {
console.log('Getting current power value');
const params = {
FunctionName: lambdaARN,
Qualifier: '$LATEST',
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
const config = await lambda.getFunctionConfiguration(params).promise();
return config.MemorySize;
};
/**
* Retrieve a given Lambda Function's architecture and whether its state is Pending
*/
module.exports.getLambdaConfig = async(lambdaARN, alias) => {
console.log(`Getting current function config for alias ${alias}`);
const params = {
FunctionName: lambdaARN,
Qualifier: alias,
};
let architecture, isPending;
const lambda = utils.lambdaClientFromARN(lambdaARN);
const config = await lambda.getFunctionConfiguration(params).promise();
if (typeof config.Architectures !== 'undefined') {
architecture = config.Architectures[0];
} else {
architecture = 'x86_64';
}
if (typeof config.State !== 'undefined') {
// see https://docs.aws.amazon.com/lambda/latest/dg/functions-states.html
// the most likely state here is Pending, but it could also be
// - Failed: it means the version creation failed (can't do much about it, the invocation will fail anyway)
// - Inactive: it means the version hasn't been invoked for 14 days (can't happen because we always create new versions)
isPending = config.State === 'Pending';
} else {
isPending = false;
}
return {architecture, isPending};
};
/**
* Update a given Lambda Function's memory size (always $LATEST version).
*/
module.exports.setLambdaPower = (lambdaARN, value) => {
console.log('Setting power to ', value);
const params = {
FunctionName: lambdaARN,
MemorySize: parseInt(value, 10),
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.updateFunctionConfiguration(params).promise();
};
/**
* Publish a new Lambda Version (version number will be returned).
*/
module.exports.publishLambdaVersion = (lambdaARN /*, alias*/) => {
console.log('Publishing new version');
const params = {
FunctionName: lambdaARN,
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.publishVersion(params).promise();
};
/**
* Delete a given Lambda Version.
*/
module.exports.deleteLambdaVersion = (lambdaARN, version) => {
console.log('Deleting version ', version);
const params = {
FunctionName: lambdaARN,
Qualifier: version,
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.deleteFunction(params).promise();
};
/**
* Create a new Lambda Alias and associate it with the given Lambda Version.
*/
module.exports.createLambdaAlias = (lambdaARN, alias, version) => {
console.log('Creating Alias ', alias);
const params = {
FunctionName: lambdaARN,
FunctionVersion: version,
Name: alias,
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.createAlias(params).promise();
};
/**
* Create a new Lambda Alias and associate it with the given Lambda Version.
*/
module.exports.updateLambdaAlias = (lambdaARN, alias, version) => {
console.log('Updating Alias ', alias);
const params = {
FunctionName: lambdaARN,
FunctionVersion: version,
Name: alias,
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.updateAlias(params).promise();
};
/**
* Delete a given Lambda Alias.
*/
module.exports.deleteLambdaAlias = (lambdaARN, alias) => {
console.log('Deleting alias ', alias);
const params = {
FunctionName: lambdaARN,
Name: alias,
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.deleteAlias(params).promise();
};
/**
* Invoke a (pre/post-)processor Lambda function and return its output (data.Payload).
*/
module.exports.invokeLambdaProcessor = async(processorARN, payload, preOrPost = 'Pre') => {
const processorData = await utils.invokeLambda(processorARN, null, payload);
if (processorData.FunctionError) {
throw new Error(`${preOrPost}Processor ${processorARN} failed with error ${processorData.Payload} and payload ${JSON.stringify(payload)}`);
}
return processorData.Payload;
};
/**
* Wrapper around Lambda function invocation with pre/post-processor functions.
*/
module.exports.invokeLambdaWithProcessors = async(lambdaARN, alias, payload, preARN, postARN) => {
var actualPayload = payload; // might change based on pre-processor
// first invoke pre-processor, if provided
if (preARN) {
console.log('Invoking pre-processor');
// overwrite payload with pre-processor's output (only if not empty)
const preProcessorOutput = await utils.invokeLambdaProcessor(preARN, payload, 'Pre');
if (preProcessorOutput) {
actualPayload = preProcessorOutput;
}
}
// invoke function to be power-tuned
const invocationResults = await utils.invokeLambda(lambdaARN, alias, actualPayload);
// then invoke post-processor, if provided
if (postARN) {
console.log('Invoking post-processor');
// note: invocation may have failed (invocationResults.FunctionError)
await utils.invokeLambdaProcessor(postARN, invocationResults.Payload, 'Post');
}
return {
actualPayload,
invocationResults,
};
};
/**
* Invoke a given Lambda Function:Alias with payload and return its logs.
*/
module.exports.invokeLambda = (lambdaARN, alias, payload) => {
console.log(`Invoking function ${lambdaARN}:${alias || '$LATEST'} with payload ${JSON.stringify(payload)}`);
const params = {
FunctionName: lambdaARN,
Qualifier: alias,
Payload: payload,
LogType: 'Tail', // will return logs
};
const lambda = utils.lambdaClientFromARN(lambdaARN);
return lambda.invoke(params).promise();
};
/**
* Fetch the body of an S3 object, given an S3 path such as s3://BUCKET/KEY
*/
module.exports.fetchPayloadFromS3 = async(s3Path) => {
console.log('Fetch payload from S3', s3Path);
if (typeof s3Path !== 'string' || s3Path.indexOf('s3://') === -1) {
throw new Error('Invalid S3 path, not a string in the format s3://BUCKET/KEY');
}
const URI = url.parse(s3Path);
URI.pathname = decodeURIComponent(URI.pathname || '');
const bucket = URI.hostname;
const key = URI.pathname.slice(1);
if (!bucket || !key) {
throw new Error(`Invalid S3 path: "${s3Path}" (bucket: ${bucket}, key: ${key})`);
}
const data = await utils._fetchS3Object(bucket, key);
try {
// try to parse into JSON object
return JSON.parse(data);
} catch (_) {
// otherwise return as is
return data;
}
};
module.exports._fetchS3Object = async(bucket, key) => {
const s3 = new AWS.S3();
try {
const response = await s3.getObject({
Bucket: bucket,
Key: key,
}).promise();
return response.Body.toString('utf-8');
} catch (err) {
if (err.statusCode === 403) {
throw new Error(
`Permission denied when trying to read s3://${bucket}/${key}. ` +
'You might need to re-deploy the app with the correct payloadS3Bucket parameter.',
);
} else if (err.statusCode === 404) {
throw new Error(
`The object s3://${bucket}/${key} does not exist. ` +
'Make sure you are trying to access an existing object in the correct bucket.',
);
} else {
throw new Error(`Unknown error when trying to read s3://${bucket}/${key}. ${err.message}`);
}
}
};
/**
* Generate a list of `num` payloads (repeated or weighted)
*/
module.exports.generatePayloads = (num, payloadInput) => {
if (Array.isArray(payloadInput)) {
// if array, generate a list of payloads based on weights
// fail if empty list or missing weight/payload
if (payloadInput.length === 0 || payloadInput.some(p => !p.weight || !p.payload)) {
throw new Error('Invalid weighted payload structure');
}
if (num < payloadInput.length) {
throw new Error(`You have ${payloadInput.length} payloads and only "num"=${num}. Please increase "num".`);
}
// we use relative weights (not %), so here we compute the total weight
const total = payloadInput.map(p => p.weight).reduce((a, b) => a + b, 0);
// generate an array of num items (to be filled)
const payloads = utils.range(num);
// iterate over weighted payloads and fill the array based on relative weight
let done = 0;
for (let i = 0; i < payloadInput.length; i++) {
const p = payloadInput[i];
var howMany = Math.floor(p.weight * num / total);
if (howMany < 1) {
throw new Error('Invalid payload weight (num is too small)');
}
// make sure the last item fills the remaining gap
if (i === payloadInput.length - 1) {
howMany = num - done;
}
// finally fill the list with howMany items
payloads.fill(utils.convertPayload(p.payload), done, done + howMany);
done += howMany;
}
return payloads;
} else {
// if not an array, always use the same payload (still generate a list)
const payloads = utils.range(num);
payloads.fill(utils.convertPayload(payloadInput), 0, num);
return payloads;
}
};
/**
* Convert payload to string, if it's not a string already
*/
module.exports.convertPayload = (payload) => {
/**
* Return true only if the input is a JSON-encoded string.
* For example, '"test"' or '{"key": "value"}'.
*/
const isJsonString = (s) => {
if (typeof s !== 'string')
return false;
try {
JSON.parse(s);
} catch (e) {
return false;
}
return true;
};
// optionally convert everything into string
if (typeof payload !== 'undefined' && !isJsonString(payload)) {
// note: 'just a string' becomes '"just a string"'
console.log('Converting payload to JSON string from ', typeof payload);
payload = JSON.stringify(payload);
}
return payload;
};
/**
* Compute average price, given average duration.
*/
module.exports.computePrice = (minCost, minRAM, value, duration) => {
// it's just proportional to ms (ceiled) and memory value
return Math.ceil(duration) * minCost * (value / minRAM);
};
module.exports.parseLogAndExtractDurations = (data) => {
return data.map(log => {
const logString = utils.base64decode(log.LogResult || '');
return utils.extractDuration(logString);
});
};
/**
* Compute total cost
*/
module.exports.computeTotalCost = (minCost, minRAM, value, durations) => {
if (!durations || !durations.length) {
return 0;
}
// compute corresponding cost for each duration
const costs = durations.map(duration => utils.computePrice(minCost, minRAM, value, duration));
// sum all together
return costs.reduce((a, b) => a + b, 0);
};
/**
* Compute average duration
*/
module.exports.computeAverageDuration = (durations, discardTopBottom) => {
if (!durations || !durations.length) {
return 0;
}
// a percentage of durations will be discarded (trimmed mean)
const toBeDiscarded = parseInt(durations.length * discardTopBottom, 10);
if (discardTopBottom > 0 && toBeDiscarded === 0) {
// not an error, but worth logging
// this happens when you have less than 5 invocations
// (only happens if dryrun or in tests)
console.log('not enough results to discard');
}
const newN = durations.length - 2 * toBeDiscarded;
// compute trimmed mean (discard a percentage of low/high values)
const averageDuration = durations
.sort(function(a, b) { return a - b; }) // sort numerically
.slice(toBeDiscarded, toBeDiscarded > 0 ? -toBeDiscarded : durations.length) // discard first/last values
.reduce((a, b) => a + b, 0) // sum all together
/ newN
;
return averageDuration;
};
/**
* Extract duration (in ms) from a given Lambda's CloudWatch log.
*/
module.exports.extractDuration = (log) => {
const regex = /\tBilled Duration: (\d+) ms/m;
const match = regex.exec(log);
if (match == null) return 0;
return parseInt(match[1], 10);
};
/**
* Encode a given string to base64.
*/
module.exports.base64decode = (str) => {
return Buffer.from(str, 'base64').toString();
};
/**
* Generate a list of size n.
*/
module.exports.range = (n) => {
if (n === null || typeof n === 'undefined') {
n = -1;
}
return Array.from(Array(n).keys());
};
module.exports.regionFromARN = (arn) => {
if (typeof arn !== 'string' || arn.split(':').length !== 7) {
throw new Error('Invalid ARN: ' + arn);
}
return arn.split(':')[3];
};
module.exports.lambdaClientFromARN = (lambdaARN) => {
const region = this.regionFromARN(lambdaARN);
return new AWS.Lambda({region});
};
/**
* Generate a URL with encoded stats.
* Note: the URL hash is never sent to the server.
*/
module.exports.buildVisualizationURL = (stats, baseURL) => {
function encode(inputList, EncodeType = null) {
EncodeType = EncodeType || Float32Array;
inputList = new EncodeType(inputList);
inputList = new Uint8Array(inputList.buffer);
return Buffer.from(inputList).toString('base64');
}
// sort by power
stats.sort((p1, p2) => {
return p1.power - p2.power;
});
const sizes = stats.map(p => p.power);
const times = stats.map(p => p.duration);
const costs = stats.map(p => p.cost);
const hash = [
encode(sizes, Int16Array),
encode(times),
encode(costs),
].join(';');
return baseURL + '#' + hash;
};
/**
* Using the prices supplied,
* to figure what the base price is for the
* supplied region.
*/
module.exports.baseCostForRegion = (priceMap, region) => {
if (priceMap[region]) {
return priceMap[region];
}
console.log(region + ' not found in base price map, using default: ' + priceMap['default']);
return priceMap['default'];
};
module.exports.sleep = async(sleepBetweenRunsMs) => {
await new Promise(resolve => setTimeout(resolve, sleepBetweenRunsMs));
};