From a7b408f06a890d447fc288987d699b18cdc15ade Mon Sep 17 00:00:00 2001 From: pweimann Date: Wed, 30 Oct 2024 11:33:33 +0100 Subject: [PATCH] ci: implement energy consumption check (WIP) --- .github/workflows/ci.yaml | 55 ++++++++++++++++-- .../energy-consumption-baseline.yaml | 15 +++++ scripts/compare-energy.js | 56 +++++++++++++++++++ 3 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/energy-consumption-baseline.yaml create mode 100644 scripts/compare-energy.js diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1150cb1..3cbc46f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -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 @@ -85,8 +92,46 @@ 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: test + run: | + echo "runId: ${{steps.get-baseline.outputs.baseline_run_id}}" + - 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: Debug file existence + run: | + ls -la ${{ github.workspace }}/baseline-data + cat ${{ github.workspace }}/baseline-data/total-data.json || echo "File not found" + - name: Compare energy consumption + run: node scripts/compare-energy.js +# - name: Compare energy consumption +# run: | +# echo "Comparing energy consumption with baseline" +# CURRENT_ENERGY=$(jq '.energy_joules' /tmp/eco-ci/total-data.json) +# BASELINE_ENERGY=$(jq '.energy_joules' ${{ github.workspace }}/baseline-data/total-data.json) +# echo "Current energy consumption: ${CURRENT_ENERGY} joules" +# echo "Baseline energy consumption: ${BASELINE_ENERGY} joules" + - 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 diff --git a/.github/workflows/energy-consumption-baseline.yaml b/.github/workflows/energy-consumption-baseline.yaml new file mode 100644 index 0000000..c8b05e1 --- /dev/null +++ b/.github/workflows/energy-consumption-baseline.yaml @@ -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 diff --git a/scripts/compare-energy.js b/scripts/compare-energy.js new file mode 100644 index 0000000..d1df462 --- /dev/null +++ b/scripts/compare-energy.js @@ -0,0 +1,56 @@ +import fs from "fs"; +import core from "@actions/core"; + +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/energy-data.json`, 'utf8')); + + const currentEnergy = currentData.energy_joules; + const baselineEnergy = baselineData.energy_joules; + + // Calculate percentage difference + const percentDiff = ((currentEnergy - baselineEnergy) / baselineEnergy) * 100; + const threshold = 10; + + // Create summary table + const summary = [ + '### Energy Consumption Comparison 🔋\n', + '| Metric | Value | Unit |', + '| --- | --- | --- |', + `| Current Consumption | ${currentEnergy.toFixed(2)} | joules |`, + `| Baseline Consumption | ${baselineEnergy.toFixed(2)} | joules |`, + `| Difference | ${percentDiff.toFixed(2)} | % |`, + '\n' + ].join('\n'); + + core.summary.addRaw(summary).write(); + + // Log results + console.log('Energy Consumption Analysis:'); + console.log(`Current: ${currentEnergy.toFixed(2)} joules`); + console.log(`Baseline: ${baselineEnergy.toFixed(2)} joules`); + console.log(`Difference: ${percentDiff.toFixed(2)}%`); + + // Evaluate results + if (percentDiff > threshold) { + const message = `Energy consumption increased by ${percentDiff.toFixed(2)}% (above ${threshold}% threshold)`; + core.setFailed(message); + core.summary.addRaw(`🔴 **Result**: Failed - ${message}`).write(); + } else if (percentDiff < 0) { + const message = `Energy consumption decreased by ${Math.abs(percentDiff).toFixed(2)}%`; + core.notice(message); + core.summary.addRaw(`🟢 **Result**: Passed - ${message}`).write(); + } else { + const message = `Energy consumption increased by ${percentDiff.toFixed(2)}% (within ${threshold}% threshold)`; + core.notice(message); + core.summary.addRaw(`🟡 **Result**: Passed - ${message}`).write(); + } + + } catch (error) { + core.setFailed(error.message); + } +} + +compareEnergy();