Skip to content

Commit d5742ba

Browse files
committed
Fix script to run on ubuntu 24.04
Signed-off-by: Kohei Tokunaga <[email protected]>
1 parent c6a64f1 commit d5742ba

File tree

5 files changed

+98
-73
lines changed

5 files changed

+98
-73
lines changed

script/benchmark/hello-bench/src/hello.py

+70-70
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3737
# SOFTWARE.
3838

39-
import os, sys, subprocess, select, random, urllib2, time, json, tempfile, shutil, itertools
39+
import os, sys, subprocess, select, random, time, json, tempfile, shutil, itertools
4040

4141
TMP_DIR = tempfile.mkdtemp()
4242
LEGACY_MODE = "legacy"
@@ -176,7 +176,7 @@ def __init__(self, repository='docker.io/library', srcrepository='docker.io/libr
176176
elif runtime == "podman":
177177
self.controller = PodmanController()
178178
else:
179-
print 'Unknown runtime mode: '+runtime
179+
print ('Unknown runtime mode: '+runtime)
180180
exit(1)
181181
self.repository = repository
182182
self.srcrepository = srcrepository
@@ -236,15 +236,15 @@ def run_cmd_arg_wait(self, image, cid, runargs):
236236
stderr=subprocess.STDOUT,
237237
stdout=subprocess.PIPE)
238238
while True:
239-
l = p.stdout.readline()
239+
l = p.stdout.readline().decode("utf-8")
240240
if l == '':
241241
continue
242-
print 'out: ' + l.strip()
242+
print ('out: ' + l.strip())
243243
# are we done?
244244
if l.find(runargs.waitline) >= 0:
245245
runtime = time.time() - startrun
246246
# cleanup
247-
print 'DONE'
247+
print ('DONE')
248248
cmd = self.controller.task_kill_cmd(cid)
249249
rc = os.system(cmd)
250250
assert(rc == 0)
@@ -262,18 +262,18 @@ def run_cmd_stdin(self, image, cid, runargs):
262262
cmd = self.controller.task_start_cmd(cid)
263263
startrun = time.time()
264264
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
265-
print runargs.stdin
266-
out, _ = p.communicate(runargs.stdin)
265+
print (runargs.stdin)
266+
out, _ = p.communicate(runargs.stdin.encode())
267267
runtime = time.time() - startrun
268-
print out
268+
print (out)
269269
assert(p.returncode == 0)
270270
return createtime, runtime
271271

272272
def run(self, bench, cid):
273273
name = bench.name
274274
image = self.fully_qualify(bench.name)
275275

276-
print "Pulling the image..."
276+
print ("Pulling the image...")
277277
pullcmd = self.controller.pull_cmd(image)
278278
startpull = time.time()
279279
rc = os.system(pullcmd)
@@ -291,7 +291,7 @@ def run(self, bench, cid):
291291
elif name in BenchRunner.CMD_STDIN:
292292
createtime, runtime = self.run_cmd_stdin(image=image, cid=cid, runargs=BenchRunner.CMD_STDIN[name])
293293
else:
294-
print 'Unknown bench: '+name
294+
print ('Unknown bench: '+name)
295295
exit(1)
296296

297297
return pulltime, createtime, runtime
@@ -300,7 +300,7 @@ def convert_echo_hello(self, src, dest, option):
300300
period=10
301301
cmd = ('%s %s -cni -period %s -entrypoint \'["/bin/sh", "-c"]\' -args \'["echo hello"]\' %s %s' %
302302
(self.optimizer, option, period, src, dest))
303-
print cmd
303+
print (cmd)
304304
rc = os.system(cmd)
305305
assert(rc == 0)
306306

@@ -312,7 +312,7 @@ def convert_cmd_arg(self, src, dest, runargs, option):
312312
entry = '-entrypoint \'["/bin/sh", "-c"]\''
313313
cmd = ('%s %s -cni -period %s %s %s %s %s' %
314314
(self.optimizer, option, period, entry, genargs_for_optimization(runargs.arg), src, dest))
315-
print cmd
315+
print (cmd)
316316
rc = os.system(cmd)
317317
assert(rc == 0)
318318

