From 35f927e64e4d3c87bc2ac04c2c1a19cda4ef63f1 Mon Sep 17 00:00:00 2001
From: Delyan Angelov <delian66@gmail.com>
Date: Sun, 15 Sep 2019 18:08:16 +0300
Subject: [PATCH] repl: add some timing statistics when running REPL test files

---
 compiler/tests/repl/.gitignore      |  1 +
 compiler/tests/repl/repl_test.v     | 59 +++++---------------
 compiler/tests/repl/run.v           | 19 +++++++
 compiler/tests/repl/runner/runner.v | 83 +++++++++++++++++++++++++++++
 examples/database/pg/.gitignore     |  1 +
 5 files changed, 118 insertions(+), 45 deletions(-)
 create mode 100644 compiler/tests/repl/.gitignore
 create mode 100644 compiler/tests/repl/run.v
 create mode 100644 compiler/tests/repl/runner/runner.v
 create mode 100644 examples/database/pg/.gitignore

diff --git a/compiler/tests/repl/.gitignore b/compiler/tests/repl/.gitignore
new file mode 100644
index 0000000000..f5bdd214e0
--- /dev/null
+++ b/compiler/tests/repl/.gitignore
@@ -0,0 +1 @@
+run
diff --git a/compiler/tests/repl/repl_test.v b/compiler/tests/repl/repl_test.v
index 29eb0cd8fa..e2aa8588b2 100644
--- a/compiler/tests/repl/repl_test.v
+++ b/compiler/tests/repl/repl_test.v
@@ -1,24 +1,9 @@
 import os
