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..262e663 --- /dev/null +++ b/scripts/compare-energy.js @@ -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();