make test: easy profiling via make test PROFILE=1

Change-Id: Ib845578485f523b7f14e98c83d05f78db382ecde
Signed-off-by: Klement Sekera <ksekera@cisco.com>
diff --git a/test/Makefile b/test/Makefile
index 4e2863c..939f88b 100644
--- a/test/Makefile
+++ b/test/Makefile
@@ -25,6 +25,19 @@
 FORCE_FOREGROUND=0
 endif
 
+ifdef PROFILE_OUTPUT
+PROFILE_OUTPUT_OPTS=-o $(PROFILE_OUTPUT)
+endif
+
+ifndef PROFILE_SORT_BY
+PROFILE_SORT_BY=cumtime
+endif
+
+ifeq ($(PROFILE),1)
+PYTHON_PROFILE_OPTS=-m cProfile $(PROFILE_OUTPUT_OPTS) -s $(PROFILE_SORT_BY)
+FORCE_FOREGROUND=1
+endif
+
 verify-no-running-vpp:
 	@if [ "$(VPP_PIDS)" != "" ]; then \
 		echo; \
@@ -95,7 +108,7 @@
 	@touch $@
 
 define retest-func
-@env VPP_TEST_FAILED_DIR=$(VPP_TEST_FAILED_DIR) scripts/setsid_wrapper.sh $(FORCE_FOREGROUND) $(PYTHON_VENV_PATH)/bin/activate python run_tests.py -d $(TEST_DIR) $(UNITTEST_EXTRA_OPTS) || env VPP_TEST_FAILED_DIR=$(VPP_TEST_FAILED_DIR) COMPRESS_FAILED_TEST_LOGS=$(COMPRESS_FAILED_TEST_LOGS) scripts/compress_failed.sh
+env VPP_TEST_FAILED_DIR=$(VPP_TEST_FAILED_DIR) scripts/setsid_wrapper.sh $(FORCE_FOREGROUND) $(PYTHON_VENV_PATH)/bin/activate python $(PYTHON_PROFILE_OPTS) run_tests.py -d $(TEST_DIR) $(UNITTEST_EXTRA_OPTS) || env VPP_TEST_FAILED_DIR=$(VPP_TEST_FAILED_DIR) COMPRESS_FAILED_TEST_LOGS=$(COMPRESS_FAILED_TEST_LOGS) scripts/compress_failed.sh
 endef
 
 .PHONY: sanity
@@ -252,6 +265,10 @@
 	@echo " EXTERN_PLUGINS=<path>- path to out-of-tree plugins to be loaded by vpp under test"
 	@echo " EXTERN_COV_DIR=<path>- path to out-of-tree prefix, where source, object and .gcda files can be found for coverage report"
 	@echo ""
+	@echo " PROFILE=1            - enable profiling of test framework via cProfile module"
+	@echo " PROFILE_SORT_BY=opt  - sort profiling report by opt - consult cProfile documentation for possible values (default: cumtime)"
+	@echo " PROFILE_OUTPUT=file  - output profiling info to file - use absolute path (default: stdout)"
+	@echo ""
 	@echo " TEST_DEBUG=1         - turn on debugging of the test framework itself (expert)"
 	@echo ""
 	@echo "Creating test documentation"
diff --git a/test/run_tests.py b/test/run_tests.py
index c21acab..02e4738 100644
--- a/test/run_tests.py
+++ b/test/run_tests.py
@@ -199,10 +199,16 @@
         retries = int(os.getenv("RETRIES", 0))
     except ValueError:
         retries = 0
+
+    try:
+        force_foreground = int(os.getenv("FORCE_FOREGROUND", 0))
+    except ValueError:
+        force_foreground = 0
     attempts = retries + 1
     if attempts > 1:
         print("Perform %s attempts to pass the suite..." % attempts)
-    if (debug is not None and debug.lower() in ["gdb", "gdbserver"]) or step:
+    if (debug is not None and debug.lower() in ["gdb", "gdbserver"]) or step\
+            or force_foreground:
         # don't fork if requiring interactive terminal..
         sys.exit(not VppTestRunner(
             verbosity=verbose, failfast=failfast).run(suite).wasSuccessful())