102 lines
4.0 KiB
Rust
102 lines
4.0 KiB
Rust
//! UUID 列使用真实 SQLite 行;性能探针只比较列解码,不代表完整 Model 成本。
|
|
use super::*;
|
|
use crate::value::uuid::Uuid;
|
|
use rusqlite::{Connection, params, types::Value};
|
|
use std::{hint::black_box, time::Instant};
|
|
|
|
#[test]
|
|
fn uuid_columns_preserve_bytes_null_and_driver_errors() {
|
|
let connection = Connection::open_in_memory().unwrap();
|
|
let bytes = *uuid::Uuid::parse_str("018f040a-785e-7000-8000-000000000001")
|
|
.unwrap()
|
|
.as_bytes();
|
|
let expected = Uuid::from(uuid::Uuid::from_bytes(bytes));
|
|
let mut statement = connection.prepare("SELECT ?1").unwrap();
|
|
for value in [
|
|
Value::Blob(bytes.to_vec()),
|
|
Value::Null,
|
|
Value::Blob(vec![]),
|
|
Value::Blob(bytes[..15].to_vec()),
|
|
Value::Blob(vec![0; 17]),
|
|
Value::Text(expected.to_string()),
|
|
Value::Integer(1),
|
|
] {
|
|
let (required, optional) = statement
|
|
.query_row(params![value], |raw| {
|
|
let row = Row::Sqlite(raw);
|
|
Ok((row.get::<Uuid>(0), row.get::<Option<Uuid>>(0)))
|
|
})
|
|
.unwrap();
|
|
match value {
|
|
Value::Blob(ref value) if value.as_slice() == bytes => {
|
|
assert_eq!(required.unwrap(), expected);
|
|
assert_eq!(optional.unwrap(), Some(expected));
|
|
}
|
|
Value::Null => {
|
|
assert_eq!(required.unwrap_err().kind, super::super::Kind::InvalidData);
|
|
assert_eq!(optional.unwrap(), None);
|
|
}
|
|
_ => {
|
|
for error in [required.unwrap_err(), optional.unwrap_err()] {
|
|
assert_eq!(error.kind, super::super::Kind::InvalidData);
|
|
assert!(std::error::Error::source(&error).is_some());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
#[ignore = "bounded UUID column comparison; run explicitly with --release --nocapture"]
|
|
fn uuid_column_cost() {
|
|
let mut connection = Connection::open_in_memory().unwrap();
|
|
connection
|
|
.execute_batch("CREATE TABLE sample(id INTEGER PRIMARY KEY,value BLOB)")
|
|
.unwrap();
|
|
let transaction = connection.transaction().unwrap();
|
|
{
|
|
let mut insert = transaction
|
|
.prepare("INSERT INTO sample VALUES (?1,?2)")
|
|
.unwrap();
|
|
for index in 0..65536 {
|
|
let value = uuid::Uuid::from_u128(index as u128);
|
|
insert.execute(params![index, value.as_bytes()]).unwrap();
|
|
}
|
|
}
|
|
transaction.commit().unwrap();
|
|
let mut statement = connection
|
|
.prepare("SELECT value FROM sample WHERE id < ?1 ORDER BY id")
|
|
.unwrap();
|
|
let mut samples = Vec::new();
|
|
for size in [1, 32, 1024, 65536] {
|
|
for repeat in -1..7 {
|
|
for offset in 0..2 {
|
|
let native = (repeat + 1 + offset) % 2 == 0;
|
|
let start = Instant::now();
|
|
let mut rows = statement.query([size]).unwrap();
|
|
let mut values = Vec::with_capacity(size as usize);
|
|
while let Some(row) = rows.next().unwrap() {
|
|
let value = if native {
|
|
// 独立对照只调用驱动与 uuid,不经过 Dever 的 Row/ColumnValue。
|
|
uuid::Uuid::from_bytes(row.get::<_, [u8; 16]>(0).unwrap())
|
|
} else {
|
|
Row::Sqlite(row).get::<Uuid>(0).unwrap().into()
|
|
};
|
|
values.push(value);
|
|
}
|
|
let elapsed_ns = start.elapsed().as_nanos();
|
|
assert_eq!(values.len(), size as usize);
|
|
for (index, value) in values.iter().enumerate() {
|
|
assert_eq!(value.as_u128(), index as u128);
|
|
}
|
|
black_box(&values);
|
|
samples.push(serde_json::json!({
|
|
"implementation":if native {"native"} else {"dever"},
|
|
"size":size,"repeat":repeat,"elapsed_ns":elapsed_ns,"checked":true
|
|
}));
|
|
}
|
|
}
|
|
}
|
|
println!("UUID_COLUMN {}", serde_json::to_string(&samples).unwrap());
|
|
}
|