Benchmark paragraph adding. Demonstrates issue #139. (#456)

Couple of TODO comments to where things appear to be slow.
This commit is contained in:
Gunnsteinn Hall 2019-04-30 22:09:05 +00:00 committed by GitHub
parent 7b7eccd65e
commit 07ba42176d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

@ -266,6 +266,11 @@ func (blk *Block) translate(tx, ty float64) {
// drawToPage draws the block on a PdfPage. Generates the content streams and appends to the PdfPage's content
// stream and links needed resources.
func (blk *Block) drawToPage(page *model.PdfPage) error {
// TODO(gunnsth): Appears very wasteful to do this all the time.
// Possibly create another wrapper around model.PdfPage (creator.page) which can keep track of whether
// this has already been done.
// Check if Page contents are wrapped - if not wrap it.
content, err := page.GetAllContentStreams()
if err != nil {
@ -374,6 +379,10 @@ func (blk *Block) mergeBlocks(toAdd *Block) error {
func mergeContents(contents *contentstream.ContentStreamOperations, resources *model.PdfPageResources,
contentsToAdd *contentstream.ContentStreamOperations, resourcesToAdd *model.PdfPageResources) error {
// TODO(gunnsth): It seems rather expensive to mergeContents all the time. A lot of repetition.
// It would be more efficient to perform the merge at the very and when we have all the "blocks"
// for each page.
// To properly add contents from a block, we need to handle the resources that the block is
// using and make sure it is accessible in the modified Page.
//

View File

@ -0,0 +1,35 @@
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package creator
import (
"bytes"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func benchmarkParagraphAdding(b *testing.B, loops int) {
for n := 0; n < b.N; n++ {
c := New()
for i := 0; i < loops; i++ {
p := c.NewParagraph(fmt.Sprintf("Paragraph %d - %d", n, i))
err := c.Draw(p)
require.NoError(b, err)
}
var buf bytes.Buffer
err := c.Write(&buf)
require.NoError(b, err)
}
}
// Benchmark adding multiple paragraphs and writing out.
// Check the effects of varying number of paragraphs written out.
func BenchmarkParagraphAdding1(b *testing.B) { benchmarkParagraphAdding(b, 1) }
func BenchmarkParagraphAdding10(b *testing.B) { benchmarkParagraphAdding(b, 10) }
func BenchmarkParagraphAdding100(b *testing.B) { benchmarkParagraphAdding(b, 100) }