Skip to content

Commit 54e6c58

Browse files
committed
Provide more debug info
1 parent 2cd3d45 commit 54e6c58

File tree

7 files changed

+35
-15
lines changed

7 files changed

+35
-15
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ harness = false
9494
default = ["embedded-domain-resolver", "full-regex-handling", "single-thread"]
9595
full-regex-handling = []
9696
single-thread = [] # disables `Send` and `Sync` on `Engine`.
97-
regex-debug-info = []
97+
debug-info = []
9898
css-validation = ["cssparser", "selectors"]
9999
content-blocking = []
100100
embedded-domain-resolver = ["addr"] # Requires setting an external domain resolver if disabled.

src/blocker.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -487,13 +487,13 @@ impl Blocker {
487487
regex_manager.set_discard_policy(new_discard_policy);
488488
}
489489

490-
#[cfg(feature = "regex-debug-info")]
490+
#[cfg(feature = "debug-info")]
491491
pub fn discard_regex(&self, regex_id: u64) {
492492
let mut regex_manager = self.borrow_regex_manager();
493493
regex_manager.discard_regex(regex_id);
494494
}
495495

496-
#[cfg(feature = "regex-debug-info")]
496+
#[cfg(feature = "debug-info")]
497497
pub fn get_regex_debug_info(&self) -> crate::regex_manager::RegexDebugInfo {
498498
let regex_manager = self.borrow_regex_manager();
499499
regex_manager.get_debug_info()

src/engine.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ pub struct Engine {
5858
filter_data_context: FilterDataContextRef,
5959
}
6060

61+
#[cfg(feature = "debug-info")]
62+
pub struct EngineDebugInfo {
63+
pub regex_debug_info: crate::regex_manager::RegexDebugInfo,
64+
65+
pub flatbuffer_size: usize,
66+
pub resources_total_length: usize,
67+
}
68+
6169
impl Default for Engine {
6270
fn default() -> Self {
6371
Self::from_filter_set(FilterSet::new(false), false)
@@ -241,14 +249,18 @@ impl Engine {
241249
self.blocker.set_regex_discard_policy(new_discard_policy);
242250
}
243251

244-
#[cfg(feature = "regex-debug-info")]
252+
#[cfg(feature = "debug-info")]
245253
pub fn discard_regex(&mut self, regex_id: u64) {
246254
self.blocker.discard_regex(regex_id);
247255
}
248256

249-
#[cfg(feature = "regex-debug-info")]
250-
pub fn get_regex_debug_info(&self) -> crate::regex_manager::RegexDebugInfo {
251-
self.blocker.get_regex_debug_info()
257+
#[cfg(feature = "debug-info")]
258+
pub fn get_debug_info(&self) -> EngineDebugInfo {
259+
EngineDebugInfo {
260+
regex_debug_info: self.blocker.get_regex_debug_info(),
261+
flatbuffer_size: self.filter_data_context.memory.data().len(),
262+
resources_total_length: self.resources.resources_total_length(),
263+
}
252264
}
253265

254266
/// Serializes the `Engine` into a binary format so that it can be quickly reloaded later.

src/regex_manager.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const DEFAULT_DISCARD_UNUSED_TIME: Duration = Duration::from_secs(180);
3939

4040
/// Reports [`RegexManager`] metrics that may be useful for creating an optimized
4141
/// [`RegexManagerDiscardPolicy`].
42-
#[cfg(feature = "regex-debug-info")]
42+
#[cfg(feature = "debug-info")]
4343
pub struct RegexDebugInfo {
4444
/// Information about each regex contained in the [`RegexManager`].
4545
pub regex_data: Vec<RegexDebugEntry>,
@@ -48,7 +48,7 @@ pub struct RegexDebugInfo {
4848
}
4949

5050
/// Describes metrics about a single regex from the [`RegexManager`].
51-
#[cfg(feature = "regex-debug-info")]
51+
#[cfg(feature = "debug-info")]
5252
pub struct RegexDebugEntry {
5353
/// Id for this particular regex, which is constant and unique for its lifetime.
5454
///
@@ -312,7 +312,7 @@ impl RegexManager {
312312
}
313313

314314
/// Discard one regex, identified by its id from a [`RegexDebugEntry`].
315-
#[cfg(feature = "regex-debug-info")]
315+
#[cfg(feature = "debug-info")]
316316
pub fn discard_regex(&mut self, regex_id: u64) {
317317
self.map
318318
.iter_mut()
@@ -322,7 +322,7 @@ impl RegexManager {
322322
});
323323
}
324324

325-
#[cfg(feature = "regex-debug-info")]
325+
#[cfg(feature = "debug-info")]
326326
pub(crate) fn get_debug_regex_data(&self) -> Vec<RegexDebugEntry> {
327327
use itertools::Itertools;
328328
self.map
@@ -336,13 +336,13 @@ impl RegexManager {
336336
.collect_vec()
337337
}
338338

339-
#[cfg(feature = "regex-debug-info")]
339+
#[cfg(feature = "debug-info")]
340340
pub(crate) fn get_compiled_regex_count(&self) -> usize {
341341
self.compiled_regex_count
342342
}
343343

344344
/// Collect metrics that may be useful for creating an optimized [`RegexManagerDiscardPolicy`].
345-
#[cfg(feature = "regex-debug-info")]
345+
#[cfg(feature = "debug-info")]
346346
pub fn get_debug_info(&self) -> RegexDebugInfo {
347347
RegexDebugInfo {
348348
regex_data: self.get_debug_regex_data(),

tests/unit/engine.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ mod tests {
218218
let mut engine = Engine::from_rules_parametrised(rules, Default::default(), false, true);
219219
let data = engine.serialize().to_vec();
220220

221+
#[cfg(feature = "debug-info")]
222+
{
223+
let debug_info = engine.get_debug_info();
224+
let expected_size = 8_527_344_f32;
225+
assert!(debug_info.flatbuffer_size >= (expected_size * 0.99) as usize);
226+
assert!(debug_info.flatbuffer_size <= (expected_size * 1.01) as usize);
227+
}
228+
221229
let expected_hash: u64 = if cfg!(feature = "css-validation") {
222230
2942520321544562177
223231
} else {

tests/unit/filters/network_matchers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ mod match_tests {
678678

679679
#[test]
680680
#[ignore] // Not going to handle lookaround regexes
681-
#[cfg(feature = "regex-debug-info")]
681+
#[cfg(feature = "debug-info")]
682682
fn check_lookaround_regex_handled() {
683683
{
684684
let filter = r#"/^https?:\/\/([0-9a-z\-]+\.)?(9anime|animeland|animenova|animeplus|animetoon|animewow|gamestorrent|goodanime|gogoanime|igg-games|kimcartoon|memecenter|readcomiconline|toonget|toonova|watchcartoononline)\.[a-z]{2,4}\/(?!([Ee]xternal|[Ii]mages|[Ss]cripts|[Uu]ploads|ac|ajax|assets|combined|content|cov|cover|(img\/bg)|(img\/icon)|inc|jwplayer|player|playlist-cat-rss|static|thumbs|wp-content|wp-includes)\/)(.*)/$image,other,script,~third-party,xmlhttprequest,domain=~animeland.hu"#;

tests/unit/regex_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[cfg(all(test, feature = "regex-debug-info"))]
1+
#[cfg(all(test, feature = "debug-info"))]
22
mod tests {
33
use super::super::*;
44

0 commit comments

Comments
 (0)