bench-run.lua 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #!/usr/bin/lua
  2. local nkey = "slow"
  3. local mode = "time"
  4. do
  5. local i = 1
  6. while i <= #arg do
  7. if arg[i] == "--fast" then nkey = "fast"
  8. elseif arg[i] == "--medium" then nkey = "medium"
  9. elseif arg[i] == "--slow" then nkey = "slow"
  10. elseif arg[i] == "--time" then mode = "time"
  11. elseif arg[i] == "--perf" then mode = "perf"
  12. end
  13. i = i + 1
  14. end
  15. end
  16. -- fast : runs on a blink of an eye (for testing / debugging)
  17. -- medium : the aot version takes more than 1 second
  18. -- slow : the jit version takes more than 1 second
  19. local benchs = {
  20. { name = "binarytrees", fast = 5, medium = 16, slow = 16 },
  21. { name = "fannkuch", fast = 5, medium = 10, slow = 11 },
  22. { name = "fasta", fast = 100, medium = 1000000, slow = 2500000 },
  23. { name = "knucleotide", fast = 100, medium = 1000000, slow = 1000000 },
  24. { name = "mandelbrot", fast = 20, medium = 2000, slow = 4000 },
  25. { name = "nbody", fast = 100, medium = 1000000, slow = 5000000 },
  26. { name = "spectralnorm", fast = 100, medium = 1000, slow = 4000 },
  27. }
  28. local impls = {
  29. { name = "jit", suffix = "", interpreter = "luajit", compile = false },
  30. { name = "jof", suffix = "", interpreter = "luajit -j off", compile = false },
  31. { name = "lua", suffix = "", interpreter = "../src/lua", compile = false, },
  32. { name = "aot", suffix = "_aot", interpreter = "../src/lua", compile = "../src/luaot" },
  33. { name = "trm", suffix = "_trm", interpreter = "../src/lua", compile = "../src/luaot-trampoline"},
  34. }
  35. --
  36. -- Shell
  37. --
  38. local function quote(s)
  39. if string.find(s, '^[A-Za-z0-9_./]*$') then
  40. return s
  41. else
  42. return "'" .. s:gsub("'", "'\\''") .. "'"
  43. end
  44. end
  45. local function prepare(cmd_fmt, ...)
  46. local params = table.pack(...)
  47. return (string.gsub(cmd_fmt, '([%%][%%]?)(%d*)', function(s, i)
  48. if s == "%" then
  49. return quote(params[tonumber(i)])
  50. else
  51. return "%"..i
  52. end
  53. end))
  54. end
  55. local function run(cmd_fmt, ...)
  56. return (os.execute(prepare(cmd_fmt, ...)))
  57. end
  58. local function exists(filename)
  59. return run("test -f %1", filename)
  60. end
  61. --
  62. -- Recompile
  63. --
  64. io.stderr:write("Recompiling the compiler...\n")
  65. assert(run("cd .. && make guess --quiet >&2"))
  66. io.stderr:write("...done\n")
  67. for _, b in ipairs(benchs) do
  68. for _, s in ipairs(impls) do
  69. local mod = b.name .. s.suffix
  70. if s.compile and not exists(mod .. ".so") then
  71. assert(run(s.compile.." %1.lua -o %2.c", b.name, mod))
  72. assert(run("../scripts/compile %2.c", b.name, mod))
  73. end
  74. end
  75. end
  76. --
  77. -- Execute
  78. --
  79. local function bench_cmd(b, impl)
  80. local module
  81. if string.match(impl.interpreter, "luajit") and exists(b.name.."_jit.lua") then
  82. module = b.name .. "_jit"
  83. else
  84. module = b.name .. impl.suffix
  85. end
  86. local n = assert(b[nkey])
  87. return prepare(impl.interpreter .. " main.lua %1 %2 > /dev/null", module, n)
  88. end
  89. if mode == "time" then
  90. for _, b in ipairs(benchs) do
  91. for _, impl in ipairs(impls) do
  92. local cmd = bench_cmd(b, impl)
  93. for rep = 1, 20 do
  94. print(string.format("RUN %s %s %s", b.name, impl.name, rep))
  95. assert(run("/usr/bin/time -p sh -c %1 2>&1", cmd))
  96. print()
  97. end
  98. end
  99. end
  100. elseif mode == "perf" then
  101. for _, b in ipairs(benchs) do
  102. for _, impl in ipairs(impls) do
  103. if impl.name == "lua" or impl.name == "cor" then
  104. local cmd = bench_cmd(b, impl)
  105. --print(string.format("RUN %s %s %s", b.name, impl.name, rep))
  106. assert(run("LANG=C perf stat sh -c %1 2>&1", cmd))
  107. print()
  108. end
  109. end
  110. end
  111. else
  112. error("impossible")
  113. end