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
|
# File 'lib/benchcc/compiler.rb', line 17
def call(input_file:, features:, compiler_executable:, compiler_options:,
compilation_timeout:, execution_timeout:)
stats = {}
Dir.mktmpdir do |tmp_dir|
if features.include?(:compilation_time)
compiler_options << '-ftime-report'
end
if features.include?(:execution_time)
compiler_options << "-o#{tmp_dir}/a.out"
end
if features.include?(:memory_usage)
compiler_executable = "/usr/bin/time -l #{compiler_executable}"
end
command = "#{compiler_executable} #{compiler_options.join(' ')} #{input_file}"
stdout, stderr, status = Timeout::timeout(compilation_timeout) {
Open3.capture3(command)
}
if not status.success?
raise CompilationError.new(" > \#{command}\n \#{stderr}\n\n [compiling:\n \#{input_file.read}\n ]\n EOS\n )\n end\n\n if features.include?(:compilation_time)\n section = -> (title) {\n title.gsub!(' ', '\\s')\n /(\n ===-+===\\n\n .*\#{title}.*\\n\n ===-+===\\n\n (.|\\n)+?(?====)\n )|(\n ===-+===\\n\n .*\#{title}.*\\n\n ===-+===\\n\n (.|\\n)+\n )/x\n }\n time = stderr.match(section[\"Miscellaneous Ungrouped Timers\"]).to_s\n .match(/(\\d+\\.?\\d+).+?Total/)[1]\n stats.merge!({compilation_time: time})\n end\n\n if features.include?(:memory_usage)\n memusg = stderr.match(/(\\d+)\\s+maximum/)[1]\n stats.merge!({memory_usage: memusg})\n end\n\n if features.include?(:execution_time)\n command = \"/usr/bin/time \#{tmp_dir}/a.out\"\n stdout, stderr, status = Timeout::timeout(execution_timeout) {\n Open3.capture3(command)\n }\n if status.success?\n time = stderr.match(/(\\d+\\.?\\d*)\\s+real/)[1]\n stats.merge!({execution_time: time})\n else\n raise ExecutionError.new(<<-EOS.strip_heredoc\n > \#{command}\n \#{stderr}\n\n [running:\n \#{input_file.read}\n ]\n EOS\n )\n end\n end\n\n return stats\n end\nend\n".strip_heredoc
|