Added new command line options.
authorJan Vrany <jan.vrany@fit.cvut.cz>
Sat, 08 Mar 2014 16:29:50 +0000
changeset 205 63a0130d7951
parent 204 97b7cdaeb0e8
child 206 29602f0696d8
Added new command line options. * `--setup` / `--setup-script` runs code before any benchmark * `--teardown` / `--teardown-script` runs code after benchmask are run
s/BenchmarkRunner.st
s/s.rc
s/stx/stx.rc
s/tests/BenchmarkRunnerTests.st
s/tests/tests.rc
--- a/s/BenchmarkRunner.st	Sat Mar 08 16:02:47 2014 +0000
+++ b/s/BenchmarkRunner.st	Sat Mar 08 16:29:50 2014 +0000
@@ -38,7 +38,7 @@
 !BenchmarkRunner methodsFor:'main'!
 
 main:argv0
-    | i report name file params classes runs argv desc |
+    | i report name file params classes runs argv desc setup setupScript teardown teardownScript |
 
     params := Dictionary new.
     classes := OrderedCollection new.
@@ -69,11 +69,11 @@
                 file := argv at: i.
                 i := i + 1.
             ].
-            arg = '--arguments' ifTrue:[
+            (arg = '--arguments' or:[arg = '--script']) ifTrue:[
                 | arguments argumentsF |
 
                 i > argv size ifTrue:[
-                    self error:'--arguments requires a file parameter.'
+                    self error: arg, ' requires a file parameter.'
                 ].                
                 argumentsF := (arguments := (argv at: i)) asFilename.
                 argumentsF exists ifFalse:[
@@ -113,6 +113,44 @@
                 i := i + 1.
             ].
 
+            arg = '--setup' ifTrue:[ 
+                i > argv size ifTrue:[
+                    self error:'--setup requires a string parameter.'
+                ].                
+                setup := argv at: i.
+                i := i + 1.     
+            ].
+            arg = '--teardown' ifTrue:[ 
+                i > argv size ifTrue:[
+                    self error:'--teardown requires a string parameter.'
+                ].                
+                teardown := argv at: i.
+                i := i + 1.     
+            ].
+
+            (arg = '--setup-script') ifTrue:[
+                i > argv size ifTrue:[
+                    self error: arg, ' requires a file parameter.'
+                ].                
+                setupScript := (argv at: i) asFilename.
+                setupScript exists ifFalse:[
+                    self error:'no such file: ', (argv at: i).
+                ].
+                i := i + 1.
+            ].  
+            (arg = '--teardown-script') ifTrue:[
+                i > argv size ifTrue:[
+                    self error: arg, ' requires a file parameter.'
+                ].                
+                teardownScript := (argv at: i) asFilename.
+                teardownScript exists ifFalse:[
+                    self error:'no such file: ', (argv at: i).
+                ].
+                i := i + 1.
+            ].  
+
+
+
             arg = '--description' ifTrue:[
                 i > argv size ifTrue:[
                     self error:'--description requires a string parameter.'
@@ -165,6 +203,10 @@
         ]
     ].
 
+    "Run setup, if any"
+    setup notNil ifTrue:[ Compiler evaluate: setup ].
+    setupScript notNil ifTrue:[ Compiler evaluate: setupScript asFilename contents asString ].
+
     "Run suite"
     result := BenchmarkResult new.
     result runs: runs.
@@ -185,7 +227,11 @@
             write: result on: BenchmarkPlatform current stdout
     ].
 
-    "Modified: / 08-03-2014 / 11:00:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Run teardown"
+    teardown notNil ifTrue:[ Compiler evaluate: teardown ].
+    teardownScript notNil ifTrue:[ Compiler evaluate: teardownScript asFilename contents asString ].
+
+    "Modified: / 08-03-2014 / 16:23:51 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 options
@@ -197,7 +243,12 @@
   -o FILE ................ write output to FILE instead of to standard output (default: stdout)
   -n RUNS ................ how many times to run each bechmark (default: 5)
   -r REPORTCLASS ......... user REPORTCLASS to generate report (default: BenchmarkReportText)
+  --script FILE 
   --arguments FILE ....... read additional arguments from FILE
+  --setup EXPR ........... evaluate EXPR before actually running any benchmark
+  --setup-script FILE .... evaluate code in FILE before actually running any benchmark
+  --teardown EXPR ........ evaluate EXPR after all benchmarks are run
+  --teardown-script FILE . evaluate code in FILE  after all benchmarks are run
   --text ................. generate text report (equivalent to -r BenchmarkReportText)
   --json ................. generate JSON report (equivalent to -r BenchmarkReportJSON)
   --tag TAG .............. tag for the current benchmark set (default: "default")
@@ -213,7 +264,7 @@
 '
 
     "Created: / 06-06-2013 / 11:01:42 / Jan Vrany <jan.vrany@fit.cvut.cz>"
-    "Modified: / 08-03-2014 / 10:49:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 08-03-2014 / 16:15:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 ! !
 
 !BenchmarkRunner methodsFor:'private-parsing'!
--- a/s/s.rc	Sat Mar 08 16:02:47 2014 +0000
+++ b/s/s.rc	Sat Mar 08 16:29:50 2014 +0000
@@ -25,7 +25,7 @@
       VALUE "LegalCopyright", "My CopyRight or CopyLeft\0"
       VALUE "ProductName", "LibraryName\0"
       VALUE "ProductVersion", "6.2.3.0\0"
-      VALUE "ProductDate", "Sat, 08 Mar 2014 16:00:12 GMT\0"
+      VALUE "ProductDate", "Sat, 08 Mar 2014 16:28:27 GMT\0"
     END
 
   END
--- a/s/stx/stx.rc	Sat Mar 08 16:02:47 2014 +0000
+++ b/s/stx/stx.rc	Sat Mar 08 16:29:50 2014 +0000
@@ -25,7 +25,7 @@
       VALUE "LegalCopyright", "My CopyRight or CopyLeft\0"
       VALUE "ProductName", "ProductName\0"
       VALUE "ProductVersion", "6.2.3.0\0"
-      VALUE "ProductDate", "Sat, 08 Mar 2014 16:00:17 GMT\0"
+      VALUE "ProductDate", "Sat, 08 Mar 2014 16:28:31 GMT\0"
     END
 
   END
--- a/s/tests/BenchmarkRunnerTests.st	Sat Mar 08 16:02:47 2014 +0000
+++ b/s/tests/BenchmarkRunnerTests.st	Sat Mar 08 16:29:50 2014 +0000
@@ -146,11 +146,36 @@
     | runner |
 
     runner := BenchmarkRunner new.
-    runner main: #('--json' '--description' 'Some Desc' 'BenchmarkTestsSuiteA').
-
-    self assert: runner result outcomes size = 2.
+    runner main: #('--setup' 'Smalltalk at: #''test_basic_01g'' put: 12345' 'BenchmarkTestsSuiteA').
+    [
+    self assert: (Smalltalk includesKey:#'test_basic_01g').
+    self assert: (Smalltalk at:#'test_basic_01g') == 12345.
+    ] ensure:[ 
+        (Smalltalk includesKey:#'test_basic_01g') ifTrue:[ 
+            Smalltalk removeKey:#'test_basic_01g'
+        ].
+    ].
 
     "Created: / 19-09-2013 / 22:43:37 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+    "Modified: / 08-03-2014 / 16:27:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
+test_basic_01h
+
+    | runner |
+
+    runner := BenchmarkRunner new.
+    runner main: #('--teardown' 'Smalltalk at: #''test_basic_01g'' put: 12345' 'BenchmarkTestsSuiteA').
+    [
+    self assert: (Smalltalk includesKey:#'test_basic_01g').
+    self assert: (Smalltalk at:#'test_basic_01g') == 12345.
+    ] ensure:[ 
+        (Smalltalk includesKey:#'test_basic_01g') ifTrue:[ 
+            Smalltalk removeKey:#'test_basic_01g'
+        ].
+    ].
+
+    "Created: / 08-03-2014 / 16:27:47 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
 test_basic_02a
@@ -181,6 +206,33 @@
     "Modified: / 01-08-2013 / 19:14:54 / Jan Vrany <jan.vrany@fit.cvut.cz>"
 !
 
+test_basic_02b
+
+    | runner file |
+
+    "How to make a temporary file on Pharo?"
+    Smalltalk isSmalltalkX ifFalse:[ ^  self ].
+
+    file := Filename newTemporary.
+    [
+    file writingFileDo:[:s|
+        s nextPutLine: '-Dmsecs=10'.
+        s nextPutLine: 'BenchmarkTestsSuiteA#benchmark2'.
+    ].
+    runner := BenchmarkRunner new.
+    runner main: (Array with:'--script' with: file pathName).
+
+    self assert: runner result outcomes size = 1.
+    self assert: runner result outcomes anElement times size = 5.
+    self assert: runner result outcomes anElement times first < 200.
+
+    ] ensure:[
+        file remove
+    ].
+
+    "Created: / 08-03-2014 / 16:09:25 / Jan Vrany <jan.vrany@fit.cvut.cz>"
+!
+
 test_basic_03a
 
     | runner |
--- a/s/tests/tests.rc	Sat Mar 08 16:02:47 2014 +0000
+++ b/s/tests/tests.rc	Sat Mar 08 16:29:50 2014 +0000
@@ -25,7 +25,7 @@
       VALUE "LegalCopyright", "My CopyRight or CopyLeft\0"
       VALUE "ProductName", "ProductName\0"
       VALUE "ProductVersion", "6.2.3.0\0"
-      VALUE "ProductDate", "Sat, 08 Mar 2014 16:00:14 GMT\0"
+      VALUE "ProductDate", "Sat, 08 Mar 2014 16:28:29 GMT\0"
     END
 
   END