Skip to content

Commit

Permalink
misc: benchmark support more images
Browse files Browse the repository at this point in the history
add support for golang, java(amazoncorretto), ruby, and python.

Signed-off-by: Yadong Ding <[email protected]>
  • Loading branch information
Desiki-high authored and imeoer committed May 8, 2023
1 parent 44f3b16 commit ad8f870
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 16 deletions.
166 changes: 159 additions & 7 deletions misc/benchmark/bench.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@ def timer(cmd):

class RunArgs:
def __init__(
self, waitURL=""
self, env={}, arg="", stdin="", stdin_sh="sh", waitline="", mount=[], waitURL=""
):
self.env = env
self.arg = arg
self.stdin = stdin
self.stdin_sh = stdin_sh
self.waitline = waitline
self.mount = mount
self.waitURL = waitURL


Expand All @@ -109,12 +115,39 @@ def set_tag(self, tag):
class BenchRunner:
CMD_URL_WAIT = {
"wordpress": RunArgs(waitURL="http://localhost:80"),
"node": RunArgs(
arg="node /src/index.js",
mount=[("node", "/src")],
waitURL="http://localhost:80",
),
}

# complete listing
CMD_STDIN = {
"golang": RunArgs(
stdin="cd /src; go run main.go", mount=[("go", "/src")]
),
"amazoncorretto": RunArgs(
stdin="cd /src; javac Main.java; java Main", mount=[("java", "/src")]
),
"ruby": RunArgs(stdin='ruby -e "puts \\"hello\\""')
}

CMD_ARG = {
"python": RunArgs(arg="python -c 'print(\"hello\")'"),
}

# support images
ALL = dict(
[
("wordpress", Bench("wordpress", "web-framework")),
(b.name, b)
for b in [
Bench("golang", "language"),
Bench("python", "language"),
Bench("ruby", "language"),
Bench("amazoncorretto", "language"),
Bench("node", "web-framework"),
Bench("wordpress", "web-framework"),
]
]
)

Expand All @@ -140,9 +173,11 @@ def image_ref(self, repo):
def run(self, bench):
repo = image_repo(bench.name)
if repo in BenchRunner.CMD_URL_WAIT:
return self.run_cmd_url_wait(
repo=bench.name, runargs=BenchRunner.CMD_URL_WAIT[repo]
)
return self.run_cmd_url_wait(repo=bench.name, runargs=BenchRunner.CMD_URL_WAIT[repo])
elif repo in BenchRunner.CMD_ARG:
return self.run_cmd_arg(repo=bench.name, runargs=BenchRunner.CMD_ARG[repo])
elif repo in BenchRunner.CMD_STDIN:
return self.run_cmd_stdin(repo=bench.name, runargs=BenchRunner.CMD_STDIN[repo])
else:
print("Unknown bench: " + repo)
sys.exit(1)
Expand Down Expand Up @@ -188,13 +223,113 @@ def run_cmd_url_wait(self, repo, runargs):
read_amount, read_count = "-", "-"
if self.snapshotter == "nydus":
read_amount, read_count = metrics.collect_backend()
image_size = metrics.collect_size(image_repo(repo), image_tag(repo))
try:
image_size = metrics.collect_size(image_repo(repo), image_tag(repo))
except Exception:
image_size = 0

if self.cleanup:
self.clean_up(image_ref, container_id)

return pull_elapsed, create_elapsed, run_elapsed, image_size, read_amount, read_count

def run_cmd_arg(self, repo, runargs):
assert len(runargs.mount) == 0

image_ref = self.image_ref(repo)
container_name = repo.replace(":", "-")

pull_cmd = self.pull_cmd(image_ref)
print(pull_cmd)

print("Pulling image %s ..." % image_ref)
with timer(pull_cmd) as t:
pull_elapsed = t

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

print("Creating container for image %s ..." % image_ref)
with timer(create_cmd) as t:
create_elapsed = t

run_cmd = self.task_start_cmd(container_name, iteration=False)
print(run_cmd)

with timer(run_cmd) as t:
run_elapsed = t

print("Run time: %f s" % run_elapsed)

read_amount, read_count = "-", "-"
if self.snapshotter == "nydus":
read_amount, read_count = metrics.collect_backend()
try:
image_size = metrics.collect_size(image_repo(repo), image_tag(repo))
except Exception:
image_size = 0

