Skip to content

Commit

Permalink
Fix: Update memory calculations
Browse files Browse the repository at this point in the history
* Add interval time for aggregated wattage data
* Incorporate instance memory
  • Loading branch information
gabibeyer committed Apr 26, 2024
1 parent 4d693e1 commit 457664d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
18 changes: 14 additions & 4 deletions pkg/calculator/calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"reflect"
"time"

"github.com/cnkei/gospline"
Expand All @@ -28,7 +29,7 @@ func operationalEmissions(ctx context.Context, interval time.Duration, p *parame
case v1.CPU.String():
return cpu(ctx, interval, p)
case v1.Memory.String():
return memory(ctx, p)
return memory(ctx, interval, p)
case v1.Storage.String():
return errors.New("error storage is not yet being calculated")
case v1.Network.String():
Expand Down Expand Up @@ -91,19 +92,28 @@ func cpu(ctx context.Context, interval time.Duration, p *parameters) error {
// memory is calculated based on the TEADs pkgRAM calculations over
// various memory stress loads. Using the memory usage from the instance
// we can get the estimated Power consumption of the instance
func memory(ctx context.Context, p *parameters) error {
func memory(ctx context.Context, interval time.Duration, p *parameters) error {
logger := log.FromContext(ctx)
var err error

if p.factors.RAMWatt == nil {
if reflect.DeepEqual(p.factors.RAMWatt, emptyWattage) {
return fmt.Errorf("not calculating memory - RAM wattage data not found")
}

p.metric.Energy, err = cubicSplineInterpolation(p.factors.PkgWatt, p.metric.Usage)
if p.metric.Usage == 0 {
return fmt.Errorf("no memory usage found")
}

p.metric.Energy, err = cubicSplineInterpolation(p.factors.RAMWatt, p.metric.Usage)
if err != nil {
return err
}

// RAMWatt is the energy consumption for a single GB of memory, so multiply
// the energy usage by the total instance memoryHours to get the energy consumption
memoryHours := (interval.Minutes() / float64(60)) * p.factors.MemoryGB
p.metric.Energy *= memoryHours

p.metric.Emissions = v1.NewResourceEmission(
p.metric.Energy*p.pue*p.grid,
v1.GCO2eq,
Expand Down
11 changes: 7 additions & 4 deletions pkg/calculator/calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ func params() *parameters {
Wattage: 1.00,
},
},
VCPU: 2.0,
VCPU: 2.0,
MemoryGB: 1,
},
embodiedFactor: 1000,
}
Expand Down Expand Up @@ -322,6 +323,7 @@ func TestCalculateMemory(t *testing.T) {
type testcase struct {
name string
params *parameters
interval time.Duration // this is nanoseconds
emissions float64
energy float64
hasErr bool
Expand All @@ -333,8 +335,9 @@ func TestCalculateMemory(t *testing.T) {
return &testcase{
name: "default t3.micro at 27%",
params: params(),
energy: 0.00040240120731707316,
emissions: 0.0033801701414634144,
interval: 5 * time.Minute,
energy: 3.353343394308943e-05,
emissions: 0.0002816808451219512,
}
}(),
func() *testcase {
Expand All @@ -352,7 +355,7 @@ func TestCalculateMemory(t *testing.T) {
}(),
} {
t.Run(test.name, func(t *testing.T) {
err := memory(context.TODO(), test.params)
err := memory(context.TODO(), test.interval, test.params)
actualEmissions := test.params.metric.Emissions.Value
actualEnergy := test.params.metric.Energy

Expand Down

0 comments on commit 457664d

Please sign in to comment.