-
Notifications
You must be signed in to change notification settings - Fork 18
/
benchmark
executable file
·1976 lines (1734 loc) · 75.7 KB
/
benchmark
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# TODO use class Row
import sys
import argparse
import subprocess as sp
import os
import stat
import os.path
from shutil import which
import time
# from pprint import pprint
import random
from datetime import datetime
from os.path import expanduser
from string import Template as Tm
from timeit import default_timer as timer
import importlib
DEFAULT_PROGRAM_NAME = 'main'
SUPPORTED_OPERATIONS = ['ast-check', 'check', 'compile', 'build']
LANGUAGE_EXES = {'C': ['tcc', 'cuik', 'cproc', 'gcc', 'clang', ],
'C3': ['c3c', ],
'C++': ['g++', 'clang++', ],
'D': ['dmd', 'ldmd2', 'gdc', ],
'Zig': ['zig', ],
'Nim': ['nim', ],
'Rust': ['rustc', ],
'Ada': ['gnat', ],
'Vox': ['vox', ],
'C#': ['mcs', # mcs is Mono C# compiler version 6.12.0.200
'csc'], # csc is Microsoft (R) Visual C# Compiler
'Go': ['go', 'gccgo', ],
# 'V': ['v', ],
'Swift': ['swiftc', ],
'Java': ['javac', ],
'Julia': ['julia', ],
'OCaml': ['ocamlc', 'ocamlopt', ],
'Pareas': ['pareas', ],
'Mojo': ['mojo', ],
}
TEMPLATED_SUPPORTED_LANGUAGES = ['C++', 'Java', 'D', 'Swift', 'Vox', 'Rust',
'Zig', 'V', 'C3', 'Julia', 'Mojo']
C_WARN_FLAGS = ['-Wall', '-Wextra']
CXX_WARN_FLAGS = ['-Wno-c++11-extensions']
RUSTC_NO_WARNINGS_FLAGS = ['-A', 'warnings'] # disable warnings for `rustc`
RUSTC_FLAGS = [] # set to `RUSTC_NO_WARNINGS_FLAGS` to disable warnings
RUSTC_NIGHTLY_FLAGS = ['-Z', 'threads=8'] # See: https://blog.rust-lang.org/2023/11/09/parallel-rustc.html
JULIA_INTERPRET_FLAGS = ['--compile=min'] # See: https://github.com/JuliaLang/julia/issues/41360#issuecomment-872075102
JULIA_COMPILE_FLAGS = ['-O0'] # See: https://github.com/JuliaLang/julia/issues/41360#issuecomment-872075102
ROOT_PATH = 'generated'
RANGE = range(5, 20)
VERSIONS = [''] + [f"-{i}" for i in RANGE]
# instead of home = os.getenv('HOME') that doesn’t work on Windows
HOME = expanduser('~')
class Row: # TODO use
"""Result row."""
__slots__ = ['lang',
'templated',
'check_dur_per_func',
'build_dur_per_func',
'run_dur_per_func',
'rss_mu',
'exe_version',
'exe_path']
def __init__(self,
lang,
templated,
check_dur_per_func,
build_dur_per_func,
run_dur_per_func,
rss_mu,
exe_version,
exe_path):
self.lang = lang
self.templated = templated
self.check_dur_per_func = check_dur_per_func
self.build_dur_per_func = build_dur_per_func
self.run_dur_per_func = run_dur_per_func
self.rss_mu = rss_mu
self.exe_version = exe_version
self.exe_pat = exe_path
MARKDOWN_TABLE_TITLES = [
'Lang-uage',
'Temp-lated',
'AST-Check Time [us/fn]',
'Check Time [us/fn]',
'Compile Time [us/fn]',
'Build Time [us/fn]',
'Run Time [us/fn]',
'Check RSS [kB/fn]',
'Build RSS [kB/fn]',
'Exec Version',
'Exec Path',
]
LANG_IX = 0
TEMPLATED_IX = 1
AST_CHECK_DUR_PER_FUNC_IX = 2 # AST-check duration
CHECK_DUR_PER_FUNC_IX = 3 # check duration
COMPILE_DUR_PER_FUNC_IX = 4 # compilation duration
BUILD_DUR_PER_FUNC_IX = 5 # build duration
RUN_DUR_PER_FUNC_IX = 6 # run duration
CHECK_RSS_MU_IX = 7 # check RSS memory usage
BUILD_RSS_MU_IX = 8 # build RSS memory usage
EXE_VERSION_IX = 9
EXE_PATH_IX = 10
MARKDOWN_TABLE_LENGTH = EXE_PATH_IX + 1
assert len(MARKDOWN_TABLE_TITLES) == MARKDOWN_TABLE_LENGTH
def table_row(row,
args,
lang,
op,
exe_path,
compiler_version,
ast_check_dur,
check_dur,
compile_dur,
build_dur,
run_dur,
check_rss_mu,
build_rss_mu,
templated):
if exe_path.startswith(HOME):
exe_path = os.path.join('~', exe_path.lstrip(HOME))
row[LANG_IX] = lang
row[TEMPLATED_IX] = 'Yes' if templated else 'No'
if ast_check_dur is not None:
row[AST_CHECK_DUR_PER_FUNC_IX] = ast_check_dur / (args.function_count * args.function_depth)
if check_dur is not None:
row[CHECK_DUR_PER_FUNC_IX] = check_dur / (args.function_count * args.function_depth)
if compile_dur is not None:
row[COMPILE_DUR_PER_FUNC_IX] = compile_dur / (args.function_count * args.function_depth)
if build_dur is not None:
row[BUILD_DUR_PER_FUNC_IX] = build_dur / (args.function_count * args.function_depth)
if run_dur is not None:
row[RUN_DUR_PER_FUNC_IX] = run_dur / (args.function_count * args.function_depth)
if isinstance(check_rss_mu, (int, float)):
row[CHECK_RSS_MU_IX] = check_rss_mu / (args.function_count * args.function_depth)
elif check_rss_mu is not None:
row[CHECK_RSS_MU_IX] = check_rss_mu
if isinstance(build_rss_mu, (int, float)):
row[BUILD_RSS_MU_IX] = build_rss_mu / (args.function_count * args.function_depth)
elif build_rss_mu is not None:
row[BUILD_RSS_MU_IX] = build_rss_mu
row[EXE_VERSION_IX] = compiler_version
row[EXE_PATH_IX] = os.path.basename(exe_path)
return row
RANDOMIZE_FLAG = True
if RANDOMIZE_FLAG:
random.seed(str(datetime.now()))
def get_version(version_run):
result = next(part
for part
in version_run.stdout.decode('utf-8').split()
if (part[0].isdigit() or
part[0] == 'v' and part[1].isdigit())) # v2.096.1-beta.1-187-gb25be89b3
return result.split('~')[0].split('ubuntu1')[0] # strip ubuntu version
def touchFile(path):
with open(path, 'a'):
os.utime(path, None)
def out_directory(lang):
return os.path.join(ROOT_PATH,
lang.lower())
def out_binary(lang):
return DEFAULT_PROGRAM_NAME
def sourceIdOf(lang, templated, run_ix):
if lang in TEMPLATED_SUPPORTED_LANGUAGES:
key = lang + ('-Templated' if templated else '-Untemplated')
else:
key = lang
return key + "-" + str(run_ix)
def fill_in_speedups(results, column_ix):
# calculate minimum
min_key = None # key of minimum
min_val = None # value of minimum
for key, result in results.items():
if isinstance(result[column_ix], (int, float)):
if min_val is None or (min_val > result[column_ix]):
min_key = key
min_val = result[column_ix]
if min_key is not None:
rel_tag = results[min_key][EXE_PATH_IX]
for key, result in results.items():
result[column_ix] = stringify_metrics(abs_n=result[column_ix],
min_val=min_val,
column_ix=column_ix,
rel_tag=rel_tag)
def stringify_metrics(abs_n, min_val, column_ix, rel_tag):
if isinstance(abs_n, str):
return '{:>13}'.format(abs_n) # propagate as is
if abs_n is None:
return '{:>6}'.format('N/A') # https://en.wikipedia.org/wiki/N/A
if abs_n == min_val:
rel_str = " (best)"
else:
rel_n = abs_n / min_val
rel_str = " (" + factor_str(rel_n) + "x" ")"
if column_ix == AST_CHECK_DUR_PER_FUNC_IX:
return to_str_in_microseconds(abs_n) + rel_str
elif column_ix == CHECK_DUR_PER_FUNC_IX:
return to_str_in_microseconds(abs_n) + rel_str
elif column_ix == COMPILE_DUR_PER_FUNC_IX:
return to_str_in_microseconds(abs_n) + rel_str
elif column_ix == BUILD_DUR_PER_FUNC_IX:
return to_str_in_microseconds(abs_n) + rel_str
elif column_ix == RUN_DUR_PER_FUNC_IX:
return to_str_in_nanoseconds(abs_n) + rel_str
elif column_ix == CHECK_RSS_MU_IX:
return to_str_memory_usage(abs_n) + rel_str
elif column_ix == BUILD_RSS_MU_IX:
return to_str_memory_usage(abs_n) + rel_str
def to_str_in_microseconds(dur):
return '{:6.1f}'.format(1e6 * dur)
def to_str_in_nanoseconds(dur):
return '{:6.0f}'.format(1e9 * dur) if dur is not None else ''
def factor_str(factor):
return '{:.1f}'.format(factor)
def to_str_memory_usage(mu):
return '{:6.1f}'.format(mu / 1e3)
def repeat_to_length(string_to_expand, length):
return (string_to_expand * (int(length / len(string_to_expand)) + 1))[:length]
def markdown_header(text, nr): # Markdown header
return '#' * nr + ' ' + text
def markdown_table(titles, results):
fill_in_speedups(results=results, column_ix=AST_CHECK_DUR_PER_FUNC_IX)
fill_in_speedups(results=results, column_ix=CHECK_DUR_PER_FUNC_IX)
fill_in_speedups(results=results, column_ix=COMPILE_DUR_PER_FUNC_IX)
fill_in_speedups(results=results, column_ix=BUILD_DUR_PER_FUNC_IX)
fill_in_speedups(results=results, column_ix=RUN_DUR_PER_FUNC_IX)
fill_in_speedups(results=results, column_ix=CHECK_RSS_MU_IX)
fill_in_speedups(results=results, column_ix=BUILD_RSS_MU_IX)
result = ''
result += '| '
for col in titles:
result += str(col) + ' | '
result += '\n'
result += '| '
for ix, col in enumerate(range(len(titles))):
if ix == TEMPLATED_IX:
result += repeat_to_length("-", len(titles[ix])) + ' | '
else:
result += ':' + repeat_to_length("-", len(titles[ix]) - 2) + ':' + ' | '
result += '\n'
for key, row in results.items():
result += '| '
for ix, cl in enumerate(range(len(titles))):
col = row[ix]
col_str = str(col) if col is not None else 'N/A'
result += col_str + repeat_to_length(" ", len(titles[ix]) - len(col_str)) + ' | '
result += '\n'
return result
def match_lang_exe(args, lang, exe_name):
try:
if args.language_exes[lang] and exe_name not in args.language_exes[lang]:
return None
except KeyError:
pass
exe_path = which(exe_name)
return exe_path
def bench_Ada_using_gcc(results, lang, code_paths, args, op, templated):
exe_flags = ['compile'] if op == 'build' else ['check']
for gcc_version in VERSIONS:
exe = match_lang_exe(args, lang, 'gnat' + str(gcc_version))
if exe:
version = get_version(sp.run([exe, '--version'], stdout=sp.PIPE))
compile_file(code_paths,
out_flag_and_exe=['-o', out_binary(lang)],
exe=exe,
runner=True,
exe_flags=exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_C_using_cproc(results, lang, code_paths, args, op, templated):
if op == 'check':
exe_flags = ['-emit-qbe']
out_flag_and_exe = ['-o', '/dev/null']
elif op == 'compile':
exe_flags = ['-c']
out_flag_and_exe = ['-o', '/dev/null']
elif op == 'build':
exe_flags = []
out_flag_and_exe = ['-o', out_binary(lang)]
# Disable build for now until I find out how to fix linker errors:
# ld: error: unable to find library -l:crt1.o
# ld: error: unable to find library -l:crti.o
# ld: error: unable to find library -lc
# ld: error: unable to find library -l:crtn.o
# cproc: link: process 2424876 exited with status 1
# return False
else:
return None
exe = match_lang_exe(args, lang, 'cproc')
if exe:
version = 'unknown'
compile_file(code_paths,
out_flag_and_exe=out_flag_and_exe,
exe=exe,
runner=(op == 'build'),
exe_flags=exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_C_using_tcc(results, lang, code_paths, args, op, templated):
if op == 'check':
exe_flags = ['-c']
out_flag_and_exe = ['-o', '/dev/null']
elif op == 'compile':
exe_flags = ['-c']
out_flag_and_exe = []
elif op == 'build':
exe_flags = [] # TODO add version that skips linking via: exe_flags = ['-c']
out_flag_and_exe = ['-o', out_binary(lang)]
else:
return None
exe = match_lang_exe(args, lang, 'tcc')
if exe:
version = get_version(sp.run([exe, '-v'], stdout=sp.PIPE))
compile_file(code_paths,
out_flag_and_exe=out_flag_and_exe,
exe=exe,
runner=(op == 'build'),
exe_flags=exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_C_using_cuik(results, lang, code_paths, args, op, templated):
if op == 'check':
exe_flags = ['-c', '-xe']
out_flag_and_exe = []
elif op == 'compile':
exe_flags = ['-c']
out_flag_and_exe = []
elif op == 'build':
exe_flags = [] # TODO add version that skips linking via: exe_flags = ['-c']
out_flag_and_exe = ['-o', out_binary(lang)]
else:
return None
exe = match_lang_exe(args, lang, 'cuik')
if exe:
version = "~master"
compile_file(code_paths,
out_flag_and_exe=out_flag_and_exe,
exe=exe,
runner=(op == 'build'),
exe_flags=exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def gcc_clang_op_flags(op, lang):
if op == 'check':
exe_flags = ['-c', '-fsyntax-only']
out_flag_and_exe = []
elif op == 'compile':
exe_flags = ['-c']
out_flag_and_exe = []
elif op == 'build':
exe_flags = [] # TODO add version that skips linking via: exe_flags = ['-c']
out_flag_and_exe = ['-o', out_binary(lang)]
else:
return (None, None)
return (exe_flags, out_flag_and_exe)
def bench_C_and_Cxx_using_gcc(results, lang, code_paths, args, op,
templated):
(exe_flags, out_flag_and_exe) = gcc_clang_op_flags(op, lang)
if exe_flags is None:
return None
FLAGS = []
if lang == 'C':
FLAGS = C_WARN_FLAGS
elif lang == 'C++':
FLAGS = CXX_WARN_FLAGS
for gcc_version in VERSIONS:
if lang == 'C':
exe = match_lang_exe(args, lang, 'gcc' + str(gcc_version))
elif lang == 'C++':
exe = match_lang_exe(args, lang, 'g++' + str(gcc_version))
else:
return None
if exe:
output = sp.run([exe, '--version'], stdout=sp.PIPE).stdout
version = output.decode('utf-8').split()[2].split('-')[0]
compile_file(code_paths,
out_flag_and_exe=out_flag_and_exe,
exe=exe,
runner=True,
exe_flags=exe_flags + FLAGS,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_C_and_Cxx_using_clang(results, lang, code_paths, args, op,
templated):
(exe_flags, out_flag_and_exe) = gcc_clang_op_flags(op, lang)
if exe_flags is None:
return None
FLAGS = []
if lang == 'C':
FLAGS = C_WARN_FLAGS + ['-fno-color-diagnostics', '-fno-caret-diagnostics', '-fno-diagnostics-show-option']
elif lang == 'C++':
FLAGS = CXX_WARN_FLAGS
for clang_version in VERSIONS:
if lang == 'C':
exe = match_lang_exe(args, lang, 'clang' + str(clang_version))
elif lang == 'C++':
exe = match_lang_exe(args, lang, 'clang' + str(clang_version))
else:
return None
if exe:
version = get_version(sp.run([exe, '--version'], stdout=sp.PIPE))
compile_file(code_paths,
out_flag_and_exe=out_flag_and_exe,
exe=exe,
runner=True,
exe_flags=FLAGS + exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_D(results, code_paths, args, op, templated, use_dips):
lang = 'D'
d_flags = ['-w', '-dip1008', '-dip1000'] if use_dips else []
if op == 'check':
exe_flags = ['-o-']
elif op == 'compile':
exe_flags = ['-c']
elif op == 'build':
if which('ld.lld') is not None:
exe_flags = ["-Xcc=-fuse-ld=lld"]
elif which('ld.gold') is not None:
exe_flags = ["-Xcc=-fuse-ld=gold"]
else:
exe_flags = []
else:
return None
try:
exes = args.language_exes[lang]
except KeyError:
return
if not exes:
exes = LANGUAGE_EXES[lang]
# dmd
for exe in filter(lambda exe: which(exe), exes):
if not which(exe):
continue
if exe.startswith('dmd'):
version = get_version(sp.run([exe, '--version'], stdout=sp.PIPE))
compile_file(code_paths=code_paths,
out_flag_and_exe=['-of=' + out_binary(lang)] if op == 'build' else [],
exe=exe,
runner=True,
exe_flags=d_flags + exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
elif exe.startswith('ldmd2'):
version = sp.run([exe, '--version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[6][1:-2]
compile_file(code_paths=code_paths,
out_flag_and_exe=['-of=' + out_binary(lang)] if op == 'build' else [],
exe=exe,
runner=True,
exe_flags=d_flags + exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
elif exe.startswith('gdc'):
exe_flags = ['-fsyntax-only'] if op == 'check' else []
version = sp.run([exe, '--version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[3]
compile_file(code_paths=code_paths,
out_flag_and_exe=['-o' + out_binary(lang)] if op == 'build' else [],
exe=exe,
runner=True,
exe_flags=exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_Vox(results, code_paths, args, op, templated):
lang = 'Vox'
if op == 'check':
exe_flags = ['--check-only']
out_flag_and_exe = []
elif op == 'compile':
return None # no supported
elif op == 'build':
exe_flags = []
out_flag_and_exe = ['--of=' + out_binary(lang)]
else:
return None
exe = match_lang_exe(args, lang, 'vox')
if exe:
compile_file(code_paths,
out_flag_and_exe=out_flag_and_exe,
exe=exe,
runner=True, # os.name == 'nt', # only on Windows for now. TODO https://forum.dlang.org/post/[email protected]
exe_flags=exe_flags,
args=args,
op=op,
compiler_version='master', # TODO lookup Git version
lang=lang,
templated=templated,
results=results)
def bench_CSharp_using_mono(results, code_paths, args, op, templated):
lang = 'C#'
exe = match_lang_exe(args, lang, 'mcs')
if exe:
exe_flags = ['-target:exe'] + ([] if op == 'build' else [''])
version = sp.run([exe, '--version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[4]
compile_file(code_paths,
out_flag_and_exe=['-out:' + out_binary(lang)],
exe=exe,
runner=True,
exe_flags=exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_CSharp_using_csc(results, code_paths, args, op, templated):
lang = 'C#'
exe = match_lang_exe(args, lang, 'csc')
if exe:
exe_flags = ['-target:exe'] + ([] if op == 'build' else [''])
version = sp.run([exe, '-version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[0]
compile_file(code_paths,
out_flag_and_exe=['-out:' + out_binary(lang)],
exe=exe,
runner=True,
exe_flags=exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_Go(results, code_paths, args, op, templated):
bench_Go_using_go(results, code_paths, args, op, templated)
bench_Go_using_gccgo(results, code_paths, args, op, templated)
def bench_Go_using_go(results, code_paths, args, op, templated):
lang = 'Go'
version = None # unknown
if op == 'build':
exe = which('go')
exe_flags = ['build']
version = sp.run([exe, 'version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[2][2:]
out_flag_and_exe = ['-o', out_binary(lang)]
elif op == 'check':
exe = which('gotype')
exe_flags = []
version_exe = which('go') # guess it be same as `go`
if version_exe is not None:
version = sp.run([version_exe, 'version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[2][2:]
out_flag_and_exe = []
else:
return None
exe = match_lang_exe(args, lang, exe)
if exe:
compile_file(code_paths,
out_flag_and_exe=out_flag_and_exe,
exe=exe,
runner=True,
exe_flags=exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_Go_using_gccgo(results, code_paths, args, op, templated):
lang = 'Go'
for gccgo_version in VERSIONS:
exe = match_lang_exe(args, lang, 'gccgo' + str(gccgo_version))
if exe:
version = sp.run([exe, '--version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[3]
compile_file(code_paths,
out_flag_and_exe=[], # TODO this fails out_flag_and_exe=['-o', out_binary(lang)],
exe=exe,
runner=True,
exe_flags=[] if op == 'build' else ['-fsyntax-only', '-S'],
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_Swift(results, code_paths, args, op, templated):
lang = 'Swift'
exe = (match_lang_exe(args, lang, 'swiftc') or
which(os.path.join(HOME, '.local/swift-5.3.3-RELEASE-ubuntu20.04/usr/bin/swiftc')))
if op == 'build':
exe_flags = ['']
elif op == 'check':
exe_flags = ['-typecheck']
else:
return None
if exe:
version = sp.run([exe, '--version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[2]
compile_file(code_paths,
out_flag_and_exe=['-o', out_binary(lang)],
exe=exe,
runner=True,
exe_flags=exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_OCaml(results, code_paths, args, op, templated, bytecode):
lang = 'OCaml'
if bytecode:
exe = match_lang_exe(args, lang, 'ocamlc')
else:
exe = match_lang_exe(args, lang, 'ocamlopt')
if exe:
version = sp.run([exe, '--version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[0]
compile_file(code_paths,
out_flag_and_exe=['-o', out_binary(lang)],
exe=exe,
runner=([which('ocamlrun')] if
bytecode else
True),
exe_flags=['-c'] if bytecode else ['-linscan'],
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_V(results, code_paths, args, op, templated):
lang = 'V' # vlang.io
exe = match_lang_exe(args, lang, 'v')
if exe:
version = sp.run([exe, '--version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[1]
compile_file(code_paths,
out_flag_and_exe=['-o', out_binary(lang)],
exe=exe,
runner=True,
exe_flags=[], # -backend flag fails
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_C3(results, code_paths, args, op, templated):
lang = 'C3'
exe = (match_lang_exe(args, lang, 'c3c') or
which(os.path.join(HOME, 'ware/c3c/build/c3c')))
if exe:
exe_flags = ['-O0']
if op == 'check':
exe_flags += ['compile-only'] + ['-C']
elif op == 'compile':
exe_flags += ['compile-only']
else:
exe_flags += ['compile']
version = sp.run([exe, '--version'], stdout=sp.PIPE).stdout.decode('utf-8').split(':')[1].split()[0]
compile_file(code_paths,
out_flag_and_exe=['-o', out_binary(lang)],
exe=exe,
runner=True,
exe_flags=exe_flags,
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
zig_no_llvm_build_flags = ['-fno-llvm', '-fno-lld', '-fno-clang'] # See https://stackoverflow.com/a/78995648/683710 and https://github.com/ziglang/zig/issues/6251
def bench_Zig(results, code_paths, args, op, templated):
lang = 'Zig'
if op == 'ast-check': # full syntactic analysis and partial semantic analysis
exe_flags = ['ast-check']
out_flag_and_exe = []
elif op == 'check':
exe_flags = ['build-obj', '-fno-emit-bin'] + zig_no_llvm_build_flags
out_flag_and_exe = []
elif op == 'compile':
exe_flags = ['build-obj'] + zig_no_llvm_build_flags
out_flag_and_exe = []
elif op == 'build':
exe_flags = ['build-exe'] + zig_no_llvm_build_flags
out_flag_and_exe = ['--name', out_binary(lang)]
else:
return None
exe = match_lang_exe(args, lang, 'zig')
if exe:
version = sp.run([exe, 'version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[0]
compile_file(code_paths,
out_flag_and_exe=out_flag_and_exe,
exe=exe,
runner=True,
exe_flags=exe_flags,
args=args, # no syntax flag currently so compile to object file instead
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_Mojo(results, code_paths, args, op, templated):
lang = 'Mojo'
exe = match_lang_exe(args, lang, 'mojo')
if exe:
version = sp.run([exe, '--version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[1]
compile_file(code_paths,
out_flag_and_exe=['-o', out_binary(lang)],
exe=exe,
runner=True,
exe_flags=['build', '-O0'],
args=args, # no syntax flag currently so compile to object file instead
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_Nim(results, code_paths, args, op, templated, exe_flags=None):
lang = 'Nim'
exe = match_lang_exe(args, lang, 'nim')
# tcc_exe = which('tcc')
# exe_flags = ['--hints:off', '--checks:off', '--stacktrace:off'] + ([('--cc:tcc')] if tcc_exe else [])
exe_flags = ['--hints:off', '--checks:off', '--stacktrace:off']
if op == 'check':
exe_flags += ['check']
elif op == 'build':
# the bottleneck is clang compilation
exe_flags += ['c', '--mm:refc', '--opt:none'] # TODO detect when --gc:arc is available
else:
return None
if exe:
version = sp.run([exe, '--version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[3]
compile_file(code_paths,
out_flag_and_exe=['--out:' + out_binary(lang)],
exe=exe,
runner=True,
exe_flags=exe_flags,
args=args, # no syntax flag currently so compile to object file instead
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def set_rustup_channel(channel):
with sp.Popen(['rustup', 'default', channel],
stdout=sp.PIPE,
stderr=sp.PIPE) as proc:
proc.communicate()
def rust_flags(channel):
flags = RUSTC_FLAGS
if channel == 'nightly':
process = sp.Popen('rustc -Z help', shell=True, stdout=sp.PIPE)
output, error = process.communicate()
if 'threads=val' in output.decode('utf-8'):
flags += RUSTC_NIGHTLY_FLAGS
return flags
def bench_Rust(results, code_paths, args, op, templated):
lang = 'Rust'
rustup_exe = which('rustup')
if rustup_exe:
rustup_channels = ['stable', 'nightly']
else:
rustup_channels = [None]
try:
exes = args.language_exes[lang]
except KeyError:
return
if not exes:
exes = LANGUAGE_EXES[lang]
for channel in rustup_channels:
if rustup_exe is not None:
set_rustup_channel(channel)
for exe in exes:
version = sp.run([exe, '--version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[1]
if op == 'check':
# `cargo check` uses `rustc --emit=metadata`
check_args = ['--emit=metadata']
out_flag_and_exe = []
elif op == 'build':
out_flag_and_exe = ['-o', out_binary(lang)]
else:
continue
exe_flags = ([] if op == 'build' else check_args) + rust_flags(channel)
compile_file(code_paths,
out_flag_and_exe=out_flag_and_exe,
exe=exe,
runner=True,
exe_flags=exe_flags, # https://github.com/rust-lang/rfcs/issues/1476
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_Java(results, code_paths, args, op, templated):
lang = 'Java'
exe = match_lang_exe(args, lang, 'javac')
if exe:
try:
task = sp.run([exe, '-version'],
stdout=sp.PIPE,
stderr=sp.PIPE)
version = (task.stdout or task.stderr).decode('utf-8').split()[1]
except Exception:
print("WARNING: Failed to decode version from neither stdout:" + str(task.stdout) + " nor stderr:" + str(task.stderr) +
" of command " + str([exe, '-version']) + ", defaulting version of " + lang + " to `none`",
file=sys.stderr)
version = 'unknown'
compile_file(code_paths,
out_flag_and_exe=[],
exe=exe,
runner=[which('java'), '-classpath', '.'],
exe_flags=['-Xdiags:verbose'],
args=args,
op=op,
compiler_version=version,
lang=lang,
templated=templated,
results=results)
def bench_Pareas(results, code_paths, args, op, templated):
lang = 'Pareas'
exe = match_lang_exe(args, lang, 'pareas')
if exe:
compile_file(code_paths,
out_flag_and_exe=['-o', out_binary(lang)],
exe=exe,
runner=False,
exe_flags=['--check'] if op == 'check' else [],
args=args,
op=op,
compiler_version='unknown',
lang=lang,
templated=templated,
results=results)
def bench_Julia(results, code_paths, args, op, templated):
lang = 'Julia'
exe = match_lang_exe(args, lang, 'julia')
if exe:
version = sp.run([exe, '--version'], stdout=sp.PIPE).stdout.decode('utf-8').split()[2]
compile_file(code_paths,
out_flag_and_exe=[],
exe=exe,
runner=False,
exe_flags=JULIA_INTERPRET_FLAGS, # TODO: use JULIA_COMPILE_FLAGS aswell
args=args,
op=op,