Skip to content

Commit

Permalink
add timings to debug logs to track format conversion speed.
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillFish8 committed Jul 4, 2021
1 parent 88c5243 commit d08fd70
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use bytes::{BufMut, BytesMut};
use gotham::state::{FromState, State};
use gotham_derive::{StateData, StaticResponseExtender};
use hashbrown::HashMap;
use log::error;
use log::{error, debug};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
use webp::Encoder;
use std::sync::Arc;
use std::time::Instant;

use image::imageops;
use image::{load_from_memory_with_format, DynamicImage};
Expand Down Expand Up @@ -62,7 +63,9 @@ macro_rules! convert {
|| -> anyhow::Result<BytesMut> {
let buff = BytesMut::new();
let mut writer = buff.writer();
let start = Instant::now;
$e.write_to(&mut writer, $d)?;
debug!("format {:?} conversion took {:?}", $d, start.elapsed());
Ok(writer.into_inner())
}()
}};
Expand Down Expand Up @@ -147,7 +150,9 @@ async fn convert_image(img: Arc<DynamicImage>, cfg: StateConfig) -> Result<(Imag
if is_enabled!(ImageFormat::WebP, cfg.0.formats) {
let cloned = img.clone();
let handle = tokio::task::spawn_blocking(move || -> Result<(ImageFormat, BytesMut)> {
let start = Instant::now();
let raw = Encoder::from_image(cloned.as_ref()).encode();
debug!("format {:?} conversion took {:?}", image::ImageFormat::WebP, start.elapsed());
let webp = BytesMut::from(raw.as_ref());

Ok((ImageFormat::WebP, webp))
Expand Down
4 changes: 2 additions & 2 deletions tests/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def get_base_data(file: str) -> str:

def test_png_upload1():
global working_ids
data = get_base_data("./samples/news.png")
data = get_base_data("./samples/sunset.jpeg")
payload = {
"format": "png",
"format": "jpeg",
"data": data,
}
r = requests.post("http://127.0.0.1:7070/admin/create/image", json=payload)
Expand Down

0 comments on commit d08fd70

Please sign in to comment.