Document viewer API takes a long time to render PNG document in .NET

Hi,

I have a technical question to ask. Vladimir has been helping me with my technical isuses, but I thought because the topic is different so I started a new topic.

Basically, I have uploaded a png file in Azure Blob. The size is only 58Kb.
When I use Groupdocs.Viewer with PngViewOptions to render the document, it takes an incredible amount of time to render.

Below is a snippet of my code which convert then png file which is in a ‘Stream’.

public List<byte[]> PreviewAsPng(Stream documentStream)
{
        using (Viewer viewer = new Viewer(documentStream))
        {
            var pages = new List<MemoryStream>();
            var streamFactory = new MemoryPageStreamFactory(pages);
            var options = new PngViewOptions(streamFactory);
            viewer.View(options);

            var imageByteArray = new List<byte[]>();
            foreach (var page in pages)
            {
                imageByteArray.Add(page.ToArray());
            }

            return imageByteArray;
        }
}

When it reaches the line viewer.Viewer(options), the code takes an incredible amount of time to render.
I am not sure why it is not the case for other types of documents say: docx and pdf.

Thank you.

Regards,
Sandi

@sandieng

Thank you for creating the new topic. Could you please provide the file so we could reproduce the issue at our end? From the previous topics, I can see that you’ve been running the app in a Docker container is it the same app?

Hi Vladimir,

The png file is attached. It is not exactly the same app I used before. I have ported the implementation to a different app. However, it is still running in a Docker container.
The code snippet I mentioned earlier is what I am using now to test with different document formats.
Note that the input parameter is a stream containing the png image. Before, it was a physical file location.

Cheers,
SandiUser story 90 pull request.PNG (46.9 KB)

@sandieng

Thank you for attaching the sample file. Could you please run the sample_app.zip (44.1 KB) and check if you can reproduce the issue with this app to make sure that we’re on the same page. The app may hang because we’re trying to detect file-type by reading its content so it is recommended to pass LoadOptions with specified file-type as the second Viewer constructor’s parameter e.g.

FileType fileType = FileType.FromExtension(extension);
LoadOptions loadOptions = new LoadOptions(fileType);

using (Viewer viewer = new Viewer(stream, loadOptions))
...

When you do specify the type of file the processing would be faster because Viewer will skip the step where it trying to detect the type of the file.

Hi Vladimir,

Good morning. I downloaded your code and ran it on my local machine. The time it took to render the png is between 2.6 - 2.9 seconds. This is not as slow as what I experience in my actual app (average 12 seconds). But, the question is with a small png file, is 2.6 seconds acceptable?

Cheers,
Sandi

@sandieng

It is possible that it’s due to two main reasons:

  • the app is running in Debug configuration
  • the “Cold Start” is measured here which is typically slower than the next executions

Please check the updated sample_app.zip (44.3 KB) where the app is running in Release configuration and the same image is rendered tent times in a loop. The results that I’ve got locally when running in Dockert:

Iteration 0; Time taken: 1237.6421ms
Iteration 1; Time taken: 241.8769ms
Iteration 2; Time taken: 223.0921ms
Iteration 3; Time taken: 219.62ms
Iteration 4; Time taken: 238.0878ms
Iteration 5; Time taken: 221.6887ms
Iteration 6; Time taken: 215.8349ms
Iteration 7; Time taken: 228.5608ms
Iteration 8; Time taken: 219.2316ms
Iteration 9; Time taken: 212.3425ms

As you can see the first execution is slower if we compare it with the rest. Could you please run the app and share your results?

Hi Vladimir,

I have similar results when I am using my POC code. However, in real environment, the results are not great. I tried with the load option and the performance has improved somewhat.

From your experience, which types of rendering is faster: html option, pdf option or png option?
I am having the impression that rendering any kinds of documents in png format is faster, correct me if I am wrong.

Thank you.
Sandi

@sandieng

If possible please share sample files so we could investigate the issues with performance.

Based on the internal performance-profile reports that we’re preparing before each release to make sure we do not degrade I can say that rendering to PNG is faster in most cases except Word to PNG. The performance mostly depends on the type of file and the complexity of its content. By complexity, I mean count of elements: shapes, images, charts, etc.

In case you’re planning to render some specific subset of documents then it is possible to measure rendering to different formats and comparing the results.

Hi Vladimir,

I am not sure what else I can try to improve the performance apart from using the LoadOptions.
When my code is running inside our main application in a Docker container, rendering a 7Kb png file is causing a timeout.

Another observation, rendering a 90 pages (Pdf 5000K.pdf) pdf document causes the app to crash and run out of memory. This doesn’t happen when I run the app in IIS Express.

To simulate the issue, choose to run the app in Docker instead of IIS Express.
Then use Postman and do a GET using this URL: https://localhost:49153/documentviewer/withoption?filepath=documents/pdf 5000k.pdf

The app will crash. The app I use to test is here: https://github.com/sandieng/API.DocumentViewer

Thank you.
Sandi

@sandieng

We’ve reproduced “Out of memory” issue and logged it in our bug-tracker with ID VIEWERNET-3283. We’ll let you know in case of any updates.

Hi Vladimir,

Thank you.
What about the slow rendering with a small PNG file in Docker container?
In my app, I have supplied a small PNG file (7KB). The same slowness also occurs with JPG and JPEG files.

Sandi

@sandieng

Unfortunately, I can’t reproduce this issue. How much time on average does it take to render the 7KB PNG file in your environment?

I suspect the issue is with our Docker + Kubernetes.
One another note, is it possible for the Viewer to:

  1. Return a certain number of pages, ie: 10 pages, 20 pages, 50 pages
  2. Specific page, ie: page 1 only, page 5 only, page 2, and page 7-12, etc

I am asking this because we want to allow paging on the client side or for the client to retrieve a certain page or a certain number of pages.

Cheers,
Sandi

@sandieng

Sure, the View method has the overload that accepts page number(s) as a second parameter:

viewer.View(viewOptions, 1); 
viewer.View(viewOptions, 1, 2, 3);
viewer.View(viewOptions, 3, 2, 1);
viewer.View(viewOptions, new int[] { 1, 3, 5 });

The pages will be rendered in order that you specified. To retrieve count pages you can use GetViewInfo method - this method returns a list of pages that Viewer can render.

Hi Vladimir,

Thank you for getting back to me quickly.
My company has purchased the licence, my next question is how do I include the licence in our code?
We want to launch this document preview this weekend. So, if you can respond quickly that will be appreciated.

Cheers,
Sandi

This question has a response in a related forum thread.

@sandieng

OOM issue has been fixed in GroupDocs.Viewer 23.1. The package is available at NuGet and GroupDocs.Downloads.

Have a nice day!