Skip to content

Commit ba431c2

Browse files
committed
calculate-cache.sh: extract runs_on and template from workflow file
Signed-off-by: Norio Nomura <[email protected]>
1 parent 1aa5b34 commit ba431c2

File tree

1 file changed

+55
-75
lines changed

1 file changed

+55
-75
lines changed

hack/calculate-cache.sh

Lines changed: 55 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env bash
2-
# This script calculates the expected content size, actual cached size, and cache-keys used in caching method prior and after
2+
# This script calculates the expected content size, actual cached size, and cache-keys used in caching method before and after
33
# implementation in https://github.com/lima-vm/lima/pull/2508
44
#
55
# Answer to the question in https://github.com/lima-vm/lima/pull/2508#discussion_r1699798651
@@ -19,43 +19,30 @@ arch=x86_64
1919
LIMA_HOME=$(mktemp -d)
2020
export LIMA_HOME
2121

22-
# shellcheck disable=SC2034
23-
macos_12=(
24-
# integration
25-
examples/default.yaml
26-
# vmnet
27-
examples/vmnet.yaml
28-
# upgrade
29-
https://raw.githubusercontent.com/lima-vm/lima/v0.15.1/examples/ubuntu-lts.yaml
30-
)
31-
32-
# shellcheck disable=SC2034
33-
ubuntu_2204=(
34-
# integration-linux
35-
examples/alpine.yaml
36-
examples/debian.yaml
37-
examples/fedora.yaml
38-
examples/archlinux.yaml
39-
examples/opensuse.yaml
40-
examples/experimental/net-user-v2.yaml
41-
examples/experimental/9p.yaml
42-
examples/docker.yaml
43-
examples/../hack/test-templates/alpine-9p-writable.yaml
44-
examples/../hack/test-templates/test-misc.yaml
45-
)
46-
47-
# shellcheck disable=SC2034
48-
macos_13=(
49-
# vz
50-
examples/experimental/vz.yaml
51-
examples/fedora.yaml
52-
)
22+
# parse the workflow file and print runs-on and template
23+
function print_runs_on_template_from_workflow() {
24+
yq -o=j "$1" | jq -r '
25+
"./.github/actions/setup_cache_for_template" as $action |
26+
"\\$\\{\\{\\s*(?<path>\\S*)\\s*\\}\\}" as $pattern |
27+
.jobs | map_values(
28+
."runs-on" as $runs_on |
29+
{
30+
template: .steps | map_values(select(.uses == $action)) | first |.with.template,
31+
matrix: .strategy.matrix
32+
} | select(.template) |
33+
. + { path: .template | (if test($pattern) then sub(".*\($pattern).*";"\(.path)")|split(".") else null end) } |
34+
(
35+
.template as $template|
36+
if .path then
37+
getpath(.path)|map(. as $item|$template|sub($pattern;$item))
38+
else
39+
[$template]
40+
end
41+
) | map("\($runs_on)\t\(.)")
5342
54-
runners=(
55-
macos_12
56-
ubuntu_2204
57-
macos_13
58-
)
43+
) | flatten |.[]
44+
'
45+
}
5946

6047
function runner_os_from_runner() {
6148
# shellcheck disable=SC2249
@@ -149,50 +136,43 @@ actual_cache_sizes=$(
149136
jq 'sort_by(.createdAt)|reverse|unique_by(.key)|sort_by(.key)|map({"key":.key,"value":.sizeInBytes})|from_entries'
150137
)
151138

139+
workflows=(
140+
.github/workflows/test.yml
141+
)
142+
152143
# shellcheck disable=SC2016
153-
for cache_method in prior after; do
154-
echo "==> expected content size, actual cached size, and cache-keys used in caching method ${cache_method} implementation in https://github.com/lima-vm/lima/pull/2508"
144+
echo "=> compare expected content size, actual cached size, and cache-keys used before and after the change in https://github.com/lima-vm/lima/pull/2508"
145+
# iterate over before and after
146+
for cache_method in before after; do
147+
echo "==> ${cache_method}"
155148
echo "content-size actual-size cache-key"
156149
output_yaml=$(
157-
for runner in "${runners[@]}"; do
150+
for workflow in "${workflows[@]}"; do
151+
print_runs_on_template_from_workflow "${workflow}"
152+
done | while IFS=$'\t' read -r runner template; do
158153
runner_os=$(runner_os_from_runner "${runner}")
159-
declare -n ref="${runner}"
160-
tepmlates_used_in_test_yml=("${ref[@]}")
161-
for template in "${tepmlates_used_in_test_yml[@]}"; do
162-
location_digest_size_hash=$(print_location_digest_size_hash_from_template "${template}") || continue
163-
read -r location digest size hash containerd containerd_location containerd_digest containerd_size <<<"${location_digest_size_hash}"
164-
if [[ ${cache_method} == prior ]]; then
165-
key=${runner_os}-${hash}
166-
elif [[ ${digest} == null ]]; then
167-
key=image:$(basename "${location}")-url-sha256:$(echo -n "${location}" | sha256sum | cut -d' ' -f1)
154+
location_digest_size_hash=$(print_location_digest_size_hash_from_template "${template}") || continue
155+
read -r location digest size hash containerd containerd_location containerd_digest containerd_size <<<"${location_digest_size_hash}"
156+
if [[ ${cache_method} != after ]]; then
157+
key=${runner_os}-${hash}
158+
elif [[ ${digest} == null ]]; then
159+
key=image:$(basename "${location}")-url-sha256:$(echo -n "${location}" | sha256sum | cut -d' ' -f1)
160+
else
161+
key=image:$(basename "${location}")-${digest}
162+
fi
163+
if [[ ${containerd} == true ]]; then
164+
if [[ ${cache_method} != after ]]; then
165+
# previous caching method packages the containerd archive with the image
166+
size=$((size + containerd_size))
168167
else
169-
key=image:$(basename "${location}")-${digest}
170-
fi
171-
if [[ ${containerd} == true ]]; then
172-
if [[ ${cache_method} == prior ]]; then
173-
# previous caching method packages the containerd archive with the image
174-
size=$((size + containerd_size))
175-
else
176-
# new caching method packages the containerd archive separately
177-
containerd_key=containerd:$(basename "${containerd_location}")-${containerd_digest}
178-
cat <<-EOF
179-
- key: ${containerd_key}
180-
template: ${template}
181-
location: ${containerd_location}
182-
digest: ${containerd_digest}
183-
size: ${containerd_size}
184-
EOF
185-
fi
168+
# new caching method packages the containerd archive separately
169+
containerd_key=containerd:$(basename "${containerd_location}")-${containerd_digest}
170+
printf -- "- key: %s\n template: %s\n location: %s\n digest: %s\n size: %s\n" \
171+
"${containerd_key}" "${template}" "${containerd_location}" "${containerd_digest}" "${containerd_size}"
186172
fi
187-
cat <<-EOF
188-
- key: ${key}
189-
template: ${template}
190-
location: ${location}
191-
digest: ${digest}
192-
size: ${size}
193-
EOF
194-
# echo -e "- key: ${key}\n template: ${template}\n location: ${location}\n size: ${size}\n containerd: ${containerd}\n containerd_location: ${containerd_location}\n containerd_digest: ${containerd_digest}"
195-
done
173+
fi
174+
printf -- "- key: %s\n template: %s\n location: %s\n digest: %s\n size: %s\n" \
175+
"${key}" "${template}" "${location}" "${digest}" "${size}"
196176
done
197177
)
198178
cat <<<"${output_yaml}" >".calculate-cache-collected-info-${cache_method}.yaml"

0 commit comments

Comments
 (0)