Files
core/test/performance/uuid_reference.py
T
2026-11-27 06:03:56 +00:00

88 lines
3.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""UUID 解析、规范输出与内容校验;Rust 使用同版本 uuid,Go 复用 encoding/hex。"""
def source(language):
if language == 'dever':
return '''rule requiredUuid(value: Uuid?) (out: Uuid)
| value | out |
| null | fail(Invalid) |
| other | value |
rule requiredText(value: Text?) (out: Text)
| value | out |
| null | fail(Invalid) |
| other | value |
rule requiredInt(value: Int?) (out: Int)
| value | out |
| null | fail(Invalid) |
| other | value |
roundtrip(value: Int) (out: Int) {
source = "550e8400-e29b-41d4-a716-" + int.toText(value + 100000000000)
parsed = requiredUuid(uuid.parse(source))
last = requiredText(text.at(uuid.toText(parsed), 35))
out = requiredInt(int.parse(last))
}
loop measure(n: Int) (out: Int)
| each value in range(0, n) | result |
| out | sum roundtrip(value) |
cmd bench = app.measure
'''
if language == 'rust':
return '''use uuid::Uuid;
fn parse(value: &str) -> Option<Uuid> {
if value.len() != 36 { return None; }
Uuid::try_parse(value).ok()
}
fn roundtrip(value: i64) -> i64 {
let mut source = String::from("550e8400-e29b-41d4-a716-");
source.push_str(&value.checked_add(100000000000).unwrap().to_string());
let parsed = parse(&source).unwrap();
let last = parsed.hyphenated().to_string().chars().nth(35).unwrap().to_string();
last.parse().unwrap()
}
fn main() {
let n: i64 = std::env::args().nth(1).unwrap().parse().unwrap();
let mut total = 0i64;
for value in 0..n { total = total.checked_add(roundtrip(value)).expect("sum overflow"); }
println!("{total}");
}
'''
return '''package main
import ("encoding/hex"; "fmt"; "os"; "strconv")
func parse(value string) [16]byte {
if len(value) != 36 || value[8] != '-' || value[13] != '-' || value[18] != '-' || value[23] != '-' { panic("UUID format") }
var raw [32]byte
copy(raw[:8], value[:8]); copy(raw[8:12], value[9:13]); copy(raw[12:16], value[14:18])
copy(raw[16:20], value[19:23]); copy(raw[20:], value[24:])
var uuid [16]byte
if _, err := hex.Decode(uuid[:], raw[:]); err != nil { panic(err) }
return uuid
}
func toText(value [16]byte) string {
var text [36]byte
hex.Encode(text[:8], value[:4]); hex.Encode(text[9:13], value[4:6]); hex.Encode(text[14:18], value[6:8])
hex.Encode(text[19:23], value[8:10]); hex.Encode(text[24:], value[10:])
text[8], text[13], text[18], text[23] = '-', '-', '-', '-'
return string(text[:])
}
func add(left, right int64) int64 {
sum := left + right
if (right > 0 && sum < left) || (right < 0 && sum > left) { panic("sum overflow") }
return sum
}
func roundtrip(value int64) int64 {
source := "550e8400-e29b-41d4-a716-" + strconv.FormatInt(add(value, 100000000000), 10)
text := toText(parse(source))
last := string([]rune(text)[35])
result, err := strconv.ParseInt(last, 10, 64)
if err != nil { panic(err) }
return result
}
func main() {
n, err := strconv.ParseInt(os.Args[1], 10, 64)
if err != nil { panic(err) }
total := int64(0)
for value := int64(0); value < n; value++ { total = add(total, roundtrip(value)) }
fmt.Println(total)
}
'''