From 7179508c8c1ad9c8fa45ae0a8dd72ac92e1d346f Mon Sep 17 00:00:00 2001 From: Jeroen Bobbeldijk Date: Sat, 23 Sep 2017 14:10:24 +0200 Subject: [PATCH] Add benchmark --- .gitignore | 1 + sign/sign_test.go | 61 +++++++++++- testfiles/benchmark.pdf | 201 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 262 insertions(+), 1 deletion(-) create mode 100644 testfiles/benchmark.pdf diff --git a/.gitignore b/.gitignore index 2a377a5..44eba8a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea *.pdf *.pdf.* +!benchmark.pdf \ No newline at end of file diff --git a/sign/sign_test.go b/sign/sign_test.go index d7b7110..12c8265 100644 --- a/sign/sign_test.go +++ b/sign/sign_test.go @@ -98,7 +98,7 @@ func TestSignPDF(t *testing.T) { certificate_data_block, _ := pem.Decode([]byte(signCertPem)) if certificate_data_block == nil { - t.Errorf("failed to parse PEM block containing the certificate", err.Error()) + t.Errorf("failed to parse PEM block containing the certificate") return } @@ -162,3 +162,62 @@ func TestSignPDF(t *testing.T) { } } } + +func BenchmarkSignPDF(b *testing.B) { + certificate_data_block, _ := pem.Decode([]byte(signCertPem)) + if certificate_data_block == nil { + b.Errorf("failed to parse PEM block containing the certificate") + return + } + + cert, err := x509.ParseCertificate(certificate_data_block.Bytes) + if err != nil { + b.Errorf("%s", err.Error()) + return + } + + key_data_block, _ := pem.Decode([]byte(signKeyPem)) + if key_data_block == nil { + b.Errorf("failed to parse PEM block containing the private key") + return + } + + pkey, err := x509.ParsePKCS1PrivateKey(key_data_block.Bytes) + if err != nil { + b.Errorf("%s", err.Error()) + return + } + + certificate_chains := make([][]*x509.Certificate, 0) + + for n := 0; n < b.N; n++ { + err := SignFile("../testfiles/benchmark.pdf", "../testfiles/benchmark.pdf.tmp", SignData{ + Signature: SignDataSignature{ + Info: SignDataSignatureInfo{ + Name: "Jeroen Bobbeldijk", + Location: "Rotterdam", + Reason: "Test", + ContactInfo: "Geen", + Date: time.Now().Local(), + }, + CertType: 2, + Approval: false, + }, + Signer: pkey, + Certificate: cert, + CertificateChains: certificate_chains, + TSA: TSA{ + URL: "http://aatl-timestamp.globalsign.com/tsa/aohfewat2389535fnasgnlg5m23", + }, + RevocationData: revocation.InfoArchival{}, + RevocationFunction: DefaultEmbedRevocationStatusFunction, + }) + + os.Remove("../testfiles/benchmark.pdf.tmp") + + if err != nil { + b.Errorf("%s: %s", "benchmark.pdf", err.Error()) + return + } + } +} diff --git a/testfiles/benchmark.pdf b/testfiles/benchmark.pdf new file mode 100644 index 0000000..65e6bcf --- /dev/null +++ b/testfiles/benchmark.pdf @@ -0,0 +1,201 @@ +%PDF-2.0 + +1 0 obj +<< + /Type /Catalog + /Metadata 2 0 R + /Pages 3 0 R +>> +endobj + +2 0 obj +<< + /Length 2351 + /Type /Metadata + /Subtype /XML +>> +stream + + + + Datalogics - example producer program name here + Copyright 2017 PDF Association + PDF 2.0 sample example + + + 2017-05-24T10:30:11Z + 2017-07-11T07:55:11Z + 2017-07-11T07:55:11Z + Datalogics - example creator tool name here + + + application/pdf + + + A simple PDF 2.0 example file + + + + + Datalogics Incorporated + + + + + Demonstration of a simple PDF 2.0 file. + + + + + Copyright 2017 PDF Association. Licensed to the public under Creative Commons Attribution-ShareAlike 4.0 International license. + + + + + True + + + + + + uuid:3eef2166-8332-abb4-3d31-77334578873f + uuid:991bcce7-ee70-11a3-91aa-77bbe2181fd8 + + + +endstream +endobj + +3 0 obj +<< + /Type /Pages + /Kids [4 0 R] + /Count 1 +>> +endobj + +4 0 obj +<< + /Type /Page + /Parent 3 0 R + /MediaBox [0 0 612 396] + /Contents [5 0 R 6 0 R] + /Resources << + /Font << /F1 7 0 R >> + >> +>> +endobj + +5 0 obj +<< /Length 744 >> +stream +% Save the current graphic state +q + +% Draw a black line segment, using the default line width. +150 250 m +150 350 l +S + +% Draw a thicker, dashed line segment. +4 w % Set line width to 4 points +[4 6] 0 d % Set dash pattern to 4 units on, 6 units off +150 250 m +400 250 l +S +[] 0 d % Reset dash pattern to a solid line +1 w % Reset line width to 1 unit + +% Draw a rectangle with a 1-unit red border, filled with light blue. +1.0 0.0 0.0 RG % Red for stroke color +0.5 0.75 1.0 rg % Light blue for fill color +200 300 50 75 re +B + +% Draw a curve filled with gray and with a colored border. +0.5 0.1 0.2 RG +0.7 g +300 300 m +300 400 400 400 400 300 c +b + +% Restore the graphic state to what it was at the beginning of this stream +Q + +endstream +endobj + +6 0 obj +<< /Length 166 >> +stream +% A text block that shows "Hello World" +% No color is set, so this defaults to black in DeviceGray colorspace +BT + /F1 24 Tf + 100 100 Td + (Hello World) Tj +ET +endstream +endobj + +7 0 obj +<< + /Type /Font + /Subtype /Type1 + /BaseFont /Helvetica + /FirstChar 33 + /LastChar 126 + /Widths 8 0 R + /FontDescriptor 9 0 R +>> +endobj + +8 0 obj +[ 278 355 556 556 889 667 222 333 333 389 584 278 333 278 278 556 + 556 556 556 556 556 556 556 556 556 278 278 584 584 584 556 1015 + 667 667 722 722 667 611 778 722 278 500 667 556 833 722 778 667 + 778 722 667 611 722 667 944 667 667 611 278 278 278 469 556 222 + 556 556 500 556 556 278 556 556 222 222 500 222 833 556 556 556 + 556 333 500 278 556 500 722 500 500 500 334 260 334 584 ] +endobj + +% This FontDescriptor contains only the required entries for PDF 2.0 +% for unembedded standard 14 fonts that contain Latin characters +9 0 obj +<< + /Type /FontDescriptor + /FontName /Helvetica + /Flags 32 + /FontBBox [ -166 -225 1000 931 ] + /ItalicAngle 0 + /Ascent 718 + /Descent -207 + /CapHeight 718 + /StemV 88 + /MissingWidth 0 +>> +endobj + +% The object cross-reference table. The first entry +% denotes the start of PDF data in this file. +xref +0 10 +0000000000 65535 f +0000000012 00000 n +0000000092 00000 n +0000002543 00000 n +0000002615 00000 n +0000002778 00000 n +0000003583 00000 n +0000003807 00000 n +0000003968 00000 n +0000004520 00000 n +trailer +<< + /Size 10 + /Root 1 0 R + /ID [ <31c7a8a269e4c59bc3cd7df0dabbf388><31c7a8a269e4c59bc3cd7df0dabbf388> ] +>> +startxref +4847 +%%EOF