#
#   userver -- a fairly simple web server designed for experimentation.
#   Copyright (C) 2003  Hewlett-Packard Company
#   Contributed by Tim Brecht <brecht@cs.uwaterloo.ca>
#   Partially based on servers by: 
#      David Mosberger <david.mosberger@hp.com> and
#      Abhishek Chandra <abhishek@cs.umass.edu>
#   See AUTHORS file for list of contributions.
#
#   This file is part of userver, a simple web server designed for
#   performance experiments.
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License as
#   published by the Free Software Foundation; either version 2 of the
#   License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
#   02111-1307 USA
#


include Makefile.base

MAKE-GPROF      = Makefile.gprof
MAKE-NOGPROF    = Makefile.nogprof
MAKE-SEND       = Makefile.send
MAKE-SEND-GPROF = Makefile.send-gprof
BIN             = $(HOME)/bin

PGMS =  userver userver-gprof userver-send

cur: userver
# cur: userver-send

all: userver man

man: userver.txt userver.ps userver.pdf userver.html

userver.txt: userver.1
	groff -man -Tascii userver.1 > userver.txt

userver.ps: userver.1
	groff -man -Tps userver.1 > userver.ps

userver.pdf: userver.ps
	ps2pdf userver.ps userver.pdf

userver.html: userver.1
	man2html userver.1 > userver.html

os: FORCE
	echo os = $(OS)

userver-gprof: FORCE
	$(MAKE) -f $(MAKE-GPROF) userver-gprof

userver-send: FORCE
	$(MAKE) -f $(MAKE-SEND) userver-send

userver-send-gprof: FORCE
	$(MAKE) -f $(MAKE-SEND-GPROF) userver-send-gprof

predepend:
	touch .depend

depend: predepend userver-depend

userver-depend: predepend FORCE
	$(MAKE) -f $(MAKE-NOGPROF) userver-depend

userver-send-depend: FORCE
	$(MAKE) -f $(MAKE-SEND) userver-send-depend

userver: FORCE
	$(MAKE) -f $(MAKE-NOGPROF) userver

test: testall
testall: test1 test2 test3 test4 test5 test6 test7 test8 test9 test10 test11

OPTS = -D 0x0

# NOTE: because the client and server are both running on
# the same machine we have to leave a bit of extra time for
# the server to start before starting the client and a bit
# of extra time for the server to finish dumping stats, events,
# etc. after it has been killed and before starting the next
# test (otherwise the server can't bind the ip addr / port combo

test1: FORCE
	@echo "Running Basic Test Subset"
	@echo "Running test 1 - should get 100 replies and 0 errors"
	$(CSH) -c "./userver $(OPTS) >& test1.out &"
	sleep 4
	$(CSH) -c "./scripts/run 100 |& egrep 'Reply status:|Errors:'"
	@./scripts/kill-server
	sleep 4
	@echo ""

test2:  FORCE
	@echo "Running test 2 - should get 100 replies and 0 errors"
	$(CSH) -c "./userver -C $(OPTS) >& test2.out &"
	sleep 4
	$(CSH) -c "./scripts/run 100 |& egrep 'Reply status:|Errors:'"
	@./scripts/kill-server
	sleep 4
	@echo ""

test3:  FORCE
	@echo "Running test 3 - should get 100 replies and 0 errors"
	$(CSH) -c "./userver -C --use-sendfile --use-tcp-cork $(OPTS) >& test3.out &"
	sleep 4
	$(CSH) -c "./scripts/run 100 |& egrep 'Reply status:|Errors:'"
	@./scripts/kill-server
	sleep 4
	@echo ""

test4:  FORCE
	@echo "Running test 4 - should get 5000 replies and 0 errors in about 10 seconds"
	$(CSH) -c "./userver -C --use-sendfile --use-tcp-cork -c 450 -f 1000 $(OPTS) >& test4.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 500 5000 |& egrep 'Reply status:|Errors:'"
	@./scripts/kill-server
	sleep 4
	@echo ""

test5:  FORCE
	@echo "Running test 5 - should get 1000 replies and 0 errors in about 10 seconds"
	$(CSH) -c "./userver -C --use-sendfile --use-tcp-cork -m 0 -e 10 -c 450 -f 1000 $(OPTS) >& test5.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 100 1000 |& egrep 'Reply status:|Errors:'"
	@./scripts/kill-server
	sleep 4
	@echo ""

test6:  FORCE
	@echo "Running test 6 - should get 4000 replies and 0 errors in about 10 seconds"
	$(CSH) -c "./userver -C --use-sendfile --use-tcp-cork -m 0 -c 450 -f 1000 --use-poll $(OPTS) >& test6.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 400 4000 |& egrep 'Reply status:|Errors:'"
	@./scripts/kill-server
	sleep 4
	@echo ""

test7:  FORCE
	@echo "Running IP Address Subset"
	@echo "Running test 7 - should get 100 replies and 0 errors"
	$(CSH) -c "./userver --ip-addr 127.0.0.1 $(OPTS) >& test7.out &"
	sleep 8
	$(CSH) -c "./scripts/run 100 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 8
	@echo ""

