feat: add parameter expansion script

This commit is contained in:
gmega 2024-12-19 12:12:22 -03:00
parent 469ae82009
commit 983ea0e2c7
No known key found for this signature in database
GPG Key ID: 6290D34EAD824B18
5 changed files with 81 additions and 2 deletions

View File

View File

@ -0,0 +1,39 @@
import itertools
import json
import sys
from json import JSONDecodeError
from typing import Dict, Any, List
class ParameterMatrix:
def __init__(self, parameters: Dict[str, Any]):
self.parameters = parameters
def expand(self, run_id: bool = False) -> List[Dict[str, Any]]:
expandable = {k: v for k, v in self.parameters.items() if isinstance(v, list)}
fixed = {k: v for k, v in self.parameters.items() if k not in expandable}
expansion = [
dict(zip(expandable.keys(), values), **fixed)
for values in itertools.product(*expandable.values())
]
if run_id:
for i, item in enumerate(expansion, start=1):
item["runId"] = i
return expansion
if __name__ == "__main__":
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} '<json_string>'")
sys.exit(1)
try:
matrix_str = json.loads(sys.argv[1])
except JSONDecodeError as err:
print(f"Error decoding JSON: {err}")
sys.exit(1)
print(json.dumps(ParameterMatrix(matrix_str).expand()))

View File

View File

@ -0,0 +1,36 @@
from benchmarks.k8s.parameter_matrix import ParameterMatrix
def test_should_expand_simple_parameter_lists():
matrix = ParameterMatrix(
{
"a": [1, 2],
"b": [3, 4],
"c": "foo",
"d": 5
}
)
assert matrix.expand() == [
{"a": 1, "b": 3, "c": "foo", "d": 5},
{"a": 1, "b": 4, "c": "foo", "d": 5},
{"a": 2, "b": 3, "c": "foo", "d": 5},
{"a": 2, "b": 4, "c": "foo", "d": 5},
]
def test_should_add_run_id_when_requested():
matrix = ParameterMatrix(
{
"a": [1, 2],
"b": [3, 4],
"c": "foo",
"d": 5
}
)
assert matrix.expand(run_id=True) == [
{"a": 1, "b": 3, "c": "foo", "d": 5, "runId": 1},
{"a": 1, "b": 4, "c": "foo", "d": 5, "runId": 2},
{"a": 2, "b": 3, "c": "foo", "d": 5, "runId": 3},
{"a": 2, "b": 4, "c": "foo", "d": 5, "runId": 4},
]

View File

@ -1,16 +1,20 @@
FROM bitnami/kubectl:1.31.1 as kubectl
FROM bitnami/kubectl:1.31.1 AS kubectl
FROM debian:bookworm-slim
FROM python:3.12-slim
COPY --from=kubectl /opt/bitnami/kubectl/bin/kubectl /usr/local/bin/kubectl
RUN apt-get update && apt-get install -y curl
RUN curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
RUN curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
RUN curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
RUN chmod 700 get_helm.sh
RUN ./get_helm.sh
RUN mkdir /opt/bittorrent-benchmarks
WORKDIR /opt/bittorrent-benchmarks
COPY ./k8s ./k8s
COPY ./docker ./docker
COPY ./benchmarks/k8s/parameter_matrix.py .