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.

90 lignes · 2215 octetsBrut · Historique
1// Text forms per call (issue gabla108/louise#303): what an app pays to read an id from a
2// URL, a form or a cookie, and to write one back. Not a test: build it
3// with `louise build --release` in this directory and run `build/uuid_bench`.
4// Each line is the time per call; the number in brackets adds up what the
5// calls gave back, so the work cannot be skipped.
6
7import std/bytes
8import std/io
9import std/list
10import std/result
11import std/time.{type clock}
12import uuid
13
14// Calls per measurement.
15fn rounds :: -> Int
16{
17 20000
18}
19
20fn per_call :: label: String work: fn(Int) -> Int -> () / <io, clock>
21{
22 let start = time.now ()
23 let total = list.fold (list.range 1 (rounds ())) 0 fn acc i { acc + work i }
24 let ns = time.in_nanoseconds (time.since (time.now ()) start) / rounds ()
25 io.println "{label}: {ns} ns per call ({total})"
26}
27
28/// 1 when `text` decodes with `a` and encodes back to itself.
29fn round_trip :: text: String a: uuid.Alphabet -> Int
30{
31 uuid.decode text a
32 |> result.map
33 fn id
34 {
35 if uuid.encode id a == text
36 {
37 1
38 }
39 else
40 {
41 0
42 }
43 }
44 |> result.unwrap_or 0
45}
46
47pub fn main :: -> () / <io, clock>
48{
49 // A fixed id: the measurements do not depend on the clock.
50 let id =
51 uuid.v7_from
52 1700000000000
53 (bytes.from_list (list.map (list.range 1 10) fn i { i * 23 }))
54 let text = uuid.encode id (uuid.crockford ())
55 per_call
56 "decode + encode, crockford()"
57 fn _ { round_trip text (uuid.crockford ()) }
58 per_call "crockford() alone" fn _ { uuid.width (uuid.crockford ()) }
59 let crockford = uuid.crockford ()
60 per_call
61 "decode, alphabet kept"
62 fn _
63 {
64 uuid.decode text crockford |> result.map fn _ { 1 } |> result.unwrap_or 0
65 }
66 per_call
67 "encode, alphabet kept"
68 fn _
69 {
70 if uuid.encode id crockford == text
71 {
72 1
73 }
74 else
75 {
76 0
77 }
78 }
79 per_call
80 "to_rfc + from_rfc"
81 fn _
82 {
83 match uuid.from_rfc (uuid.to_rfc id)
84 { Some(back) if back == id -> 1, _ -> 0 }
85 }
86 let base62 = uuid.base62 ()
87 per_call
88 "decode + encode, base62"
89 fn _ { round_trip (uuid.encode id base62) base62 }
90}