diff --git a/benchmarks/k8s/__init__.py b/benchmarks/k8s/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/benchmarks/k8s/parameter_matrix.py b/benchmarks/k8s/parameter_matrix.py new file mode 100644 index 0000000..797d809 --- /dev/null +++ b/benchmarks/k8s/parameter_matrix.py @@ -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]} ''") + 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())) diff --git a/benchmarks/k8s/tests/__init__.py b/benchmarks/k8s/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/benchmarks/k8s/tests/test_parameter_matrix.py b/benchmarks/k8s/tests/test_parameter_matrix.py new file mode 100644 index 0000000..806363d --- /dev/null +++ b/benchmarks/k8s/tests/test_parameter_matrix.py @@ -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}, + ] \ No newline at end of file diff --git a/docker/bittorrent-benchmarks-workflows.Dockerfile b/docker/bittorrent-benchmarks-workflows.Dockerfile index 20e8f0d..cb56adf 100644 --- a/docker/bittorrent-benchmarks-workflows.Dockerfile +++ b/docker/bittorrent-benchmarks-workflows.Dockerfile @@ -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 .