| 1 | module main |
| 2 | |
| 3 | import crypto.sha256 |
| 4 | import os |
| 5 | import reader |
| 6 | import time |
| 7 | |
| 8 | struct AltReadCase { |
| 9 | name string |
| 10 | unit string |
| 11 | } |
| 12 | |
| 13 | fn main() { |
| 14 | size := if os.args.len > 1 { os.args[1].int() } else { 262144 } |
| 15 | samples := if os.args.len > 2 { os.args[2].int() } else { 11 } |
| 16 | if size < 128 || samples < 1 || samples > 101 || samples % 2 == 0 { |
| 17 | panic('usage: alt_reader [bytes>=128] [odd samples 1..101] [--for]') |
| 18 | } |
| 19 | mut cases := [ |
| 20 | AltReadCase{'identifiers', 'alpha beta gamma delta\n'}, |
| 21 | AltReadCase{'calls', 'f(alpha,g(beta),h(gamma,delta))\n'}, |
| 22 | AltReadCase{'infix', 'result:=a+b*c<d && !flag\n'}, |
| 23 | AltReadCase{'vectors', '[alpha,42,fn(x){x+1},[beta,gamma]]\n'}, |
| 24 | AltReadCase{'control', 'if x>0 {while y<10 {y=y+1}} else {fn(z){z*z}(2)}\n'}, |
| 25 | AltReadCase{'for-calls', 'for(1,2,3)\n'}, |
| 26 | ] |
| 27 | if os.args.contains('--for') { |
| 28 | cases << AltReadCase{'for-loops', 'for(i:=0,i<10,i=i+1){total=total+i}\n'} |
| 29 | } |
| 30 | for c in cases { |
| 31 | source := c.unit.repeat(size / c.unit.len + 1) |
| 32 | warm := reader.read_alt(source) or { panic(err.msg()) } |
| 33 | mut times := []u64{len: samples} |
| 34 | for i in 0 .. samples { |
| 35 | start := time.sys_mono_now() |
| 36 | doc := reader.read_alt(source) or { panic(err.msg()) } |
| 37 | times[i] = time.sys_mono_now() - start |
| 38 | if doc.forms.len != warm.forms.len || doc.arena.len() != warm.arena.len() { |
| 39 | panic('inconsistent reader result for ${c.name} in sample ${i}') |
| 40 | } |
| 41 | } |
| 42 | times.sort() |
| 43 | println('{"case":"${c.name}","sha256":"${sha256.hexhash(source)}","bytes":${source.len},"nodes":${warm.arena.len()},"forms":${warm.forms.len},"samples":${samples},"read_ns":${times[samples / 2]}}') |
| 44 | } |
| 45 | } |
| 46 | |