Skip to content

Commit

Permalink
Merge branch 'main' into no-workflow-id-call
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-mm committed Aug 14, 2023
2 parents 05bfe35 + fc0c473 commit c9f3744
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 63 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ on:
permissions:
contents: read
actions: read
pull-requests: write

jobs:
test-action:
Expand Down Expand Up @@ -49,3 +50,4 @@ jobs:
uses: ./
with:
task: display-results
pr-comment: true
1 change: 1 addition & 0 deletions .github/workflows/update-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ on:
description: The tag to update
required: true
options:
- v2.1
- v2
- v1

Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ jobs:
- automatically false if send-data is also false
- `pr-comment`: (optional) (default: false)
- used with display-results
- if on, will post a comment on the PR issue with the Eco-CI results
- if on, will post a comment on the PR issue with the Eco-CI results. only occurs if the triggering event is a pull_request
- remember to set `pull-requests: write` to true in your workflow file


#### Continuing on Errors

Expand Down Expand Up @@ -168,7 +170,7 @@ jobs:
To use Eco-CI in your gitlab pipeline, you must first include a reference to the eco-ci-gitlab.yml file as such:
```
include:
remote: 'https://github.com/green-coding-berlin/eco-ci-energy-estimation/blob/main/eco-ci-gitlab.yml'
remote: 'https://raw.githubusercontent.com/green-coding-berlin/eco-ci-energy-estimation/main/eco-ci-gitlab.yml'
```

