| 1 |
use lopdf::{dictionary, Document, Object, ObjectStream, SaveOptions}; |
| 2 |
use std::time::Instant; |
| 3 |
|
| 4 |
#[test] |
| 5 |
fn test_can_be_compressed_performance() { |
| 6 |
// Create a document with many objects |
| 7 |
let mut doc = Document::with_version("1.5"); |
| 8 |
let mut object_ids = Vec::new(); |
| 9 |
|
| 10 |
// Create 1000 objects |
| 11 |
for i in 0..1000 { |
| 12 |
let obj_id = doc.add_object(dictionary! { |
| 13 |
"Type" => format!("TestObject{}", i), |
| 14 |
"Index" => i as i64, |
| 15 |
"Data" => format!("This is test object number {}", i) |
| 16 |
}); |
| 17 |
object_ids.push(obj_id); |
| 18 |
} |
| 19 |
|
| 20 |
// Add some to trailer |
| 21 |
doc.trailer.set("Root", object_ids[0]); |
| 22 |
doc.trailer.set("Info", object_ids[1]); |
| 23 |
doc.trailer.set("Custom1", object_ids[2]); |
| 24 |
doc.trailer.set("Custom2", object_ids[3]); |
| 25 |
|
| 26 |
// Measure performance of can_be_compressed checks |
| 27 |
let start = Instant::now(); |
| 28 |
let mut compressible_count = 0; |
| 29 |
|
| 30 |
for &id in &object_ids { |
| 31 |
if let Some(obj) = doc.objects.get(&id) { |
| 32 |
if ObjectStream::can_be_compressed(id, obj, &doc) { |
| 33 |
compressible_count += 1; |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
let duration = start.elapsed(); |
| 39 |
|
| 40 |
println!("Checked {} objects in {:?}", object_ids.len(), duration); |
| 41 |
println!("Compressible objects: {}", compressible_count); |
| 42 |
|
| 43 |
// All objects should be compressible (none are encryption dicts) |
| 44 |
assert_eq!(compressible_count, object_ids.len(), |
| 45 |
"All non-encryption objects should be compressible"); |
| 46 |
|
| 47 |
// Performance check: should complete in reasonable time |
| 48 |
assert!(duration.as_millis() < 100, |
| 49 |
"Performance check took too long: {:?}", duration); |
| 50 |
} |
| 51 |
|
| 52 |
#[test] |
| 53 |
fn test_save_performance_with_trailer_objects() { |
| 54 |
let mut doc = Document::with_version("1.5"); |
| 55 |
let pages_id = doc.new_object_id(); |
| 56 |
|
| 57 |
// Create many pages |
| 58 |
let mut page_ids = Vec::new(); |
| 59 |
for i in 0..100 { |
| 60 |
let page_id = doc.add_object(dictionary! { |
| 61 |
"Type" => "Page", |
| 62 |
"Parent" => pages_id, |
| 63 |
"MediaBox" => vec![0.into(), 0.into(), 612.into(), 792.into()], |
| 64 |
"Contents" => Object::Reference((1000 + i, 0)) |
| 65 |
}); |
| 66 |
page_ids.push(page_id); |
| 67 |
} |
| 68 |
|
| 69 |
doc.objects.insert(pages_id, Object::Dictionary(dictionary! { |
| 70 |
"Type" => "Pages", |
| 71 |
"Kids" => page_ids.iter().map(|&id| Object::Reference(id)).collect::<Vec<_>>(), |
| 72 |
"Count" => page_ids.len() as i64 |
| 73 |
})); |
| 74 |
|
| 75 |
let catalog_id = doc.add_object(dictionary! { |
| 76 |
"Type" => "Catalog", |
| 77 |
"Pages" => pages_id |
| 78 |
}); |
| 79 |
|
| 80 |
let info_id = doc.add_object(dictionary! { |
| 81 |
"Title" => "Performance Test PDF", |
| 82 |
"PageCount" => page_ids.len() as i64 |
| 83 |
}); |
| 84 |
|
| 85 |
// Add many custom entries to trailer |
| 86 |
doc.trailer.set("Root", catalog_id); |
| 87 |
doc.trailer.set("Info", info_id); |
| 88 |
for i in 0..10 { |
| 89 |
doc.trailer.set(format!("Custom{}", i).as_bytes(), Object::Integer(i)); |
| 90 |
} |
| 91 |
|
| 92 |
// Measure save performance |
| 93 |
let options = SaveOptions::builder() |
| 94 |
.use_object_streams(true) |
| 95 |
.build(); |
| 96 |
|
| 97 |
let start = Instant::now(); |
| 98 |
let mut output = Vec::new(); |
| 99 |
doc.save_with_options(&mut output, options).unwrap(); |
| 100 |
let save_duration = start.elapsed(); |
| 101 |
|
| 102 |
println!("Saved {} page PDF in {:?}", page_ids.len(), save_duration); |
| 103 |
println!("Output size: {} bytes", output.len()); |
| 104 |
|
| 105 |
// Should complete quickly even with many objects |
| 106 |
assert!(save_duration.as_millis() < 500, |
| 107 |
"Save took too long: {:?}", save_duration); |
| 108 |
|
| 109 |
// Verify compression worked |
| 110 |
let content = String::from_utf8_lossy(&output); |
| 111 |
assert!(content.contains("/ObjStm"), "Object streams should be created"); |
| 112 |
} |
| 113 |
|
| 114 |
#[test] |
| 115 |
fn test_encryption_check_performance() { |
| 116 |
// Test the specific encryption dictionary check performance |
| 117 |
let mut doc = Document::with_version("1.5"); |
| 118 |
|
| 119 |
// Create an encryption dictionary |
| 120 |
let encrypt_id = doc.add_object(dictionary! { |
| 121 |
"Filter" => "Standard", |
| 122 |
"V" => 2 |
| 123 |
}); |
| 124 |
|
| 125 |
// Set it in trailer |
| 126 |
doc.trailer.set("Encrypt", encrypt_id); |
| 127 |
|
| 128 |
// Create many other objects |
| 129 |
let mut other_ids = Vec::new(); |
| 130 |
for i in 0..1000 { |
| 131 |
let id = doc.add_object(Object::Integer(i)); |
| 132 |
other_ids.push(id); |
| 133 |
} |
| 134 |
|
| 135 |
// Time the encryption check |
| 136 |
let start = Instant::now(); |
| 137 |
|
| 138 |
// Check encryption dictionary |
| 139 |
let encrypt_obj = doc.objects.get(&encrypt_id).unwrap(); |
| 140 |
let encrypt_compressible = ObjectStream::can_be_compressed(encrypt_id, encrypt_obj, &doc); |
| 141 |
|
| 142 |
// Check many non-encryption objects |
| 143 |
for &id in &other_ids[..100] { // Check first 100 |
| 144 |
if let Some(obj) = doc.objects.get(&id) { |
| 145 |
let _ = ObjectStream::can_be_compressed(id, obj, &doc); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
let duration = start.elapsed(); |
| 150 |
|
| 151 |
println!("Encryption check performance: {:?} for 101 objects", duration); |
| 152 |
|
| 153 |
assert!(!encrypt_compressible, "Encryption dict should not be compressible"); |
| 154 |
assert!(duration.as_micros() < 1000, "Check should be very fast"); |
| 155 |
} |