Skip to content

Commit

Permalink
feat: sort final output list by memory
Browse files Browse the repository at this point in the history
  • Loading branch information
janritter committed Aug 4, 2022
1 parent b6c85e3 commit e5fff7d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
17 changes: 15 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"sort"
"strings"
"time"

Expand Down Expand Up @@ -92,8 +93,9 @@ var rootCmd = &cobra.Command{
helper.LogInfo("Test for %dMB finished", memory)
}

for memory, duration := range durationResults {
helper.LogSuccess("%d MB - Average Duration: %f ms - Cost: %.10f USD", memory, duration, costResults[memory])
sorted := memorySortedList(durationResults)
for _, memory := range sorted {
helper.LogSuccess("%d MB - Average Duration: %f ms - Cost: %.10f USD", memory, durationResults[memory], costResults[memory])
}

helper.LogInfo("Changing Lambda to pre-test memory value of %dMB", resetMemoryValue)
Expand Down Expand Up @@ -222,3 +224,14 @@ func calculateCost(duration float64, memory int) float64 {

return costForMemoryInMilliseconds * duration
}

func memorySortedList(results map[int]float64) []int {
keys := make([]int, 0, len(results))
for key := range results {
keys = append(keys, key)
}

sort.IntSlice(keys).Sort()

return keys
}
19 changes: 19 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,22 @@ func TestCalculateCost(t *testing.T) {
t.Errorf("expected %f, got %f", expected, actual)
}
}

func TestMemorySortedList(t *testing.T) {
expected := []int{128, 256, 384, 512}
actual := memorySortedList(map[int]float64{
384: 0.0,
128: 0.0,
512: 0.0,
256: 0.0,
})

if len(actual) != len(expected) {
t.Errorf("expected %d elements, got %d", len(expected), len(actual))
}
for i := range expected {
if actual[i] != expected[i] {
t.Errorf("expected element %d, got %d", expected[i], actual[i])
}
}
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/janritter/aws-lambda-live-tuner
go 1.18

require (
github.com/aws/aws-sdk-go v1.44.68
github.com/aws/aws-sdk-go v1.44.69
github.com/fatih/color v1.13.0
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.5.0
Expand All @@ -27,7 +27,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.0 // indirect
golang.org/x/sys v0.0.0-20220731174439-a90be440212d // indirect
golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/aws/aws-sdk-go v1.44.68 h1:7zNr5+HLG0TMq+ZcZ8KhT4eT2KyL7v+u7/jANKEIinM=
github.com/aws/aws-sdk-go v1.44.68/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go v1.44.69 h1:3A3DEizrCK6dAbBoRGh8KmoZij7She9snclG1ixY/xQ=
github.com/aws/aws-sdk-go v1.44.69/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down Expand Up @@ -331,6 +333,8 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220731174439-a90be440212d h1:Sv5ogFZatcgIMMtBSTTAgMYsicp25MXBubjXNDKwm80=
golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704 h1:Y7NOhdqIOU8kYI7BxsgL38d0ot0raxvcW+EMQU2QrT4=
golang.org/x/sys v0.0.0-20220803195053-6e608f9ce704/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down

0 comments on commit e5fff7d

Please sign in to comment.