// Text forms per call (issue gabla108/louise#303): what an app pays to read an id from a // URL, a form or a cookie, and to write one back. Not a test: build it // with `louise build --release` in this directory and run `build/uuid_bench`. // Each line is the time per call; the number in brackets adds up what the // calls gave back, so the work cannot be skipped. import std/bytes import std/io import std/list import std/result import std/time.{type clock} import uuid // Calls per measurement. fn rounds :: -> Int { 20000 } fn per_call :: label: String work: fn(Int) -> Int -> () / { let start = time.now () let total = list.fold (list.range 1 (rounds ())) 0 fn acc i { acc + work i } let ns = time.in_nanoseconds (time.since (time.now ()) start) / rounds () io.println "{label}: {ns} ns per call ({total})" } /// 1 when `text` decodes with `a` and encodes back to itself. fn round_trip :: text: String a: uuid.Alphabet -> Int { uuid.decode text a |> result.map fn id { if uuid.encode id a == text { 1 } else { 0 } } |> result.unwrap_or 0 } pub fn main :: -> () / { // A fixed id: the measurements do not depend on the clock. let id = uuid.v7_from 1700000000000 (bytes.from_list (list.map (list.range 1 10) fn i { i * 23 })) let text = uuid.encode id (uuid.crockford ()) per_call "decode + encode, crockford()" fn _ { round_trip text (uuid.crockford ()) } per_call "crockford() alone" fn _ { uuid.width (uuid.crockford ()) } let crockford = uuid.crockford () per_call "decode, alphabet kept" fn _ { uuid.decode text crockford |> result.map fn _ { 1 } |> result.unwrap_or 0 } per_call "encode, alphabet kept" fn _ { if uuid.encode id crockford == text { 1 } else { 0 } } per_call "to_rfc + from_rfc" fn _ { match uuid.from_rfc (uuid.to_rfc id) { Some(back) if back == id -> 1, _ -> 0 } } let base62 = uuid.base62 () per_call "decode + encode, base62" fn _ { round_trip (uuid.encode id base62) base62 } }