Skip to content

Commit

Permalink
run.sh: add image size to result
Browse files Browse the repository at this point in the history
Signed-off-by: Bin Tang <[email protected]>
  • Loading branch information
sctb512 committed Apr 3, 2023
1 parent 17a4eb2 commit 5b80bcc
Showing 1 changed file with 60 additions and 11 deletions.
71 changes: 60 additions & 11 deletions hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,17 @@ def timer(cmd):

class RunArgs:
def __init__(
self, env={}, arg="", stdin="", stdin_sh="sh", waitline="", mount=[], waitURL="", runtime="", shmSize="", workDir=""
self,
env={},
arg="",
stdin="",
stdin_sh="sh",
waitline="",
mount=[],
waitURL="",
runtime="",
shmSize="",
workDir="",
):
self.env = env
self.arg = arg
Expand Down Expand Up @@ -235,6 +245,14 @@ def set_tag(self, tag):
self.name = f"{self.name}:{tag}"


class BenchResult:
def __init__(self, pull_elapsed, create_elapsed, run_elapsed, size):
self.pull_elapsed = pull_elapsed
self.create_elapsed = create_elapsed
self.run_elapsed = run_elapsed
self.size = size


class BenchRunner:
def __init__(
self,
Expand Down Expand Up @@ -284,7 +302,9 @@ def load_bench_config(self):
args = line["bench_args"]
print(f"CMD_ARG_WAIT image: {name}, args: {args}")
cmd_arg_wait_runner[name] = RunArgs(
env=dict([(item["key"], item["value"]) for item in args["envs"]]) if "envs" in args else {},
env=dict([(item["key"], item["value"]) for item in args["envs"]])
if "envs" in args
else {},
waitline=args["wait_line"] if "wait_line" in args else "",
mount=[(m["host_path"], m["container_path"]) for m in args["mount"]]
if "mount" in args
Expand All @@ -306,7 +326,9 @@ def load_bench_config(self):
args = line["bench_args"]
print(f"CMD_STDIN image: {name}, args: {args}")
cmd_stdin_runner[name] = RunArgs(
env=dict([(item["key"], item["value"]) for item in args["envs"]]) if "envs" in args else {},
env=dict([(item["key"], item["value"]) for item in args["envs"]])
if "envs" in args
else {},
mount=[(m["host_path"], m["container_path"]) for m in args["mount"]]
if "mount" in args
else [],
Expand All @@ -327,7 +349,9 @@ def load_bench_config(self):
args = line["bench_args"]
print(f"CMD_ARG image: {name}, args: {args}")
cmd_arg_runner[name] = RunArgs(
env=dict([(item["key"], item["value"]) for item in args["envs"]]) if "envs" in args else {},
env=dict([(item["key"], item["value"]) for item in args["envs"]])
if "envs" in args
else {},
mount=[(m["host_path"], m["container_path"]) for m in args["mount"]]
if "mount" in args
else [],
Expand All @@ -348,7 +372,9 @@ def load_bench_config(self):
args = line["bench_args"]
print(f"CMD_URL_WAIT image: {name}, args: {args}")
cmd_url_wait_runner[name] = RunArgs(
env=dict([(item["key"], item["value"]) for item in args["envs"]]) if "envs" in args else {},
env=dict([(item["key"], item["value"]) for item in args["envs"]])
if "envs" in args
else {},
waitURL=args["wait_url"] if "wait_url" in args else "",
mount=[(m["host_path"], m["container_path"]) for m in args["mount"]]
if "mount" in args
Expand Down Expand Up @@ -387,6 +413,8 @@ def run_echo_hello(self, repo: str):
with timer(pull_cmd) as t:
pull_elapsed = t

size = self.image_size(image_ref)

create_cmd = self.create_echo_hello_cmd(image_ref, container_name)
print(create_cmd)

Expand All @@ -403,7 +431,7 @@ def run_echo_hello(self, repo: str):
if self.cleanup:
self.clean_up(image_ref, container_name)

return pull_elapsed, create_elapsed, run_elapsed
return BenchResult(pull_elapsed, create_elapsed, run_elapsed, size)

def run_cmd_arg(self, repo, runargs):
image_ref = self.image_ref(repo)
Expand All @@ -416,6 +444,8 @@ def run_cmd_arg(self, repo, runargs):
with timer(pull_cmd) as t:
pull_elapsed = t

size = self.image_size(image_ref)

create_cmd = self.create_cmd_arg_cmd(image_ref, container_name, runargs)
print(create_cmd)

Expand All @@ -432,7 +462,7 @@ def run_cmd_arg(self, repo, runargs):
if self.cleanup:
self.clean_up(image_ref, container_name)

return pull_elapsed, create_elapsed, run_elapsed
return BenchResult(pull_elapsed, create_elapsed, run_elapsed, size)

def run_cmd_arg_wait(self, repo, runargs):
image_ref = self.image_ref(repo)
Expand All @@ -445,6 +475,8 @@ def run_cmd_arg_wait(self, repo, runargs):
with timer(pull_cmd) as t:
pull_elapsed = t

size = self.image_size(image_ref)

create_cmd = self.create_cmd_arg_wait_cmd(image_ref, container_name, runargs)
print(create_cmd)

Expand Down Expand Up @@ -482,7 +514,7 @@ def run_cmd_arg_wait(self, repo, runargs):
if self.cleanup:
self.clean_up(image_ref, container_name)

return pull_elapsed, create_elapsed, run_elapsed
return BenchResult(pull_elapsed, create_elapsed, run_elapsed, size)

def run_cmd_stdin(self, repo, runargs):
image_ref = self.image_ref(repo)
Expand All @@ -495,6 +527,8 @@ def run_cmd_stdin(self, repo, runargs):
with timer(pull_cmd) as t:
pull_elapsed = t

size = self.image_size(image_ref)

create_cmd = self.create_cmd_stdin_cmd(image_ref, container_name, runargs)
print(create_cmd)

Expand Down Expand Up @@ -530,7 +564,7 @@ def run_cmd_stdin(self, repo, runargs):
if self.cleanup:
self.clean_up(image_ref, container_name)

return pull_elapsed, create_elapsed, run_elapsed
return BenchResult(pull_elapsed, create_elapsed, run_elapsed, size)

def run_cmd_url_wait(self, repo, runargs):
image_ref = self.image_ref(repo)
Expand All @@ -543,6 +577,8 @@ def run_cmd_url_wait(self, repo, runargs):
with timer(pull_cmd) as t:
pull_elapsed = t

size = self.image_size(image_ref)

create_cmd = self.create_cmd_url_wait_cmd(image_ref, container_id, runargs)
print(create_cmd)

Expand Down Expand Up @@ -574,7 +610,7 @@ def run_cmd_url_wait(self, repo, runargs):
if self.cleanup:
self.clean_up(image_ref, container_id)

return pull_elapsed, create_elapsed, run_elapsed
return BenchResult(pull_elapsed, create_elapsed, run_elapsed, size)

def run(self, bench):
repo = image_repo(bench.name)
Expand Down Expand Up @@ -602,6 +638,12 @@ def pull_cmd(self, image_ref):
f"nerdctl --snapshotter {self.snapshotter} pull {insecure_flag} {image_ref}"
)

def image_size(self, image_ref):
cmd = f"nerdctl images {str(image_ref)}"
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
out = p.communicate()[0].decode("utf-8")
return " ".join(out.split()[-2:])

def create_echo_hello_cmd(self, image_ref, container_id):
return f"nerdctl --snapshotter {self.snapshotter} create --net=host --name={container_id} {image_ref} -- echo hello"

Expand Down Expand Up @@ -892,7 +934,13 @@ def main():

for bench in benches:
for _ in range(bench_times):
pull_elapsed, create_elapsed, run_elapsed = runner.operation(op, bench)
bench_result = runner.operation(op, bench)
pull_elapsed, create_elapsed, run_elapsed, size = (
bench_result.pull_elapsed,
bench_result.create_elapsed,
bench_result.run_elapsed,
bench_result.size,
)

total_elapsed = f"{pull_elapsed + create_elapsed + run_elapsed: .6f}"
timetamp = int(time.time() * 1000)
Expand All @@ -909,6 +957,7 @@ def main():
"create_elapsed": create_elapsed,
"run_elapsed": run_elapsed,
"total_elapsed": total_elapsed,
"image_size": size,
}
line = json.dumps(row)
elif output_format == "csv":
Expand Down

0 comments on commit 5b80bcc

Please sign in to comment.