@@ -323,10 +323,10 @@ def convert_cmd_arg_wait(self, src, dest, runargs, option):
323323
a = tmp_copy(a)
324324
mounts += '--mount type=bind,src=%s,dst=%s,options=rbind ' % (a,b)
325325
period = 90
326-
env = ' '.join(['-env %s=%s' % (k,v) for k,v in runargs.env.iteritems()])
326+
env = ' '.join(['-env %s=%s' % (k,v) for k,v in runargs.env.items()])
327327
cmd = ('%s %s -cni -period %s %s %s %s %s %s' %
328328
(self.optimizer, option, period, mounts, env, genargs_for_optimization(runargs.arg), src, dest))
329-
print cmd
329+
print (cmd)
330330
rc = os.system(cmd)
331331
assert(rc == 0)
332332

@@ -339,36 +339,36 @@ def convert_cmd_stdin(self, src, dest, runargs, option):
339339
period = 60
340340
cmd = ('%s %s -i -cni -period %s %s -entrypoint \'["/bin/sh", "-c"]\' %s %s %s' %
341341
(self.optimizer, option, period, mounts, genargs_for_optimization(runargs.stdin_sh), src, dest))
342-
print cmd
342+
print (cmd)
343343
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
344-
print runargs.stdin
345-
out,_ = p.communicate(runargs.stdin)
346-
print out
344+
print (runargs.stdin)
345+
out,_ = p.communicate(runargs.stdin.encode())
346+
print (out)
347347
p.wait()
348348
assert(p.returncode == 0)
349349

350350
def push_img(self, dest):
351351
cmd = '%s %s' % (self.pusher, dest)
352-
print cmd
352+
print (cmd)
353353
rc = os.system(cmd)
354354
assert(rc == 0)
355355

356356
def pull_img(self, src):
357357
cmd = '%s %s' % (self.puller, src)
358-
print cmd
358+
print (cmd)
359359
rc = os.system(cmd)
360360
assert(rc == 0)
361361

362362
def copy_img(self, src, dest):
363363
cmd = 'crane copy %s %s' % (src, dest)
364-
print cmd
364+
print (cmd)
365365
rc = os.system(cmd)
366366
assert(rc == 0)
367367

368368
def convert_and_push_img(self, src, dest):
369369
self.pull_img(src)
370370
cmd = '%s --no-optimize %s %s' % (self.optimizer, src, dest)
371-
print cmd
371+
print (cmd)
372372
rc = os.system(cmd)
373373
assert(rc == 0)
374374
self.push_img(dest)
@@ -384,7 +384,7 @@ def optimize_img(self, name, src, dest, option):
384384
elif name in BenchRunner.CMD_STDIN:
385385
self.convert_cmd_stdin(src=src, dest=dest, runargs=BenchRunner.CMD_STDIN[name], option=option)
386386
else:
387-
print 'Unknown bench: '+name
387+
print ('Unknown bench: '+name)
388388
exit(1)
389389
self.push_img(dest)
390390

@@ -403,7 +403,7 @@ def operation(self, op, bench, cid):
403403
self.prepare(bench)
404404
return 0, 0, 0
405405
else:
406-
print 'Unknown operation: '+op
406+
print ('Unknown operation: '+op)
407407
exit(1)
408408

409409
class ContainerdController:
@@ -415,15 +415,15 @@ def pull_cmd(self, image):
415415
if self.is_lazypull:
416416
base_cmd = "ctr-remote i rpull"
417417
cmd = '%s %s' % (base_cmd, image)
418-
print cmd
418+
print (cmd)
419419
return cmd
420420

421421
def create_echo_hello_cmd(self, image, cid):
422422
snapshotter_opt = ""
423423
if self.is_lazypull:
424424
snapshotter_opt = "--snapshotter=stargz"
425425
cmd = '%s c create --net-host %s -- %s %s echo hello' % (CTR, snapshotter_opt, image, cid)
426-
print cmd
426+
print (cmd)
427427
return cmd
428428

429429
def create_cmd_arg_cmd(self, image, cid, runargs):
@@ -433,21 +433,21 @@ def create_cmd_arg_cmd(self, image, cid, runargs):
433433
cmd = '%s c create --net-host %s ' % (CTR, snapshotter_opt)
434434
cmd += '-- %s %s ' % (image, cid)
435435
cmd += runargs.arg
436-
print cmd
436+
print (cmd)
437437
return cmd
438438

