module main import flag import io import os import scheme import time @[heap] struct Source { data []u8 mut: position int } fn (mut source Source) read(mut bytes []u8) !int { if source.position == source.data.len { return io.Eof{} } remaining := source.data.len - source.position count := if bytes.len < remaining { bytes.len } else { remaining } copy(mut bytes[..count], source.data[source.position..source.position + count]) source.position += count return count } fn median(mut values []u64) f64 { values.sort() return f64(values[values.len / 2]) / 1e6 } fn verify(value scheme.Value, kind string, count int) ! { match kind { 'string' { if value.as_string()!.len != count { return error('wrong string length') } } 'escaped-string' { if value.as_string()!.len != count * 2 { return error('wrong decoded string length') } } 'identifier' { if value.symbol_name()!.len != count { return error('wrong symbol length') } } 'ints' { mut cursor := value for _ in 0 .. count { car, cdr := cursor.pair_parts()! if car.as_integer()! != 123 { return error('wrong list element') } cursor = cdr } if cursor.kind() != .nil { return error('wrong list length') } } else { return error('unknown case') } } } fn run_case(mut vm scheme.VM, kind string, count int, samples int) ! { text := match kind { 'string' { '"' + 'a'.repeat(count) + '"' } 'escaped-string' { '"' + r'a\"'.repeat(count) + '"' } 'identifier' { 'a'.repeat(count) + ' ' } 'int-stream' { '123 '.repeat(count) } else { '(' + '123 '.repeat(count) + ')' } } mut source := &Source{ data: text.bytes() } mut times := []u64{cap: samples} for sample in -1 .. samples { source.position = 0 start := time.sys_mono_now() port := vm.input_port(source)! mut value := scheme.unspecified() mut found := 0 mut sum := i64(0) if kind == 'int-stream' { for { value = vm.read(port)! if value.kind() == .eof { break } found++ sum += value.as_integer()! } } else { value = vm.read(port)! } elapsed := time.sys_mono_now() - start if kind == 'int-stream' { if found != count || sum != i64(123) * count { return error('incorrect integer stream') } } else { verify(value, kind, count)! } vm.close_input_port(port)! if sample >= 0 { times << elapsed } } println('${kind:16} ${text.len:12} ${median(mut times):12.3f}') } fn run() ! { mut flags := flag.new_flag_parser(os.args) flags.application('ports') flags.description('Checked embedding benchmark: create a buffered input port and read one datum or an integer stream.') flags.skip_executable() size := flags.int('size', 0, 1000000, 'String/symbol bytes or list elements.') samples := flags.int('samples', 0, 15, 'Measured samples, after one warm-up (odd: 1..101).') flags.limit_free_args_to_exactly(0)! flags.finalize()! if size < 1 || size > 10000000 || samples < 1 || samples > 101 || samples % 2 == 0 { return error('invalid size or sample count') } mut vm := scheme.new() println('Includes port creation, framing, parsing and mutable datum conversion. Verification follows timing.') println('case source bytes median ms') for kind in ['string', 'escaped-string', 'identifier', 'ints', 'int-stream'] { run_case(mut vm, kind, size, samples)! } } fn main() { run() or { eprintln(err) exit(1) } }