-
Notifications
You must be signed in to change notification settings - Fork 141
Setup basic perf CI #417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Setup basic perf CI #417
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e8cc677
Add new benchmarks, update the lists
atuchin-m 460aade
Add perf-ci.yml
atuchin-m ce8233d
Fix the tests
atuchin-m 4dbe150
Fix perf-ci.yml
atuchin-m 9808df1
add default-panic-hook
atuchin-m e3c1fa2
Fix tests for css-validation
atuchin-m 0060001
Remove installing Rust toolchain
atuchin-m 58fb5dc
use js for update lists
atuchin-m d16e389
Fix review issues
atuchin-m 927f23a
Add a memory bench
atuchin-m 5460d2b
Remove python for .yml
atuchin-m 430769d
use command line args in update-lists.js
atuchin-m baf562a
Update ref for github-action-benchmark
atuchin-m ee56a32
Revert "Update ref for github-action-benchmark"
thypon 1f141cb
try pinning
thypon 91490e8
update rule-match-first-request
atuchin-m 199d257
use mkdtempSync
atuchin-m File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# CI for performance benchmarking | ||
# Domains of interest: | ||
# * startup speed (== to parse and load all rules) | ||
# * network filter matching (== the avg time to check a request) | ||
# * first request matching delay (== time to check the first request) | ||
# * memory usage after loading rules and after a few requests | ||
name: Performance CI | ||
|
||
on: | ||
push: | ||
branches: [ master ] | ||
pull_request: | ||
|
||
permissions: | ||
contents: write | ||
pages: write | ||
pull-requests: write | ||
|
||
jobs: | ||
benchmark: | ||
name: Performance benchmarking | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 | ||
|
||
- name: Bench network filter matching | ||
run: cargo bench --bench bench_matching rule-match-browserlike/brave-list -- --output-format bencher | tee -a output.txt | ||
|
||
- name: Bench first request matching delay | ||
run: cargo bench --bench bench_matching rule-match-first-request -- --output-format bencher | tee -a output.txt | ||
|
||
- name: Bench startup speed | ||
run: cargo bench --bench bench_rules blocker_new/brave-list -- --output-format bencher | tee -a output.txt | ||
|
||
- name: Bench memory usage | ||
run: cargo bench --bench bench_memory -- --output-format bencher | tee -a output.txt | ||
|
||
- name: Store benchmark result | ||
uses: benchmark-action/github-action-benchmark@d48d326b4ca9ba73ca0cd0d59f108f9e02a381c7 # v1.20.4 | ||
with: | ||
name: Rust Benchmark | ||
tool: 'cargo' | ||
output-file-path: output.txt | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
alert-threshold: '130%' # fails on +30% regression | ||
comment-on-alert: true | ||
fail-on-alert: true | ||
comment-always: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* Copyright (c) 2025 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
use criterion::*; | ||
use std::alloc::{GlobalAlloc, Layout, System}; | ||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use adblock::Engine; | ||
use adblock::request::Request; | ||
|
||
#[path = "../tests/test_utils.rs"] | ||
mod test_utils; | ||
use test_utils::rules_from_lists; | ||
|
||
// Custom allocator to track memory usage | ||
#[global_allocator] | ||
static ALLOCATOR: MemoryTracker = MemoryTracker::new(); | ||
|
||
struct MemoryTracker { | ||
allocated: AtomicUsize, | ||
internal: System, | ||
} | ||
|
||
impl MemoryTracker { | ||
const fn new() -> Self { | ||
Self { | ||
allocated: AtomicUsize::new(0), | ||
internal: System, | ||
} | ||
} | ||
|
||
fn current_usage(&self) -> usize { | ||
self.allocated.load(Ordering::SeqCst) | ||
} | ||
|
||
fn reset(&self) { | ||
self.allocated.store(0, Ordering::SeqCst); | ||
} | ||
} | ||
|
||
unsafe impl GlobalAlloc for MemoryTracker { | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
let ret = self.internal.alloc(layout); | ||
if !ret.is_null() { | ||
self.allocated.fetch_add(layout.size(), Ordering::SeqCst); | ||
} | ||
ret | ||
} | ||
|
||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { | ||
self.internal.dealloc(ptr, layout); | ||
self.allocated.fetch_sub(layout.size(), Ordering::SeqCst); | ||
} | ||
|
||
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 { | ||
let ret = self.internal.realloc(ptr, layout, new_size); | ||
if !ret.is_null() { | ||
self.allocated.fetch_sub(layout.size(), Ordering::SeqCst); | ||
self.allocated.fetch_add(new_size, Ordering::SeqCst); | ||
} | ||
ret | ||
} | ||
|
||
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { | ||
let ret = self.internal.alloc_zeroed(layout); | ||
if !ret.is_null() { | ||
self.allocated.fetch_add(layout.size(), Ordering::SeqCst); | ||
} | ||
ret | ||
} | ||
} | ||
|
||
#[allow(non_snake_case)] | ||
#[derive(Serialize, Deserialize, Clone)] | ||
struct TestRequest { | ||
frameUrl: String, | ||
url: String, | ||
cpt: String, | ||
} | ||
|
||
impl From<&TestRequest> for Request { | ||
fn from(v: &TestRequest) -> Self { | ||
Request::new(&v.url, &v.frameUrl, &v.cpt).unwrap() | ||
} | ||
} | ||
|
||
fn load_requests() -> Vec<TestRequest> { | ||
let requests_str = rules_from_lists(&["data/requests.json"]); | ||
let reqs: Vec<TestRequest> = requests_str | ||
.into_iter() | ||
.map(|r| serde_json::from_str(&r)) | ||
.filter_map(Result::ok) | ||
.collect(); | ||
reqs | ||
} | ||
|
||
fn bench_memory_usage(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("memory-usage"); | ||
group.sample_size(10); | ||
group.measurement_time(std::time::Duration::from_secs(1)); | ||
|
||
let mut noise = 0; | ||
let all_requests = load_requests(); | ||
let first_1000_requests: Vec<_> = all_requests.iter().take(1000).collect(); | ||
|
||
group.bench_function("brave-list-initial", |b| { | ||
let mut result = 0; | ||
b.iter_custom(|iters| { | ||
for _ in 0..iters { | ||
ALLOCATOR.reset(); | ||
let rules = rules_from_lists(&["data/brave/brave-main-list.txt"]); | ||
let engine = Engine::from_rules(rules, Default::default()); | ||
|
||
noise += 1; // add some noise to make criterion happy | ||
result += ALLOCATOR.current_usage() + noise; | ||
|
||
// Prevent engine from being optimized | ||
criterion::black_box(&engine); | ||
} | ||
|
||
// Return the memory usage as a Duration | ||
std::time::Duration::from_nanos(result as u64) | ||
}); | ||
}); | ||
|
||
group.bench_function("brave-list-after-1000-requests", |b| { | ||
b.iter_custom(|iters| { | ||
let mut result = 0; | ||
for _ in 0..iters { | ||
ALLOCATOR.reset(); | ||
let rules = rules_from_lists(&["data/brave/brave-main-list.txt"]); | ||
let engine = Engine::from_rules(rules, Default::default()); | ||
|
||
for request in first_1000_requests.clone() { | ||
criterion::black_box(engine.check_network_request(&request.into())); | ||
} | ||
|
||
noise += 1; // add some noise to make criterion happy | ||
result += ALLOCATOR.current_usage() + noise; | ||
|
||
// Prevent engine from being optimized | ||
criterion::black_box(&engine); | ||
} | ||
|
||
// Return the memory usage as a Duration | ||
std::time::Duration::from_nanos(result as u64) | ||
}) | ||
}); | ||
|
||
group.finish(); | ||
} | ||
|
||
criterion_group!(benches, bench_memory_usage); | ||
criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.