Skip to content

Commit

Permalink
Make replicas and counts clearer (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
obierlaire authored Sep 12, 2023
1 parent 66f8daa commit c448c54
Show file tree
Hide file tree
Showing 17 changed files with 369 additions and 343 deletions.
27 changes: 15 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,21 @@ Download the latest release from [releases page](https://github.com/carboniferio
```bash
$ carbonifer plan

------------------------------ ---------------- ------- ------------------------
resource type name count emissions per instance
------------------------------ ---------------- ------- ------------------------
google_sql_database_instance instance 1 2.1716 gCO2eq/h
google_compute_disk first 1 0.0449 gCO2eq/h
google_compute_instance first 1 43.2803 gCO2eq/h
google_compute_instance second 1 0.4489 gCO2eq/h
google_compute_region_disk regional-first 1 0.0898 gCO2eq/h
google_compute_subnetwork first unsupported
google_compute_network vpc_network unsupported
------------------------------ ---------------- ------- ------------------------
Total 5 46.0356 gCO2eq/h
Average estimation of CO2 emissions per instance:

------------------------------------------- ------- ---------- ------------------------
resource count replicas emissions per instance
------------------------------------------- ------- ---------- ------------------------
google_compute_disk.first 1 1 0.0422 gCO2eq/h
google_compute_instance.first 1 1 33.5977 gCO2eq/h
google_compute_instance.second 1 1 0.4248 gCO2eq/h
google_compute_region_disk.regional-first 1 2 0.0844 gCO2eq/h
google_sql_database_instance.instance 1 2 2.0550 gCO2eq/h
google_compute_subnetwork.first unsupported
google_compute_network.vpc_network unsupported
------------------------------------------- ------- ---------- ------------------------
Total 7 38.3433 gCO2eq/h
------------------------------------------- ------- ---------- ------------------------

```

Expand Down
7 changes: 4 additions & 3 deletions internal/estimate/estimate.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func EstimateResources(resourceList map[string]resources.Resource) estimation.Es
unsupportedResources = append(unsupportedResources, resource)
}

estimationTotal.Power = estimationTotal.Power.Add(estimationResource.Power.Mul(estimationResource.Count))
estimationTotal.CarbonEmissions = estimationTotal.CarbonEmissions.Add(estimationResource.CarbonEmissions.Mul(estimationResource.Count))
estimationTotal.ResourcesCount = estimationTotal.ResourcesCount.Add(estimationResource.Count)
estimationTotal.Power = estimationTotal.Power.Add(estimationResource.Power.Mul(estimationResource.TotalCount))
estimationTotal.CarbonEmissions = estimationTotal.CarbonEmissions.Add(estimationResource.CarbonEmissions.Mul(estimationResource.TotalCount))
estimationTotal.ResourcesCount = estimationTotal.ResourcesCount.Add(estimationResource.TotalCount)
}

return estimation.EstimationReport{
Expand Down Expand Up @@ -94,5 +94,6 @@ func estimateNotSupported(resource resources.UnsupportedResource) *estimation.Es
Power: decimal.Zero,
CarbonEmissions: decimal.Zero,
AverageCPUUsage: decimal.Zero,
TotalCount: decimal.Zero,
}
}
2 changes: 1 addition & 1 deletion internal/estimate/estimate/Energy.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func estimateWattHour(resource *resources.ComputeResource) decimal.Decimal {
storageInWh,
gpuEstimationInWh,
)
replicationFactor := resource.Specs.ReplicationFactor
replicationFactor := resource.Identification.ReplicationFactor
if replicationFactor == 0 {
replicationFactor = 1
}
Expand Down
9 changes: 8 additions & 1 deletion internal/estimate/estimate/Resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,19 @@ func EstimateSupportedResource(resource resources.Resource) *estimation.Estimati
resource.GetIdentification().Count,
)

if resource.GetIdentification().Name == "my_cluster_autoscaled" {
log.Println("my_cluster_autoscaled")
}

count := int64(computeResource.Identification.Count)
replicationFactor := int64(computeResource.Identification.ReplicationFactor)

