Repository: benhoyt/goawk
Pull request #120 (2022-05-19 20:22:30)

Rename benchmarks, remove examples dir, add Mandelbrot test


Patch:
diff --git a/scripts/benchmark_awks.py b/scripts/benchmark_awks.py
index 77aa934..7fbd919 100755
--- a/scripts/benchmark_awks.py
+++ b/scripts/benchmark_awks.py
@@ -12,29 +12,30 @@ import time
 
 AWKS = [
     './goawk',
-    './orig', # GoAWK without perf improvements (commit 8ab5446)
+    './orig', # GoAWK without perf improvements
     'original-awk',
     'gawk',
     'mawk',
 ]
 NORM_INDEX = AWKS.index('original-awk')
-# Only get the mean of these tests because these are the only ones
-# we show in the GoAWK article.
-TESTS_TO_MEAN = [
-    'tt.01',
-    'tt.02',
-    'tt.02a',
-    'tt.03',
-    'tt.03a',
-    'tt.04',
-    'tt.05',
-    'tt.06',
-    'tt.07',
-    'tt.big',
-    'tt.x1',
-    'tt.x2',
-]
-MEAN_TESTS = []
+TESTS_TO_MEAN = None  # By default, calculate the mean of all tests
+if False:
+    # Only get the mean of these tests because these are the only ones
+    # we show in the GoAWK article.
+    TESTS_TO_MEAN = [
+        'tt.01_print',
+        'tt.02_print_NR_NF',
+        'tt.02a_print_length',
+        'tt.03_sum_length',
+        'tt.03a_sum_field',
+        'tt.04_printf_fields',
+        'tt.05_concat_fields',
+        'tt.06_count_lengths',
+        'tt.07_even_fields',
+        'tt.big_complex_program',
+        'tt.x1_mandelbrot',
+        'tt.x2_sum_loop',
+    ]
 NUM_RUNS = 6
 MIN_TIME = 0.5
 PROGRAM_GLOB = 'testdata/tt.*'
@@ -50,13 +51,13 @@ def repeat_file(input_file, repeated_file, n):
             shutil.copyfileobj(fin, fout)
 
 
-print('Test      ', end='')
+print('Test                         ', end='')
 for awk in AWKS:
     display_awk = os.path.basename(awk)
     display_awk = display_awk.replace('original-awk', 'awk')
-    print('| {:>5} '.format(display_awk), end='')
+    print('| {:>8} '.format(display_awk), end='')
 print()
-print('-'*9 + ' | -----'*len(AWKS))
+print('-'*28 + ' | --------'*len(AWKS))
 
 repeats_created = []
 products = [1] * len(AWKS)
@@ -100,18 +101,19 @@ for program in programs:
     norm_time = awk_times[NORM_INDEX]
     speeds = [norm_time/t for t in awk_times]
     test_name = program.split('/')[1]
-    if test_name in TESTS_TO_MEAN:
+    if TESTS_TO_MEAN is None or test_name in TESTS_TO_MEAN:
         num_products += 1
         for i in range(len(AWKS)):
             products[i] *= speeds[i]
 
-    print('{:9}'.format(test_name), end='')
+    display_name = test_name.split('_')[0] + ' (' + ' '.join(test_name.split('_')[1:]) + ')'
+    print('{:28}'.format(display_name), end='')
     for i, awk in enumerate(AWKS):
-        print(' | {:5.2f}'.format(speeds[i]), end='')
+        print(' | {:8.2f}'.format(speeds[i]), end='')
     print()
 
-print('-'*9 + ' | -----'*len(AWKS))
-print('**Geo mean** ', end='')
+print('-'*28 + ' | --------'*len(AWKS))
+print('**Geo mean**                ', end='')
 for i, awk in enumerate(AWKS):
     print(' | **{:.2f}**'.format(products[i] ** (1.0/num_products)), end='')
 print()
