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
|
%% ``Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.
%%
%% The Initial Developer of the Original Code is Ericsson Utvecklings AB.
%% Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
%% AB. All Rights Reserved.''
%%
%% $Id$
%%
-module(bench).
%% User interface
-export([run/0]).
%% Exported to be used in spawn
-export([measure/4]).
%% Internal constants
-define(MAX, 999999999999999).
-define(RANGE_MAX, 16#7ffffff).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Interface
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%---------------------------------------------------------------------------
%% run() -> _
%%
%% Compiles and runs all benchmarks in the current directory,
%% and creates a report
%%---------------------------------------------------------------------------
run() ->
run(compiler_options()).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Generic Benchmark functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%---------------------------------------------------------------------------
%% compiler_options() -> OptionsList
%% OptionsList = list() - See Erlang/OTP module compile
%%---------------------------------------------------------------------------
compiler_options() ->
[report_errors, report_warnings].
%%---------------------------------------------------------------------------
%% run(OptionsList) ->
%% OptionsList = list() - See Erlang/OTP module compile
%%
%% Help function to run/0.
%%---------------------------------------------------------------------------
run(OptionsList) ->
Bms = compile_benchmarks(OptionsList),
run_benchmarks(Bms),
report().
%%---------------------------------------------------------------------------
%% compile_benchmarks(OptionsList) -> [BmInfo| _]
%% OptionsList = list() - See Erlang/OTP module compile
%% BmInfo = {Module, Iterations, [BmFunctionName| _]}
%% Module = atom()
%% Iterations = integer()
%% BmFunctionName = atom()
%%
%% Compiles all benchmark modules in the current directory and
%% returns info about the benchmarks.
%%---------------------------------------------------------------------------
compile_benchmarks(OptionsList) ->
{ok, FilesInCurrentDir} = file:list_dir("."),
ErlFiles = [ErlFile || ErlFile <- lists:sort(FilesInCurrentDir),
lists:suffix(".erl", ErlFile)],
lists:foldr(fun(File, BmInfoAcc) ->
case lists:suffix("_bm.erl", File) of
true ->
BmInfo = bm_compile(File, OptionsList),
[BmInfo | BmInfoAcc];
false ->
just_compile(File, OptionsList),
BmInfoAcc
end
end, [], ErlFiles).
%%---------------------------------------------------------------------------
%% just_compile(FileName, OptionsList) -> ok
%% FileName = string()
%% OptionsList = list() - See Erlang/OTP module compile
%%
%% Compiles a support module.
%%---------------------------------------------------------------------------
just_compile(FileName, OptionsList) ->
io:format("Compiling ~s...\n", [FileName]), % Progress info to user
case c:c(FileName, OptionsList) of
{ok, _Mod} ->
ok;
%% If compilation fails there is no point in trying to continue
error ->
Reason =
lists:flatten(
io_lib:format("Could not compile file ~s", [FileName])),
exit(self(), Reason)
end.
%%---------------------------------------------------------------------------
%% bm_compile(FileName, OptionsList) -> BmInfo
%% FileName = string()
%% OptionsList = list() - See Erlang/OTP module compile
%% BmInfo = {Module, Iterations, [BmFunctionName| _]}
%% Iterations = integer()
%% Module = atom()
%% BmFunctionName = atom()
%%
%% Compiles the benchmark module implemented in <FileName> and returns
%% information about the benchmark tests.
%%---------------------------------------------------------------------------
bm_compile(FileName, OptionsList) ->
io:format("Compiling ~s...\n", [FileName]), % Progress info to user
case c:c(FileName, OptionsList) of
{ok, Mod} ->
bm_cases(Mod);
%% If compilation fails there is no point in trying to continue
error ->
Reason =
lists:flatten(
io_lib:format("Could not compile file ~s", [FileName])),
exit(self(), Reason)
end.
%%---------------------------------------------------------------------------
%% bm_cases(Module) -> {Module, Iter, [BmFunctionName |_]}
%% Module = atom()
%% Iter = integer()
%% BmFunctionName = atom()
%%
%% Fetches the number of iterations and the names of the benchmark
%% functions for the module <Module>.
%%---------------------------------------------------------------------------
bm_cases(Module) ->
case catch Module:benchmarks() of
{Iter, BmList} when integer(Iter), list(BmList) ->
{Module, Iter, BmList};
%% The benchmark is incorrect implemented there is no point in
%% trying to continue
Other ->
Reason =
lists:flatten(
io_lib:format("Incorrect return value: ~p "
"from ~p:benchmarks()",
[Other, Module])),
exit(self(), Reason)
end.
%%---------------------------------------------------------------------------
%% run_benchmarks(Bms) ->
%% Bms = [{Module, Iter, [BmFunctionName |_]} | _]
%% Module = atom()
%% Iter = integer()
%% BmFunctionName = atom()
%%
%% Runs all the benchmark tests described in <Bms>.
%%---------------------------------------------------------------------------
run_benchmarks(Bms) ->
Ver = erlang:system_info(version),
Machine = erlang:system_info(machine),
SysInfo = {Ver,Machine},
Res = [bms_run(Mod, Tests, Iter, SysInfo) || {Mod,Iter,Tests} <- Bms],
%% Create an intermediate file that is later used to generate a bench
%% mark report.
Name = Ver ++ [$.|Machine] ++ ".bmres",
{ok, IntermediatFile} = file:open(Name, [write]),
%% Create mark that identifies version of the benchmark modules
io:format(IntermediatFile, "~p.\n", [erlang:phash(Bms, ?RANGE_MAX)]),
io:format(IntermediatFile, "~p.\n", [Res]),
file:close(IntermediatFile).
%%---------------------------------------------------------------------------
%% bms_run(Module, BmTests, Iter, Info) ->
%% Module = atom(),
%% BmTests = [BmFunctionName|_],
%% BmFunctionName = atom()
%% Iter = integer(),
%% SysInfo = {Ver, Machine}
%% Ver = string()
%% Machine = string()
%%
%% Runs all benchmark tests in module <Module>.
%%---------------------------------------------------------------------------
bms_run(Module, BmTests, Iter, SysInfo) ->
io:format("Running ~s:", [Module]), % Progress info to user
Res =
{Module,{SysInfo,[{Bm, bm_run(Module, Bm, Iter)} || Bm <- BmTests]}},
io:nl(),
Res.
%%---------------------------------------------------------------------------
%% bm_run(Module, BmTest, Iter) -> Elapsed
%% Module = atom(),
%% BmTest = atom(),
%% Iter = integer()
%% Elapsed = integer() - elapsed time in milliseconds.
%%
%% Runs the benchmark Module:BmTest(Iter)
%%---------------------------------------------------------------------------
bm_run(Module, BmTest, Iter) ->
io:format(" ~s", [BmTest]), % Progress info to user
spawn_link(?MODULE, measure, [self(), Module, BmTest, Iter]),
receive
{Elapsed, ok} ->
Elapsed;
{_Elapsed, Fault} ->
io:nl(),
Reason =
lists:flatten(
io_lib:format("~w", [Fault])),
exit(self(), Reason)
end.
%%---------------------------------------------------------------------------
%% measure(Parent, Module, BmTest, Iter) -> _
%% Parent = pid(),
%% Module = atom(),
%% BmTest = atom(),
%% Iter = integer()
%%
%% Measures the time it take to execute Module:Bm(Iter)
%% and send the result to <Parent>.
%%---------------------------------------------------------------------------
measure(Parent, Module, BmTest, Iter) ->
statistics(runtime),
Res = (catch apply(Module, BmTest, [Iter])),
{_TotalRunTime, TimeSinceLastCall} = statistics(runtime),
Parent ! {TimeSinceLastCall, Res}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Report functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%---------------------------------------------------------------------------
%% report() -> _
%%
%% Creates a report of the bench marking test that appeals to a human.
%% Currently this means creating a html-file. (Other formats could be added)
%%---------------------------------------------------------------------------
report() ->
{ok, AllFiles} = file:list_dir("."),
BmResultFiles = [File || File <- AllFiles, lists:suffix(".bmres", File)],
Results = fetch_bmres_data(BmResultFiles),
create_report(Results).
%%---------------------------------------------------------------------------
%% fetch_bmres_data(BmResultFiles) -> Results
%% BmResultFiles = [FileName | _]
%% FileName = string()
%% Results = [[{Bm, Res} | _]]
%% Bm = atom() - Name of benchmark module
%% Res = [{VersionInfo, [{Test, Time} | _]}]
%% VersionInfo = {Ver, Machine}
%% Ver = string()
%% Machine = string()
%% Test = atom()
%% Time = integer()
%%
%% Reads result data from intermediate files
%%---------------------------------------------------------------------------
fetch_bmres_data(BmResultFiles) ->
fetch_bmres_data(BmResultFiles, [], undefined).
%%---------------------------------------------------------------------------
%% fetch_bmres_data(BmResultFiles, AccResData, Check) -> Results
%% BmResultFiles = [FileName | _]
%% FileName = string()
%% AccResData = see Results fetch_bmres_data/1
%% Check = integer() | undefined (first time)
%%
%% Help function to fetch_bmres_data/1
%%---------------------------------------------------------------------------
fetch_bmres_data([], AccResData, _Check) ->
AccResData;
fetch_bmres_data([Name | BmResultFiles], AccResData, Check) ->
{DataList, NewCheck} = read_bmres_file(Name, Check),
fetch_bmres_data(BmResultFiles, [DataList| AccResData], NewCheck).
%%---------------------------------------------------------------------------
%% read_bmres_file(Name, Check) ->
%% Name = string()
%% Check = integer() | undefined
%%
%% Reads the data from the result files. Checks that all result
%% files where created with the same set of tests.
%%---------------------------------------------------------------------------
read_bmres_file(Name, Check) ->
case file:consult(Name) of
{ok, [Check1, List]} when Check =:= undefined, integer(Check1) ->
{List, Check1};
{ok, [Check, List]} when integer(Check) ->
{List, Check};
{ok, [Check1, _List]} when integer(Check1) ->
Reason =
lists:flatten(
io_lib:format("Different test setup, remove old setup "
"result by removing *.bmres files and "
"try again", [])),
exit(self(), Reason);
{error, Reason} when atom(Reason) ->
exit(self(), Reason);
{error, Reason} ->
exit(self(), file:format(Reason))
end.
%%---------------------------------------------------------------------------
%% create_report(Results) ->
%% Results = see Results fetch_bmres_data/1
%%
%% Organizes <Result> so it will be right for create_html_report/1
%% i.e. group results for the same benchmark test, run on different versions
%% of erlang.
%%---------------------------------------------------------------------------
create_report(Results) ->
Dictionary =
lists:foldl(fun(BmResultList, Dict0) ->
lists:foldl(fun({Bm, VerResult}, Dict1) ->
dict:append(Bm, VerResult,
Dict1)
end,Dict0, BmResultList)
end,
dict:new(), Results),
create_html_report(dict:to_list(Dictionary)).
%%---------------------------------------------------------------------------
%% create_html_report(ResultList) -> _
%% ResultList = [{Bm, Res} | _]
%% Bm = atom() - Name of benchmark module
%% Res = [{VersionInfo, [{Test, Time} | _]} | _]
%% VersionInfo = {Ver, Machine}
%% Ver = string()
%% Machine = string()
%% Test = atom()
%% Time = integer()
%%
%% Writes the result to an html-file
%%---------------------------------------------------------------------------
create_html_report(ResultList) ->
{ok, OutputFile} = file:open("index.html", [write]),
%% Create the beginning of the result html-file.
Head = Title = "Benchmark Results",
io:put_chars(OutputFile, "<html>\n"),
io:put_chars(OutputFile, "<head>\n"),
io:format(OutputFile, "<title>~s</title>\n", [Title]),
io:put_chars(OutputFile, "</head>\n"),
io:put_chars(OutputFile, "<body bgcolor=\"#FFFFFF\" text=\"#000000\"" ++
" link=\"#0000FF\" vlink=\"#800080\" alink=\"#FF0000\">\n"),
io:format(OutputFile, "<h1>~s</h1>\n", [Head]),
%% Add the result tables
lists:foreach(fun(Element) ->
create_html_table(OutputFile, Element) end,
ResultList),
%% Put in the end-html tags
io:put_chars(OutputFile, "</body>\n"),
io:put_chars(OutputFile, "</html>\n"),
file:close(OutputFile).
%%---------------------------------------------------------------------------
%% create_html_table(File, {Bm, Res}) -> _
%% File = file() - html file to write data to.
%% Bm = atom() - Name of benchmark module
%% Res = [{VersionInfo, [{Test, Time} | _]}]
%% VersionInfo = {Ver, Machine}
%% Ver = string()
%% Machine = string()
%% Test = atom()
%% Time = integer()
%%
%% Creates a html table that displays the result of the benchmark <Bm>.
%%---------------------------------------------------------------------------
create_html_table(File, {Bm, Res}) ->
{MinTime, Order} = min_time_and_sort(Res),
io:format(File, "<h2>~s</h2>\n" , [Bm]),
%% Fun that calculates relative measure values and puts them in
%% a dictionary
RelativeMesureFun = fun({TestName, Time}, Dict1) ->
dict:append(TestName, Time/MinTime, Dict1)
end,
%% For all erlang versions that the benchmark tests has been run,
%% calculate the relative measure values and put them in a dictionary.
ResultDict =
lists:foldl(fun({_VerInfo, Bms}, Dict0) ->
lists:foldl(RelativeMesureFun, Dict0, Bms) end,
dict:new(), Res),
%% Create the table and its headings
io:put_chars(File, "<table border=0 cellpadding=1><tr>"
"<td bgcolor=\"#000000\">\n"),
io:put_chars(File, "<table cellpadding=3 border=0 cellspacing=1>\n"),
io:put_chars(File, "<tr bgcolor=white>"),
io:put_chars(File, "<td>Test</td>"),
Heads = table_headers(Res),
lists:foreach(fun({Ver,Machine}) -> io:format(File, "<td>~s<br>~s</td>",
[Ver,Machine]) end, Heads),
io:put_chars(File, "</tr>\n"),
%% Create table rows
lists:foreach(fun(Name) ->
create_html_row(File, Name, ResultDict)
end, Order),
%% Tabel end-tags
io:put_chars(File, "</table></td></tr></table>\n"),
%% Create link to benchmark source code
io:format(File, "<p><a href=\"~s.erl\">Source for ~s.erl</a>\n",
[Bm,Bm]).
%%---------------------------------------------------------------------------
%% create_html_row(File, Name, Dict) -> _
%% File = file() - html file to write data to.
%% Name = atom() - Name of benchmark test
%% Dict = dict() - Dictonary where the relative time measures for
%% the test can be found.
%%
%% Creates an actual html table-row.
%%---------------------------------------------------------------------------
create_html_row(File, Name, Dict) ->
ReletiveTimes = dict:fetch(Name, Dict),
io:put_chars(File, "<tr bgcolor=white>\n"),
io:format(File, "<td>~s</td>", [Name]),
lists:foreach(fun(Time) ->
io:format(File, "<td>~-8.2f</td>", [Time]) end,
ReletiveTimes),
io:put_chars(File, "</tr>\n").
%%---------------------------------------------------------------------------
%% min_time_and_sort(ResultList) -> {MinTime, Order}
%% ResultList = [{VersionInfo, [{Test, Time} | _]}]
%% MinTime = integer() - The execution time of the fastes test
%% Order = [BmFunctionName|_] - the order of the testcases in
%% increasing execution time.
%% BmFunctionName = atom()
%%---------------------------------------------------------------------------
min_time_and_sort(ResultList) ->
%% Use the results from the run on the highest version
%% of Erlang as norm.
{_, TestRes} =
lists:foldl(fun ({{Ver, _}, ResList},
{CurrentVer, _}) when Ver > CurrentVer ->
{Ver, ResList};
(_, VerAndRes) ->
VerAndRes
end, {"0", []}, ResultList),
{lists:foldl(fun ({_, Time0}, Min1) when Time0 < Min1 ->
Time0;
(_, Min1) ->
Min1
end, ?MAX, TestRes),
[Name || {Name, _} <- lists:keysort(2, TestRes)]}.
%%---------------------------------------------------------------------------
%% table_headers(VerResultList) -> SysInfo
%% VerResultList = [{{Ver, Machine},[{BmFunctionName, Time}]} | _]
%% Ver = string()
%% Machine = string()
%% BmFunctionName = atom()
%% Time = integer()
%% SysInfo = {Ver, Machine}
%%---------------------------------------------------------------------------
table_headers(VerResultList) ->
[SysInfo || {SysInfo, _} <- VerResultList].
|