Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix e-net and e-mem plugins #62

Merged
merged 12 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion src/__tests__/unit/lib/e-mem/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('lib/e-mem: ', () => {
}
});

it('does not throw an error for a missing `energy-per-gb` but instead uses the default value of 0.38.', async () => {
it('does not throw an error for a missing `energy-per-gb` but instead uses the default value of 0.000392.', async () => {
const data = [
{
timestamp: '2023-11-02T10:35:31.820Z',
Expand All @@ -97,5 +97,39 @@ describe('lib/e-mem: ', () => {
expect(response[0]['memory/energy']).toEqual(expectedMemory);
});
});

describe('execute() with no global config: ', () => {
it('gets energy-memory from fallback.', async () => {
const globalConfig = {};
const eMem = EMem(globalConfig);

const inputs = [
{
'memory/utilization': 80,
'memory/capacity': 16,
duration: 3600,
timestamp: '2022-01-01T01:00:00Z',
},
{
'memory/utilization': 60,
'memory/capacity': 8,
duration: 3600,
timestamp: '2022-01-01T01:00:00Z',
},
];
expect.assertions(3);

const result = await eMem.execute(inputs);

expect(result).toHaveLength(inputs.length);
result.forEach((output, index) => {
expect(output['memory/energy']).toBeCloseTo(
inputs[index]['memory/capacity'] *
(inputs[index]['memory/utilization'] / 100) *
0.000392
);
});
});
});
});
});
33 changes: 33 additions & 0 deletions src/__tests__/unit/lib/e-net/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,36 @@ describe('lib/e-net', () => {
});
});
});

describe('execute() without global config: ', () => {
it('calculates energy for each input.', async () => {
const globalConfig = {};
const eNet = ENet(globalConfig);
const inputs = [
{
'network/data-in': 10,
'network/data-out': 5,
duration: 3600,
timestamp: '2022-01-01T01:00:00Z',
},
{
'network/data-in': 20,
'network/data-out': 15,
duration: 3600,
timestamp: '2022-01-01T01:00:00Z',
},
];

expect.assertions(3);

const result = await eNet.execute(inputs);

expect(result).toHaveLength(inputs.length);
result.forEach((output, index) => {
expect(output['network/energy']).toBeCloseTo(
(inputs[index]['network/data-in'] + inputs[index]['network/data-out']) *
0.001
);
});
});
});
2 changes: 1 addition & 1 deletion src/lib/e-mem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
### Plugin global config

- `energy-per-gb`: a coefficient for energy in kWh per GB. If not provided,
defaults to 0.000392. (optional)
defaults to 0.000392 (optional), take from [this case study](https://www.cloudcarbonfootprint.org/docs/methodology/#memory)

### Inputs

Expand Down
7 changes: 4 additions & 3 deletions src/lib/e-mem/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ export const EMem = (globalConfig: ConfigParams): PluginInterface => {

const validateConfig = () => {
const schema = z.object({
'energy-per-gb': z.number().gt(0),
'energy-per-gb': z.number().gte(0.000392),
});

//Manually add default value from CCF: https://www.cloudcarbonfootprint.org/docs/methodology/#memory
const energyPerGB = globalConfig['energy-per-gb'] ?? 0.000392;
// Manually add default value from CCF: https://www.cloudcarbonfootprint.org/docs/methodology/#memory
const energyPerGB =
(globalConfig && globalConfig['energy-per-gb']) ?? 0.000392;

return validate<z.infer<typeof schema>>(schema, {
...globalConfig,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/e-net/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

### Plugin global config

- `energy-per-gb`: coefficient for converting data transferred to energy, in kWh/GB. The default, if no data or invalid data is provided, is 0.001 kWh/GB, taken from [this case study](https://github.com/Green-Software-Foundation/sci-guide/blob/dev/use-case-submissions/msft-eShoppen.md).
- `energy-per-gb`: coefficient for converting data transferred to energy, in kWh/GB. The default, if no data or invalid data is provided, is 0.001 kWh/GB, taken from [case study 1](https://www.cloudcarbonfootprint.org/docs/methodology/#chosen-coefficient) and [case study 2](https://www.cloudcarbonfootprint.org/docs/methodology/#appendix-iv-recent-networking-studies).

### Inputs

Expand Down
14 changes: 8 additions & 6 deletions src/lib/e-net/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,17 @@ export const ENet = (globalConfig: ConfigParams): PluginInterface => {
*/
const validateConfig = () => {
const schema = z.object({
'energy-per-gb': z.number(),
'energy-per-gb': z.number().gte(0.001),
});

// Manually add default value
if (!globalConfig['energy-per-gb'] || globalConfig['energy-per-gb'] === 0) {
globalConfig['energy-per-gb'] = 0.001;
}
// Manually add default value from CCF: https://www.cloudcarbonfootprint.org/docs/methodology/#chosen-coefficient
const energyPerGB =
(globalConfig && globalConfig['energy-per-gb']) ?? 0.001;

return validate<z.infer<typeof schema>>(schema, globalConfig);
return validate<z.infer<typeof schema>>(schema, {
...globalConfig,
'energy-per-gb': energyPerGB,
});
};

/**
Expand Down
Loading