test8:  FORCE
	@echo "Running test 8 - should get 100 replies and 0 errors"
	$(CSH) -c "./userver --ip-addr 0.0.0.0 $(OPTS) >& test8.out &"
	sleep 8
	$(CSH) -c "./scripts/run 100 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 8
	@echo ""

test9:  FORCE
	@echo "Running test 9 - should get 10 replies and 0 errors"
	$(CSH) -c "./userver --ip-addr 127.0.0.1:6802 $(OPTS) >& test9.out &"
	sleep 8
	$(CSH) -c "./scripts/runaddrport 127.0.0.1 6802 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 8
	@echo ""

test10:  FORCE
	@echo "Running test 10 - should get 1000 replies and 0 errors"
	$(CSH) -c "./userver $(OPTS) -C -c 10 -f 5000 >& test10.out &"
	sleep 8
	$(CSH) -c "./scripts/runrate 100 1000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 8
	@echo ""

test11:  FORCE
	@echo "Running test 11 - should get 2000 replies and 0 errors"
	$(CSH) -c "./userver $(OPTS) -C -c 100 -f 1700 >& test11.out &"
	sleep 8
	$(CSH) -c "./scripts/runrate 200 2000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 8
	@echo ""


testsend: testsend1 testsend2 testsend3 testsend4 testsend5

testsend1: FORCE
	@echo "Running SEND Test Subset"
	@echo "Running test 1 - should get 100 replies and 0 errors"
	$(CSH) -c "./userver-send -B -E -Y -U -N $(OPTS) >& test1.out &"
	sleep 4
	$(CSH) -c "./scripts/run 100 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server-send
	sleep 2
	@echo ""


testsend2: FORCE
	@echo "Running test 2 - should get 100 replies and 0 errors"
	$(CSH) -c "./userver-send -C -B -E -Y -U -N $(OPTS) >& test2.out &"
	sleep 4
	$(CSH) -c "./scripts/run 100 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server-send
	sleep 2
	@echo ""

testsend3: FORCE
	@echo "Running test 3 - should get 100 replies and 0 errors"
	$(CSH) -c "./userver-send -C --use-sendfile --use-tcp-cork -B -E -Y -U -N $(OPTS) >& test3.out &"
	sleep 4
	$(CSH) -c "./scripts/run 100 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server-send
	sleep 2
	@echo ""

testsend4: FORCE
	@echo "Running test 4 - should get 5000 replies and 0 errors in about 10 seconds"
	$(CSH) -c "./userver-send -C --use-sendfile --use-tcp-cork -c 450 -f 1000 -B -E -Y -U -N $(OPTS) >& test4.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 500 5000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server-send
	sleep 2
	@echo ""

testsend5: FORCE
	@echo "Running test 5 - should get 4000 replies and 0 errors in about 10 seconds"
	$(CSH) -c "./userver-send -C --use-sendfile --use-tcp-cork -m 0 -c 450 -f 1000 -B -E -Y -U -N $(OPTS) >& test5.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 400 4000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server-send
	sleep 2
	@echo ""


testaperture: testaperture1 testaperture2

testaperture1: FORCE
	@echo "Running test 1 - should get 10 replies and 0 errors"
	$(CSH) -c "./userver-send -C -B -E -Y -U -N $(OPTS) --auto-accept-aperture=1 >& test1.out &"
	sleep 4
	$(CSH) -c "./scripts/run 10 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server-send
	sleep 2
	@echo ""

testaperture2: FORCE
	@echo "Running test 2 - should get 4000 replies and 0 errors in about 10 seconds"
	$(CSH) -c "./userver-send -C --use-sendfile --use-tcp-cork -m 0 -c 450 -f 1000 -B -E -Y -U -N $(OPTS) --auto-accept-aperture=10 >& test2.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 400 4000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server-send
	sleep 2
	@echo ""


AIO_OPTS = --use-socket-aio --aio-accept-thold=10 -D 0x0 # --use-aio-wait

testaio: testaio1 testaio2 testaio3 testaio4 testaio5 testaio6 testaio7 \
         testaio8 testaio9 testaio10 testaio11 testaio12 testaio13 testaio14

testaio1:
	@echo "Running AIO Test Subset"
	@echo "Running test 1 - should get 1 replies and 0 errors"
	$(CSH) -c "./userver -c 2 -f 30 -m 1 $(AIO_OPTS) >& test1.out &"
	sleep 4
	$(CSH) -c "./scripts/run 1 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2

testaio2:
	@echo "Running test 2 - should get 2 replies and 0 errors"
	$(CSH) -c "./userver -c 2 -f 30 -m 1 $(AIO_OPTS) >& test2.out &"
	sleep 4
	$(CSH) -c "./scripts/run 2 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2

testaio3:
	@echo "Running test 3 - should get 10 replies and 0 errors"
	$(CSH) -c "./userver -c 2 -f 30 -m 1 $(AIO_OPTS) >& test3.out &"
	sleep 4
	$(CSH) -c "./scripts/run 10 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2

