diff --git a/Makefile b/Makefile index 938949d..5d2d27f 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,7 @@ LUA_INCLUDE_DIR ?= $(PREFIX)/include LUA_LIB_DIR ?= $(PREFIX)/lib/lua/$(LUA_VERSION) INSTALL ?= install -.PHONY: all test install +.PHONY: all test install stress_test clean all: ; @@ -17,3 +17,8 @@ install: all test: all PATH=$(OPENRESTY_PREFIX)/nginx/sbin:$$PATH prove -I../test-nginx/lib -r t +stress_test: + make -C stress_test ITERATOR=8 + +clean: + make -C stress_test clean diff --git a/stress_test/Makefile b/stress_test/Makefile new file mode 100644 index 0000000..9b98f3e --- /dev/null +++ b/stress_test/Makefile @@ -0,0 +1,56 @@ +.PHONY: all clean +default : all + +TARGZ_INPUTS := http://nginx.org/download/nginx-1.7.9.tar.gz \ + https://github.com/cloudflare/lua-aho-corasick/archive/46f1a4146b8b9021b8df25a39572d1242ece899d.tar.gz + +LOCALFILES := $(foreach f, $(TARGZ_INPUTS), $(call notdir, $f)) +KEYFILES := $(patsubst %.tar.gz, %.key, $(LOCALFILES)) +TMP_FILE := a.tmp +TMP_DIR := tmp + +ITERATOR := 4 +LUAJIT := luajit + +###################################################################### +# +# Download *.tar.gz files from Internet +# +###################################################################### +# +define DOWNLOAD +$$(notdir $(1)) : + wget $(1) -O $$@ +endef +$(foreach k,$(TARGZ_INPUTS),$(eval $(call DOWNLOAD,$(k)))) + +###################################################################### +# +# generate files of keys from ther *.tar.gz downloaded from Internet +# +###################################################################### +# +define GEN_KEY +$$(patsubst %.tar.gz, %.key, $(1)): $(1) + rm -rf $(TMP_DIR); \ + mkdir $(TMP_DIR); \ + tar -C $(TMP_DIR) -zxvf $(1); \ + find $(TMP_DIR) -type f -exec cat {} \; > $(TMP_FILE); \ + ./key.py $(TMP_FILE) > $$@ + rm -rf $(TMP_DIR) $(TMP_FILE) +endef +$(foreach k,$(LOCALFILES),$(eval $(call GEN_KEY,$(k)))) + +all: + @for f in $(KEYFILES); do \ + echo "Testing with $${f}..."; \ + for i in `seq $(ITERATOR)`; do \ + echo "Iteration $${i}"; \ + rm -f $${f}; \ + $(MAKE) $${f} > /dev/null; \ + $(LUAJIT) ./test.lua $${f}; \ + done; \ + done + +clean: + rm -rf $(LOCALFILES) $(KEYFILES) $(TMP_FILE) $(TMP_DIR) diff --git a/stress_test/key.py b/stress_test/key.py new file mode 100755 index 0000000..e8913da --- /dev/null +++ b/stress_test/key.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python + +import random +import fileinput +import sys +import os + +def print_usage(prog): + print ("%s [list of files]" % prog) + return + +if len(sys.argv) == 1: + print_usage(sys.argv[0]) + sys.exit(1) + +for f in sys.argv[1:]: + if not os.access(f, os.R_OK): + print "file '%s' is not readable" % f + sys.exit(1) + +for line in fileinput.input(): + columns = line.split() + cn = len(columns) + if cn > 0: + col_idx = int(random.random() * cn + 0.5) + if col_idx == cn: + col_idx = 0 + print columns[col_idx] + +sys.exit(0) diff --git a/stress_test/test.lua b/stress_test/test.lua new file mode 100644 index 0000000..c89450e --- /dev/null +++ b/stress_test/test.lua @@ -0,0 +1,67 @@ +local argv = {...} +if #argv ~= 1 then + print ("Usage test.lua key-file") + os.exit(1) +end + +local keyfile = io.open(argv[1]) +if not keyfile then + io.write(string.format("Fail to open key file '%s'\n", argv[1])) + os.exit(1) +end + +ngx = {} +ngx.new = function() return os.time() end + +package.path = "../lib/resty/?.lua;../lib/resty/lrucache/?.lua;" .. package.path +lruffi = require "pureffi" +lru = require "lrucache" + +local key_num = 128 +local lru_inst = lru.new(key_num) +local lruffi_inst = lruffi.new(key_num, 0.5) + +local key_vect = {} +local key_idx = 0 +local key_cnt = 0 + +local function compare() + for i = 1, key_idx do + local key = key_vect[i] + local val1 = lru_inst:get(key) + local val2 = lruffi_inst:get(key) + -- print(key, val1, val2) + if val1 ~= val2 then + io.write( + string.format("disagree on key '%s', values are %d vs %d\n", + key, val1, val2)) + os.exit(1) + end + end +end + +local function main() + for line in keyfile:lines() do + + lru_inst:set(line, key_cnt) + lruffi_inst:set(line, key_cnt) + + key_cnt = key_cnt + 1 + key_idx = key_idx + 1 + key_vect[key_idx] = line + + if key_idx == key_num then + compare() + for i = 1, key_idx do + key_vect[i] = nil + end + key_idx = 0 + end + end + + compare() +end + +main() + +os.exit(0)