# Override on command line to select a different benchmark file.
BENCHMARK=basic.bench

# A multiplier; repeated actions are run this many times.
MULT=1

# The parent of the avatar who runs the benchmark. Most benchmarks require
# a builder.
AVATAR=abstract/builder

MOOROOT=$(shell perl -mMooix::Root -e 'print $$Mooix::Root->id')
RESULTS=$(BENCHMARK:.bench=.results)

build:
install:
uninstall:
test:

benchmark: root
	@if [ ! -e $(BENCHMARK) ]; then \
		echo "bad benchmark" >&2; \
		exit 1; \
	fi
	$(MAKE) cleanbenchmarker
	$(MAKE) benchmarker
	if [ -x /usr/bin/savelog -a -e $(RESULTS) ]; then \
		savelog $(RESULTS); \
	fi
	cpp $(BENCHMARK) 2>/dev/null | ./genscript $(MULT) | \
		mooscript benchmarker > $(RESULTS)
	$(MAKE) cleanbenchmarker

results:
	@echo System information:
	@echo ------------------
	@uname -a
	@if [ -e /proc/cpuinfo ]; then \
		egrep 'model name|cpu MHz' /proc/cpuinfo; \
	fi
	@free
	@echo
	@echo Benchmarks:
	@echo ----------
	@./getresults $(RESULTS)

benchmarker: root
# -u root does not currently work
	makeavatar `pwd`/benchmarker -u mooadmin \
		-p $(MOOROOT)/$(AVATAR) -n benchmarker
	echo root > benchmarker/users
# start in the void
	rm -f benchmarker/home benchmarker/location
	touch benchmarker/home

cleanbenchmarker:
	if [ -e benchmarker ]; then removeavatar benchmarker; fi
	sleep 5
	rm -rf benchmarker

root:
	@if [ `whoami` != root ]; then \
		echo "benchmark must run as root" >&2; \
		exit 1; \
	fi

clean:
	rm -f *~
	if [ -e benchmarker ]; then removeavatar benchmarker; fi

realclean: clean
	rm -f *.results*
