vile / bench / memory / main.v
101 lines · 96 sloc · 3.71 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module main
2
3import os
4import reader
5import strconv
6
7struct MemoryCase {
8 name string
9 source string
10}
11
12fn cases(size int) []MemoryCase {
13 mixed := '(define (square x) (+ (* x x) 123))\n'
14 quoted := "'`(alpha ,beta ,@gamma)\n"
15 return [
16 MemoryCase{'mixed', mixed.repeat(size / mixed.len + 1)},
17 MemoryCase{'flat-list', '(' + 'a '.repeat(size / 2) + ')'},
18 MemoryCase{'flat-vector', '#(' + '1 '.repeat(size / 2) + ')'},
19 MemoryCase{'quoted', quoted.repeat(size / quoted.len + 1)},
20 MemoryCase{'atoms', 'name 123 #t '.repeat(size / 12 + 1)},
21 MemoryCase{'string', '"' + 'x'.repeat(size) + '"'},
22 MemoryCase{'escaped', '"' + 'x'.repeat(size) + r'\"' + '"'},
23 MemoryCase{'uppercase', 'a'.repeat(size) + 'Z'},
24 ]
25}
26
27fn measure(c MemoryCase, samples int) ! {
28 warm := reader.read_all(c.source)!
29 gc_collect()
30 mut read_bytes := u64(0)
31 mut write_bytes := u64(0)
32 mut value_bytes := u64(0)
33 mut read_gcs := u64(0)
34 mut write_gcs := u64(0)
35 mut value_gcs := u64(0)
36 mut output := 0
37 for _ in 0 .. samples {
38 read_before := gc_heap_usage().total_bytes
39 read_gc_before := collections()
40 doc := reader.read_all(c.source)!
41 read_gcs += collections() - read_gc_before
42 read_bytes += u64(gc_heap_usage().total_bytes - read_before)
43 if doc.arena.len() != warm.arena.len() || doc.forms.len != warm.forms.len {
44 return error('${c.name}: parse counts changed')
45 }
46 write_before := gc_heap_usage().total_bytes
47 write_gc_before := collections()
48 mut written := 0
49 for id in doc.forms {
50 written += doc.arena.write(id)!.len
51 }
52 write_gcs += collections() - write_gc_before
53 write_bytes += u64(gc_heap_usage().total_bytes - write_before)
54 if output != 0 && written != output {
55 return error('${c.name}: printer length changed')
56 }
57 output = written
58 if c.name in ['string', 'escaped', 'uppercase'] {
59 value_before := gc_heap_usage().total_bytes
60 value_gc_before := collections()
61 value := if c.name == 'uppercase' {
62 doc.arena.symbol_name(doc.forms[0])!
63 } else {
64 doc.arena.string_value(doc.forms[0])!
65 }
66 value_gcs += collections() - value_gc_before
67 value_bytes += u64(gc_heap_usage().total_bytes - value_before)
68 if value.len == 0 { return error('unexpected empty decoded value') }
69 }
70 }
71 println('${c.name:12} ${c.source.len:9} ${warm.arena.len():8} ${read_bytes / u64(samples):12} ${write_bytes / u64(samples):12} ${value_bytes / u64(samples):12} ${read_gcs:8} ${write_gcs:9} ${value_gcs:9} ${warm.arena.storage_bytes():12}')
72}
73
74fn main() {
75 $if !gcboehm ? {
76 eprintln('This allocation benchmark requires -gc boehm (the default).')
77 exit(1)
78 }
79 size := strconv.parse_int(if os.args.len > 1 { os.args[1] } else { '65536' }, 10, 32) or { panic(err) }
80 samples := strconv.parse_int(if os.args.len > 2 { os.args[2] } else { '9' }, 10, 32) or { panic(err) }
81 selected := if os.args.len > 3 { os.args[3] } else { '' }
82 if size < 128 || samples < 1 || samples > 101 {
83 eprintln('Usage: memory benchmark [bytes >= 128] [samples: 1..101] [case]')
84 exit(1)
85 }
86 mut found := false
87 println('GC enabled: ${gc_is_enabled()}; fresh documents; allocation totals include allocator rounding.')
88 println('Bytes are mean allocation per operation; GC counts are totals across ${samples} samples.')
89 println('Corpus construction, warm-up and explicit pre-case collection are excluded. Arenas exclude source, roots and scratch.')
90 println('Value decoding is measured for strings and uppercase symbols only; other rows report zero.')
91 println('case source nodes read alloc write alloc value alloc read GC write GC value GC arena bytes')
92 for c in cases(int(size)) {
93 if selected != '' && c.name != selected { continue }
94 found = true
95 measure(c, int(samples)) or { panic(err) }
96 }
97 if !found {
98 eprintln('unknown case: ${selected}')
99 exit(1)
100 }
101}
102