Skip to content

Commit

Permalink
use brotli to compress self profile response
Browse files Browse the repository at this point in the history
  • Loading branch information
s7tya committed Aug 23, 2024
1 parent 191132b commit 5f02213
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
19 changes: 18 additions & 1 deletion site/src/request_handlers/self_profile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashSet;
use std::io::Read;
use std::io::Write;
use std::sync::Arc;
use std::time::Instant;

Expand All @@ -8,6 +9,8 @@ use database::selector;
use database::ArtifactId;
use database::{metric::Metric, CommitType};
use headers::{ContentType, Header};
use http::header::CONTENT_ENCODING;
use http::HeaderValue;
use hyper::StatusCode;

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

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

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

if output.filename.ends_with("json") && support_brotli {
builder = builder.header(CONTENT_ENCODING, HeaderValue::from_static("br"));

let mut encoder = brotli::CompressorWriter::new(Vec::new(), 4096, 5, 22);
if let Err(e) = encoder.write_all(&output.data) {
log::error!("Failed to compress json with brotli {:?}", e);
let mut resp = http::Response::new(format!("{:?}", e).into());
*resp.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
return resp;
};
output.data = encoder.into_inner();
}

if output.is_download {
builder.headers_mut().unwrap().insert(
hyper::header::CONTENT_DISPOSITION,
Expand Down
4 changes: 3 additions & 1 deletion site/src/self_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ pub fn generate(
ProcessorType::Crox => {
let opt = serde_json::from_str(&serde_json::to_string(&params).unwrap())
.context("crox opts")?;
let data = crox::generate(self_profile_data, opt).context("crox")?;

Ok(Output {
filename: "chrome_profiler.json",
data: crox::generate(self_profile_data, opt).context("crox")?,
data,
is_download: true,
})
}
Expand Down
14 changes: 12 additions & 2 deletions site/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::{fmt, str};

use futures::{future::FutureExt, stream::StreamExt};
use headers::{Authorization, CacheControl, ContentType, ETag, Header, HeaderMapExt, IfNoneMatch};
use http::header::CACHE_CONTROL;
use http::header::{CACHE_CONTROL, CONTENT_ENCODING};
use hyper::StatusCode;
use log::{debug, error, info};
use parking_lot::{Mutex, RwLock};
Expand Down Expand Up @@ -440,8 +440,18 @@ async fn serve_req(server: Server, req: Request) -> Result<Response, ServerError
}
"/perf/processed-self-profile" => {
let ctxt: Arc<SiteCtxt> = server.ctxt.read().as_ref().unwrap().clone();
let support_brotli = req
.headers()
.get(CONTENT_ENCODING)
.and_then(|v| v.to_str().ok())
.map_or(false, |s| s.split(',').any(|part| part.trim() == "br"));
let req = check!(parse_query_string(req.uri()));
return Ok(request_handlers::handle_self_profile_processed_download(req, &ctxt).await);
return Ok(request_handlers::handle_self_profile_processed_download(
req,
&ctxt,
support_brotli,
)
.await);
}
_ if req.method() == http::Method::GET => return Ok(not_found()),
_ => {}
Expand Down

0 comments on commit 5f02213

Please sign in to comment.