testaio4:
	@echo "Running test 4 - should get 100 replies and 0 errors"
	$(CSH) -c "./userver -c 2 -f 30 -m 1 $(AIO_OPTS) >& test4.out &"
	sleep 4
	$(CSH) -c "./scripts/run 100 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2

testaio5:
	@echo "Running test 5 - should get 500 replies and 0 errors"
	$(CSH) -c "./userver -c 300 -f 500 -m 1 $(AIO_OPTS) >& test5.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 50 500 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2

testaio6:
	@echo "Running test 6 - should get 1000 replies and 0 errors"
	$(CSH) -c "./userver -c 300 -f 500 -m 1 $(AIO_OPTS) >& test6.out &"
	sleep 4
	$(CSH) -c "./scripts/runpersist 1000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2

testaio7:
	@echo "Running test 7 - should get 1000 replies and 0 errors"
	$(CSH) -c "./userver -c 300 -f 500 -m 1 $(AIO_OPTS) >& test7.out &"
	sleep 4
	$(CSH) -c "./scripts/run 1000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2

testaio8:
	@echo "Running test 8 - should get 1000 replies and 0 errors"
	$(CSH) -c "./userver -c 300 -f 500 -m 1 $(AIO_OPTS) >& test8.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 100 1000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2

testaio9:
	@echo "Running test 9 - should get 10000 replies and 0 errors"
	$(CSH) -c "./userver -c 300 -f 500 -m 1 $(AIO_OPTS) >& test9.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 100 10000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2


testaio10:
	@echo "Running test 10 - should get 1000 replies and 0 errors"
	$(CSH) -c "./userver -c 300 -f 500 -m 1 $(AIO_OPTS) -C >& test10.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 100 1000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2

testaio11:
	@echo "Running test 11 - should get 8000 replies and 0 errors"
	$(CSH) -c "./userver -c 500 -f 1024 -m 10 $(AIO_OPTS) >& test11.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 200 8000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2

testaio12:
	@echo "Running test 12 - should get 1000 replies and 0 errors"
	$(CSH) -c "./userver -c 15000 -f 32000 -m 1 $(AIO_OPTS) -C >& test12.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 100 1000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2

testaio13:
	@echo "Running test 13 - should get 9000 replies and 0 errors"
	$(CSH) -c "./userver -c 15000 -f 32000 -m 1 $(AIO_OPTS) -C >& test13.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 1000 9000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2

testaio14:
	@echo "Running test 14 - should get 20000 replies and 0 errors"
	$(CSH) -c "./userver -c 15000 -f 32000 -m 1 $(AIO_OPTS) -C >& test14.out &"
	sleep 4
	$(CSH) -c "./scripts/runrate 5000 50000 |& egrep 'Reply status:|Errors:'"
	sleep 2
	@./scripts/kill-server
	sleep 2


install: FORCE
	mv $(PGMS) $(BIN)

clean: FORCE
	rm -f *.o

vclean: FORCE
	-rm -f *.o $(PGMS) core .depend userver.trace cscope.out
	-rm -f userver.ps userver.pdf userver.txt userver.html \
	       userver.exe userver.exe.stackdump userver.pid test*.out
	    

ctags: FORCE
	ctags *.[ch]

tags: FORCE
	ctags *.[ch]

DIST_DEST = ../$(DIST_NAME)
SCRIPTS_DEST = ../$(DIST_NAME)/scripts
TESTS_DEST = ../$(DIST_NAME)/docs
INCLUDE_DEST = ../$(DIST_NAME)

dist: userver.txt userver.ps userver.pdf userver.html
	-mkdir $(DIST_DEST)
	-mkdir $(SCRIPTS_DEST)
	-mkdir $(TESTS_DEST)
	-mkdir $(INCLUDE_DEST)
	cp -p $(DIST_LIST) $(DIST_DEST)
	cp -p $(SCRIPTS) $(SCRIPTS_DEST)
	cp -p $(TESTDOCS) $(TESTS_DEST)
	cp -pr $(INCLUDE) $(INCLUDE_DEST)
	rm -rf $(INCLUDE_DEST)/include/CVS $(INCLUDE_DEST)/include/*/CVS $(INCLUDE_DEST)/include/*/*/CVS
	$(CSH) -c "cd .. ; pwd ; tar -czf $(DIST_NAME).tar.gz $(DIST_NAME)/"
	@echo "Don't forget to upload the distribution (make updist)"
	@echo "and tag the CVS tree (make cvstag) !!!"

updist:
	scp CHANGELOG bcr2.uwaterloo.ca:www/userver/src/CHANGELOG-$(DIST_VER).txt
	scp ../$(DIST_NAME).tar.gz bcr2.uwaterloo.ca:www/userver/src
	ssh bcr2.uwaterloo.ca 'chmod a+r www/userver/src/*'
	@echo "Don't forget to tag the CVS tree!!!"

cvstag:
	cvs tag Ver-$(CVS_TAG_VER)

# upindex:
#  	scp index.html bcr2:www/userver/src

FORCE:

