Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add chunk size experiment #341

Merged
merged 2 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions benchmarks/exp_chunk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2025 The Sigstore Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""Script for running a benchmark to pick a chunk parameter."""

import argparse
import timeit

import serialize


def build_parser() -> argparse.ArgumentParser:
"""Builds the command line parser for the chunk experiment."""
parser = argparse.ArgumentParser(
description="chunk size benchmark data for model signing"
)

parser.add_argument("path", help="path to model")

parser.add_argument(
"--repeat",
help="how many times to repeat each chunk size",
type=int,
default=5,
)

parser.add_argument(
"--sizes", help="chunk sizes to benchmark", nargs="+", type=int
)

return parser


def _default_sizes() -> list[int]:
# 0 is a special value to (attempt to) read whole files into RAM
# then powers of 2 between 1KB and 1GB

# https://github.com/google/pytype/issues/795
# pytype: disable=bad-return-type
return [0] + [2**i for i in range(10, 31)]
# pytype: enable=bad-return-type


if __name__ == "__main__":
chunk_args = build_parser().parse_args()

chunk_sizes = chunk_args.sizes or _default_sizes()
padding = len(f"{max(chunk_sizes)}: ")
for chunk_size in chunk_sizes:
args = serialize.build_parser().parse_args(
[chunk_args.path, f"--chunk={chunk_size}"]
)
spencerschrock marked this conversation as resolved.
Show resolved Hide resolved
times = timeit.repeat(
lambda args=args: serialize.run(args),
number=1,
repeat=chunk_args.repeat,
)
print(f"{f'{chunk_size}: ':<{padding}}{min(times):10.4f}")
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ python = ["3.9", "3.10", "3.11", "3.12", "3.13"]
description = """Custom environment for running benchmarks.
Use `hatch run +py=3... bench:generate ${args}` to generate test models.
Use `hatch run +py=3... bench:serialize ${args}` to benchmark serialization code.
Use `hatch run +py=3... bench:hash ${args}` to benchmark hashing code.
Use `hatch run +py=3... bench:chunk ${args}` to benchmark the chunk size parameter.
"""
extra-dependencies = [
"numpy",
Expand All @@ -83,6 +85,7 @@ python = ["3.9", "3.10", "3.11", "3.12", "3.13"]
generate = "python benchmarks/generate.py {args}"
serialize = "python benchmarks/serialize.py {args}"
hash = "python benchmarks/exp_hash.py {args}"
chunk = "python benchmarks/exp_chunk.py {args}"

[tool.hatch.envs.docs]
description = """Custom environment for pdoc.
Expand Down
Loading