Skip to content

Commit

Permalink
ci: implement energy consumption check (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
pweimann committed Oct 31, 2024
1 parent 2d4860f commit 719c0eb
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 5 deletions.
78 changes: 73 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
name: lpt-application-ui CI

on: [push]
on:
push:
workflow_call:
inputs:
upload:
default: false
required: false
type: boolean

permissions:
actions: read
Expand Down Expand Up @@ -85,8 +92,69 @@ jobs:
uses: green-coding-solutions/eco-ci-energy-estimation@v4
with:
task: display-results
- name: Store Baseline
uses: actions/upload-artifact@v3
- name: Get last successful baseline run ID
id: get-baseline
run: |
BASELINE_RUN_ID=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"repos/${{ github.repository }}/actions/workflows/energy-consumption-baseline.yaml/runs?per_page=1&status=success" \
--jq '.workflow_runs[0].id')
echo "baseline_run_id=${BASELINE_RUN_ID}" >> $GITHUB_OUTPUT
echo "used runId of the energy baseline run: ${BASELINE_RUN_ID}"
env:
GH_TOKEN: ${{ github.token }}
- name: Download Baseline Energy Consumption
uses: actions/download-artifact@v4
with:
name: energy-data
path: ${{ github.workspace }}/baseline-data
github-token: ${{ github.token }}
repository: ${{ github.repository }}
run-id: ${{ steps.get-baseline.outputs.baseline_run_id }}
- name: Compare energy consumption
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
try {
// Read the energy values
const currentData = JSON.parse(fs.readFileSync('/tmp/eco-ci/total-data.json', 'utf8'));
const baselineData = JSON.parse(fs.readFileSync(`${process.env.GITHUB_WORKSPACE}/baseline-data/total-data.json`, 'utf8'));
const currentEnergy = currentData.energy_joules;
const baselineEnergy = baselineData.energy_joules;
// Calculate percentage difference
let percentDiff = ((currentEnergy - baselineEnergy) / baselineEnergy) * 100;
const threshold = 10;
percentDiff = Number.parseFloat(percentDiff).toFixed(2)
// Log results
console.log('Energy Consumption Analysis:');
console.log(`Current: ${Number.parseFloat(currentEnergy).toFixed(2)} joules`);
console.log(`Baseline: ${Number.parseFloat(baselineEnergy).toFixed(2)} joules`);
console.log(`Difference: ${percentDiff}%`);
let message
// Evaluate results
if (percentDiff > threshold) {
message = `Energy consumption increased by ${percentDiff}% (above ${threshold}% threshold)`;
} else if (percentDiff < 0) {
message = `Energy consumption decreased by ${percentDiff}%`;
} else {
message = `Energy consumption increased by ${percentDiff}% (within ${threshold}% threshold)`;
}
console.log(message)
} catch (error) {
console.log(`Error occurred: ${error.message}`);
}
- name: Upload energy data
if: inputs.upload == true
uses: actions/upload-artifact@v4
with:
name: energy-baseline
path: steps.data-total.outputs.data-total-json
name: energy-data
path: /tmp/eco-ci/total-data.json
retention-days: 10
15 changes: 15 additions & 0 deletions .github/workflows/energy-consumption-baseline.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: store energy baseline

on:
# push:
workflow_dispatch:

permissions:
actions: read
contents: read

jobs:
call-ci:
uses: ./.github/workflows/ci.yaml
with:
upload: true
39 changes: 39 additions & 0 deletions scripts/compare-energy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import fs from "fs";

function compareEnergy() {
try {
// Read the energy values
const currentData = JSON.parse(fs.readFileSync('/tmp/eco-ci/total-data.json', 'utf8'));
const baselineData = JSON.parse(fs.readFileSync(`${process.env.GITHUB_WORKSPACE}/baseline-data/total-data.json`, 'utf8'));

const currentEnergy = currentData.energy_joules;
const baselineEnergy = baselineData.energy_joules;

// Calculate percentage difference
let percentDiff = ((currentEnergy - baselineEnergy) / baselineEnergy) * 100;
const threshold = 10;

percentDiff = Number.parseFloat(percentDiff).toFixed(2)
// Log results
console.log('Energy Consumption Analysis:');
console.log(`Current: ${Number.parseFloat(currentEnergy).toFixed(2)} joules`);
console.log(`Baseline: ${Number.parseFloat(baselineEnergy).toFixed(2)} joules`);
console.log(`Difference: ${percentDiff}%`);

let message
// Evaluate results
if (percentDiff > threshold) {
message = `Energy consumption increased by ${percentDiff}% (above ${threshold}% threshold)`;
} else if (percentDiff < 0) {
message = `Energy consumption decreased by ${percentDiff}%`;
} else {
message = `Energy consumption increased by ${percentDiff}% (within ${threshold}% threshold)`;
}
console.log(message)

} catch (error) {
console.log(`Error occurred: ${error.message}`);
}
}

compareEnergy();

0 comments on commit 719c0eb

Please sign in to comment.