oxedyne/fe2o3/fe2o3_austenite/tests/eval_stream.rs
3.1 KiB, 1 run
created by r1870400018:63638, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Streaming pagination holds one page at a time (U6b-S1, addendum section 1). |
| 2 | //! |
| 3 | //! A document of more than `PAGES` pages, paragraphs and blocks, is paginated through a counting sink: each |
| 4 | //! page's body is placed and dropped before the next is asked for. The process's resident memory is read at |
| 5 | //! page `AT` and at page `PAGES`, and the growth per page between them must stay under `BOUND`, set from the |
| 6 | //! measured slope of the correct pipeline. A pipeline that kept its pages (a `Vec` of bodies, frames or placed pages) grows by a |
| 7 | //! page's size per page and fails the bound. It is the only test in this target, so nothing else moves the |
| 8 | //! process's memory while it reads it. |
| 9 | |
| 10 | use oxedyne_fe2o3_austenite::driver::Nowhere; |
| 11 | use oxedyne_fe2o3_austenite::eval::styles::StyleChain; |
| 12 | use oxedyne_fe2o3_austenite::eval::{ |
| 13 | eval_source, |
| 14 | Engine, |
| 15 | World, |
| 16 | }; |
| 17 | use oxedyne_fe2o3_austenite::flow; |
| 18 | use oxedyne_fe2o3_austenite::page::Page; |
| 19 | use oxedyne_fe2o3_sys::proc_self::ProcSelf; |
| 20 | |
| 21 | use oxedyne_fe2o3_core::prelude::*; |
| 22 | |
| 23 | use std::path::Path; |
| 24 | |
| 25 | const PAGES: u32 = 1000; |
| 26 | const AT: u32 = 100; |
| 27 | const BOUND: f64 = 4096.0; // bytes a page may add between page AT and the last |
| 28 | |
| 29 | /// A document longer than `PAGES` pages: a paragraph of text, then a filled and stroked block, over and over, |
| 30 | /// so that pages fill with lines, split blocks and spacing alike. |
| 31 | fn source() -> String { |
| 32 | fmt!( |
| 33 | "#set page(width: 200pt, height: 200pt, margin: 20pt)\n\ |
| 34 | #set text(size: 10pt)\n\ |
| 35 | #for i in range({}) {{ [#lorem(40)#parbreak()]; block(width: 100%, height: 16pt, fill: luma(235), stroke: 0.5pt) }}\n", |
| 36 | PAGES * 2) |
| 37 | } |
| 38 | |
| 39 | /// Takes each page and drops it, as the pipeline's counting sink does. |
| 40 | struct CountSink { |
| 41 | pages: u32, |
| 42 | } |
| 43 | |
| 44 | impl CountSink { |
| 45 | fn page(&mut self, page: Page) { |
| 46 | self.pages += 1; |
| 47 | drop(page); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | fn resident() -> Outcome<f64> { Ok(res!(ProcSelf::sample()).rss_bytes as f64) } |
| 52 | |
| 53 | #[test] |
| 54 | fn pagination_holds_one_page_at_a_time() -> Outcome<()> { |
| 55 | let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join("eval_stream"); |
| 56 | res!(std::fs::create_dir_all(&dir).map_err(|e| err!(e, "Could not make {}.", dir.display(); IO, File))); |
| 57 | let file = dir.join("long.typ"); |
| 58 | res!(std::fs::write(&file, source()).map_err(|e| err!(e, "Could not write {}.", file.display(); IO, File))); |
| 59 | let mut world = World::new(dir.clone()); |
| 60 | let id = res!(world.load(&file)); |
| 61 | let mut engine = Engine::new(world); |
| 62 | let module = res!(eval_source(&mut engine, id)); |
| 63 | let mut paginator = flow::paginate(&module.content, &StyleChain::root()); |
| 64 | let mut sink = CountSink { pages: 0 }; |
| 65 | let (mut at, mut last) = (0.0, 0.0); |
| 66 | while let Some((page, _setup)) = res!(paginator.next_placed(&mut engine, &mut Nowhere)) { |
| 67 | sink.page(page); |
| 68 | if sink.pages == AT { |
| 69 | at = res!(resident()); |
| 70 | } |
| 71 | if sink.pages == PAGES { |
| 72 | last = res!(resident()); |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | assert_eq!(sink.pages, PAGES, "the document should lay out to at least {} pages", PAGES); |
| 77 | let slope = (last - at) / (PAGES - AT) as f64; |
| 78 | println!("resident {:.0} bytes at page {}, {:.0} at page {}: {:.1} bytes a page", at, AT, last, PAGES, slope); |
| 79 | assert!(slope < BOUND, "resident memory grew {:.1} bytes a page between pages {} and {}, over {}", |
| 80 | slope, AT, PAGES, BOUND); |
| 81 | Ok(()) |
| 82 | } |