est := &estimation.EstimationResource{
Resource: &computeResource,
Power: avgWatt.RoundFloor(10),
CarbonEmissions: carbonEmissionPerTime.RoundFloor(10),
AverageCPUUsage: decimal.NewFromFloat(viper.GetFloat64("provider.gcp.avg_cpu_use")).RoundFloor(10),
Count: decimal.NewFromInt(int64(computeResource.Identification.Count)),
TotalCount: decimal.NewFromInt(count * replicationFactor),
}
return est
}
70 changes: 37 additions & 33 deletions internal/estimate/estimate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import (

var resourceGCPComputeBasic = resources.ComputeResource{
Identification: &resources.ResourceIdentification{
Address: "google_compute_instance.machine-name-1",
Name: "machine-name-1",
ResourceType: "type-1",
Provider: providers.GCP,
Region: "europe-west9",
Count: 1,
Address: "google_compute_instance.machine-name-1",
Name: "machine-name-1",
ResourceType: "type-1",
Provider: providers.GCP,
Region: "europe-west9",
ReplicationFactor: 1,
Count: 1,
},
Specs: &resources.ComputeResourceSpecs{
VCPUs: 2,
Expand All @@ -30,12 +31,13 @@ var resourceGCPComputeBasic = resources.ComputeResource{

var resourceGCPComputeCPUType = resources.ComputeResource{
Identification: &resources.ResourceIdentification{
Address: "google_compute_instance.machine-name-2",
Name: "machine-name-2",
ResourceType: "type-1",
Provider: providers.GCP,
Region: "europe-west9",
Count: 1,
Address: "google_compute_instance.machine-name-2",
Name: "machine-name-2",
ResourceType: "type-1",
Provider: providers.GCP,
Region: "europe-west9",
ReplicationFactor: 1,
Count: 1,
},
Specs: &resources.ComputeResourceSpecs{
VCPUs: 2,
Expand All @@ -48,12 +50,13 @@ var resourceGCPComputeCPUType = resources.ComputeResource{

var resourceUnsupportedComputeBasic = resources.ComputeResource{
Identification: &resources.ResourceIdentification{
Address: "unsupported.machine-name-3",
Name: "machine-name-3",
ResourceType: "type-1",
Provider: providers.AZURE,
Region: "europe-west9",
Count: 1,
Address: "unsupported.machine-name-3",
Name: "machine-name-3",
ResourceType: "type-1",
Provider: providers.AZURE,
Region: "europe-west9",
ReplicationFactor: 1,
Count: 1,
},
Specs: &resources.ComputeResourceSpecs{
VCPUs: 2,
Expand All @@ -63,12 +66,13 @@ var resourceUnsupportedComputeBasic = resources.ComputeResource{

var resourceGCPInstanceGroup = resources.ComputeResource{
Identification: &resources.ResourceIdentification{
Address: "google_compute_instance_group.machine-group-1",
Name: "machine-group-1",
ResourceType: "type-1",
Provider: providers.GCP,
Region: "europe-west9",
Count: 3,
Address: "google_compute_instance_group.machine-group-1",
Name: "machine-group-1",
ResourceType: "type-1",
Provider: providers.GCP,
Region: "europe-west9",
ReplicationFactor: 1,
Count: 3,
},
Specs: &resources.ComputeResourceSpecs{
VCPUs: 2,
Expand All @@ -94,7 +98,7 @@ func TestEstimateResource(t *testing.T) {
Power: decimal.NewFromFloat(7.600784000).RoundFloor(10),
CarbonEmissions: decimal.NewFromFloat(0.448446256).RoundFloor(10),
AverageCPUUsage: decimal.NewFromFloat(avgCPUUse),
Count: decimal.NewFromInt(1),
TotalCount: decimal.NewFromInt(1),
},
},
{
Expand All @@ -105,7 +109,7 @@ func TestEstimateResource(t *testing.T) {
Power: decimal.NewFromFloat(9.5565660741),
CarbonEmissions: decimal.NewFromFloat(0.5638373983),
AverageCPUUsage: decimal.NewFromFloat(avgCPUUse),
Count: decimal.NewFromInt(1),
TotalCount: decimal.NewFromInt(1),
},
},
{
Expand All @@ -116,7 +120,7 @@ func TestEstimateResource(t *testing.T) {
Power: decimal.NewFromFloat(7.600784000).RoundFloor(10),
CarbonEmissions: decimal.NewFromFloat(0.448446256).RoundFloor(10),
AverageCPUUsage: decimal.NewFromFloat(avgCPUUse),
Count: decimal.NewFromInt(3),
TotalCount: decimal.NewFromInt(3),
},
},
}
Expand Down Expand Up @@ -148,7 +152,7 @@ func TestEstimateResourceKilo(t *testing.T) {
Power: decimal.NewFromFloat(5472.56448).RoundFloor(10),
CarbonEmissions: decimal.NewFromFloat(232.4745391104).RoundFloor(10),
AverageCPUUsage: decimal.NewFromFloat(avgCPUUse),
Count: decimal.NewFromInt(1),
TotalCount: decimal.NewFromInt(1),
},
},
{
Expand All @@ -159,7 +163,7 @@ func TestEstimateResourceKilo(t *testing.T) {
Power: decimal.NewFromFloat(6880.7275733647).RoundFloor(10),
CarbonEmissions: decimal.NewFromFloat(292.2933073165).RoundFloor(10),
AverageCPUUsage: decimal.NewFromFloat(avgCPUUse),
Count: decimal.NewFromInt(1),
TotalCount: decimal.NewFromInt(1),
},
},
}
Expand Down Expand Up @@ -202,7 +206,7 @@ func EqualsEstimationResource(t *testing.T, expected *estimation.EstimationResou
assert.Equal(t, expected.Power.String(), actual.Power.String())
assert.Equal(t, expected.CarbonEmissions.String(), actual.CarbonEmissions.String())
assert.Equal(t, expected.AverageCPUUsage.String(), actual.AverageCPUUsage.String())
assert.Equal(t, expected.Count.String(), actual.Count.String())
assert.Equal(t, expected.TotalCount.String(), actual.TotalCount.String())

}

Expand All @@ -227,21 +231,21 @@ func TestEstimateResources(t *testing.T) {
Power: decimal.NewFromFloat(7.600784).Round(10),
CarbonEmissions: decimal.NewFromFloat(0.448446256).Round(10),
AverageCPUUsage: decimal.NewFromFloat(avgCPUUse),
Count: decimal.NewFromInt(1),
TotalCount: decimal.NewFromInt(1),
},
{
Resource: &resourceGCPComputeCPUType,
Power: decimal.NewFromFloat(9.5565660741),
CarbonEmissions: decimal.NewFromFloat(0.5638373983),
AverageCPUUsage: decimal.NewFromFloat(avgCPUUse),
Count: decimal.NewFromInt(1),
TotalCount: decimal.NewFromInt(1),
},
{
Resource: &resourceGCPInstanceGroup,
Power: decimal.NewFromFloat(7.600784).Round(10),
CarbonEmissions: decimal.NewFromFloat(0.448446256).Round(10),
AverageCPUUsage: decimal.NewFromFloat(avgCPUUse),
Count: decimal.NewFromInt(3),
TotalCount: decimal.NewFromInt(3),
},
}
SortEstimations(&expectedResources)
Expand Down
2 changes: 1 addition & 1 deletion internal/estimate/estimation/estimation.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type EstimationResource struct {
Power decimal.Decimal `json:"PowerPerInstance"`
CarbonEmissions decimal.Decimal `json:"CarbonEmissionsPerInstance"`
AverageCPUUsage decimal.Decimal
Count decimal.Decimal
TotalCount decimal.Decimal `json:"TotalCount"` // Count * ReplicationFactor
}

// EstimationTotal is the struct that contains the total estimation
Expand Down
10 changes: 5 additions & 5 deletions internal/output/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ func GenerateReportText(report estimation.EstimationReport) string {
tableString.WriteString("\n Average estimation of CO2 emissions per instance: \n\n")

table := tablewriter.NewWriter(tableString)
table.SetHeader([]string{"resource type", "address", "count", "emissions per instance"})
table.SetHeader([]string{"resource", "count", "replicas", "emissions per instance"})

// Default sort
estimations := report.Resources
estimate.SortEstimations(&estimations)

for _, resource := range report.Resources {
table.Append([]string{
resource.Resource.GetIdentification().ResourceType,
resource.Resource.GetAddress(),
fmt.Sprintf("%v", resource.Count),
fmt.Sprintf("%v", resource.Resource.GetIdentification().Count),
fmt.Sprintf("%v", resource.Resource.GetIdentification().ReplicationFactor),
fmt.Sprintf(" %v %v", resource.CarbonEmissions.StringFixed(4), report.Info.UnitCarbonEmissionsTime),
})
}

for _, resource := range report.UnsupportedResources {
table.Append([]string{
resource.GetIdentification().ResourceType,
resource.GetIdentification().Address,
"",
"",
"unsupported",
})
}

table.SetFooter([]string{"", "Total", report.Total.ResourcesCount.String(), fmt.Sprintf(" %v %v", report.Total.CarbonEmissions.StringFixed(4), report.Info.UnitCarbonEmissionsTime)})
table.SetFooter([]string{"Total", report.Total.ResourcesCount.String(), "", fmt.Sprintf(" %v %v", report.Total.CarbonEmissions.StringFixed(4), report.Info.UnitCarbonEmissionsTime)})

// Format
table.SetAutoFormatHeaders(false)
Expand Down
9 changes: 5 additions & 4 deletions internal/plan/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,8 @@ func GetComputeResource(resourceI interface{}, resourceMapping *ResourceMapping,
Address: resourceAddress,
},
Specs: &resources.ComputeResourceSpecs{
HddStorage: decimal.Zero,
SsdStorage: decimal.Zero,
ReplicationFactor: 1,
HddStorage: decimal.Zero,
SsdStorage: decimal.Zero,
},
}

Expand Down Expand Up @@ -278,7 +277,9 @@ func GetComputeResource(resourceI interface{}, resourceMapping *ResourceMapping,
if err != nil {
return nil, errors.Wrapf(err, "Cannot parse replication factor for %v", resourceAddress)
}
computeResource.Specs.ReplicationFactor = int32(intValue)
computeResource.Identification.ReplicationFactor = int32(intValue)
} else {
computeResource.Identification.ReplicationFactor = 1
}

// Add count (case of autoscaling group)
Expand Down
66 changes: 33 additions & 33 deletions internal/plan/test/resources_aws_rds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,53 +27,53 @@ func TestGetResource_RDS(t *testing.T) {
wantResources := map[string]resources.Resource{
"aws_db_instance.first": resources.ComputeResource{
Identification: &resources.ResourceIdentification{
Address: "aws_db_instance.first",
Name: "first",
ResourceType: "aws_db_instance",
Provider: providers.AWS,
Region: "eu-west-3",
Count: 1,
Address: "aws_db_instance.first",
Name: "first",
ResourceType: "aws_db_instance",
Provider: providers.AWS,
Region: "eu-west-3",
Count: 1,
ReplicationFactor: 2,
},
Specs: &resources.ComputeResourceSpecs{
VCPUs: int32(2),
MemoryMb: int32(8192),
ReplicationFactor: 2,
HddStorage: decimal.Zero,
SsdStorage: decimal.NewFromInt(300),
VCPUs: int32(2),
MemoryMb: int32(8192),
HddStorage: decimal.Zero,
SsdStorage: decimal.NewFromInt(300),
},
},
"aws_db_instance.second": resources.ComputeResource{
Identification: &resources.ResourceIdentification{
Address: "aws_db_instance.second",
Name: "second",
ResourceType: "aws_db_instance",
Provider: providers.AWS,
Region: "eu-west-3",
Count: 1,
Address: "aws_db_instance.second",
Name: "second",
ResourceType: "aws_db_instance",
Provider: providers.AWS,
Region: "eu-west-3",
Count: 1,
ReplicationFactor: 1,
},
Specs: &resources.ComputeResourceSpecs{
VCPUs: int32(2),
MemoryMb: int32(8192),
ReplicationFactor: 1,
HddStorage: decimal.Zero,
SsdStorage: decimal.NewFromInt(200),
VCPUs: int32(2),
MemoryMb: int32(8192),
HddStorage: decimal.Zero,
SsdStorage: decimal.NewFromInt(200),
},
},
"aws_db_instance.third": resources.ComputeResource{
Identification: &resources.ResourceIdentification{
Address: "aws_db_instance.third",
Name: "third",
ResourceType: "aws_db_instance",
Provider: providers.AWS,
Region: "eu-west-3",
Count: 1,
Address: "aws_db_instance.third",
Name: "third",
ResourceType: "aws_db_instance",
Provider: providers.AWS,
Region: "eu-west-3",
Count: 1,
ReplicationFactor: 1,
},
Specs: &resources.ComputeResourceSpecs{
VCPUs: int32(2),
MemoryMb: int32(8192),
ReplicationFactor: 1,
HddStorage: decimal.Zero,
SsdStorage: decimal.NewFromInt(300),
VCPUs: int32(2),
MemoryMb: int32(8192),
HddStorage: decimal.Zero,
SsdStorage: decimal.NewFromInt(300),
},
},
}
Expand Down
Loading

0 comments on commit c448c54

Please sign in to comment.