[test-suite] r260353 - lit: Introduce the TestContext

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 9 19:56:32 PST 2016


Author: matze
Date: Tue Feb  9 21:56:32 2016
New Revision: 260353

URL: http://llvm.org/viewvc/llvm-project?rev=260353&view=rev
Log:
lit: Introduce the TestContext

The TestContext contains information about the environment while
constructing and executing the commandlines of a single test.

Modified:
    test-suite/trunk/litsupport/compiletime.py
    test-suite/trunk/litsupport/perf.py
    test-suite/trunk/litsupport/runsafely.py
    test-suite/trunk/litsupport/test.py

Modified: test-suite/trunk/litsupport/compiletime.py
URL: http://llvm.org/viewvc/llvm-project/test-suite/trunk/litsupport/compiletime.py?rev=260353&r1=260352&r2=260353&view=diff
==============================================================================
--- test-suite/trunk/litsupport/compiletime.py (original)
+++ test-suite/trunk/litsupport/compiletime.py Tue Feb  9 21:56:32 2016
@@ -5,18 +5,19 @@ import os
 import timeit
 
 
-def collect(test, result):
+def collect(context, result):
     # TODO: This is not correct yet as the directory may contain .o.time files
     # of multiple benchmarks in the case of SingleSource tests.
     try:
         compile_time = 0.0
-        basepath = os.path.dirname(test.getFilePath())
+        basepath = os.path.dirname(context.test.getFilePath())
         for path, subdirs, files in os.walk(basepath):
             for file in files:
                 if file.endswith('.o.time'):
                     fullpath = os.path.join(path, file)
                     compile_time += timeit.getUserTime(fullpath)
     except IOError:
-        logging.info("Could not find compiletime for %s" % test.getFullName())
+        logging.info("Could not find compiletime for %s" %
+                     context.test.getFullName())
         return
     result.addMetric('compile_time', lit.Test.toMetricValue(compile_time))

