#!/usr/bin/env ruby require 'socket' require 'timeout' def start(*exe) puts "Starting #{exe}" pid = spawn(*exe, out: "/dev/null") Process.detach(pid) return pid end def is_port_open?(ip, port) begin Timeout::timeout(1) do begin s = TCPSocket.new(ip, port) s.close return true rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH return false end end rescue Timeout::Error end return false end def killall(pids) pids.each do |pid| puts "Killing #{pid}" begin Process.kill "TERM", pid Process.wait pid rescue => ex puts "ERROR: Couldn't kill #{pid}. #{ex.class}=#{ex.message}" end end end def run ab = ["ab", "-n", "10000", "http://0.0.0.0:9089/main.go"] puts("$ " + ab.join(" ")) out = "" IO.popen(ab) do |io| out = io.read puts(out) out.split(/\n/).map do |l| next unless l =~ /^Requests per second:\s+(\d+.\d+)/ return $1 end end abort("failed to parse ab output: #{out}") end def main() pids = [] pids << start("docker", "run", "-p", "9089:80", "-v", "#{File.realpath(".")}/:/usr/share/nginx/html:ro", "--rm", "nginx:alpine") open = false 10.times do sleep(1) open ||= is_port_open?("0.0.0.0", 9089) rc = Process.waitpid(pids[0], Process::WNOHANG) unless rc.nil? abort("failed to start nginx container") end break if open end unless open abort("failed to start nginx container") end run() # warmup median1 = run() pids << start("./callgraph", "-skip-test", "-rancher-host", "rancher.local:8080", "http://sharelatex.local/login", "127.0.0.1") sleep(2) median2 = run() killall([pids.pop]) rescue nil puts (pids) pids << start("sudo", "tcpdump", "-i", "any", "-n") sleep(2) median3 = run() puts "####median request time/s without, with sysdig and with tcpdump: #{median1} #{median2} #{median3}" ensure killall(pids) end main