-
-fn full_path_to_v() string {
-	vname  := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
-	vexec := os.dir(os.dir(os.dir(os.dir( os.executable() )))) + os.PathSeparator + vname
-	return vexec
-}
+import compiler.tests.repl.runner
 
 fn test_the_v_compiler_can_be_invoked() {
-	vexec := full_path_to_v()
+	vexec := runner.full_path_to_v()
 	println('vexecutable: $vexec')
-	/*
-	args := os.args
-	vreal  := os.realpath('v')
-	myself := os.realpath( os.executable() )
-	wd := os.getwd() + os.PathSeparator
-	println('args are: $args')
-	println('vreal   : $vreal')
-	println('myself  : $myself')
-	println('wd      : $wd')
-    */
 	assert vexec != ''
 
 	vcmd := '$vexec --version'
@@ -33,35 +18,19 @@ fn test_the_v_compiler_can_be_invoked() {
 	assert r_error.output == '`nonexisting.v` does not exist'
 }
 
-fn test_the_v_repl() {
-	test_files := os.walk_ext('.', '.repl')
-	wd := os.getwd() + os.PathSeparator
-	vexec := full_path_to_v()
-	
-	for file in test_files {
-		fcontent := os.read_file(file) or {
+fn test_all_v_repl_files() {
+	options := runner.new_options()
+	global_start_time := runner.now()	
+	for file in options.files {
+		sticks := runner.now()
+		fres := runner.run_repl_file(options.wd, options.vexec, file) or {
 			assert false
-			break
-		}
-		content := fcontent.replace('\r', '')		
-		input := content.all_before('===output===\n')
-		output := content.all_after('===output===\n')
-		
-		input_temporary_filename := 'input_temporary_filename.txt'
-		os.write_file(input_temporary_filename, input)
-		defer { os.rm(input_temporary_filename) }		
-		r := os.exec('$vexec runrepl < $input_temporary_filename') or {
-			assert false
-			break
-		}
-		result := r.output.replace('\r','').replace('>>> ', '').replace('>>>', '').replace('... ', '').all_after('Use Ctrl-C or `exit` to exit\n').replace(wd, '' )
-		assert result == output
-		if result != output {
-			println(file)
-			println('Got : |$result|')
-			println('Expected : |$output|')
-		} else {
-			println('Repl file $file is OK')
+			eprintln( runner.tdiff_in_ms(err, sticks) )
+			continue
 		}
+		assert true
+		println( runner.tdiff_in_ms(fres, sticks) )
 	}
+	println( runner.tdiff_in_ms('<=== total time spent running REPL files', global_start_time) )
 }
+
diff --git a/compiler/tests/repl/run.v b/compiler/tests/repl/run.v
new file mode 100644
index 0000000000..c71444ae48
--- /dev/null
+++ b/compiler/tests/repl/run.v
@@ -0,0 +1,19 @@
+module main
+
+import compiler.tests.repl.runner
+import log
+
+fn main(){
+	logger := &log.Log{log.DEBUG, 'terminal'}
+	options := runner.new_options()
+	global_start_time := runner.now()
+	for file in options.files {
+		stime := runner.now()
+		fres := runner.run_repl_file(options.wd, options.vexec, file) or {
+			logger.error( runner.tdiff_in_ms(err, stime) )
+			continue
+		}
+		logger.info( runner.tdiff_in_ms(fres, stime) )
+	}
+	logger.info( runner.tdiff_in_ms('<=== total time spent running REPL files', global_start_time) )
+}
diff --git a/compiler/tests/repl/runner/runner.v b/compiler/tests/repl/runner/runner.v
new file mode 100644
index 0000000000..652c534111
--- /dev/null
+++ b/compiler/tests/repl/runner/runner.v
@@ -0,0 +1,83 @@
+module runner
+
+import os
+import time
+
+struct RunnerOptions {
+pub:
+	wd string
+	vexec string
+	files []string
+}
+
+pub fn full_path_to_v() string {
+	vname  := if os.user_os() == 'windows' { 'v.exe' } else { 'v' }
+	vexec := os.dir(os.dir(os.dir(os.dir( os.executable() )))) + os.PathSeparator + vname
+	/*
+	args := os.args
+	vreal  := os.realpath('v')
+	myself := os.realpath( os.executable() )
+	wd := os.getwd() + os.PathSeparator
+	println('args are: $args')
+	println('vreal   : $vreal')
+	println('myself  : $myself')
+	println('wd      : $wd')
+    */	
+	return vexec
+}
+
+pub fn run_repl_file(wd string, vexec string, file string) string? {
+	fcontent := os.read_file(file) or {	return error('Could not read file $file') }
+	content := fcontent.replace('\r', '')		
+	input := content.all_before('===output===\n')
+	output := content.all_after('===output===\n')
+	
+	input_temporary_filename := 'input_temporary_filename.txt'
+	os.write_file(input_temporary_filename, input)
+
+	r := os.exec('$vexec runrepl < $input_temporary_filename') or {
+		os.rm(input_temporary_filename)
+		return error('Could not execute "$vexec runrepl < $input_temporary_filename" ')
+	}
+	os.rm(input_temporary_filename)
+
+	result := r.output.replace('\r','').replace('>>> ', '').replace('>>>', '').replace('... ', '').all_after('Use Ctrl-C or `exit` to exit\n').replace(wd, '' )
+
+	if result != output {
+		return error('Difference found in REPL file: $file
+====> Got      :
+|$result|
+====> Expected :
+|$output|
+		')
+	} else {
+		return 'Repl file $file is OK'
+	}
+}
+
+pub fn new_options() RunnerOptions {
+	wd := os.getwd() + os.PathSeparator
+	vexec := full_path_to_v()
+	mut files := []string
+	if os.args.len > 1 {
+		files = os.args.right(1)
+	} else {
+		files = os.walk_ext('.', '.repl')
+	}
+	return RunnerOptions {
+		wd: wd
+		vexec: vexec
+		files: files
+	}
+}
+
+pub fn now() i64 {
+	return time.ticks()
+}
+
+pub fn tdiff_in_ms(s string, sticks i64) string {
+	eticks := time.ticks()
+	tdiff := (eticks - sticks)
+	return '${tdiff:6d} ms | $s'
+}
+
diff --git a/examples/database/pg/.gitignore b/examples/database/pg/.gitignore
new file mode 100644
index 0000000000..23830aa51c
--- /dev/null
+++ b/examples/database/pg/.gitignore
@@ -0,0 +1 @@
+customer