Skip to content

Commit 5f02213

Browse files
committed
use brotli to compress self profile response
1 parent 191132b commit 5f02213

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

site/src/request_handlers/self_profile.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashSet;
22
use std::io::Read;
3+
use std::io::Write;
34
use std::sync::Arc;
45
use std::time::Instant;
56

@@ -8,6 +9,8 @@ use database::selector;
89
use database::ArtifactId;
910
use database::{metric::Metric, CommitType};
1011
use headers::{ContentType, Header};
12+
use http::header::CONTENT_ENCODING;
13+
use http::HeaderValue;
1114
use hyper::StatusCode;
1215

1316
use crate::api::self_profile::ArtifactSizeDelta;
@@ -19,6 +22,7 @@ use crate::server::{Response, ResponseHeaders};
1922
pub async fn handle_self_profile_processed_download(
2023
body: self_profile_processed::Request,
2124
ctxt: &SiteCtxt,
25+
support_brotli: bool,
2226
) -> http::Response<hyper::Body> {
2327
log::info!("handle_self_profile_processed_download({:?})", body);
2428
let mut params = body.params.clone();
@@ -110,7 +114,7 @@ pub async fn handle_self_profile_processed_download(
110114

111115
log::trace!("got data in {:?}", start.elapsed());
112116

113-
let output =
117+
let mut output =
114118
match crate::self_profile::generate(&title, body.processor_type, base_data, data, params) {
115119
Ok(c) => c,
116120
Err(e) => {
@@ -132,6 +136,19 @@ pub async fn handle_self_profile_processed_download(
132136
})
133137
.status(StatusCode::OK);
134138

139+
if output.filename.ends_with("json") && support_brotli {
140+
builder = builder.header(CONTENT_ENCODING, HeaderValue::from_static("br"));
141+
142+
let mut encoder = brotli::CompressorWriter::new(Vec::new(), 4096, 5, 22);
143+
if let Err(e) = encoder.write_all(&output.data) {
144+
log::error!("Failed to compress json with brotli {:?}", e);
145+
let mut resp = http::Response::new(format!("{:?}", e).into());
146+
*resp.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
147+
return resp;
148+
};
149+
output.data = encoder.into_inner();
150+
}
151+
135152
if output.is_download {
136153
builder.headers_mut().unwrap().insert(
137154
hyper::header::CONTENT_DISPOSITION,

site/src/self_profile.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ pub fn generate(
3737
ProcessorType::Crox => {
3838
let opt = serde_json::from_str(&serde_json::to_string(&params).unwrap())
3939
.context("crox opts")?;
40+
let data = crox::generate(self_profile_data, opt).context("crox")?;
41+
4042
Ok(Output {
4143
filename: "chrome_profiler.json",
42-
data: crox::generate(self_profile_data, opt).context("crox")?,
44+
data,
4345
is_download: true,
4446
})
4547
}

site/src/server.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::{fmt, str};
1111

1212
use futures::{future::FutureExt, stream::StreamExt};
1313
use headers::{Authorization, CacheControl, ContentType, ETag, Header, HeaderMapExt, IfNoneMatch};
14-
use http::header::CACHE_CONTROL;
14+
use http::header::{CACHE_CONTROL, CONTENT_ENCODING};
1515
use hyper::StatusCode;
1616
use log::{debug, error, info};
1717
use parking_lot::{Mutex, RwLock};
@@ -440,8 +440,18 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
440440
}
441441
"/perf/processed-self-profile" => {
442442
let ctxt: Arc<SiteCtxt> = server.ctxt.read().as_ref().unwrap().clone();
443+
let support_brotli = req
444+
.headers()
445+
.get(CONTENT_ENCODING)
446+
.and_then(|v| v.to_str().ok())
447+
.map_or(false, |s| s.split(',').any(|part| part.trim() == "br"));
443448
let req = check!(parse_query_string(req.uri()));
444-
return Ok(request_handlers::handle_self_profile_processed_download(req, &ctxt).await);
449+
return Ok(request_handlers::handle_self_profile_processed_download(
450+
req,
451+
&ctxt,
452+
support_brotli,
453+
)
454+
.await);
445455
}
446456
_ if req.method() == http::Method::GET => return Ok(not_found()),
447457
_ => {}

0 commit comments

Comments
 (0)