ASPOSE.PDF for .NET: CPU usage goes to 100%

We use the latest version of ASPOSE.PDF for .NET, using a the below C# code, to convert a HTML file (around 5 pages) to PDF:


var license = new License();
license.SetLicense(“Aspose.Pdf.lic”);

var htmlLoadOptions = new HtmlLoadOptions();
htmlLoadOptions.PageInfo.Margin.Bottom = 10;

using (var htmlStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlInput)))
{
var doc = new Document(htmlStream, htmlLoadOptions);
using (var pdfStream = new MemoryStream())
{
doc.Save(pdfStream);
return pdfStream.ToArray();
}
}​

I used the Visual Studio Profiling Tool and noticed that with one user, the CPU was always around 20%, which was promising.

But when I used JMeter and ran a load test with only two users, the CPU usage jumped to 40% . Then with 20 concurrent users the CPU % jumped to 100 % !

We are planning to use ASPOSE.PDF in a high-traffic website but I am not sure if it will be a good idea to do so with this test result!

Are we doing something incorrect regarding our code?
Hi there,

Thanks for your inquiry. Please note Aspose.Pdf support the concurrency and multithreading but only one thread works on a document at a time. If you manipulate a single document in different threads, the results would be unstable. Please ensure that each user is working on the copy of original document, hopefully it will resolve the issue. However if the issue persist then please share a sample project here, so we will test the scenario and will guide you accordingly.

Furthermore in reference to license implementation, please note It is suggested to set license in some Application Level event. When you will set the license in application level event/scope, it will remain valid till the lifespan of application.

  • If you are developing an ASP.NET application, you can call License.SetLicense from the Global.asax.cs (Global.asax.vb) file, in the Application_Start protected method. It is called once when the application starts. Do not call License.SetLicense from within Page_Load methods since it means the license will be loaded every time a web page is loaded.
  • If you are developing a Windows Forms or console application, call License.SetLicense in your startup code, before using Aspose.Pdf for .NET classes.
  • If you are developing a class library, you can call License.SetLicense from a static constructor of your class that uses Aspose.Pdf for .NET. The static constructor will execute before an instance of your class is created making sure Aspose.PDF for .NET license is properly set.
  • in case of WCF service, depending upon its hosting:
- if it is custom ServiceHost - then you can set license in service host start event
- if it is IIS - then set license can be called in Global.asax


We are sorry for the inconvenience caused.

Thank you.

Thank you for the reply.


I posted a sample code in my original post. As you can see we grab the HTML text, convert it to PDF and then copy it into a MemoryStream. No actual file is read or written here and everything happens in one thread.

var htmlLoadOptions = new HtmlLoadOptions();
htmlLoadOptions.PageInfo.Margin.Bottom = 10;

using (var htmlStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(htmlInput)))
{
var doc = new Document(htmlStream, htmlLoadOptions);
using (var pdfStream = new MemoryStream())
{
doc.Save(pdfStream);
return pdfStream.ToArray();
}
}​

In the above code htmlInput is the text of html.

We ran a profiler on this code and it showed this line as the slow part:

var doc = new Document(htmlStream, htmlLoadOptions);


Hi there,

Thanks for your feedback. I have tested the scenario using a sample HTML file with following code snippet and noticed that cpu usage remains around 60 and then drops to 9-10 after processing. We will appreciate if you please share your sample HTML here, we will test it further and provide you information accordingly.

public static void temp()
{
    Stopwatch sw = new Stopwatch();
    sw.Start();

    try
    {
        for (int i = 0; i <= 20; i++)
        {
            Thread thread = new Thread(new ThreadStart(convertHtmltoPdf));
            thread.Start();
        }

        Console.WriteLine("conversion is done...");
    }
    catch (Exception error)
    {
        Console.WriteLine(error.ToString());
    }

    sw.Stop();
    Console.WriteLine(sw.Elapsed.TotalSeconds);
    Console.ReadLine();
}

public static void convertHtmltoPdf()
{
    try
    {
        String html = File.ReadAllText(myDir + "test_report.html");
        var htmlLoadOptions = new HtmlLoadOptions();
        htmlLoadOptions.PageInfo.Margin.Bottom = 10;

        using (var memorystream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
        {
            Document doc = new Document((memorystream), htmlLoadOptions);
            doc.Save("E:/data/temp/" + Guid.NewGuid().ToString() + "output.pdf");
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.StackTrace);
    }
}

We are sorry for the inconvenience caused.

Best Regards,