439439
def create_cmd_arg_wait_cmd(self, image, cid, runargs):
440440
snapshotter_opt = ""
441441
if self.is_lazypull:
442442
snapshotter_opt = "--snapshotter=stargz"
443-
env = ' '.join(['--env %s=%s' % (k,v) for k,v in runargs.env.iteritems()])
443+
env = ' '.join(['--env %s=%s' % (k,v) for k,v in runargs.env.items()])
444444
cmd = '%s c create --net-host %s %s '% (CTR, snapshotter_opt, env)
445445
for a,b in runargs.mount:
446446
a = os.path.join(os.path.dirname(os.path.abspath(__file__)), a)
447447
a = tmp_copy(a)
448448
cmd += '--mount type=bind,src=%s,dst=%s,options=rbind ' % (a,b)
449449
cmd += '-- %s %s %s' % (image, cid, runargs.arg)
450-
print cmd
450+
print (cmd)
451451
return cmd
452452

453453
def create_cmd_stdin_cmd(self, image, cid, runargs):
@@ -463,30 +463,30 @@ def create_cmd_stdin_cmd(self, image, cid, runargs):
463463
if runargs.stdin_sh:
464464
cmd += runargs.stdin_sh # e.g., sh -c
465465

466-
print cmd
466+
print (cmd)
467467
return cmd
468468

469469
def task_start_cmd(self, cid):
470470
cmd = '%s t start %s' % (CTR, cid)
471-
print cmd
471+
print (cmd)
472472
return cmd
473473

474474
def task_kill_cmd(self, cid):
475475
cmd = '%s t kill -s 9 %s' % (CTR, cid)
476-
print cmd
476+
print (cmd)
477477
return cmd
478478

479479
def cleanup(self, name, image):
480-
print "Cleaning up environment..."
480+
print ("Cleaning up environment...")
481481
cmd = '%s t kill -s 9 %s' % (CTR, name)
482-
print cmd
482+
print (cmd)
483483
rc = os.system(cmd) # sometimes containers already exit. we ignore the failure.
484484
cmd = '%s c rm %s' % (CTR, name)
485-
print cmd
485+
print (cmd)
486486
rc = os.system(cmd)
487487
assert(rc == 0)
488488
cmd = '%s image rm %s' % (CTR, image)
489-
print cmd
489+
print (cmd)
490490
rc = os.system(cmd)
491491
assert(rc == 0)
492492

@@ -500,29 +500,29 @@ def start_profile(self, seconds):
500500
class PodmanController:
501501
def pull_cmd(self, image):
502502
cmd = '%s pull docker://%s' % (PODMAN, image)
503-
print cmd
503+
print (cmd)
504504
return cmd
505505

506506
def create_echo_hello_cmd(self, image, cid):
507507
cmd = '%s create --name %s docker://%s echo hello' % (PODMAN, cid, image)
508-
print cmd
508+
print (cmd)
509509
return cmd
510510

511511
def create_cmd_arg_cmd(self, image, cid, runargs):
512512
cmd = '%s create --name %s docker://%s ' % (PODMAN, cid, image)
513513
cmd += runargs.arg
514-
print cmd
514+
print (cmd)
515515
return cmd
516516

517517
def create_cmd_arg_wait_cmd(self, image, cid, runargs):
518-
env = ' '.join(['--env %s=%s' % (k,v) for k,v in runargs.env.iteritems()])
518+
env = ' '.join(['--env %s=%s' % (k,v) for k,v in runargs.env.items()])
519519
cmd = '%s create %s --name %s '% (PODMAN, env, cid)
520520
for a,b in runargs.mount:
521521
a = os.path.join(os.path.dirname(os.path.abspath(__file__)), a)
522522
a = tmp_copy(a)
523523
cmd += '--mount type=bind,src=%s,dst=%s ' % (a,b)
524524
cmd += ' docker://%s %s ' % (image, runargs.arg)
525-
print cmd
525+
print (cmd)
526526
return cmd
527527

528528
def create_cmd_stdin_cmd(self, image, cid, runargs):
@@ -535,51 +535,51 @@ def create_cmd_stdin_cmd(self, image, cid, runargs):
535535
if runargs.stdin_sh:
536536
cmd += runargs.stdin_sh # e.g., sh -c
537537

538-
print cmd
538+
print (cmd)
539539
return cmd
540540

541541
def task_start_cmd(self, cid):
542542
cmd = '%s start -a %s' % (PODMAN, cid)
543-
print cmd
543+
print (cmd)
544544
return cmd
545545

546546
def task_kill_cmd(self, cid):
547547
cmd = '%s kill -s 9 %s' % (PODMAN, cid)
548-
print cmd
548+
print (cmd)
549549
return cmd
550550

