(refractor) timer and warm up query

This commit is contained in:
polv committed 2026-03-03 12:44:54 +07:00
1 parent 1b2ee0f691
commit d6f2b145c3
13 files changed
+294 -47

No files matched your search

+2 -5
View File
@@ -1,11 +1,10 @@
use std::{
fs::read_dir,
path::{Path, absolute},
time::Instant,
};
use anyhow::{Context, Ok};
use cjdic2_core::AppService;
use cjdic2_core::{AppService, Timer};
mod common;
use common::get_db_dir;
@@ -24,16 +23,14 @@ fn main() -> Result<(), anyhow::Error> {
let p = e.path();
if p.extension().and_then(|s| s.to_str()) == Some("zip") {
let start = Instant::now();
let _timer = Timer::new(format!("zip_file: {:?}", p));
println!("zip_file: {:?}", p);
println!(
"{:?}",
AppService::import_yomitan_zip_file(&mut writer, &p, "ja", |progress| {
println!("{:?}", progress);
})?,
);
println!("[{:.2?}]", start.elapsed());
} else {
println!("not zip_file: {:?}", p);
}
+6 -7
View File
@@ -1,7 +1,5 @@
use std::time::Instant;
use anyhow::Ok;
use cjdic2_core::AppService;
use cjdic2_core::{AppService, Timer};
mod common;
use common::get_db_dir;
@@ -9,10 +7,11 @@ use common::get_db_dir;
fn main() -> Result<(), anyhow::Error> {
let service = AppService::new(get_db_dir())?;
let start = Instant::now();
let mut writer = service.get_yomitan_writer()?;
AppService::remove_yomitan_dictionary(&mut writer, "JMnedict")?;
println!("[{:.2?}]", start.elapsed());
{
let _timer = Timer::new("remove one".to_string());
let mut writer = service.get_yomitan_writer()?;
AppService::remove_yomitan_dictionary(&mut writer, "JMnedict")?;
}
Ok(())
}
+7 -8
View File
@@ -1,7 +1,5 @@
use std::time::Instant;
use anyhow::Ok;
use cjdic2_core::AppService;
use cjdic2_core::{AppService, Timer};
mod common;
use common::get_db_dir;
@@ -9,11 +7,12 @@ use common::get_db_dir;
fn main() -> Result<(), anyhow::Error> {
let service = AppService::new(get_db_dir())?;
let start = Instant::now();
let rs = service.search_yomitan("擦る", "する", 10, 0)?;
println!("{:#?}", rs);
println!("n={}", rs.len());
println!("[{:.2?}]", start.elapsed());
{
let _timer = Timer::new("Search".to_string());
let rs = service.search_yomitan("擦る", "する", 10, 0)?;
println!("{:#?}", rs);
println!("n={}", rs.len());
}
Ok(())
}
+5 -8
View File
@@ -1,9 +1,5 @@
use std::time::Instant;
use anyhow::Ok;
use cjdic2_core::AppService;
use cjdic2_core::{AppService, Timer};
mod common;
use common::get_db_dir;
@@ -11,9 +7,10 @@ use common::get_db_dir;
fn main() -> Result<(), anyhow::Error> {
let service = AppService::new(get_db_dir())?;
let start = Instant::now();
service.get_yomitan_writer()?;
println!("[{:.2?}]", start.elapsed());
{
let _timer = Timer::new("update schema".to_string());
service.get_yomitan_writer()?;
}
Ok(())
}
+8
View File
@@ -38,6 +38,7 @@ impl Database {
PRAGMA jornal_mode = WAL;
PRAGMA synchronous = NORMAL;
PRAGMA foreign_keys = ON;
PRAGMA cache_size = -131072;
",
)?;
@@ -81,6 +82,13 @@ impl Database {
let uri = format!("file:{}?mode=ro&immutable=1", path);
conn.execute("ATTACH DATABASE ?1 AS yomitan", [uri])?;
conn.execute_batch(
r"
PRAGMA yomitan.mmap_size = 3000000000; -- 3_000_000_000
",
)?;
self.yomitan_attached.store(true, Ordering::Release);
}
+3 -1
View File
@@ -2,7 +2,7 @@ use rusqlite::{Result, params};
use serde::Serialize;
use crate::{
CJDicError,
CJDicError, Timer,
db::{Database, yomitan_writer::normalize_term},
};
@@ -35,6 +35,8 @@ impl YomitanDatabase {
limit: u32,
offset: u32,
) -> Result<Vec<YomitanRow>, CJDicError> {
let _timer = Timer::new(format!("search_yomitan: {} {}", q_term, q_reading));
let and_or = if q_term == q_reading { "OR" } else { "AND" };
let mut new_t = String::new();
+2 -9
View File
@@ -7,11 +7,10 @@ use std::{
collections::HashMap,
io::{Cursor, Read},
path::{Path, PathBuf},
time::Instant,
};
use zip::ZipArchive;
use crate::CJDicError;
use crate::{CJDicError, Timer};
fn blake3_hex(s: &str) -> String {
format!("{}", hash(s.as_bytes()))
@@ -274,8 +273,8 @@ impl YomitanWriter {
Callback: Fn(YomitanZipImportProgress),
{
let bundle_name = zip_file.file_name();
let _timer = Timer::new(format!("Importing {}", bundle_name));
let start_time = Instant::now();
self.conn.execute_batch("PRAGMA foreign_keys = off")?;
let cursor = Cursor::new(zip_file.bytes()?);
@@ -593,12 +592,6 @@ impl YomitanWriter {
tx.commit()?;
println!(
"Importing {} takes {:.2?}",
zip_file.file_name(),
start_time.elapsed()
);
self.conn.execute_batch(
"
PRAGMA foreign_keys = on;
+2
View File
@@ -1,10 +1,12 @@
mod db;
mod error;
mod service;
mod util;
pub use db::{YomitanRow, YomitanZipImportProgress, YomitanZipImportResult, ZipSource};
pub use error::CJDicError;
pub use service::AppService;
pub use util::Timer;
pub fn add(left: u64, right: u64) -> u64 {
left + right
+35
View File
@@ -0,0 +1,35 @@
#[cfg(debug_assertions)]
use std::time::Instant;
#[cfg(debug_assertions)]
pub struct Timer {
start: Instant,
label: String,
}
#[cfg(debug_assertions)]
impl Timer {
pub fn new(label: String) -> Self {
Self {
start: Instant::now(),
label,
}
}
}
#[cfg(debug_assertions)]
impl Drop for Timer {
fn drop(&mut self) {
println!("[{}] {:.2?}", self.label, self.start.elapsed())
}
}
#[cfg(not(debug_assertions))]
pub struct Timer;
#[cfg(not(debug_assertions))]
impl Timer {
pub fn new(_label: String) -> Self {
Self
}
}