|
| 1 | +package pdf |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func BenchmarkGetObject(b *testing.B) { |
| 10 | + // Use a test file that exists in the repo |
| 11 | + // internal/pdf is at /Users/paulvanbrouwershaven/Code/pdfsign/internal/pdf |
| 12 | + // testfiles are at /Users/paulvanbrouwershaven/Code/pdfsign/testfiles |
| 13 | + file := "../../testfiles/testfile12.pdf" |
| 14 | + if _, err := os.Stat(file); os.IsNotExist(err) { |
| 15 | + b.Skip("skipping benchmark; testfile12.pdf not found") |
| 16 | + } |
| 17 | + |
| 18 | + f, err := os.Open(file) |
| 19 | + if err != nil { |
| 20 | + b.Fatal(err) |
| 21 | + } |
| 22 | + defer f.Close() |
| 23 | + |
| 24 | + info, err := f.Stat() |
| 25 | + if err != nil { |
| 26 | + b.Fatal(err) |
| 27 | + } |
| 28 | + |
| 29 | + r, err := NewReader(f, info.Size()) |
| 30 | + if err != nil { |
| 31 | + b.Fatal(err) |
| 32 | + } |
| 33 | + |
| 34 | + // Find a valid object ID to resolve. |
| 35 | + // For testfile1.pdf (produced by simple writer), object 1 usually exists. |
| 36 | + // Or we can scan xref to find a valid one. |
| 37 | + var traceID uint32 |
| 38 | + for id, x := range r.xref { |
| 39 | + if x.offset > 0 { |
| 40 | + traceID = uint32(id) |
| 41 | + break |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + if traceID == 0 { |
| 46 | + b.Fatal("no valid object found to benchmark") |
| 47 | + } |
| 48 | + |
| 49 | + fmt.Printf("Benchmarking resolution of Object ID: %d\n", traceID) |
| 50 | + |
| 51 | + b.ResetTimer() |
| 52 | + for i := 0; i < b.N; i++ { |
| 53 | + // This should hit the cache after the first iteration |
| 54 | + _, err := r.GetObject(traceID) |
| 55 | + if err != nil { |
| 56 | + b.Fatal(err) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func BenchmarkParseAllObjects(b *testing.B) { |
| 62 | + file := "../../testfiles/testfile12.pdf" |
| 63 | + if _, err := os.Stat(file); os.IsNotExist(err) { |
| 64 | + b.Skip("skipping benchmark; testfile12.pdf not found") |
| 65 | + } |
| 66 | + |
| 67 | + f, err := os.Open(file) |
| 68 | + if err != nil { |
| 69 | + b.Fatal(err) |
| 70 | + } |
| 71 | + defer f.Close() |
| 72 | + |
| 73 | + info, err := f.Stat() |
| 74 | + if err != nil { |
| 75 | + b.Fatal(err) |
| 76 | + } |
| 77 | + |
| 78 | + // We want to measure parsing, so we need to run resolve() which populates cache. |
| 79 | + // To measure repeat parsing performance, we would need to prevent caching or create new readers. |
| 80 | + // Creating new readers involves scanning xref which is also parsing. |
| 81 | + |
| 82 | + // Option A: Create new reader each iter (measures xref parsing + object parsing if we trigger it) |
| 83 | + // Option B: Reuse reader but read distinct objects (only works if file is huge, eventually hits cache) |
| 84 | + |
| 85 | + // Let's do Option A: NewReader + Resolve All Objects. This is the "Load + Verify" scenario. |
| 86 | + |
| 87 | + b.ResetTimer() |
| 88 | + for i := 0; i < b.N; i++ { |
| 89 | + b.StopTimer() |
| 90 | + f.Seek(0, 0) // Reset file cursor |
| 91 | + b.StartTimer() |
| 92 | + |
| 93 | + r, err := NewReader(f, info.Size()) |
| 94 | + if err != nil { |
| 95 | + b.Fatal(err) |
| 96 | + } |
| 97 | + |
| 98 | + // Iterate all objects |
| 99 | + for id, x := range r.xref { |
| 100 | + if x.offset > 0 { |
| 101 | + _, err := r.GetObject(uint32(id)) |
| 102 | + if err != nil { |
| 103 | + // Some objects might be malformed or fail, but usually testfile should be clean. |
| 104 | + // Just continue or log? Fatal for now. |
| 105 | + // b.Fatal(err) |
| 106 | + // Actually, ignore errors for stress testing if file has known issues, |
| 107 | + // but testfile12 should be good. |
| 108 | + _ = err |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments