Skip to content

Commit

Permalink
Merge pull request #78 from gino247/77-base-price-selection
Browse files Browse the repository at this point in the history
Regional base price selection
  • Loading branch information
alexcasalboni committed May 6, 2020
2 parents d3c0b57 + d0038d5 commit d1219f9
Show file tree
Hide file tree
Showing 7 changed files with 3,202 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
node_modules
jspm_packages
venv
coverage
.nyc_output

# Serverless directories
.serverless
Expand All @@ -13,3 +15,4 @@ venv
serverless.yml

.idea/
.vscode/
12 changes: 7 additions & 5 deletions lambda/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
const utils = require('./utils');

const minRAM = parseInt(process.env.minRAM, 10);
const minCost = parseFloat(process.env.minCost);

/**
* Execute the given function N times in series or in parallel.
Expand Down Expand Up @@ -33,7 +32,10 @@ module.exports.handler = async(event, context) => {
results = await runInSeries(num, lambdaARN, lambdaAlias, payloads);
}

return computeStatistics(results, value);
// get base cost
const baseCost = utils.baseCostForRegion(utils.regionFromARN(lambdaARN));

return computeStatistics(baseCost, results, value);
};

const validateInput = (lambdaARN, value, num) => {
Expand Down Expand Up @@ -134,7 +136,7 @@ const runInSeries = async(num, lambdaARN, lambdaAlias, payloads) => {
return results;
};

const computeStatistics = (results, value) => {
const computeStatistics = (baseCost, results, value) => {
// use results (which include logs) to compute average duration ...

const durations = utils.parseLogAndExtractDurations(results);
Expand All @@ -143,10 +145,10 @@ const computeStatistics = (results, value) => {
console.log('Average duration: ', averageDuration);

// ... and overall statistics
const averagePrice = utils.computePrice(minCost, minRAM, value, averageDuration);
const averagePrice = utils.computePrice(baseCost, minRAM, value, averageDuration);

// .. and total cost (exact $)
const totalCost = utils.computeTotalCost(minCost, minRAM, value, durations);
const totalCost = utils.computeTotalCost(baseCost, minRAM, value, durations);

const stats = {
averagePrice,
Expand Down
26 changes: 22 additions & 4 deletions lambda/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,15 @@ module.exports.range = (n) => {
return Array.from(Array(n).keys());
};

module.exports.lambdaClientFromARN = (lambdaARN) => {
if (typeof lambdaARN !== 'string' || lambdaARN.split(':').length !== 7) {
throw new Error('Invalid ARN: ' + lambdaARN);
module.exports.regionFromARN = (arn) => {
if (typeof arn !== 'string' || arn.split(':').length !== 7) {
throw new Error('Invalid ARN: ' + arn);
}
const region = lambdaARN.split(':')[3];
return arn.split(':')[3];
};

module.exports.lambdaClientFromARN = (lambdaARN) => {
const region = this.regionFromARN(lambdaARN);
return new AWS.Lambda({region});
};

Expand Down Expand Up @@ -305,3 +309,17 @@ module.exports.buildVisualizationURL = (stats, baseURL) => {

return baseURL + '#' + hash;
};

/**
* Using the prices supplied via prices.json,
* to figure what the base price is for the
* supplied lambda's region
*/
module.exports.baseCostForRegion = (region) => {
const prices = JSON.parse(process.env.baseCosts);
if (prices[region]) {
return prices[region];
}
console.log(region + ' not found in base price map, using default: ' + prices['default']);
return prices['default'];
};
Loading

0 comments on commit d1219f9

Please sign in to comment.