Modified: test-suite/trunk/litsupport/perf.py
URL: http://llvm.org/viewvc/llvm-project/test-suite/trunk/litsupport/perf.py?rev=260353&r1=260352&r2=260353&view=diff
==============================================================================
--- test-suite/trunk/litsupport/perf.py (original)
+++ test-suite/trunk/litsupport/perf.py Tue Feb  9 21:56:32 2016
@@ -1,5 +1,5 @@
-def wrapScript(config, runscript, tmpBase):
-    profilefile = tmpBase + ".perf_data"
+def wrapScript(context, runscript):
+    profilefile = context.tmpBase + ".perf_data"
     profilescript = []
     for line in runscript:
         profilescript.append(

Modified: test-suite/trunk/litsupport/runsafely.py
URL: http://llvm.org/viewvc/llvm-project/test-suite/trunk/litsupport/runsafely.py?rev=260353&r1=260352&r2=260353&view=diff
==============================================================================
--- test-suite/trunk/litsupport/runsafely.py (original)
+++ test-suite/trunk/litsupport/runsafely.py Tue Feb  9 21:56:32 2016
@@ -1,11 +1,14 @@
 import shlex
+import timeit
 try:
     from shlex import quote  # python 3.3 and above
 except:
     from pipes import quote  # python 3.2 and earlier
 
 
-def prepareRunSafely(config, commandline, outfile):
+def prepareRunSafely(context, commandline, outfile):
+    config = context.config
+
     stdin = None
     stdout = None
     stderr = None
@@ -71,9 +74,16 @@ def prepareRunSafely(config, commandline
     return new_commandline
 
 
-def wrapScript(config, script, outfile):
+def wrapScript(context, script, suffix):
     adjusted_script = []
+    outfile = context.tmpBase + suffix
+    # Set name of timefile so getTime() can use it
+    context.timefile = outfile + ".time"
     for line in script:
-        line = prepareRunSafely(config, line, outfile)
+        line = prepareRunSafely(context, line, outfile)
         adjusted_script.append(line)
     return adjusted_script
+
+
+def getTime(context):
+    return timeit.getUserTime(context.timefile)

Modified: test-suite/trunk/litsupport/test.py
URL: http://llvm.org/viewvc/llvm-project/test-suite/trunk/litsupport/test.py?rev=260353&r1=260352&r2=260353&view=diff
==============================================================================
--- test-suite/trunk/litsupport/test.py (original)
+++ test-suite/trunk/litsupport/test.py Tue Feb  9 21:56:32 2016
@@ -14,12 +14,30 @@ import compiletime
 import timeit
 
 
-def runScript(test, litConfig, script, tmpBase, useExternalSh=True):
-    execdir = os.path.dirname(test.getExecPath())
+class TestContext:
+    """This class is used to hold data used while constructing a testrun.
+    For example this can be used by modules modifying the commandline with extra
+    instrumentation/measurement wrappers to pass the filenames of the results
+    to a final data collection step."""
+    def __init__(self, test, litConfig, original_runscript,
+                 original_verifyscript, tmpDir, tmpBase):
+        self.test = test
+        self.config = test.config
+        self.litConfig = litConfig
+        self.original_runscript = original_runscript
+        self.original_verifyscript = original_verifyscript
+        self.tmpDir = tmpDir
+        self.tmpBase = tmpBase
+
+
+def runScript(context, script, useExternalSh=True):
+    execdir = os.path.dirname(context.test.getExecPath())
     if useExternalSh:
-        res = executeScript(test, litConfig, tmpBase, script, execdir)
+        res = executeScript(context.test, context.litConfig, context.tmpBase,
+                            script, execdir)
     else:
-        res = executeScriptInternal(test, litConfig, tmpBase, script, execdir)
+        res = executeScriptInternal(context.test, context.litConfig,
+                                    context.tmpBase, script, execdir)
     return res
 
 
@@ -45,14 +63,10 @@ class TestSuiteTest(FileBasedTest):
         substitutions += [('%o', outfile)]
         runscript = applySubstitutions(runscript, substitutions)
         verifyscript = applySubstitutions(verifyscript, substitutions)
+        context = TestContext(test, litConfig, runscript, verifyscript, tmpDir,
+                              tmpBase)
 
-        profilescript = None
-        if litConfig.params.get('profile') == 'perf':
-            profilescript = perf.wrapScript(config, runscript, tmpBase)
-            profilescript = runsafely.wrapScript(config, profilescript,
-                                                 outfile=tmpBase+".perf.out")
-
-        runscript = runsafely.wrapScript(config, runscript, outfile)
+        runscript = runsafely.wrapScript(context, runscript, suffix=".out")
 
         # Create the output directory if it does not already exist.
         lit.util.mkdir_p(os.path.dirname(tmpBase))
@@ -62,7 +76,7 @@ class TestSuiteTest(FileBasedTest):
         n_runs = 1
         runtimes = []
         for n in range(n_runs):
-            res = runScript(test, litConfig, runscript, tmpBase)
+            res = runScript(context, runscript)
             if isinstance(res, lit.Test.Result):
                 return res
 
@@ -75,20 +89,21 @@ class TestSuiteTest(FileBasedTest):
                 output += "\n" + err
                 return lit.Test.Result(Test.FAIL, output)
 
-            timefile = "%s.time" % (outfile,)
             try:
-                runtime = timeit.getUserTime(timefile)
+                runtime = runsafely.getTime(context)
                 runtimes.append(runtime)
             except IOError:
                 pass
 
-        if profilescript:
-            res = runScript(test, litConfig, profilescript, tmpBase)
-            out, err, exitCode, timeoutInfo = res
+        if litConfig.params.get('profile') == 'perf':
+            profilescript = perf.wrapScript(context, context.original_runscript)
+            profilescript = runsafely.wrapScript(context, profilescript,
+                                                 suffix=".perf.out")
+            runScript(context, context.profilescript) # ignore result
 
         # Run verification script (the "VERIFY:" part)
         if len(verifyscript) > 0:
-            res = runScript(test, litConfig, verifyscript, tmpBase)
+            res = runScript(context, verifyscript)
             if isinstance(res, lit.Test.Result):
                 return res
             out, err, exitCode, timeoutInfo = res
@@ -103,6 +118,6 @@ class TestSuiteTest(FileBasedTest):
         result = lit.Test.Result(Test.PASS, output)
         if len(runtimes) > 0:
             result.addMetric('exec_time', lit.Test.toMetricValue(runtimes[0]))
-        compiletime.collect(test, result)
+        compiletime.collect(context, result)
 
         return result




More information about the llvm-commits mailing list