bench-run.lua 3.1 KB

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