#!/opt/lnxall_app/bin/lua local invoker = require "invoker" local gfmt = string.format local function checkstatus() local okay,output = invoker.invoke(invoker.OUTPUT ,"pgrep", "-x", "stress") if okay and type(output) == "string" and #output ~= 0 then return true end return false end local function freemem() local cmd = { "/bin/sh", "-c", "cat /proc/meminfo | grep MemFree | awk '{print $2}'"} local okay, str = invoker.invoke(invoker.OUTPUT, cmd) if not okay or type(str) ~= "string" then io.stdout:write("Error,failed to read avail mem 1\n") io.stdout:flush() return false end str = str:gsub("[ \r\n\t]","") local mem = tonumber(str) if type(mem) ~= "number" then io.stdout:write(gfmt("Error,failed to read avail mem 2 %s\n",type(mem))) io.stdout:flush() return false end local mem_round = math.floor((math.floor(mem / 1000 / 100) * 100) / 2) if not mem_round or mem_round < 0 then io.stdout:write("Error,invaild mem\n") io.stdout:flush() return false end return mem_round end local function check() local status = os.execute("which stress > /dev/null 2>&1") if status ~= 0 then local okay = os.execute("dpkg -i /opt/lnxall_app/bin/stress*.deb >/dev/null 2>&1") if okay ~= 0 then io.stdout:write("Error,failed to install stress") io.stdout:flush() return false end end invoker.invoke(0,"/bin/sh", "-c", "rm -r /opt/lnxall_app/bin/stress*.deb >/dev/null 2>&1") return true end local function mainfunc(arg1) local status = checkstatus() if arg1 == "1" then if not status then io.stdout:write("Error,stress not running.") io.stdout:flush() return false else local okay, output = invoker.invoke(invoker.NOSTDIO, "killall", "-KILL", "stress") if okay then io.stdout:write("Stress killed okay.\n") io.stdout:flush() return true else io.stdout:write("Error,stress failed to kill.\n") io.stdout:flush() return false end end end if status then io.stdout:write("Stress test already start.\n") io.stdout:flush() return true end local exist = check() if not exist then return false end local mem = freemem() if not mem then return false end local cmd = "" if mem == 0 then cmd = "stress --cpu 4 >/dev/null 2>&1 &" else cmd = gfmt("stress --cpu 4 --vm 2 --vm-bytes %dM --vm-keep >/dev/null 2>&1 &", mem) end os.execute(cmd) io.stdout:write(gfmt("Stress test start %d okay\n",mem)) io.stdout:flush() return true end mainfunc(arg[1]) os.exit(0)