Skip to content

Commit

Permalink
Test fixes and adding interpolation
Browse files Browse the repository at this point in the history
Signed-off-by: Gnanakeethan Balasubramaniam <[email protected]>
  • Loading branch information
gnanakeethan committed Sep 1, 2023
1 parent 57597d1 commit 7a8a5aa
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 49 deletions.
22 changes: 11 additions & 11 deletions src/lib/teads-aws/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {describe, expect, jest, test} from '@jest/globals';
import {TEADSEngineeringAWS} from './index';
import {TeadsAWS} from './index';

jest.setTimeout(30000);

describe('teads:configure test', () => {
test('initialize with params', async () => {
const impactModel = new TEADSEngineeringAWS();
const impactModel = new TeadsAWS();
await impactModel.configure('test', {
instance_type: 't2.micro',
});
Expand All @@ -15,13 +15,13 @@ describe('teads:configure test', () => {
])
).resolves.toStrictEqual([
{
e: 0.004900000000000001,
m: 0.04216723744292237 * 1000,
energy: 0.004900000000000001,
embodied: 0.04216723744292237 * 1000,
},
]);
});
test('teads:initialize with params', async () => {
const impactModel = new TEADSEngineeringAWS();
const impactModel = new TeadsAWS();
await impactModel.configure('test', {
instance_type: 'm5n.large',
});
Expand All @@ -45,16 +45,16 @@ describe('teads:configure test', () => {
])
).resolves.toStrictEqual([
{
e: 0.0067,
m: 91.94006849315068,
energy: 0.0067,
embodied: 91.94006849315068,
},
{
e: 0.011800000000000001,
m: 91.94006849315068,
energy: 0.011800000000000001,
embodied: 91.94006849315068,
},
{
e: 0.016300000000000002,
m: 91.94006849315068,
energy: 0.016300000000000002,
embodied: 91.94006849315068,
},
]);
});
Expand Down
96 changes: 58 additions & 38 deletions src/lib/teads-aws/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {IImpactModelInterface} from '../interfaces';
import Spline from 'typescript-cubic-spline';
import * as AWS_INSTANCES from './aws-instances.json';
import * as AWS_EMBODIED from './aws-embodied.json';
import {KeyValuePair} from '../../types/boavizta';
import { IImpactModelInterface } from "../interfaces";

Check failure on line 1 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Replace `·IImpactModelInterface·}·from·"../interfaces"` with `IImpactModelInterface}·from·'../interfaces'`

Check warning on line 1 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
import Spline from "typescript-cubic-spline";

Check failure on line 2 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"typescript-cubic-spline"` with `'typescript-cubic-spline'`

Check warning on line 2 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
import * as AWS_INSTANCES from "./aws-instances.json";

Check failure on line 3 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"./aws-instances.json"` with `'./aws-instances.json'`

Check warning on line 3 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
import * as AWS_EMBODIED from "./aws-embodied.json";

Check failure on line 4 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"./aws-embodied.json"` with `'./aws-embodied.json'`

Check warning on line 4 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote
import { KeyValuePair } from "../../types/boavizta";

Check failure on line 5 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Replace `·KeyValuePair·}·from·"../../types/boavizta"` with `KeyValuePair}·from·'../../types/boavizta'`

Check warning on line 5 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Strings must use singlequote

export enum Interpolation {
SPLINE = 'spline',
LINEAR = 'linear',
SPLINE = "spline",

Check failure on line 8 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"spline"` with `'spline'`
LINEAR = "linear",

Check failure on line 9 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"linear"` with `'linear'`
}

export class TeadsAWS implements IImpactModelInterface {
Expand All @@ -20,7 +20,7 @@ export class TeadsAWS implements IImpactModelInterface {
} = {};

// list of all the by Architecture
private instanceType = '';
private instanceType = "";

Check failure on line 23 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Replace `""` with `''`
private expectedLifespan = 4;
private interpolation = Interpolation.SPLINE;

Expand All @@ -41,6 +41,7 @@ export class TeadsAWS implements IImpactModelInterface {
* @param {Object} staticParams static parameters for the resource
* @param {string} staticParams.instance_type instance type from the list of supported instances
* @param {number} staticParams.expected_lifespan expected lifespan of the instance in years
* @param {Interpolation} staticParams.interpolation expected lifespan of the instance in years
*/
async configure(
name: string,
Expand All @@ -49,24 +50,28 @@ export class TeadsAWS implements IImpactModelInterface {
this.name = name;

if (staticParams === undefined) {
throw new Error('Required Parameters not provided');
throw new Error("Required Parameters not provided");

Check failure on line 53 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"Required·Parameters·not·provided"` with `'Required·Parameters·not·provided'`
}

