vile / bench / ports / main.v
120 lines · 112 sloc · 3.29 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module main
2
3import flag
4import io
5import os
6import scheme
7import time
8
9@[heap]
10struct Source {
11 data []u8
12mut:
13 position int
14}
15
16fn (mut source Source) read(mut bytes []u8) !int {
17 if source.position == source.data.len { return io.Eof{} }
18 remaining := source.data.len - source.position
19 count := if bytes.len < remaining { bytes.len } else { remaining }
20 copy(mut bytes[..count], source.data[source.position..source.position + count])
21 source.position += count
22 return count
23}
24
25fn median(mut values []u64) f64 {
26 values.sort()
27 return f64(values[values.len / 2]) / 1e6
28}
29
30fn verify(value scheme.Value, kind string, count int) ! {
31 match kind {
32 'string' {
33 if value.as_string()!.len != count { return error('wrong string length') }
34 }
35 'escaped-string' {
36 if value.as_string()!.len != count * 2 { return error('wrong decoded string length') }
37 }
38 'identifier' {
39 if value.symbol_name()!.len != count { return error('wrong symbol length') }
40 }
41 'ints' {
42 mut cursor := value
43 for _ in 0 .. count {
44 car, cdr := cursor.pair_parts()!
45 if car.as_integer()! != 123 { return error('wrong list element') }
46 cursor = cdr
47 }
48 if cursor.kind() != .nil { return error('wrong list length') }
49 }
50 else { return error('unknown case') }
51 }
52}
53
54fn run_case(mut vm scheme.VM, kind string, count int, samples int) ! {
55 text := match kind {
56 'string' { '"' + 'a'.repeat(count) + '"' }
57 'escaped-string' { '"' + r'a\"'.repeat(count) + '"' }
58 'identifier' { 'a'.repeat(count) + ' ' }
59 'int-stream' { '123 '.repeat(count) }
60 else { '(' + '123 '.repeat(count) + ')' }
61 }
62 mut source := &Source{ data: text.bytes() }
63 mut times := []u64{cap: samples}
64 for sample in -1 .. samples {
65 source.position = 0
66 start := time.sys_mono_now()
67 port := vm.input_port(source)!
68 mut value := scheme.unspecified()
69 mut found := 0
70 mut sum := i64(0)
71 if kind == 'int-stream' {
72 for {
73 value = vm.read(port)!
74 if value.kind() == .eof { break }
75 found++
76 sum += value.as_integer()!
77 }
78 } else {
79 value = vm.read(port)!
80 }
81 elapsed := time.sys_mono_now() - start
82 if kind == 'int-stream' {
83 if found != count || sum != i64(123) * count {
84 return error('incorrect integer stream')
85 }
86 } else {
87 verify(value, kind, count)!
88 }
89 vm.close_input_port(port)!
90 if sample >= 0 { times << elapsed }
91 }
92 println('${kind:16} ${text.len:12} ${median(mut times):12.3f}')
93}
94
95fn run() ! {
96 mut flags := flag.new_flag_parser(os.args)
97 flags.application('ports')
98 flags.description('Checked embedding benchmark: create a buffered input port and read one datum or an integer stream.')
99 flags.skip_executable()
100 size := flags.int('size', 0, 1000000, 'String/symbol bytes or list elements.')
101 samples := flags.int('samples', 0, 15, 'Measured samples, after one warm-up (odd: 1..101).')
102 flags.limit_free_args_to_exactly(0)!
103 flags.finalize()!
104 if size < 1 || size > 10000000 || samples < 1 || samples > 101 || samples % 2 == 0 {
105 return error('invalid size or sample count')
106 }
107 mut vm := scheme.new()
108 println('Includes port creation, framing, parsing and mutable datum conversion. Verification follows timing.')
109 println('case source bytes median ms')
110 for kind in ['string', 'escaped-string', 'identifier', 'ints', 'int-stream'] {
111 run_case(mut vm, kind, size, samples)!
112 }
113}
114
115fn main() {
116 run() or {
117 eprintln(err)
118 exit(1)
119 }
120}
121