// Build and query latency of the search package over a generated corpus // of 10,000 notes (and an FM-index of the first 500): not a test — run it // here with `louise run --native`. The corpus is the same on every run // (seeded); the hit counts are the checksum, the timings are the report. import search import search/fm import std/io import std/list import std/random.{type random} import std/string import std/time.{type clock} // The corpus size, in notes. fn notes_count :: -> Int { 10000 } // The FM-indexed part of the corpus, in notes. fn fm_notes :: -> Int { 500 } // The query count per kind. fn query_count :: -> Int { 100 } // ---- the corpus -------------------------------------------------------------- /// Pseudo-words from syllables: about 2,600, so trigrams overlap as in a /// real corpus. fn vocabulary :: -> List { let onsets = [ "" , "b" , "ch" , "d" , "f" , "g" , "h" , "j" , "k" , "l" , "m" , "n" , "p" , "r" , "s" , "sh" , "t" , "v" , "w" , "z" ] let nuclei = ["a", "an", "ar", "e", "en", "er", "i", "in", "o", "on", "or", "u", "un"] let codas = ["", "l", "m", "n", "r", "s", "sh", "t", "th", "x"] use onset <- list.flat_map onsets use nucleus <- list.flat_map nuclei use coda <- list.map codas onset <> nucleus <> coda } /// Rare terms, the kind a search is really for. fn rare_terms :: -> List { [ "adr0043" , "checkpoint" , "driftsort" , "fmindex" , "galloping" , "hibernation" , "keepsake" , "merkle" , "perceus" , "quicksort" , "roaring" , "splitmix" , "tokipona" , "trigram" , "worktree" ] } /// A note: 8 to 50 words, and a rare term or two in some. fn note :: vocab: List rare: List -> String / { let words = gen_list 8 50 fn { pick vocab } let rares = if gen_int 10 < 3 { gen_list 1 2 fn { pick rare } } else { [] } string.join (list.append words rares) " " } fn gen_list :: min: Int max: Int item: fn() -> a / -> List / { let n = min + gen_int (max - min + 1) list.map (list.range 1 n) fn _ { item () } } fn gen_int :: below: Int -> Int / { random.int below } fn pick :: xs: List -> a / { match list.drop xs (random.int (list.length xs)) { [x, ..] -> x, [] -> panic "bench: nothing to pick" } } fn corpus :: -> List<(Int, String)> / { let vocab = vocabulary () let rare = rare_terms () let texts = list.map (list.range 1 (notes_count ())) fn _ { note vocab rare } list.zip (list.range 0 (notes_count () - 1)) texts } // ---- the queries --------------------------------------------------------------- /// `query_count()` common words, two-word phrases, rare terms and short /// (1–2 character) queries. fn queries :: vocab: List rare: List -> List / { let words = list.map (list.range 1 (query_count ())) fn _ { pick vocab } let phrases = list.map (list.range 1 (query_count ())) fn _ { pick vocab <> " " <> pick vocab } let rares = list.map (list.range 1 (query_count ())) fn _ { pick rare } let shorts = list.map (list.range 1 (query_count ())) fn _ { let w = pick vocab string.slice w 0 (1 + gen_int 2) } use qs <- list.flat_map [words, phrases, rares, shorts] qs } // ---- running ---------------------------------------------------------------------- // The wall clock, not the (virtual, deterministic) monotonic one // (ADR 0004, Revision 3): a bench wants real time. fn now_ns :: -> Int / { time.unix_ns (time.now ()) } fn ms :: start: Int -> Int / { (now_ns () - start) / 1000000 } fn total_chars :: notes: List<(Int, String)> -> Int { list.fold notes 0 fn acc n { acc + string.length n.1 } } /// The number of hits of every query, summed. fn run_queries :: idx: search.Index queries: List acc: Int -> Int { match queries { [] -> acc , [q, ..rest] -> run_queries idx rest (acc + list.length (search.search idx q)) } } fn run_fm :: c: fm.Corpus patterns: List acc: Int -> Int { match patterns { [] -> acc, [p, ..rest] -> run_fm c rest (acc + fm.corpus_count c p) } } fn bench :: -> () / { let notes = corpus () let t0 = now_ns () let idx = search.build notes let build_ms = ms t0 let vocab = vocabulary () let qs = queries vocab (rare_terms ()) let t1 = now_ns () let hits = run_queries idx qs 0 let query_ms = ms t1 io.println "trigram: built {notes_count ()} notes ({total_chars notes} chars) in {build_ms} ms" io.println "trigram: {list.length qs} queries in {query_ms} ms, {hits} hits" // The FM-index of the first notes. let texts = list.map (list.take notes (fm_notes ())) fn n { n.1 } let t2 = now_ns () let c = fm.build_corpus texts let fm_build_ms = ms t2 let t3 = now_ns () let fm_hits = run_fm c qs 0 let fm_query_ms = ms t3 io.println "fm: built {fm_notes ()} notes ({total_chars (list.take notes (fm_notes ()))} chars) in {fm_build_ms} ms" io.println "fm: {list.length qs} count queries in {fm_query_ms} ms, {fm_hits} hits" } pub fn main :: -> () / { random.with_seed 42 bench }