Skip to content

Commit 06fe065

Browse files
committed
use brotli to compress self profile response
1 parent 191132b commit 06fe065

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

site/src/request_handlers/self_profile.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::io::Read;
33
use std::sync::Arc;
44
use std::time::Instant;
55

6+
use brotli::enc::BrotliEncoderParams;
67
use bytes::Buf;
78
use database::selector;
89
use database::ArtifactId;
@@ -14,11 +15,14 @@ use crate::api::self_profile::ArtifactSizeDelta;
1415
use crate::api::{self_profile, self_profile_processed, self_profile_raw, ServerResult};
1516
use crate::load::SiteCtxt;
1617
use crate::self_profile::{get_or_download_self_profile, get_self_profile_raw_data};
18+
use crate::server::maybe_compressed_response;
1719
use crate::server::{Response, ResponseHeaders};
1820

1921
pub async fn handle_self_profile_processed_download(
2022
body: self_profile_processed::Request,
2123
ctxt: &SiteCtxt,
24+
// compression: &Option<BrotliEncoderParams>,
25+
allow_compression: bool,
2226
) -> http::Response<hyper::Body> {
2327
log::info!("handle_self_profile_processed_download({:?})", body);
2428
let mut params = body.params.clone();
@@ -148,7 +152,19 @@ pub async fn handle_self_profile_processed_download(
148152
hyper::header::HeaderValue::from_static("https://profiler.firefox.com"),
149153
);
150154

151-
builder.body(hyper::Body::from(output.data)).unwrap()
155+
if output.filename.starts_with("json") && allow_compression {
156+
// maybe_compressed_response(builder, output.data, compression)
157+
maybe_compressed_response(
158+
builder,
159+
output.data,
160+
&Some(BrotliEncoderParams {
161+
quality: 5,
162+
..Default::default()
163+
}),
164+
)
165+
} else {
166+
builder.body(hyper::Body::from(output.data)).unwrap()
167+
}
152168
}
153169

154170
// Add query data entries to `profile` for any queries in `base_profile` which are not present in

site/src/server.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,10 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
342342
let allow_compression = req
343343
.headers()
344344
.get(hyper::header::ACCEPT_ENCODING)
345-
.map_or(false, |e| e.to_str().unwrap().contains("br"));
345+
.and_then(|e| e.to_str().ok())
346+
.map_or(false, |s| {
347+
s.split(',').any(|part| part.trim().starts_with("br"))
348+
});
346349

347350
let compression = if allow_compression {
348351
// In tests on /perf/graphs and /perf/get, quality = 2 reduces size by 20-40% compared to 0,
@@ -441,7 +444,12 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
441444
"/perf/processed-self-profile" => {
442445
let ctxt: Arc<SiteCtxt> = server.ctxt.read().as_ref().unwrap().clone();
443446
let req = check!(parse_query_string(req.uri()));
444-
return Ok(request_handlers::handle_self_profile_processed_download(req, &ctxt).await);
447+
return Ok(request_handlers::handle_self_profile_processed_download(
448+
req,
449+
&ctxt,
450+
allow_compression,
451+
)
452+
.await);
445453
}
446454
_ if req.method() == http::Method::GET => return Ok(not_found()),
447455
_ => {}
@@ -730,7 +738,7 @@ where
730738
}
731739
}
732740

733-
fn maybe_compressed_response(
741+
pub fn maybe_compressed_response(
734742
response: http::response::Builder,
735743
body: Vec<u8>,
736744
compression: &Option<BrotliEncoderParams>,

0 commit comments

Comments
 (0)