if ('instance_type' in staticParams) {
if ("instance_type" in staticParams) {

Check failure on line 56 in src/lib/teads-aws/index.ts

View workflow job for this annotation

GitHub Actions / build

Replace `"instance_type"` with `'instance_type'`
const instanceType = staticParams?.instance_type as string;
if (instanceType in this.computeInstances) {
this.instanceType = instanceType;
} else {
throw new Error('Instance Type not supported');
throw new Error("Instance Type not supported");
}
} else {
throw new Error('Instance Type not provided');
throw new Error("Instance Type not provided");
}

if ('expected_lifespan' in staticParams) {
if ("expected_lifespan" in staticParams) {
this.expectedLifespan = staticParams?.expected_lifespan as number;
}

if ("interpolation" in staticParams) {
this.interpolation = staticParams?.interpolation as Interpolation;
}

return this;
}

Expand All @@ -83,11 +88,11 @@ export class TeadsAWS implements IImpactModelInterface {
observations: object | object[] | undefined
): Promise<object> {
if (observations === undefined) {
throw new Error('Required Parameters not provided');
throw new Error("Required Parameters not provided");
}

if (this.instanceType === '') {
throw new Error('Configuration is incomplete');
if (this.instanceType === "") {
throw new Error("Configuration is incomplete");
}

const results: KeyValuePair[] = [];
Expand All @@ -98,7 +103,7 @@ export class TeadsAWS implements IImpactModelInterface {

results.push({
energy: e,
embodied: m,
embodied: m
});
});
}
Expand All @@ -118,20 +123,20 @@ export class TeadsAWS implements IImpactModelInterface {
*/
private calculateEnergy(observation: KeyValuePair) {
if (
!('duration' in observation) ||
!('cpu-util' in observation) ||
!('datetime' in observation)
!("duration" in observation) ||
!("cpu" in observation) ||
!("datetime" in observation)
) {
throw new Error(
'Required Parameters duration,cpu,datetime not provided for observation'
"Required Parameters duration,cpu,datetime not provided for observation"
);
}

// duration is in seconds
const duration = observation['duration'];
const duration = observation["duration"];

// convert cpu usage to percentage
const cpu = observation['cpu'] * 100.0;
const cpu = observation["cpu"] * 100.0;

// get the wattage for the instance type

Expand All @@ -141,12 +146,27 @@ export class TeadsAWS implements IImpactModelInterface {
this.computeInstances[this.instanceType].consumption.idle ?? 0,
this.computeInstances[this.instanceType].consumption.tenPercent ?? 0,
this.computeInstances[this.instanceType].consumption.fiftyPercent ?? 0,
this.computeInstances[this.instanceType].consumption.hundredPercent ?? 0,
this.computeInstances[this.instanceType].consumption.hundredPercent ?? 0
];

const spline = new Spline(x, y);

const wattage = spline.at(cpu);
let wattage = 0.0;
if (this.interpolation === Interpolation.SPLINE) {
wattage = spline.at(cpu);
} else if (this.interpolation === Interpolation.LINEAR) {
let min = 0
let max = 1
if (cpu > 10 && cpu <= 50) {
min = 1
max = 2
}
if (cpu > 50 && cpu <= 100) {
min = 2
max = 3
}
wattage = y[0] + ((y[max] - y[min]) / (x[max] - x[min])) * cpu;
}
// duration is in seconds
// wattage is in watts
// eg: 30W x 300s = 9000 J
Expand All @@ -163,7 +183,7 @@ export class TeadsAWS implements IImpactModelInterface {
* Returns model identifier
*/
modelIdentifier(): string {
return 'teads.cloud.sci';
return "teads.cloud.sci";
}

/**
Expand All @@ -173,26 +193,26 @@ export class TeadsAWS implements IImpactModelInterface {
*/
standardizeInstanceMetrics() {
AWS_INSTANCES.forEach((instance: KeyValuePair) => {
const cpus = parseInt(instance['Instance vCPU'], 10);
this.computeInstances[instance['Instance type']] = {
const cpus = parseInt(instance["Instance vCPU"], 10);
this.computeInstances[instance["Instance type"]] = {
consumption: {
idle: parseFloat(instance['Instance @ Idle'].replace(',', '.')),
tenPercent: parseFloat(instance['Instance @ 10%'].replace(',', '.')),
idle: parseFloat(instance["Instance @ Idle"].replace(",", ".")),
tenPercent: parseFloat(instance["Instance @ 10%"].replace(",", ".")),
fiftyPercent: parseFloat(
instance['Instance @ 50%'].replace(',', '.')
instance["Instance @ 50%"].replace(",", ".")
),
hundredPercent: parseFloat(
instance['Instance @ 100%'].replace(',', '.')
),
instance["Instance @ 100%"].replace(",", ".")
)
},
vCPUs: cpus,
maxvCPUs: parseInt(instance['Platform Total Number of vCPU'], 10),
name: instance['Instance type'],
maxvCPUs: parseInt(instance["Platform Total Number of vCPU"], 10),
name: instance["Instance type"]
} as KeyValuePair;
});
AWS_EMBODIED.forEach((instance: KeyValuePair) => {
this.computeInstances[instance['type']].embodiedEmission =
instance['total'];
this.computeInstances[instance["type"]].embodiedEmission =
instance["total"];
});
}

Expand All @@ -201,7 +221,7 @@ export class TeadsAWS implements IImpactModelInterface {
*/
private embodiedEmissions(observation: KeyValuePair): number {
// duration
const durationInHours = observation['duration'] / 3600;
const durationInHours = observation["duration"] / 3600;
// M = TE * (TR/EL) * (RR/TR)
// Where:
// TE = Total Embodied Emissions, the sum of Life Cycle Assessment(LCA) emissions for all hardware components
Expand Down

0 comments on commit 7a8a5aa

Please sign in to comment.