Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit a1a164b

Browse files
committed
Merge pull request #17 from eddyb/test-perf
Added a new test to test PNG loading performance and two large test PNGs.
2 parents 9fcb3a8 + 4533cdd commit a1a164b

File tree

5 files changed

+53
-6
lines changed

5 files changed

+53
-6
lines changed

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
SRCDIR="$(cd $(dirname $0) && pwd)"
44
sed "s#%VPATH%#${SRCDIR}#" ${SRCDIR}/Makefile.in > Makefile
55

6-
cp ${SRCDIR}/test.png test.png
6+
cp -ru ${SRCDIR}/test .

lib.rs

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
vers = "0.1")];
1212
#[crate_type = "lib"];
1313

14+
#[cfg(test)]
15+
extern mod extra;
16+
1417
extern mod std;
1518
use std::cast;
1619
use std::io;
@@ -25,6 +28,7 @@ pub mod ffi;
2528
#[link_args="-L. -lpng -lz -lshim"]
2629
extern {}
2730

31+
#[deriving(Eq)]
2832
pub enum ColorType {
2933
K1, K2, K4, K8, K16,
3034
KA8, KA16,
@@ -249,15 +253,19 @@ pub fn store_png(img: &Image, path: &Path) -> Result<(),~str> {
249253
250254
#[cfg(test)]
251255
mod test {
256+
use extra::test::{bench, fmt_bench_samples};
252257
use std::io;
253258
use std::io::File;
254259
use std::vec;
255-
use super::{ffi, load_png, store_png, RGB8, Image};
260+
261+
use super::{ffi, load_png, load_png_from_memory, store_png};
262+
use super::{ColorType, RGB8, RGBA8, KA8, Image};
256263
257264
#[test]
258265
#[fixed_stack_segment]
259266
fn test_valid_png() {
260-
let mut reader = match File::open_mode(&Path::new("test.png"), io::Open, io::Read) {
267+
let file = "test/servo-screenshot.png";
268+
let mut reader = match File::open_mode(&Path::new(file), io::Open, io::Read) {
261269
Some(r) => r,
262270
None => fail!("could not open file"),
263271
};
@@ -271,10 +279,49 @@ mod test {
271279
}
272280
}
273281
282+
fn load_rgba8(file: &'static str, w: u32, h: u32) {
283+
match load_png(&Path::new(file)) {
284+
Err(m) => fail!(m),
285+
Ok(image) => {
286+
assert_eq!(image.color_type, RGBA8);
287+
assert_eq!(image.width, w);
288+
assert_eq!(image.height, h);
289+
}
290+
}
291+
}
292+
274293
#[test]
275294
fn test_load() {
276-
let res = load_png(&Path::new("test.png"));
277-
assert!(res.is_ok());
295+
load_rgba8("test/servo-screenshot.png", 831, 624);
296+
297+
test_store();
298+
load_rgba8("test/store.png", 10, 10);
299+
}
300+
301+
fn bench_file_from_memory(file: &'static str, w: u32, h: u32, c: ColorType) {
302+
let mut reader = match File::open_mode(&Path::new(file), io::Open, io::Read) {
303+
Some(r) => r,
304+
None => fail!("could not open '{}'", file)
305+
};
306+
let buf = reader.read_to_end();
307+
let bs = bench::benchmark(|b| b.iter(|| {
308+
match load_png_from_memory(buf) {
309+
Err(m) => fail!(m),
310+
Ok(image) => {
311+
assert_eq!(image.color_type, c);
312+
assert_eq!(image.width, w);
313+
assert_eq!(image.height, h);
314+
}
315+
}
316+
}));
317+
println!("libpng load '{}': {}", file, fmt_bench_samples(&bs));
318+
}
319+
320+
#[test]
321+
fn test_load_perf() {
322+
bench_file_from_memory("test/servo-screenshot.png", 831, 624, RGBA8);
323+
bench_file_from_memory("test/mozilla-dinosaur-head-logo.png", 1300, 929, RGBA8);
324+
bench_file_from_memory("test/rust-huge-logo.png", 4000, 4000, KA8);
278325
}
279326
280327
#[test]
@@ -285,7 +332,7 @@ mod test {
285332
color_type: RGB8,
286333
pixels: vec::from_elem(10 * 10 * 3, 100u8),
287334
};
288-
let res = store_png(&img, &Path::new("test_store.png"));
335+
let res = store_png(&img, &Path::new("test/store.png"));
289336
assert!(res.is_ok());
290337
}
291338
}

test/mozilla-dinosaur-head-logo.png

131 KB
Loading

test/rust-huge-logo.png

147 KB
Loading
File renamed without changes.

0 commit comments

Comments
 (0)