if self.cleanup:
self.clean_up(image_ref, container_name)

return pull_elapsed, create_elapsed, run_elapsed, image_size, read_amount, read_count

def run_cmd_stdin(self, repo, runargs):
image_ref = self.image_ref(repo)
container_name = repo.replace(":", "-")

pull_cmd = self.pull_cmd(image_ref)
print(pull_cmd)

print("Pulling image %s ..." % image_ref)
with timer(pull_cmd) as t:
pull_elapsed = t

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

print("Creating container for image %s ..." % image_ref)
with timer(create_cmd) as t:
create_elapsed = t

run_cmd = self.task_start_cmd(container_name, iteration=True)
print(run_cmd)

print("Running container %s ..." % container_name)
start_run = datetime.now()

p = subprocess.Popen(
run_cmd,
shell=True,
stdin=subprocess.PIPE,
stdout=sys.stdout,
stderr=sys.stdout,
bufsize=0,
)

print(runargs.stdin)
stdin = runargs.stdin + "\nexit\n"
p.communicate(stdin.encode())
end_run = datetime.now()
run_elapsed = datetime.timestamp(end_run) - datetime.timestamp(start_run)
print("p.returncode:", p.returncode)
# assert(p.returncode == 0)

print("Run time: %f s" % run_elapsed)

read_amount, read_count = "-", "-"
if self.snapshotter == "nydus":
read_amount, read_count = metrics.collect_backend()
try:
image_size = metrics.collect_size(image_repo(repo), image_tag(repo))
except Exception:
image_size = 0

if self.cleanup:
self.clean_up(image_ref, container_name)

return pull_elapsed, create_elapsed, run_elapsed, image_size, read_amount, read_count

def pull_cmd(self, image_ref):
insecure_flag = "--insecure-registry" if self.insecure_registry else ""
return (
Expand All @@ -203,6 +338,23 @@ def pull_cmd(self, image_ref):

def create_cmd_url_wait_cmd(self, image_ref, container_id, runargs):
cmd = f"sudo nerdctl --snapshotter {self.snapshotter} create --net=host "
for a, b in runargs.mount:
a = os.path.join(os.path.dirname(os.path.abspath(__file__)), a)
cmd += f"--volume {a}:{b} "
cmd += f"--name={container_id} {image_ref}"
if len(runargs.arg) > 0:
cmd += f" -- {runargs.arg} "
return cmd

def create_cmd_arg_cmd(self, image_ref, container_id, runargs):
cmd = f"sudo nerdctl --snapshotter {self.snapshotter} create --net=host --name={container_id} {image_ref} "
return cmd + runargs.arg

def create_cmd_stdin_cmd(self, image_ref, container_id, runargs):
cmd = f"sudo nerdctl --snapshotter {self.snapshotter} create --net=host "
for a, b in runargs.mount:
a = os.path.join(os.path.dirname(os.path.abspath(__file__)), a)
cmd += f"--volume {a}:{b} "
cmd += f"--name={container_id} {image_ref}"
return cmd

Expand Down
6 changes: 6 additions & 0 deletions misc/benchmark/go/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package main
import "fmt"

func main() {
fmt.Println("hello")
}
5 changes: 5 additions & 0 deletions misc/benchmark/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Main {
public static void main(String[] args) {
System.out.println("hello");
}
}
22 changes: 13 additions & 9 deletions misc/benchmark/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,21 @@ def clean_up(self, image_ref, container_id):

def collect(self, repo) -> str:
"""
TODO: update to the condition, here is just for wait the wordpress up
collect the access files
"""
# wait wordpress response in 80 port
while True:
try:
req = urllib.request.urlopen("http://localhost:80")
print(req.status)
req.close()
break
except:
time.sleep(0.01)
if self.image == "wordpress":
while True:
try:
req = urllib.request.urlopen("http://localhost:80")
print(req.status)
req.close()
break
except:
time.sleep(0.01)
# TODO: support more images
else:
time.sleep(10)
socket = search_file(API_DIR, "api.sock")
if socket == None:
print("can't find the api.sock")
Expand Down
10 changes: 10 additions & 0 deletions misc/benchmark/node/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Load the http module to create an http server.
var http = require('http');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("hello\n");
});

server.listen(80);

0 comments on commit ad8f870

Please sign in to comment.