551551
def cleanup(self, name, image):
552-
print "Cleaning up environment..."
552+
print ("Cleaning up environment...")
553553
cmd = '%s kill -s 9 %s' % (PODMAN, name)
554-
print cmd
554+
print (cmd)
555555
rc = os.system(cmd) # sometimes containers already exit. we ignore the failure.
556556
cmd = '%s rm %s' % (PODMAN, name)
557-
print cmd
557+
print (cmd)
558558
rc = os.system(cmd)
559559
assert(rc == 0)
560560
cmd = '%s image rm %s' % (PODMAN, image)
561-
print cmd
561+
print (cmd)
562562
rc = os.system(cmd)
563563
assert(rc == 0)
564564
cmd = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../reboot_store.sh')
565-
print cmd
565+
print (cmd)
566566
rc = os.system(cmd)
567567
assert(rc == 0)
568568

569569
def main():
570570
if len(sys.argv) == 1:
571-
print 'Usage: bench.py [OPTIONS] [BENCHMARKS]'
572-
print 'OPTIONS:'
573-
print '--repository=<repository>'
574-
print '--srcrepository=<repository>'
575-
print '--all'
576-
print '--list'
577-
print '--list-json'
578-
print '--experiments'
579-
print '--profile=<seconds>'
580-
print '--runtime'
581-
print '--op=(prepare|run)'
582-
print '--mode=(%s|%s|%s)' % (LEGACY_MODE, ESTARGZ_NOOPT_MODE, ESTARGZ_MODE, ZSTDCHUNKED_MODE)
571+
print ('Usage: bench.py [OPTIONS] [BENCHMARKS]')
572+
print ('OPTIONS:')
573+
print ('--repository=<repository>')
574+
print ('--srcrepository=<repository>')
575+
print ('--all')
576+
print ('--list')
577+
print ('--list-json')
578+
print ('--experiments')
579+
print ('--profile=<seconds>')
580+
print ('--runtime')
581+
print ('--op=(prepare|run)')
582+
print ('--mode=(%s|%s|%s)' % (LEGACY_MODE, ESTARGZ_NOOPT_MODE, ESTARGZ_MODE, ZSTDCHUNKED_MODE))
583583
exit(1)
584584

585585
benches = []
@@ -594,11 +594,11 @@ def main():
594594
benches.extend(BenchRunner.ALL.values())
595595
elif parts[0] == 'list':
596596
template = '%-16s\t%-20s'
597-
print template % ('CATEGORY', 'NAME')
597+
print (template % ('CATEGORY', 'NAME'))
598598
for b in sorted(BenchRunner.ALL.values(), key=lambda b:(b.category, b.name)):
599-
print template % (b.category, b.name)
599+
print (template % (b.category, b.name))
600600
elif parts[0] == 'list-json':
601-
print json.dumps([b.__dict__ for b in BenchRunner.ALL.values()])
601+
print (json.dumps([b.__dict__ for b in BenchRunner.ALL.values()]))
602602
else:
603603
benches.append(BenchRunner.ALL[arg])
604604

@@ -633,18 +633,18 @@ def main():
633633
pull_times.append(pulltime)
634634
create_times.append(createtime)
635635
run_times.append(runtime)
636-
print 'ITERATION %s:' % i
637-
print 'elapsed %s' % elapsed
638-
print 'pull %s' % pulltime
639-
print 'create %s' % createtime
640-
print 'run %s' % runtime
636+
print ('ITERATION %s:' % i)
637+
print ('elapsed %s' % elapsed)
638+
print ('pull %s' % pulltime)
639+
print ('create %s' % createtime)
640+
print ('run %s' % runtime)
641641

642642
if profile > 0 and time.time() - for_start > profile:
643643
break
644644

645645
row = {'mode':'%s' % runner.mode, 'repo':bench.name, 'bench':bench.name, 'elapsed':sum(elapsed_times) / len(elapsed_times), 'elapsed_pull':sum(pull_times) / len(pull_times), 'elapsed_create':sum(create_times) / len(create_times), 'elapsed_run':sum(run_times) / len(run_times)}
646646
js = json.dumps(row)
647-
print '%s%s,' % (BENCHMARKOUT_MARK, js)
647+
print ('%s%s,' % (BENCHMARKOUT_MARK, js))
648648
sys.stdout.flush()
649649

650650
if __name__ == '__main__':

0 commit comments

Comments
 (0)