Skip to content

Commit

Permalink
feat(#16): added support for arm architecture in cost calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
janritter committed Sep 7, 2022
1 parent e9efc59 commit 803be32
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
12 changes: 12 additions & 0 deletions changer/lambda.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ func (c *Changer) GetCurrentMemoryValue(lambdaARN string) (int, error) {

return int(*result.MemorySize), nil
}

func (c *Changer) GetArchitecture(lambdaARN string) (string, error) {
result, err := c.lambda.GetFunctionConfiguration(&lambda.GetFunctionConfigurationInput{
FunctionName: aws.String(lambdaARN),
})
if err != nil {
helper.LogError("Failed to get architecture: %s", err)
return "", err
}

return *result.Architectures[0], nil
}
27 changes: 16 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/janritter/aws-lambda-live-tuner/analyzer"
"github.com/janritter/aws-lambda-live-tuner/changer"
"github.com/janritter/aws-lambda-live-tuner/cost"
"github.com/janritter/aws-lambda-live-tuner/helper"
"github.com/janritter/aws-lambda-live-tuner/output"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -53,6 +54,12 @@ var rootCmd = &cobra.Command{
}
helper.LogInfo("Memory value before test start: %d", resetMemoryValue)

architecture, err := changer.GetArchitecture(lambdaARN)
if err != nil {
os.Exit(1)
}
helper.LogInfo("Architecture of Lambda: %s", architecture)

durationResults := make(map[int]float64)
costResults := make(map[int]float64)
for memory := memoryMin; memory <= memoryMax; memory += memoryIncrement {
Expand Down Expand Up @@ -87,10 +94,11 @@ var rootCmd = &cobra.Command{
average := calculateAverageOfMap(invocations)
durationResults[memory] = average

cost := calculateCost(average, memory)
costResults[memory] = cost
costResult := cost.Calculate(average, memory, architecture, getRegionFromARN(lambdaARN))

costResults[memory] = costResult

helper.LogSuccess("[RESULT] Memory: %d MB - Average Duration: %f ms - Cost %.10f USD", memory, average, cost)
helper.LogSuccess("[RESULT] Memory: %d MB - Average Duration: %f ms - Cost %.10f USD", memory, average, costResult)

helper.LogInfo("Test for %dMB finished", memory)
}
Expand Down Expand Up @@ -190,6 +198,11 @@ func validateLambdaARN() {
}
}

func getRegionFromARN(arn string) string {
elements := strings.Split(arn, ":")
return elements[3]
}

func validateMemoryMinValue() {
if memoryMin < 128 {
helper.LogError("Memory min value must be greater than or equal to 128")
Expand Down Expand Up @@ -230,14 +243,6 @@ func calculateAverageOfMap(data map[string]float64) float64 {
return total / float64(len(data))
}

func calculateCost(duration float64, memory int) float64 {
gbSecond := 0.0000166667 // price for eu-central-1 x86

costForMemoryInMilliseconds := (gbSecond / 1024 * float64(memory)) / 1000

return costForMemoryInMilliseconds * duration
}

func memorySortedList(results map[int]float64) []int {
keys := make([]int, 0, len(results))
for key := range results {
Expand Down
25 changes: 25 additions & 0 deletions cost/cost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cost

var standardGBSecondX86 = map[string]float64{
"eu-central-1": 0.0000166667,
}

var standardGBSecondARM = map[string]float64{
"eu-central-1": 0.0000133334,
}

func getGBSecondPriceForArchitectureRegion(architecture, region string) float64 {
if architecture == "arm64" {
return standardGBSecondARM[region]
}

// default is x86
return standardGBSecondX86[region]
}

func Calculate(duration float64, memory int, architecture, region string) float64 {
gbSecond := getGBSecondPriceForArchitectureRegion(architecture, region)
costForMemoryInMilliseconds := (gbSecond / 1024 * float64(memory)) / 1000

return costForMemoryInMilliseconds * duration
}

0 comments on commit 803be32

Please sign in to comment.