在 shell 上执行 Lua 脚本并获取内存大小和运行时间

在一个项目中,我需要执行 Lua 脚本并获取一些信息:内存大小、CPU 时间和运行时间。我在 lua.org 上没有找到关于 Lua 编译器的某些参数的任何信息。有人知道参数或者具有这些功能的编译器吗?以后,我想用 PHP 分析我的 Lua 脚本。

  • 通过 PHP 在 shell 上执行
  • 并获取信息(内存大小、CPU 时间、运行时间)

谢谢!

点赞
用户1847592
用户1847592

为了分析您的 Lua 脚本,只需将其包装在以下代码行中:

local time_start = os.time()

--------------------------------------
-- Your script is here
-- For example, just spend 5 seconds in CPU busy loop
repeat until os.clock() > 5
-- or call some external file containing your original script:
-- dofile("path/to/your_original_script.lua")
--------------------------------------

local mem_KBytes = collectgarbage("count") -- 当前被 Lua 占用的内存
local CPU_seconds = os.clock()                  -- 消耗的 CPU 时间
local runtime_seconds = os.time() - time_start  -- 经过的 "挂钟" 时间
print(mem_KBytes, CPU_seconds, runtime_seconds)
-- 输出到标准输出:24.0205078125  5.000009  5

现在,您可以使用 shell 命令 lua path/to/this_script.lua 执行此脚本,并分析打印到标准输出的最后一行以获取信息。

2017-06-25 17:06:05