Repository navigation
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
134 lines (113 loc) · 3.39 KB
/
Copy pathbenchmark_test.go
File metadata and controls
134 lines (113 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package pdfsign_test
import (
"crypto"
"crypto/x509"
"io"
"os"
"testing"
"github.com/digitorus/pdfsign"
"github.com/digitorus/pdfsign/internal/testpki"
)
// BenchmarkAppearance creates benchmarks for logical appearance creation
func BenchmarkAppearance(b *testing.B) {
b.Run("New", func(b *testing.B) {
for i := 0; i < b.N; i++ {
pdfsign.NewAppearance(200, 100)
}
})
b.Run("Complex", func(b *testing.B) {
for i := 0; i < b.N; i++ {
app := pdfsign.NewAppearance(200, 100)
app.Border(1.0, 0, 0, 0).Background(240, 240, 240)
app.Text("Benchmark").Position(10, 10)
app.Text("Signature").Position(10, 30)
}
})
}
// BenchmarkSign benchmarks the signing process
func BenchmarkSign(b *testing.B) {
testFile := "testfiles/testfile20.pdf"
if _, err := os.Stat(testFile); os.IsNotExist(err) {
b.Skip("test file not found")
}
// Pre-load keys to exclude from benchmark
cert, key := testpki.LoadBenchKeys()
// Read file into memory to avoid I/O noise
fileData, err := os.ReadFile(testFile)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
// We need a fresh reader for each iteration because Sign modifies state/offsets
// Use a bytes reader
r := testpki.NewBytesReader(fileData)
doc, err := pdfsign.Open(r, int64(len(fileData)))
if err != nil {
b.Fatal(err)
}
// Configure
doc.Sign(key, cert).Reason("Benchmark").SignerName("Benchmarker")
// Write to discard
if _, err := doc.Write(io.Discard); err != nil {
b.Fatal(err)
}
}
}
// BenchmarkVerify benchmarks verification
func BenchmarkVerify(b *testing.B) {
testFile := "testfiles/testfile20.pdf"
if _, err := os.Stat(testFile); os.IsNotExist(err) {
b.Skip("test file not found")
}
cert, key := testpki.LoadBenchKeys()
fileData, err := os.ReadFile(testFile)
if err != nil {
b.Fatal(err)
}
// Create a signed version in memory
r := testpki.NewBytesReader(fileData)
doc, _ := pdfsign.Open(r, int64(len(fileData)))
doc.Sign(key, cert).Reason("Bench").SignerName("Bench")
// Create pipe or buffer to capture signed output
// But Document.Write takes io.Writer. We need the RESULTING bytes to Verify.
// We can write to a buffer once.
// But wait, Write returns *Result, but validation needs a ReaderAt of the signed file.
// So we need to write to a temp file or memory buffer.
// Pre-sign once
signedData := signRef(fileData, key, cert)
b.ResetTimer()
for i := 0; i < b.N; i++ {
// Open the SIGNED data
rSigned := testpki.NewBytesReader(signedData)
doc, err := pdfsign.Open(rSigned, int64(len(signedData)))
if err != nil {
b.Fatal(err)
}
// Verify
result := doc.Verify()
if err := result.Err(); err != nil {
// Might fail if trust chain not set up, but we want to measure perf
_ = err
}
_ = result.Valid() // Trigger verification
}
}
// Helper to sign once and return bytes
func signRef(input []byte, key crypto.Signer, cert *x509.Certificate) []byte {
// Simple in-memory signing helper setup
tmpIn, _ := os.CreateTemp("", "bench-in")
_, _ = tmpIn.Write(input)
_ = tmpIn.Close()
defer func() { _ = os.Remove(tmpIn.Name()) }()
tmpOut, _ := os.CreateTemp("", "bench-out")
defer func() { _ = os.Remove(tmpOut.Name()) }()
_ = tmpOut.Close()
doc, _ := pdfsign.OpenFile(tmpIn.Name())
fOut, _ := os.Create(tmpOut.Name())
doc.Sign(key, cert).Reason("Ref")
_, _ = doc.Write(fOut)
_ = fOut.Close()
outData, _ := os.ReadFile(tmpOut.Name())
return outData
}