Skip to content

perf: A small ngx resty lua library to benchmark memory and throughput of a function

Installation

If you haven't set up RPM repository subscription, sign up. Then you can proceed with the following steps.

CentOS/RHEL 7 or Amazon Linux 2

yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install lua-resty-perf

CentOS/RHEL 8+, Fedora Linux, Amazon Linux 2023

yum -y install https://extras.getpagespeed.com/release-latest.rpm
yum -y install lua5.1-resty-perf

To use this Lua library with NGINX, ensure that nginx-module-lua is installed.

This document describes lua-resty-perf v1.0.4 released on Apr 14 2021.


A simple resty lua library to benchmark memory and throughput of a function.

local function mycode()
  local x = {}
  for i = 1, 1e3 do
    local now = ngx.now()
    now = now - 45 + i
    x[i] = now
  end
  return x
end

perf.perf_time("mycode cpu profiling", function()
   mycode()
end)

perf.perf_mem("mycode memory profiling", function()
   mycode()
end)
To run it, you can use the openresty docker image:

docker run -it --rm -v ${PWD}/test.lua:/test.lua -v ${PWD}/lib/resty/perf.lua:/lib/resty/perf.lua openresty/openresty:xenial resty /test.lua

perf command line result

GitHub

You may find additional configuration tips and documentation for this module in the GitHub repository for nginx-module-perf.