Dever v0.0.2
This commit is contained in:
commit
e6e175ebcf
816 files changed
+120349
No files matched your search
@@ -0,0 +1,157 @@
|
||||
//! 同一 SQLite 版本上的真实 COUNT 成本;只操作已结束负载的临时一致副本。
|
||||
use deadpool_sqlite::rusqlite::{Connection, OpenFlags};
|
||||
use std::{fs::OpenOptions, io::Write, path::PathBuf, time::Instant};
|
||||
|
||||
#[test]
|
||||
#[ignore = "requires DEVER_TEST_COUNT_DATABASE and a new DEVER_TEST_COUNT_OUTPUT"]
|
||||
fn large_model_count_index_cost() {
|
||||
let source_path = PathBuf::from(std::env::var_os("DEVER_TEST_COUNT_DATABASE").unwrap());
|
||||
let output = std::env::var_os("DEVER_TEST_COUNT_OUTPUT").unwrap();
|
||||
let source =
|
||||
Connection::open_with_flags(&source_path, OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap();
|
||||
let snapshot = tempfile::NamedTempFile::new().unwrap();
|
||||
source
|
||||
.execute("VACUUM INTO ?1", [snapshot.path().to_str().unwrap()])
|
||||
.unwrap();
|
||||
let connection = Connection::open(snapshot.path()).unwrap();
|
||||
let rows: i64 = source
|
||||
.query_row("SELECT COUNT(*) FROM demo_service", [], |row| row.get(0))
|
||||
.unwrap();
|
||||
let version: String = connection
|
||||
.query_row("SELECT sqlite_version()", [], |row| row.get(0))
|
||||
.unwrap();
|
||||
let mut samples = serde_json::Map::new();
|
||||
for (name, indexed) in [("table", false), ("narrow_index", true)] {
|
||||
if indexed {
|
||||
connection
|
||||
.execute_batch("CREATE INDEX count_probe_id ON demo_service(id)")
|
||||
.unwrap();
|
||||
}
|
||||
let plan = connection
|
||||
.prepare("EXPLAIN QUERY PLAN SELECT COUNT(*) FROM demo_service")
|
||||
.unwrap()
|
||||
.query_map([], |row| row.get::<_, String>(3))
|
||||
.unwrap()
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.unwrap();
|
||||
let mut query = connection
|
||||
.prepare_cached("SELECT COUNT(*) FROM demo_service")
|
||||
.unwrap();
|
||||
let mut seconds = Vec::new();
|
||||
for repeat in 0..8 {
|
||||
let begin = Instant::now();
|
||||
for _ in 0..256 {
|
||||
assert_eq!(
|
||||
query.query_row([], |row| row.get::<_, i64>(0)).unwrap(),
|
||||
rows
|
||||
);
|
||||
}
|
||||
if repeat != 0 {
|
||||
seconds.push(begin.elapsed().as_secs_f64());
|
||||
}
|
||||
}
|
||||
samples.insert(
|
||||
name.into(),
|
||||
serde_json::json!({"plan":plan,"seconds":seconds}),
|
||||
);
|
||||
}
|
||||
drop(connection);
|
||||
snapshot.close().unwrap();
|
||||
let report = serde_json::json!({"sqlite_version":version,"source":source_path,"rows":rows,
|
||||
"contract":"Native SQLite query only, using the application dependency version. One warmup and seven rounds of 256 exact counts on a VACUUM INTO snapshot; original opened read-only. Not an async-resource or four-way performance gate.",
|
||||
"samples":samples,"source_read_only":true,"temporary_copy_removed":true});
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(output)
|
||||
.unwrap();
|
||||
serde_json::to_writer_pretty(&mut file, &report).unwrap();
|
||||
file.write_all(b"\n").unwrap();
|
||||
println!("{report}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[ignore = "requires DEVER_TEST_CACHE_DIRECTORY, DEVER_TEST_CACHE_KIB and a new DEVER_TEST_CACHE_OUTPUT"]
|
||||
fn large_model_native_cache_memory() {
|
||||
let directory = PathBuf::from(std::env::var_os("DEVER_TEST_CACHE_DIRECTORY").unwrap());
|
||||
let output = std::env::var_os("DEVER_TEST_CACHE_OUTPUT").unwrap();
|
||||
let kib: i64 = std::env::var("DEVER_TEST_CACHE_KIB")
|
||||
.unwrap()
|
||||
.parse()
|
||||
.unwrap();
|
||||
assert!(kib > 0);
|
||||
let rss = || {
|
||||
std::fs::read_to_string("/proc/self/status")
|
||||
.unwrap()
|
||||
.lines()
|
||||
.find(|line| line.starts_with("VmRSS:"))
|
||||
.unwrap()
|
||||
.split_whitespace()
|
||||
.nth(1)
|
||||
.unwrap()
|
||||
.parse::<u64>()
|
||||
.unwrap()
|
||||
};
|
||||
let initial = rss();
|
||||
let mut paths: Vec<_> = std::fs::read_dir(&directory)
|
||||
.unwrap()
|
||||
.map(|entry| entry.unwrap().path())
|
||||
.filter(|path| {
|
||||
path.extension()
|
||||
.is_some_and(|extension| extension == "sqlite")
|
||||
})
|
||||
.collect();
|
||||
paths.sort();
|
||||
assert!(!paths.is_empty());
|
||||
let mut connections = Vec::new();
|
||||
for path in &paths {
|
||||
for _ in 0..2 {
|
||||
let connection =
|
||||
Connection::open_with_flags(path, OpenFlags::SQLITE_OPEN_READ_ONLY).unwrap();
|
||||
// 只调本连接的原生缓存;数据库文件以只读方式打开,不改原始结果。
|
||||
connection.pragma_update(None, "cache_size", -kib).unwrap();
|
||||
let actual: i64 = connection
|
||||
.pragma_query_value(None, "cache_size", |row| row.get(0))
|
||||
.unwrap();
|
||||
assert_eq!(actual, -kib);
|
||||
connections.push(connection);
|
||||
}
|
||||
}
|
||||
let version: String = connections[0]
|
||||
.query_row("SELECT sqlite_version()", [], |row| row.get(0))
|
||||
.unwrap();
|
||||
let mut samples = Vec::new();
|
||||
for repeat in 0..8 {
|
||||
let begin = Instant::now();
|
||||
for connection in &connections {
|
||||
for _ in 0..4 {
|
||||
let rows: i64 = connection
|
||||
.query_row("SELECT COUNT(*) FROM demo_service", [], |row| row.get(0))
|
||||
.unwrap();
|
||||
let jobs: i64 = connection
|
||||
.query_row("SELECT COUNT(*) FROM _dever_job", [], |row| row.get(0))
|
||||
.unwrap();
|
||||
assert!(rows > 0 && jobs > 0);
|
||||
}
|
||||
}
|
||||
let seconds = begin.elapsed().as_secs_f64();
|
||||
if repeat != 0 {
|
||||
samples.push(serde_json::json!({"seconds":seconds,"rss_kib":rss()}));
|
||||
}
|
||||
}
|
||||
let populated = rss();
|
||||
for connection in &connections {
|
||||
connection.execute_batch("PRAGMA shrink_memory").unwrap();
|
||||
}
|
||||
let released = rss();
|
||||
drop(connections);
|
||||
let report = serde_json::json!({"sqlite_version":version,"directory":directory,"connections":paths.len()*2,"cache_kib_per_connection":kib,"initial_rss_kib":initial,"populated_rss_kib":populated,"after_native_shrink_rss_kib":released,"after_close_rss_kib":rss(),"samples":samples,"source_read_only":true,"contract":"Native SQLite on the stopped real workload files; two read-only connections per physical tenant, four exact model and queue counts each, one warmup and seven rounds. Each cache size runs in a separate test process. Native shrink_memory diagnoses retained page-cache memory; no application, async-resource or four-way performance claim."});
|
||||
let mut file = OpenOptions::new()
|
||||
.write(true)
|
||||
.create_new(true)
|
||||
.open(output)
|
||||
.unwrap();
|
||||
serde_json::to_writer_pretty(&mut file, &report).unwrap();
|
||||
file.write_all(b"\n").unwrap();
|
||||
println!("{report}");
|
||||
}
|
||||
Reference in new issue
Block a user