tobogganSe connecter

packages

The Louise packages, each released on its own

Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.

Packages: search's bench and README654afd2 · il y a 3 sem.
218 lignes · 5277 octetsBrut · Historique
1// Build and query latency of the search package over a generated corpus
2// of 10,000 notes (and an FM-index of the first 500): not a test — run it
3// here with `louise run --native`. The corpus is the same on every run
4// (seeded); the hit counts are the checksum, the timings are the report.
5
6import search
7import search/fm
8import std/io
9import std/list
10import std/random.{type random}
11import std/string
12import std/time.{type clock}
13
14// The corpus size, in notes.
15fn notes_count :: -> Int
16{
17 10000
18}
19
20// The FM-indexed part of the corpus, in notes.
21fn fm_notes :: -> Int
22{
23 500
24}
25
26// The query count per kind.
27fn query_count :: -> Int
28{
29 100
30}
31
32// ---- the corpus --------------------------------------------------------------
33
34/// Pseudo-words from syllables: about 2,600, so trigrams overlap as in a
35/// real corpus.
36fn vocabulary :: -> List<String>
37{
38 let onsets =
39 [ ""
40 , "b"
41 , "ch"
42 , "d"
43 , "f"
44 , "g"
45 , "h"
46 , "j"
47 , "k"
48 , "l"
49 , "m"
50 , "n"
51 , "p"
52 , "r"
53 , "s"
54 , "sh"
55 , "t"
56 , "v"
57 , "w"
58 , "z"
59 ]
60 let nuclei =
61 ["a", "an", "ar", "e", "en", "er", "i", "in", "o", "on", "or", "u", "un"]
62 let codas = ["", "l", "m", "n", "r", "s", "sh", "t", "th", "x"]
63 use onset <- list.flat_map onsets
64 use nucleus <- list.flat_map nuclei
65 use coda <- list.map codas
66 onset <> nucleus <> coda
67}
68
69/// Rare terms, the kind a search is really for.
70fn rare_terms :: -> List<String>
71{
72 [ "adr0043"
73 , "checkpoint"
74 , "driftsort"
75 , "fmindex"
76 , "galloping"
77 , "hibernation"
78 , "keepsake"
79 , "merkle"
80 , "perceus"
81 , "quicksort"
82 , "roaring"
83 , "splitmix"
84 , "tokipona"
85 , "trigram"
86 , "worktree"
87 ]
88}
89
90/// A note: 8 to 50 words, and a rare term or two in some.
91fn note :: vocab: List<String> rare: List<String> -> String / <random>
92{
93 let words = gen_list 8 50 fn { pick vocab }
94 let rares = if gen_int 10 < 3 { gen_list 1 2 fn { pick rare } } else { [] }
95 string.join (list.append words rares) " "
96}
97
98fn gen_list ::
99 min: Int
100 max: Int
101 item: fn() -> a / <random>
102 -> List<a> / <random>
103{
104 let n = min + gen_int (max - min + 1)
105 list.map (list.range 1 n) fn _ { item () }
106}
107
108fn gen_int :: below: Int -> Int / <random>
109{
110 random.int below
111}
112
113fn pick :: xs: List<a> -> a / <random>
114{
115 match list.drop xs (random.int (list.length xs))
116 { [x, ..] -> x, [] -> panic "bench: nothing to pick" }
117}
118
119fn corpus :: -> List<(Int, String)> / <random>
120{
121 let vocab = vocabulary ()
122 let rare = rare_terms ()
123 let texts = list.map (list.range 1 (notes_count ())) fn _ { note vocab rare }
124 list.zip (list.range 0 (notes_count () - 1)) texts
125}
126
127// ---- the queries ---------------------------------------------------------------
128
129/// `query_count()` common words, two-word phrases, rare terms and short
130/// (1–2 character) queries.
131fn queries :: vocab: List<String> rare: List<String> -> List<String> / <random>
132{
133 let words = list.map (list.range 1 (query_count ())) fn _ { pick vocab }
134 let phrases =
135 list.map
136 (list.range 1 (query_count ()))
137 fn _ { pick vocab <> " " <> pick vocab }
138 let rares = list.map (list.range 1 (query_count ())) fn _ { pick rare }
139 let shorts =
140 list.map
141 (list.range 1 (query_count ()))
142 fn _
143 {
144 let w = pick vocab
145 string.slice w 0 (1 + gen_int 2)
146 }
147 use qs <- list.flat_map [words, phrases, rares, shorts]
148 qs
149}
150
151// ---- running ----------------------------------------------------------------------
152
153// The wall clock, not the (virtual, deterministic) monotonic one
154// (ADR 0004, Revision 3): a bench wants real time.
155
156fn now_ns :: -> Int / <clock>
157{
158 time.unix_ns (time.now ())
159}
160
161fn ms :: start: Int -> Int / <clock>
162{
163 (now_ns () - start) / 1000000
164}
165
166fn total_chars :: notes: List<(Int, String)> -> Int
167{
168 list.fold notes 0 fn acc n { acc + string.length n.1 }
169}
170
171/// The number of hits of every query, summed.
172fn run_queries :: idx: search.Index queries: List<String> acc: Int -> Int
173{
174 match queries
175 { [] -> acc
176 , [q, ..rest] ->
177 run_queries idx rest (acc + list.length (search.search idx q))
178 }
179}
180
181fn run_fm :: c: fm.Corpus patterns: List<String> acc: Int -> Int
182{
183 match patterns
184 { [] -> acc, [p, ..rest] -> run_fm c rest (acc + fm.corpus_count c p) }
185}
186
187fn bench :: -> () / <io, clock, random>
188{
189 let notes = corpus ()
190 let t0 = now_ns ()
191 let idx = search.build notes
192 let build_ms = ms t0
193 let vocab = vocabulary ()
194 let qs = queries vocab (rare_terms ())
195 let t1 = now_ns ()
196 let hits = run_queries idx qs 0
197 let query_ms = ms t1
198 io.println
199 "trigram: built {notes_count ()} notes ({total_chars notes} chars) in {build_ms} ms"
200 io.println "trigram: {list.length qs} queries in {query_ms} ms, {hits} hits"
201 // The FM-index of the first notes.
202 let texts = list.map (list.take notes (fm_notes ())) fn n { n.1 }
203 let t2 = now_ns ()
204 let c = fm.build_corpus texts
205 let fm_build_ms = ms t2
206 let t3 = now_ns ()
207 let fm_hits = run_fm c qs 0
208 let fm_query_ms = ms t3
209 io.println
210 "fm: built {fm_notes ()} notes ({total_chars (list.take notes (fm_notes ()))} chars) in {fm_build_ms} ms"
211 io.println
212 "fm: {list.length qs} count queries in {fm_query_ms} ms, {fm_hits} hits"
213}
214
215pub fn main :: -> () / <io, clock>
216{
217 random.with_seed 42 bench
218}