diff --git a/examples/mandel.awk b/testdata/tt.x1_mandelbrot
similarity index 95%
rename from examples/mandel.awk
rename to testdata/tt.x1_mandelbrot
index e4a1a2b..661046b 100644
--- a/examples/mandel.awk
+++ b/testdata/tt.x1_mandelbrot
@@ -2,7 +2,8 @@
 
 BEGIN {
     # Constants to determine size and coordinates of grid
-    width = 150; height = 50
+    if (!width) width = 600
+    if (!height) height = 300
     min_x = -2.1; max_x = 0.6
     min_y = -1.2; max_y = 1.2
     iters = 32


Test patch:
diff --git a/goawk_test.go b/goawk_test.go
index 6de65c2..9274a23 100644
--- a/goawk_test.go
+++ b/goawk_test.go
@@ -698,3 +698,40 @@ func TestCSVDocExamples(t *testing.T) {
 		t.Error("unexpectedly in test at end of file")
 	}
 }
+
+func TestMandelbrot(t *testing.T) {
+	stdout, stderr, err := runGoAWK([]string{"-v", "width=80", "-v", "height=25", "-f", "testdata/tt.x1_mandelbrot"}, "")
+	if err != nil {
+		t.Fatalf("expected no error, got %v (%q)", err, stderr)
+	}
+	expected := `
+................................................................................
+......................................................--+-----..................
+....................................................-----+*+-++-................
+.................................................--------+* *+-----.............
+..............................................--------+#     #%*-------.........
+.........................................------------++$       +-----------.....
+...................................---------*  # +*              # *+++++%+--...
+............................----------------++                       @   *----..
+.......................-+----------------+$                             %+----..
+..................-------*++%++**+++---++                                  #+--.
+...............----------+*         #*++*                                 %*---.
+.............-------+++++*              #                                 #----.
+....------+-------++**@  @                                               ------.
+....------+-------++**@  @                                               ------.
+.............-------+++++*              #                                 #----.
+...............----------+*         #*++*                                 %*---.
+..................-------*++%++**+++---++                                  #+--.
+.......................-+----------------+$                             %+----..
+............................----------------++                       @   *----..
+...................................---------*  # +*              # *+++++%+--...
+.........................................------------++$       +-----------.....
+..............................................--------+#     #%*-------.........
+.................................................--------+* *+-----.............
+....................................................-----+*+-++-................
+......................................................--+-----..................
+`[1:]
+	if stdout != expected {
+		t.Fatalf("expected:\n%s\ngot:\n%s", expected, stdout)
+	}
+}
diff --git a/testdata/tt.01 b/testdata/tt.01_print
similarity index 100%
rename from testdata/tt.01
rename to testdata/tt.01_print
diff --git a/testdata/tt.02 b/testdata/tt.02_print_NR_NF
similarity index 100%
rename from testdata/tt.02
rename to testdata/tt.02_print_NR_NF
diff --git a/testdata/tt.02a b/testdata/tt.02a_print_length
similarity index 100%
rename from testdata/tt.02a
rename to testdata/tt.02a_print_length
diff --git a/testdata/tt.03 b/testdata/tt.03_sum_length
similarity index 100%
rename from testdata/tt.03
rename to testdata/tt.03_sum_length
diff --git a/testdata/tt.03a b/testdata/tt.03a_sum_field
similarity index 100%
rename from testdata/tt.03a
rename to testdata/tt.03a_sum_field
diff --git a/testdata/tt.04 b/testdata/tt.04_printf_fields
similarity index 100%
rename from testdata/tt.04
rename to testdata/tt.04_printf_fields
diff --git a/testdata/tt.05 b/testdata/tt.05_concat_fields
similarity index 100%
rename from testdata/tt.05
rename to testdata/tt.05_concat_fields
diff --git a/testdata/tt.06 b/testdata/tt.06_count_lengths
similarity index 100%
rename from testdata/tt.06
rename to testdata/tt.06_count_lengths
diff --git a/testdata/tt.07 b/testdata/tt.07_even_fields
similarity index 100%
rename from testdata/tt.07
rename to testdata/tt.07_even_fields
diff --git a/testdata/tt.08 b/testdata/tt.08_even_lengths
similarity index 100%
rename from testdata/tt.08
rename to testdata/tt.08_even_lengths
diff --git a/testdata/tt.08z_regex_simple b/testdata/tt.08z_regex_simple
new file mode 100644
index 0000000..cb7dc93
--- /dev/null
+++ b/testdata/tt.08z_regex_simple
@@ -0,0 +1,1 @@
+/foo.*d/ { n++ } END { print n }
diff --git a/testdata/tt.09 b/testdata/tt.09_regex_starts_with
similarity index 100%
rename from testdata/tt.09
rename to testdata/tt.09_regex_starts_with
diff --git a/testdata/tt.10 b/testdata/tt.10_regex_ends_with
similarity index 100%
rename from testdata/tt.10
rename to testdata/tt.10_regex_ends_with
diff --git a/testdata/tt.10a b/testdata/tt.10a_regex_ends_with_var
similarity index 100%
rename from testdata/tt.10a
rename to testdata/tt.10a_regex_ends_with_var
diff --git a/testdata/tt.11 b/testdata/tt.11_substr
similarity index 100%
rename from testdata/tt.11
rename to testdata/tt.11_substr
diff --git a/testdata/tt.12 b/testdata/tt.12_update_fields
similarity index 100%
rename from testdata/tt.12
rename to testdata/tt.12_update_fields
diff --git a/testdata/tt.13 b/testdata/tt.13_array_ops
similarity index 100%
rename from testdata/tt.13
rename to testdata/tt.13_array_ops
diff --git a/testdata/tt.13a b/testdata/tt.13a_array_printf
similarity index 100%
rename from testdata/tt.13a
rename to testdata/tt.13a_array_printf
diff --git a/testdata/tt.14 b/testdata/tt.14_function_call
similarity index 100%
rename from testdata/tt.14
rename to testdata/tt.14_function_call
diff --git a/testdata/tt.15 b/testdata/tt.15_format_lines
similarity index 100%
rename from testdata/tt.15
rename to testdata/tt.15_format_lines
diff --git a/testdata/tt.16 b/testdata/tt.16_count_words
similarity index 100%
rename from testdata/tt.16
rename to testdata/tt.16_count_words
diff --git a/testdata/tt.big b/testdata/tt.big_complex_program
similarity index 100%
rename from testdata/tt.big
rename to testdata/tt.big_complex_program
diff --git a/testdata/tt.x1 b/testdata/tt.x1
deleted file mode 100644
index 091ea9e..0000000
--- a/testdata/tt.x1
+++ /dev/null
@@ -1,45 +0,0 @@
-# Generate and display the Mandelbrot set
-
-BEGIN {
-    # Constants to determine size and coordinates of grid
-    width = 600; height = 300
-    min_x = -2.1; max_x = 0.6
-    min_y = -1.2; max_y = 1.2
-    iters = 32
-
-    # "Colors", from '.' (diverges fastest) to '@' (diverges slowly),
-    # and then ' ' for doesn't diverge within `iters` iterations.
-    colors[0] = "."
-    colors[1] = "-"
-    colors[2] = "+"
-    colors[3] = "*"
-    colors[4] = "%%"
-    colors[5] = "#"
-    colors[6] = "$"
-    colors[7] = "@"
-    colors[8] = " "
-
-    # Loop from top to bottom, and for each line left to right
-    inc_y = (max_y-min_y) / height
-    inc_x = (max_x-min_x) / width
-    y = min_y
-    for (row=0; row<height; row++) {
-        x = min_x
-        for (col=0; col<width; col++) {
-            zr = zi = 0
-            for (i=0; i<iters; i++) {
-                # Main calculation: z = z^2 + c
-                old_zr = zr
-                zr = zr*zr - zi*zi + x
-                zi = 2*old_zr*zi + y
-                # Stop when magnitude is greater than 2
-                if (zr*zr + zi*zi > 4) break
-            }
-            # Scale "color" according to how fast it diverged
-            printf colors[int(i*8/iters)]
-            x += inc_x
-        }
-        y += inc_y
-        printf "\n"
-    }
-}