36
36
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37
37
# SOFTWARE.
38
38
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
40
40
41
41
TMP_DIR = tempfile .mkdtemp ()
42
42
LEGACY_MODE = "legacy"
@@ -176,7 +176,7 @@ def __init__(self, repository='docker.io/library', srcrepository='docker.io/libr
176
176
elif runtime == "podman" :
177
177
self .controller = PodmanController ()
178
178
else :
179
- print 'Unknown runtime mode: ' + runtime
179
+ print ( 'Unknown runtime mode: ' + runtime )
180
180
exit (1 )
181
181
self .repository = repository
182
182
self .srcrepository = srcrepository
@@ -236,15 +236,15 @@ def run_cmd_arg_wait(self, image, cid, runargs):
236
236
stderr = subprocess .STDOUT ,
237
237
stdout = subprocess .PIPE )
238
238
while True :
239
- l = p .stdout .readline ()
239
+ l = p .stdout .readline (). decode ( "utf-8" )
240
240
if l == '' :
241
241
continue
242
- print 'out: ' + l .strip ()
242
+ print ( 'out: ' + l .strip () )
243
243
# are we done?
244
244
if l .find (runargs .waitline ) >= 0 :
245
245
runtime = time .time () - startrun
246
246
# cleanup
247
- print 'DONE'
247
+ print ( 'DONE' )
248
248
cmd = self .controller .task_kill_cmd (cid )
249
249
rc = os .system (cmd )
250
250
assert (rc == 0 )
@@ -262,18 +262,18 @@ def run_cmd_stdin(self, image, cid, runargs):
262
262
cmd = self .controller .task_start_cmd (cid )
263
263
startrun = time .time ()
264
264
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 () )
267
267
runtime = time .time () - startrun
268
- print out
268
+ print ( out )
269
269
assert (p .returncode == 0 )
270
270
return createtime , runtime
271
271
272
272
def run (self , bench , cid ):
273
273
name = bench .name
274
274
image = self .fully_qualify (bench .name )
275
275
276
- print "Pulling the image..."
276
+ print ( "Pulling the image..." )
277
277
pullcmd = self .controller .pull_cmd (image )
278
278
startpull = time .time ()
279
279
rc = os .system (pullcmd )
@@ -291,7 +291,7 @@ def run(self, bench, cid):
291
291
elif name in BenchRunner .CMD_STDIN :
292
292
createtime , runtime = self .run_cmd_stdin (image = image , cid = cid , runargs = BenchRunner .CMD_STDIN [name ])
293
293
else :
294
- print 'Unknown bench: ' + name
294
+ print ( 'Unknown bench: ' + name )
295
295
exit (1 )
296
296
297
297
return pulltime , createtime , runtime
@@ -300,7 +300,7 @@ def convert_echo_hello(self, src, dest, option):
300
300
period = 10
301
301
cmd = ('%s %s -cni -period %s -entrypoint \' ["/bin/sh", "-c"]\' -args \' ["echo hello"]\' %s %s' %
302
302
(self .optimizer , option , period , src , dest ))
303
- print cmd
303
+ print ( cmd )
304
304
rc = os .system (cmd )
305
305
assert (rc == 0 )
306
306
@@ -312,7 +312,7 @@ def convert_cmd_arg(self, src, dest, runargs, option):
312
312
entry = '-entrypoint \' ["/bin/sh", "-c"]\' '
313
313
cmd = ('%s %s -cni -period %s %s %s %s %s' %
314
314
(self .optimizer , option , period , entry , genargs_for_optimization (runargs .arg ), src , dest ))
315
- print cmd
315
+ print ( cmd )
316
316
rc = os .system (cmd )
317
317
assert (rc == 0 )
318
318
@@ -323,10 +323,10 @@ def convert_cmd_arg_wait(self, src, dest, runargs, option):
323
323
a = tmp_copy (a )
324
324
mounts += '--mount type=bind,src=%s,dst=%s,options=rbind ' % (a ,b )
325
325
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 ()])
327
327
cmd = ('%s %s -cni -period %s %s %s %s %s %s' %
328
328
(self .optimizer , option , period , mounts , env , genargs_for_optimization (runargs .arg ), src , dest ))
329
- print cmd
329
+ print ( cmd )
330
330
rc = os .system (cmd )
331
331
assert (rc == 0 )
332
332
@@ -339,36 +339,36 @@ def convert_cmd_stdin(self, src, dest, runargs, option):
339
339
period = 60
340
340
cmd = ('%s %s -i -cni -period %s %s -entrypoint \' ["/bin/sh", "-c"]\' %s %s %s' %
341
341
(self .optimizer , option , period , mounts , genargs_for_optimization (runargs .stdin_sh ), src , dest ))
342
- print cmd
342
+ print ( cmd )
343
343
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 )
347
347
p .wait ()
348
348
assert (p .returncode == 0 )
349
349
350
350
def push_img (self , dest ):
351
351
cmd = '%s %s' % (self .pusher , dest )
352
- print cmd
352
+ print ( cmd )
353
353
rc = os .system (cmd )
354
354
assert (rc == 0 )
355
355
356
356
def pull_img (self , src ):
357
357
cmd = '%s %s' % (self .puller , src )
358
- print cmd
358
+ print ( cmd )
359
359
rc = os .system (cmd )
360
360
assert (rc == 0 )
361
361
362
362
def copy_img (self , src , dest ):
363
363
cmd = 'crane copy %s %s' % (src , dest )
364
- print cmd
364
+ print ( cmd )
365
365
rc = os .system (cmd )
366
366
assert (rc == 0 )
367
367
368
368
def convert_and_push_img (self , src , dest ):
369
369
self .pull_img (src )
370
370
cmd = '%s --no-optimize %s %s' % (self .optimizer , src , dest )
371
- print cmd
371
+ print ( cmd )
372
372
rc = os .system (cmd )
373
373
assert (rc == 0 )
374
374
self .push_img (dest )
@@ -384,7 +384,7 @@ def optimize_img(self, name, src, dest, option):
384
384
elif name in BenchRunner .CMD_STDIN :
385
385
self .convert_cmd_stdin (src = src , dest = dest , runargs = BenchRunner .CMD_STDIN [name ], option = option )
386
386
else :
387
- print 'Unknown bench: ' + name
387
+ print ( 'Unknown bench: ' + name )
388
388
exit (1 )
389
389
self .push_img (dest )
390
390
@@ -403,7 +403,7 @@ def operation(self, op, bench, cid):
403
403
self .prepare (bench )
404
404
return 0 , 0 , 0
405
405
else :
406
- print 'Unknown operation: ' + op
406
+ print ( 'Unknown operation: ' + op )
407
407
exit (1 )
408
408
409
409
class ContainerdController :
@@ -415,15 +415,15 @@ def pull_cmd(self, image):
415
415
if self .is_lazypull :
416
416
base_cmd = "ctr-remote i rpull"
417
417
cmd = '%s %s' % (base_cmd , image )
418
- print cmd
418
+ print ( cmd )
419
419
return cmd
420
420
421
421
def create_echo_hello_cmd (self , image , cid ):
422
422
snapshotter_opt = ""
423
423
if self .is_lazypull :
424
424
snapshotter_opt = "--snapshotter=stargz"
425
425
cmd = '%s c create --net-host %s -- %s %s echo hello' % (CTR , snapshotter_opt , image , cid )
426
- print cmd
426
+ print ( cmd )
427
427
return cmd
428
428
429
429
def create_cmd_arg_cmd (self , image , cid , runargs ):
@@ -433,21 +433,21 @@ def create_cmd_arg_cmd(self, image, cid, runargs):
433
433
cmd = '%s c create --net-host %s ' % (CTR , snapshotter_opt )
434
434
cmd += '-- %s %s ' % (image , cid )
435
435
cmd += runargs .arg
436
- print cmd
436
+ print ( cmd )
437
437
return cmd
438
438
439
439
def create_cmd_arg_wait_cmd (self , image , cid , runargs ):
440
440
snapshotter_opt = ""
441
441
if self .is_lazypull :
442
442
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 ()])
444
444
cmd = '%s c create --net-host %s %s ' % (CTR , snapshotter_opt , env )
445
445
for a ,b in runargs .mount :
446
446
a = os .path .join (os .path .dirname (os .path .abspath (__file__ )), a )
447
447
a = tmp_copy (a )
448
448
cmd += '--mount type=bind,src=%s,dst=%s,options=rbind ' % (a ,b )
449
449
cmd += '-- %s %s %s' % (image , cid , runargs .arg )
450
- print cmd
450
+ print ( cmd )
451
451
return cmd
452
452
453
453
def create_cmd_stdin_cmd (self , image , cid , runargs ):
@@ -463,30 +463,30 @@ def create_cmd_stdin_cmd(self, image, cid, runargs):
463
463
if runargs .stdin_sh :
464
464
cmd += runargs .stdin_sh # e.g., sh -c
465
465
466
- print cmd
466
+ print ( cmd )
467
467
return cmd
468
468
469
469
def task_start_cmd (self , cid ):
470
470
cmd = '%s t start %s' % (CTR , cid )
471
- print cmd
471
+ print ( cmd )
472
472
return cmd
473
473
474
474
def task_kill_cmd (self , cid ):
475
475
cmd = '%s t kill -s 9 %s' % (CTR , cid )
476
- print cmd
476
+ print ( cmd )
477
477
return cmd
478
478
479
479
def cleanup (self , name , image ):
480
- print "Cleaning up environment..."
480
+ print ( "Cleaning up environment..." )
481
481
cmd = '%s t kill -s 9 %s' % (CTR , name )
482
- print cmd
482
+ print ( cmd )
483
483
rc = os .system (cmd ) # sometimes containers already exit. we ignore the failure.
484
484
cmd = '%s c rm %s' % (CTR , name )
485
- print cmd
485
+ print ( cmd )
486
486
rc = os .system (cmd )
487
487
assert (rc == 0 )
488
488
cmd = '%s image rm %s' % (CTR , image )
489
- print cmd
489
+ print ( cmd )
490
490
rc = os .system (cmd )
491
491
assert (rc == 0 )
492
492
@@ -500,29 +500,29 @@ def start_profile(self, seconds):
500
500
class PodmanController :
501
501
def pull_cmd (self , image ):
502
502
cmd = '%s pull docker://%s' % (PODMAN , image )
503
- print cmd
503
+ print ( cmd )
504
504
return cmd
505
505
506
506
def create_echo_hello_cmd (self , image , cid ):
507
507
cmd = '%s create --name %s docker://%s echo hello' % (PODMAN , cid , image )
508
- print cmd
508
+ print ( cmd )
509
509
return cmd
510
510
511
511
def create_cmd_arg_cmd (self , image , cid , runargs ):
512
512
cmd = '%s create --name %s docker://%s ' % (PODMAN , cid , image )
513
513
cmd += runargs .arg
514
- print cmd
514
+ print ( cmd )
515
515
return cmd
516
516
517
517
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 ()])
519
519
cmd = '%s create %s --name %s ' % (PODMAN , env , cid )
520
520
for a ,b in runargs .mount :
521
521
a = os .path .join (os .path .dirname (os .path .abspath (__file__ )), a )
522
522
a = tmp_copy (a )
523
523
cmd += '--mount type=bind,src=%s,dst=%s ' % (a ,b )
524
524
cmd += ' docker://%s %s ' % (image , runargs .arg )
525
- print cmd
525
+ print ( cmd )
526
526
return cmd
527
527
528
528
def create_cmd_stdin_cmd (self , image , cid , runargs ):
@@ -535,51 +535,51 @@ def create_cmd_stdin_cmd(self, image, cid, runargs):
535
535
if runargs .stdin_sh :
536
536
cmd += runargs .stdin_sh # e.g., sh -c
537
537
538
- print cmd
538
+ print ( cmd )
539
539
return cmd
540
540
541
541
def task_start_cmd (self , cid ):
542
542
cmd = '%s start -a %s' % (PODMAN , cid )
543
- print cmd
543
+ print ( cmd )
544
544
return cmd
545
545
546
546
def task_kill_cmd (self , cid ):
547
547
cmd = '%s kill -s 9 %s' % (PODMAN , cid )
548
- print cmd
548
+ print ( cmd )
549
549
return cmd
550
550
551
551
def cleanup (self , name , image ):
552
- print "Cleaning up environment..."
552
+ print ( "Cleaning up environment..." )
553
553
cmd = '%s kill -s 9 %s' % (PODMAN , name )
554
- print cmd
554
+ print ( cmd )
555
555
rc = os .system (cmd ) # sometimes containers already exit. we ignore the failure.
556
556
cmd = '%s rm %s' % (PODMAN , name )
557
- print cmd
557
+ print ( cmd )
558
558
rc = os .system (cmd )
559
559
assert (rc == 0 )
560
560
cmd = '%s image rm %s' % (PODMAN , image )
561
- print cmd
561
+ print ( cmd )
562
562
rc = os .system (cmd )
563
563
assert (rc == 0 )
564
564
cmd = os .path .join (os .path .dirname (os .path .abspath (__file__ )), '../reboot_store.sh' )
565
- print cmd
565
+ print ( cmd )
566
566
rc = os .system (cmd )
567
567
assert (rc == 0 )
568
568
569
569
def main ():
570
570
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 ) )
583
583
exit (1 )
584
584
585
585
benches = []
@@ -594,11 +594,11 @@ def main():
594
594
benches .extend (BenchRunner .ALL .values ())
595
595
elif parts [0 ] == 'list' :
596
596
template = '%-16s\t %-20s'
597
- print template % ('CATEGORY' , 'NAME' )
597
+ print ( template % ('CATEGORY' , 'NAME' ) )
598
598
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 ) )
600
600
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 ()]) )
602
602
else :
603
603
benches .append (BenchRunner .ALL [arg ])
604
604
@@ -633,18 +633,18 @@ def main():
633
633
pull_times .append (pulltime )
634
634
create_times .append (createtime )
635
635
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 )
641
641
642
642
if profile > 0 and time .time () - for_start > profile :
643
643
break
644
644
645
645
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 )}
646
646
js = json .dumps (row )
647
- print '%s%s,' % (BENCHMARKOUT_MARK , js )
647
+ print ( '%s%s,' % (BENCHMARKOUT_MARK , js ) )
648
648
sys .stdout .flush ()
649
649
650
650
if __name__ == '__main__' :
0 commit comments