|
16 | 16 | # See the License for the specific language governing permissions and |
17 | 17 | # limitations under the License. |
18 | 18 |
|
19 | | -import JSON |
| 19 | +import BenchmarkTools |
| 20 | +using Printf |
| 21 | + |
| 22 | +# Benchmark variables: |
| 23 | +name = "cabsf"; |
| 24 | +repeats = 3; |
20 | 25 |
|
21 | 26 | """ |
22 | | - gen( re, im, name ) |
| 27 | + print_version() |
| 28 | +
|
| 29 | +Prints the TAP version. |
23 | 30 |
|
24 | | -Generate fixture data and write to file. |
| 31 | +# Examples |
| 32 | +
|
| 33 | +``` julia |
| 34 | +julia> print_version() |
| 35 | +``` |
| 36 | +""" |
| 37 | +function print_version() |
| 38 | + @printf( "TAP version 13\n" ); |
| 39 | +end |
| 40 | + |
| 41 | +""" |
| 42 | + print_summary( total, passing ) |
| 43 | +
|
| 44 | +Print the benchmark summary. |
25 | 45 |
|
26 | 46 | # Arguments |
27 | 47 |
|
28 | | -* `re`: domain (real components) |
29 | | -* `im`: domain (imaginary components) |
30 | | -* `name::AbstractString`: output filename |
| 48 | +* `total`: total number of tests |
| 49 | +* `passing`: number of passing tests |
31 | 50 |
|
32 | 51 | # Examples |
33 | 52 |
|
34 | 53 | ``` julia |
35 | | -julia> re = range( -1.0, stop = 1.0, length = 2001 ); |
36 | | -julia> im = range( -1.0, stop = 1.0, length = 2001 ); |
37 | | -julia> gen( re, im, \"data.json\" ); |
| 54 | +julia> print_summary( 3, 3 ) |
38 | 55 | ``` |
39 | 56 | """ |
40 | | -function gen( re, im, name ) |
41 | | - z = complex.( re, im ); |
42 | | - y = Array{Float32}( undef, length(z) ); |
43 | | - for i in eachindex(z) |
44 | | - y[i] = abs( ComplexF32( z[i] ) ); |
45 | | - end |
| 57 | +function print_summary( total, passing ) |
| 58 | + @printf( "#\n" ); |
| 59 | + @printf( "1..%d\n", total ); # TAP plan |
| 60 | + @printf( "# total %d\n", total ); |
| 61 | + @printf( "# pass %d\n", passing ); |
| 62 | + @printf( "#\n" ); |
| 63 | + @printf( "# ok\n" ); |
| 64 | +end |
| 65 | + |
| 66 | +""" |
| 67 | + print_results( iterations, elapsed ) |
| 68 | +
|
| 69 | +Print benchmark results. |
| 70 | +
|
| 71 | +# Arguments |
| 72 | +
|
| 73 | +* `iterations`: number of iterations |
| 74 | +* `elapsed`: elapsed time (in seconds) |
| 75 | +
|
| 76 | +# Examples |
| 77 | +
|
| 78 | +``` julia |
| 79 | +julia> print_results( 1000000, 0.131009101868 ) |
| 80 | +``` |
| 81 | +""" |
| 82 | +function print_results( iterations, elapsed ) |
| 83 | + rate = iterations / elapsed |
46 | 84 |
|
47 | | - # Store data to be written to file as a collection: |
48 | | - data = Dict([ |
49 | | - ("re", re), |
50 | | - ("im", im), |
51 | | - ("expected", y) |
52 | | - ]); |
53 | | - |
54 | | - # Based on the script directory, create an output filepath: |
55 | | - filepath = joinpath( dir, name ); |
56 | | - |
57 | | - # Write the data to the output filepath as JSON: |
58 | | - outfile = open( filepath, "w" ); |
59 | | - write( outfile, JSON.json(data) ); |
60 | | - write( outfile, "\n" ); |
61 | | - close( outfile ); |
| 85 | + @printf( " ---\n" ); |
| 86 | + @printf( " iterations: %d\n", iterations ); |
| 87 | + @printf( " elapsed: %0.9f\n", elapsed ); |
| 88 | + @printf( " rate: %0.9f\n", rate ); |
| 89 | + @printf( " ...\n" ); |
62 | 90 | end |
63 | 91 |
|
64 | | -# Get the filename: |
65 | | -file = @__FILE__; |
| 92 | +""" |
| 93 | + benchmark() |
| 94 | +
|
| 95 | +Run a benchmark. |
| 96 | +
|
| 97 | +# Notes |
| 98 | +
|
| 99 | +* Benchmark results are returned as a two-element array: [ iterations, elapsed ]. |
| 100 | +* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation. |
| 101 | +* The elapsed time is in seconds. |
| 102 | +
|
| 103 | +# Examples |
| 104 | +
|
| 105 | +``` julia |
| 106 | +julia> out = benchmark(); |
| 107 | +``` |
| 108 | +""" |
| 109 | +function benchmark() |
| 110 | + t = BenchmarkTools.@benchmark abs( ComplexF32( (rand()*1000.0) - 500.0, (rand()*1000.0) - 500.0 ) ) samples=1e6 |
| 111 | + |
| 112 | + # Compute the total "elapsed" time and convert from nanoseconds to seconds: |
| 113 | + s = sum( t.times ) / 1.0e9; |
| 114 | + |
| 115 | + # Determine the number of "iterations": |
| 116 | + iter = length( t.times ); |
66 | 117 |
|
67 | | -# Extract the directory in which this file resides: |
68 | | -dir = dirname( file ); |
| 118 | + # Return the results: |
| 119 | + [ iter, s ]; |
| 120 | +end |
| 121 | + |
| 122 | +""" |
| 123 | + main() |
| 124 | +
|
| 125 | +Run benchmarks. |
| 126 | +
|
| 127 | +# Examples |
| 128 | +
|
| 129 | +``` julia |
| 130 | +julia> main(); |
| 131 | +``` |
| 132 | +""" |
| 133 | +function main() |
| 134 | + print_version(); |
| 135 | + for i in 1:repeats |
| 136 | + @printf( "# julia::%s\n", name ); |
| 137 | + results = benchmark(); |
| 138 | + print_results( results[ 1 ], results[ 2 ] ); |
| 139 | + @printf( "ok %d benchmark finished\n", i ); |
| 140 | + end |
| 141 | + print_summary( repeats, repeats ); |
| 142 | +end |
69 | 143 |
|
70 | | -# Generate fixture data: |
71 | | -re = range( -1000.0, stop = 1000.0, length = 2003 ); |
72 | | -im = range( -1000.0, stop = 1000.0, length = 2003 ); |
73 | | -gen( re, im, "data.json" ); |
| 144 | +main(); |
0 commit comments