and you call the various scripts in your pipeline with call like this:
Expand All @@ -190,21 +192,23 @@ variables:
ECO_CI_SEND_DATA: "false"
```

Then, for each job you need to export the artifacts:
Then, for each job you need to export the artifacts. We currently export the pipeline data as a regular artifact, as well as make use of Gitlab's [Metric Report](https://docs.gitlab.com/ee/ci/testing/metrics_reports.html) artifact (which we output to the default metrics.txt):

```
artifacts:
paths:
- eco-ci-output.txt
- eco-ci-total-data.json
reports:
metrics: metrics.txt
```

Here is a sample .gitlab-ci.yml example file to illustrate:

```
image: ubuntu:22.04
include:
remote: 'https://github.com/green-coding-berlin/eco-ci-energy-estimation/blob/main/eco-ci-gitlab.yml'
remote: 'https://raw.githubusercontent.com/green-coding-berlin/eco-ci-energy-estimation/main/eco-ci-gitlab.yml'
stages:
- test
Expand All @@ -228,6 +232,8 @@ test-job:
artifacts:
paths:
- eco-ci-output.txt
reports:
metrics: metrics.txt
```


Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ runs:
env:
PR_NUMBER: ${{ github.event.pull_request.number }}
run: |
COMMENT=$(cat "/tmp/eco-ci/output-multiline.txt")
COMMENT=$(cat "/tmp/eco-ci/output-pr.txt" | jq -Rs '.')
API_URL="${{ github.api_url }}/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments"
curl -X POST -H "Authorization: Bearer ${{github.token}}" -d @- $API_URL <<EOF
{
"body": "$COMMENT"
"body": $COMMENT
}
EOF
22 changes: 19 additions & 3 deletions eco-ci-gitlab.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,30 @@ cache:
- echo 'running eco-ci measure script'
- |
repo=$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME
/tmp/eco-ci/main/scripts/make_measurement.sh -l "$ECO_CI_LABEL" -r $CI_PIPELINE_ID -b $CI_COMMIT_REF_NAME -R $repo -c $CI_COMMIT_SHA -sd $ECO_CI_SEND_DATA -s "gitlab"
/tmp/eco-ci/main/scripts/make_measurement.sh \
-l "$ECO_CI_LABEL" \
-r $CI_PIPELINE_ID \
-b $CI_COMMIT_REF_NAME \
-R $repo \
-c $CI_COMMIT_SHA \
-sd $ECO_CI_SEND_DATA \
-s "gitlab" \
.display_results:
script:
- echo 'running eco-ci display script'
- |
set -x
FORMAT_CLR="\e[44m" && TXT_CLEAR="\e[0m"
repo=$CI_PROJECT_NAMESPACE/$CI_PROJECT_NAME
/tmp/eco-ci/main/scripts/display_results.sh -b $CI_COMMIT_REF_NAME -db $ECO_CI_DISPLAY_BADGE -r $CI_PIPELINE_ID -R $repo -dt $ECO_CI_DISPLAY_TABLE -dg $ECO_CI_DISPLAY_GRAPH -sd $ECO_CI_SEND_DATA -s "gitlab"
/tmp/eco-ci/main/scripts/display_results.sh \
-b $CI_COMMIT_REF_NAME \
-db $ECO_CI_DISPLAY_BADGE \
-r $CI_PIPELINE_ID \
-R $repo \
-dt $ECO_CI_DISPLAY_TABLE \
-dg $ECO_CI_DISPLAY_GRAPH \
-sd $ECO_CI_SEND_DATA \
-s "gitlab" \
- echo -e "$FORMAT_CLR$(cat /tmp/eco-ci/output.txt)$TXT_CLEAR"
- cp /tmp/eco-ci/output.txt ./eco-ci-output.txt
- cp /tmp/eco-ci/output.txt ./eco-ci-output.txt
104 changes: 50 additions & 54 deletions scripts/display_results.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ source "$(dirname "$0")/vars.sh" read_vars

function display_results {
output="/tmp/eco-ci/output.txt"
output_multiline="/tmp/eco-ci/output-multiline.txt"
output_pr="/tmp/eco-ci/output-pr.txt"

if [[ $MEASUREMENT_RAN != true ]]; then
echo "Running a measurement to have at least one result to display."
Expand All @@ -29,72 +29,68 @@ function display_results {
fi
fi

if [[ ${display_table} == 'true' ]]; then
cpu_avg=$(awk '{ total += $1; count++ } END { print total/count }' /tmp/eco-ci/cpu-util-total.txt)
total_energy=$(awk '{sum+=$1} END {print sum}' /tmp/eco-ci/energy-total.txt)
power_avg=$(awk '{ total += $1; count++ } END { print total/count }' /tmp/eco-ci/energy-total.txt)
time=$(($(date +%s) - $(cat /tmp/eco-ci/timer-total.txt)))

# get series of measurement values
for varname in ${!measurement_@}; do
# Extract the measurement number from the variable name
measurement_number="${varname#*_}"
# Extract the value of the current measurement variable
measurement_value="${!varname}"
# Use eval to assign individual variables based on the measurement value
eval "label_${measurement_number}=$(echo $measurement_value | awk -F'[,:]' '{print $2}')"
eval "cpu_avg_${measurement_number}=$(echo $measurement_value | awk -F'[,:]' '{print $4}')"
eval "total_energy_${measurement_number}=$(echo $measurement_value | awk -F'[,:]' '{print $6}')"
eval "power_avg_${measurement_number}=$(echo $measurement_value | awk -F'[,:]' '{print $8}')"
eval "time_${measurement_number}=$(echo $measurement_value | awk -F'[,:]' '{print $10}')"

max_measurement_number=$measurement_number
done

echo "Eco-Ci Output:<br/><br/>" >> $output_multiline
echo "Total Energy [Joules]: $total_energy<br/>" >> $output_multiline
echo "Total Avg. CPU Utilization: $cpu_avg<br/>" >> $output_multiline
echo "Total Avg. Power [Watts]: $power_avg<br/>" >> $output_multiline
echo "Total Duration [seconds]: $time<br/>" >> $output_multiline
echo "--------------------------------<br/>" >> $output_multiline
cpu_avg=$(awk '{ total += $1; count++ } END { print total/count }' /tmp/eco-ci/cpu-util-total.txt)
total_energy=$(awk '{sum+=$1} END {print sum}' /tmp/eco-ci/energy-total.txt)
power_avg=$(awk '{ total += $1; count++ } END { print total/count }' /tmp/eco-ci/energy-total.txt)
time=$(($(date +%s) - $(cat /tmp/eco-ci/timer-total.txt)))

# Get series of measurement values
for varname in ${!measurement_@}; do
# Extract the measurement number from the variable name
measurement_number="${varname#*_}"
# Extract the value of the current measurement variable
measurement_value="${!varname}"

# Use eval to assign individual variables based on the measurement value
eval "label_${measurement_number}=$(echo $measurement_value | awk -F'[,:]' '{print $2}')"
eval "cpu_avg_${measurement_number}=$(echo $measurement_value | awk -F'[,:]' '{print $4}')"
eval "total_energy_${measurement_number}=$(echo $measurement_value | awk -F'[,:]' '{print $6}')"
eval "power_avg_${measurement_number}=$(echo $measurement_value | awk -F'[,:]' '{print $8}')"
eval "time_${measurement_number}=$(echo $measurement_value | awk -F'[,:]' '{print $10}')"

max_measurement_number=$measurement_number
done

## Gitlab Specific Output
if [[ $source == 'gitlab' ]]; then
echo "\"$CI_JOB_NAME: Energy [Joules]:\" $total_energy" | tee -a $output metrics.txt
echo "\"$CI_JOB_NAME: Avg. CPU Utilization:\" $cpu_avg" | tee -a $output metrics.txt
echo "\"$CI_JOB_NAME: Avg. Power [Watts]:\" $power_avg" | tee -a $output metrics.txt
echo "\"$CI_JOB_NAME: Duration [seconds]:\" $time" | tee -a $output metrics.txt
echo "----------------" >> $output

for (( i=1; i<=$max_measurement_number; i++ )); do
echo "Label $i: $(eval echo \$label_$i)<br/>" >> $output_multiline
echo "Energy Used [Joules]: $(eval echo \$total_energy_$i)<br/>" >> $output_multiline
echo "Avg. CPU Utilization: $(eval echo \$cpu_avg_$i)<br/>" >> $output_multiline
echo "Avg. Power [Watts]: $(eval echo \$power_avg_$i)<br/>" >> $output_multiline
echo "Duration [seconds]: $(eval echo \$time_$i)<br/>" >> $output_multiline
echo "--------------------------------<br/>" >> $output_multiline
echo "\"${CI_JOB_NAME}: Label: $(eval echo \$label_$i): Energy Used [Joules]:\" $(eval echo \$total_energy_$i)" | tee -a $output metrics.txt
echo "\"${CI_JOB_NAME}: Label: $(eval echo \$label_$i): Avg. CPU Utilization:\" $(eval echo \$cpu_avg_$i)" | tee -a $output metrics.txt
echo "\"${CI_JOB_NAME}: Label: $(eval echo \$label_$i): Avg. Power [Watts]:\" $(eval echo \$power_avg_$i)" | tee -a $output metrics.txt
echo "\"${CI_JOB_NAME}: Label: $(eval echo \$label_$i): Duration [seconds]:\" $(eval echo \$time_$i)" | tee -a $output metrics.txt
echo "----------------" >> $output
done

fi

if [[ ${display_table} == 'true' ]]; then
## Used for the main output display for github (step summary) / gitlab (artifacts)
if [[ $source == 'github' ]]; then
echo "|Label|🖥 avg. CPU utilization [%]|🔋 Total Energy [Joules]|🔌 avg. Power [Watts]|Duration [Seconds]|" >> $output
echo "|---|---|---|---|---|" >> $output
echo "|Total Run|$cpu_avg|$total_energy|$power_avg|$time|" >> $output
echo "Eco-CI Output: " >> $output_pr
echo "|Label|🖥 avg. CPU utilization [%]|🔋 Total Energy [Joules]|🔌 avg. Power [Watts]|Duration [Seconds]|" | tee -a $output $output_pr
echo "|---|---|---|---|---|" | tee -a $output $output_pr
echo "|Total Run|$cpu_avg|$total_energy|$power_avg|$time|" | tee -a $output $output_pr
#display measurument lines in table summary
for (( i=1; i<=$max_measurement_number; i++ ))
do
echo "|$(eval echo \$label_$i)|$(eval echo \$cpu_avg_$i)|$(eval echo \$total_energy_$i)|$(eval echo \$power_avg_$i)|$(eval echo \$time_$i)|" >> $output
echo "|$(eval echo \$label_$i)|$(eval echo \$cpu_avg_$i)|$(eval echo \$total_energy_$i)|$(eval echo \$power_avg_$i)|$(eval echo \$time_$i)|" | tee -a $output $output_pr
done

# echo -e "$final_line" >> $output
echo '' >> $output
elif [[ $source == 'gitlab' ]]; then
echo $(cat "/tmp/eco-ci/output-short.txt") >> $output
echo '' | tee -a $output $output_pr
fi



fi

if [[ ${display_graph} == 'true' ]]; then
if [[ $source == 'github' ]]; then
echo '📈 Energy graph:' >> $output
echo '```bash' >> $output
echo ' ' >> $output
cat /tmp/eco-ci/energy-total.txt | /home/runner/go/bin/asciigraph -h 10 -c "Watts over time" >> $output
echo ' ```' >> $output
echo '📈 Energy graph:' | tee -a $output $output_pr
echo '```bash' | tee -a $output $output_pr
echo ' ' | tee -a $output $output_pr
cat /tmp/eco-ci/energy-total.txt | /home/runner/go/bin/asciigraph -h 10 -c "Watts over time" | tee -a $output $output_pr
echo ' ```' | tee -a $output $output_pr
elif [[ $source == 'gitlab' ]]; then
echo '📈 Energy graph:' >> $output
cat /tmp/eco-ci/energy-total.txt | /home/runner/go/bin/asciigraph -h 10 -c "Watts over time" >> $output
Expand Down

0 comments on commit c9f3744

Please sign in to comment.