Closed srinivas-mallisetty closed 7 years ago
It should take this amount of time only for the first call. You can either compile the views in the background on application start (with Compile
and Run
) or compile the views as part of your application (ie at compile instead of runtime). I have seen people using the second approach but have no running code sample, so I would suggest the first one.
Once the view is compiled Run
should be fast.
didn't get what you mean.
Can you give me a sample code?
The code sample is (read the comments)
var result1 = Engine.Razor.RunCompile(template, "templateKey", null, new { Name = "Hello" }); // Slow because it is the first call
var result2 = Engine.Razor.RunCompile(template, "templateKey", null, new { Name = "World" }); // Fast because the compilation is cached via `templateKey`
Or
Engine.Razor.Compile(template, "templateKey", null); // Run this in the application startup or in background
//...
var result = Engine.Razor.Run("templateKey", null, new { Name = "World" }); // Fast because the template is already compiled.
Thanks for your help. Let me give a try at this.
i got another problem, a template will be cached by the templateKey after compile and it will not change before i clean up and rebuild the project even it had been modified, if i dont use the cache it takes too long to generate a view. any idea to make it notice modified and recompile? or any else high performance solution?
@hellojiaru Yes, please read https://antaris.github.io/RazorEngine/TemplateManager.html.
You can look into the implementations and change them to your needs here: https://github.com/Antaris/RazorEngine/blob/master/src/source/RazorEngine.Core/Templating/WatchingResolvePathTemplateManager.cs and https://github.com/Antaris/RazorEngine/blob/master/src/source/RazorEngine.Core/Templating/InvalidatingCachingProvider.cs
Hi, I have a very similar issue with slow rendering of HTML pages using RazorEngine. I started a support ticket with Microsoft, who helped me identify RazorEngine.dll was causing the delay:
"_The request for /ssw/Home/FieldEducation has taken 37 seconds to execute. Perfview stack view cleary shows us that the delay is with the compilation. ii. Razorengine has invoked the compilation and it took 37 seconds to complete"
Should I start a new support issue? Or work with this thread? I have a lot of good debugging support/data for my issue. Don't want to hi-jack the tread. Thank you!
Hi Goose,
You can use this thread for continuing the discussion so that all the discussion will be available at the same place.
Ok, thanks. I will copy and paste a summary of the debug efforts here. We have a C# MVC application that exhibits some significant delay, we took the worst/slowest page and focused on that URL for testing. From MS technician:
Problem:
Getting 37 seconds delay while browsing https://sswis.unc.edu/ssw/Home/FieldEducation application hosted on Microsoft Windows Server 2008 R2 Enterprise(SSWNT5) on IIS 7.5
Summary :
i. The request for /ssw/Home/FieldEducation has taken 37 seconds to execute. Perfview stack view cleary shows us that the delay is with the compilation. ii. Razorengine has invoked the compilation and it took 37 seconds to complete
First, the debug diag, showing time:
Name First Last
|+ Request ID 00000000-0000-0000-3b38-0080000000df URL: GET('/ssw/Home/FieldEducation', '') 2,770.36 40,563.19
||+ Thread (4924) CPU=319ms (.NET ThreadPool) 2,770.36 40,563.19
|| + ntdll!? 2,770.36 40,563.19
|| |+ OTHER <<kernel32!?>> 2,770.36 40,563.19
|| | + system.web.mvc!IHttpAsyncHandler.EndProcessRequest 2,771.36 40,562.19
|| | |+ system.web.mvc!MvcHandler.EndProcessRequest 2,771.36 40,562.19
|| | | + system.web.mvc!System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncVoid1[System.Web.Mvc.MvcHandler+ProcessRequestState].CallEndDelegate(class System.IAsyncResult) 2,771.36 40,562.19 || | | + system.web.mvc!System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(class System.IAsyncResult,value class ProcessRequestState) 2,771.36 40,562.19 || | | + system.web.mvc!IAsyncController.EndExecute 2,771.36 40,562.19 || | | + system.web.mvc!Controller.EndExecute 2,771.36 40,562.19 || | | + system.web.mvc!System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncVoid
1[System.Canon].CallEndDelegate(class System.IAsyncResult) 2,771.36 40,562.19
|| | | + system.web.mvc!System.Web.Mvc.Controller.1[System.Web.Mvc.Controller+ExecuteCoreState].CallEndDelegate(class System.IAsyncResult) 2,771.36 40,562.19 || | | + system.web.mvc!System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(class System.IAsyncResult,value class ExecuteCoreState) 2,771.36 40,562.19 || | | + system.web.mvc!AsyncControllerActionInvoker.EndInvokeAction 2,771.36 40,562.19 || | | + system.web.mvc!System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResult
1[System.Boolean].CallEndDelegate(class System.IAsyncResult) 2,771.36 40,562.19
|| | | + system.web.mvc!System.Web.Mvc.Async.AsyncControllerActionInvoker+<>cDisplayClass21.
And the results from the execution thread (you can see razor version at the end):
iii. In the dumps too we are seeing the same behavior
Address Completed Timeout Time (secs) ThreadId ReturnCode Verb Url ======== ========= ======= =========== ======== ========== ==== ======================== 03d6196c no 110 44 32 200 GET /ssw/Home/FieldEducation
Call stack for the thread in execution showing that compilation is causing the delay
0:032> kL
00 01b1d854 77621605 ntdll!NtWaitForMultipleObjects+0x15
01 01b1d8f0 75761a08 KERNELBASE!WaitForMultipleObjectsEx+0x100
02 01b1d938 6f16a604 kernel32!WaitForMultipleObjectsExImplementation+0xe0
03 01b1d988 6f16a1f5 clr!WaitForMultipleObjectsEx_SO_TOLERANT+0x3c
04 (Inline) -------- clr!Thread::DoAppropriateAptStateWait+0x35
05 01b1da10 6f16a2e6 clr!Thread::DoAppropriateWaitWorker+0x237
06 01b1da7c 6f0b9334 clr!Thread::DoAppropriateWait+0x64
07 01b1db84 6c0964f0 clr!WaitHandleNative::CorWaitOneNative+0x16b
08 01b1db98 6c0964c4 mscorlib_ni!System.Threading.WaitHandle.InternalWaitOne(System.Runtime.InteropServices.SafeHandle, Int64, Boolean, Boolean)+0x20
09 01b1dbb4 6d6cded6 mscorlib_ni!System.Threading.WaitHandle.WaitOne(Int32, Boolean)+0x28
0a 01b1dca4 6d6cd6cd System_ni!System.CodeDom.Compiler.Executor.ExecWaitWithCaptureUnimpersonated(Microsoft.Win32.SafeHandles.SafeUserTokenHandle, System.String, System.String, System.CodeDom.Compiler.TempFileCollection, System.String ByRef, System.String ByRef, System.String)+0x796
0b 01b1dce4 6d650bec System_ni!System.CodeDom.Compiler.Executor.ExecWaitWithCapture(Microsoft.Win32.SafeHandles.SafeUserTokenHandle, System.String, System.String, System.CodeDom.Compiler.TempFileCollection, System.String ByRef, System.String ByRef, System.String)+0x41
0c 01b1dd1c 6d650356 System_ni!Microsoft.CSharp.CSharpCodeGenerator.Compile(System.CodeDom.Compiler.CompilerParameters, System.String, System.String, System.String, System.String ByRef, Int32 ByRef, System.String)+0x108
0d 01b1dd80 6db13e44 System_ni!Microsoft.CSharp.CSharpCodeGenerator.FromFileBatch(System.CodeDom.Compiler.CompilerParameters, System.String[])+0x1da
0e 01b1ddcc 6db121f4 System_ni!Microsoft.CSharp.CSharpCodeGenerator.FromDomBatch(System.CodeDom.Compiler.CompilerParameters, System.CodeDom.CodeCompileUnit[])+0x1ec
0f 01b1ddfc 6daf1f60 System_ni!Microsoft.CSharp.CSharpCodeGenerator.System.CodeDom.Compiler.ICodeCompiler.CompileAssemblyFromDomBatch(System.CodeDom.Compiler.CompilerParameters, System.CodeDom.CodeCompileUnit[])+0x30
10 01b1de0c 112134d1 System_ni!System.CodeDom.Compiler.CodeDomProvider.CompileAssemblyFromDom(System.CodeDom.Compiler.CompilerParameters, System.CodeDom.CodeCompileUnit[])+0x18
11 01b1df24 11212d6b RazorEngine!RazorEngine.Compilation.DirectCompilerServiceBase.Compile(RazorEngine.Compilation.TypeContext)+0x621
12 01b1df98 11212288 RazorEngine!RazorEngine.Compilation.DirectCompilerServiceBase.CompileType(RazorEngine.Compilation.TypeContext)+0x93
13 01b1e060 11211f1f RazorEngine!RazorEngine.Templating.TemplateService.CreateTemplateType(System.String, System.Type)+0x2e0
14 01b1e0b0 11211e05 RazorEngine!RazorEngine.Templating.TemplateService.CreateTemplate(System.String, System.Type, System.Object)+0xb7
15 01b1e0e0 11211cd0 RazorEngine!RazorEngine.Templating.TemplateService.Parse(System.String, System.Object, RazorEngine.Templating.DynamicViewBag, System.String)+0x5d
16 01b1e104 11211b10 RazorEngine!RazorEngine.Razor.Parse(System.String)+0x40
17 01b1e13c 116c2be1 Renci_Ssw_Web!Renci.Ssw.Web.Common.BaseWebViewPage1[[System.__Canon, mscorlib]].ParseRazor(System.String)+0x50 18 01b1e1bc 11cdf637 App_Web_us23jmao!ASP._Page_Views_Shared__Layout_cshtml.Execute()+0x1d1 19 01b1e1e0 11cdf37a System_Web_WebPages!System.Web.WebPages.WebPageBase.ExecutePageHierarchy()+0xc7 1a 01b1e1f8 11cdde53 System_Web_Mvc!System.Web.Mvc.WebViewPage.ExecutePageHierarchy()+0x6a 1b 01b1e20c 116c14e9 System_Web_WebPages!System.Web.WebPages.WebPageBase.ExecutePageHierarchy(System.Web.WebPages.WebPageContext, System.IO.TextWriter, System.Web.WebPages.WebPageRenderingBase)+0x5b 1c 01b1e238 116c13eb System_Web_WebPages!System.Web.WebPages.WebPageBase+<>c__DisplayClass3.<RenderPageCore>b__2(System.IO.TextWriter)+0xe9 1d 01b1e24c 116c13c1 System_Web_WebPages!System.Web.WebPages.HelperResult.WriteTo(System.IO.TextWriter)+0xb 1e 01b1e24c 116c1218 System_Web_WebPages!System.Web.WebPages.WebPageBase.Write(System.Web.WebPages.HelperResult)+0x51 1f 01b1e260 116c0fd6 System_Web_WebPages!System.Web.WebPages.WebPageBase.RenderSurrounding(System.String, System.Action
11<System.Web.Mvc.IResultFilter>, Int32, System.Web.Mvc.ResultExecutingContext, System.Web.Mvc.ControllerContext, System.Web.Mvc.ActionResult)+0x39 27 01b1e37c 1168de1d System_Web_Mvc!System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(System.Collections.Generic.IList
11<System.Web.Mvc.IResultFilter>, System.Web.Mvc.ActionResult)+0x35 29 01b1e3d0 1168cd55 System_Web_Mvc!System.Web.Mvc.Async.AsyncControllerActionInvoker+<>c__DisplayClass21+<>c__DisplayClass2b.<BeginInvokeAction>b__1c()+0xae 2a 01b1e3fc 1168cd1b System_Web_Mvc!System.Web.Mvc.Async.AsyncControllerActionInvoker+<>c__DisplayClass21.<BeginInvokeAction>b__1e(System.IAsyncResult)+0x25 2b 01b1e40c 1168cbb2 System_Web_Mvc!System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResult
1[[System.Canon, mscorlib]].CallEndDelegate(System.IAsyncResult)+0xfffffc4b
2c 01b1e40c 1168c9ac System_Web_Mvc!System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase`1[[System.Canon, mscorlib]].End()+0xfffffc12
2d 01b1e414 1168c966 System_Web_Mvc!System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(System.IAsyncResult)+0x1c
2e 01b1e41c 1168c92d System_Web_Mvc!System.Web.Mvc.Controller.1[[System.Web.Mvc.Controller+ExecuteCoreState, System.Web.Mvc]].CallEndDelegate(System.IAsyncResult)+0x25 30 01b1e448 1168c888 System_Web_Mvc!System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase
1[[System.Canon, mscorlib]].End()+0xfffff43f
31 01b1e46c 1168c7ad System_Web_Mvc!System.Web.Mvc.Controller.EndExecuteCore(System.IAsyncResult)+0x28
32 01b1e474 1168c775 System_Web_Mvc!System.Web.Mvc.Controller.1[[System.Web.Mvc.Controller+ExecuteCoreState, System.Web.Mvc]].CallEndDelegate(System.IAsyncResult)+0xfffffe6d 34 01b1e494 1168c6ee System_Web_Mvc!System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase
1[[System.Canon, mscorlib]].End()+0xfffff43f
35 01b1e4a0 1168c6bb System_Web_Mvc!System.Web.Mvc.Controller.EndExecute(System.IAsyncResult)+0x1e
36 01b1e4a8 1168c63e System_Web_Mvc!System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(System.IAsyncResult)+0xb
37 01b1e4c4 1168c5dd System_Web_Mvc!System.Web.Mvc.MvcHandler.1[[System.Web.Mvc.Controller+ExecuteCoreState, System.Web.Mvc]].CallEndDelegate(System.IAsyncResult)+0xfffffcd5 39 01b1e4f0 1168c2b0 System_Web_Mvc!System.Web.Mvc.Async.AsyncResultWrapper+WrappedAsyncResultBase
1[[System.Canon, mscorlib]].End()+0xfffff43f
3a 01b1e4fc 1168be0a System_Web_Mvc!System.Web.Mvc.MvcHandler.EndProcessRequest(System.IAsyncResult)+0x20
3b 01b1e504 6a740c4d System_Web_Mvc!System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(System.IAsyncResult)+0xa
3c 01b1e548 69de984c System_Web_ni!System.Web.HttpApplication+CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()+0x93453d
3d 01b1e588 69dfb8e7 System_Web_ni!System.Web.HttpApplication.ExecuteStep(IExecutionStep, Boolean ByRef)+0x9c
3e 01b1e66c 69de9a90 System_Web_ni!System.Web.HttpApplication+PipelineStepManager.ResumeSteps(System.Exception)+0x427
3f 01b1e680 69de656b System_Web_ni!System.Web.HttpApplication.BeginProcessRequestNotification(System.Web.HttpContext, System.AsyncCallback)+0x60
40 01b1e6d0 69deba01 System_Web_ni!System.Web.HttpRuntime.ProcessRequestNotificationPrivate(System.Web.Hosting.IIS7WorkerRequest, System.Web.HttpContext)+0xbb
41 01b1e7c8 69deb70f System_Web_ni!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr, IntPtr, IntPtr, Int32)+0x2d1
42 01b1e7f0 00b3ac3e System_Web_ni!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32)+0x1f
43 01b1e820 6ef593f9 CLRStub[StubLinkStub]@289ea3fb00b3ac3e
44 01b1e844 6ef594ea webengine4!W3_MGD_HANDLER::ProcessNotification+0x5a
45 01b1e864 6ef5955d webengine4!W3_MGD_HANDLER::DoWork+0x2cb
46 01b1e8ac 6ef59d7d webengine4!RequestDoWork+0x2ea
47 01b1e8cc 701103b5 webengine4!CMgdEngHttpModule::OnExecuteRequestHandler+0x18
48 01b1e8e0 701111c0 iiscore!NOTIFICATION_CONTEXT::RequestDoWork+0x128
49 01b1e958 700fdf13 iiscore!NOTIFICATION_CONTEXT::CallModulesInternal+0x305
4a 01b1e9a0 7010068b iiscore!NOTIFICATION_CONTEXT::CallModules+0x28
4b 01b1e9c4 7010267b iiscore!W3_CONTEXT::DoStateRequestExecuteHandler+0x36
4c 01b1ec2c 701062e4 iiscore!W3_CONTEXT::DoWork+0xd7
4d 01b1ec4c 7010633d iiscore!W3_MAIN_CONTEXT::ContinueNotificationLoop+0x1f
4e 01b1ec60 701007da iiscore!W3_MAIN_CONTEXT::ProcessIndicateCompletion+0x1f
4f 01b1ec70 6ef59b41 iiscore!W3_CONTEXT::IndicateCompletion+0x75
50 01b1ec80 6ef59b79 webengine4!W3_MGD_HANDLER::IndicateCompletion+0x38
51 01b1ec90 69e4a861 webengine4!MgdIndicateCompletion+0x22
52 01b1ecd0 69debb80 System_Web_ni!DomainNeutralILStubClass.IL_STUB_PInvoke(IntPtr, System.Web.RequestNotificationStatus ByRef)+0x3d
53 01b1edc4 69deb70f System_Web_ni!System.Web.Hosting.PipelineRuntime.ProcessRequestNotificationHelper(IntPtr, IntPtr, IntPtr, Int32)+0x450
54 01b1edec 00b3ac3e System_Web_ni!System.Web.Hosting.PipelineRuntime.ProcessRequestNotification(IntPtr, IntPtr, IntPtr, Int32)+0x1f
55 01b1ee0c 6f12e8cd CLRStub[StubLinkStub]@289ea3fb00b3ac3e
56 01b1ee6c 6f05cde5 clr!UM2MThunk_Wrapper+0x76
57 01b1ef30 6f12e83b clr!Thread::DoADCallBack+0xb4
58 01b1ef88 00b3ac99 clr!UM2MDoADCallBack+0x92
59 01b1efbc 6ef593f9 CLRStub[StubLinkStub]@289ea3fb00b3ac99
5a 01b1efe0 6ef5a326 webengine4!W3_MGD_HANDLER::ProcessNotification+0x5a
5b 01b1eff0 6f12147a webengine4!ProcessNotificationCallback+0x32
5c 01b1f038 6f179085 clr!UnManagedPerAppDomainTPCount::DispatchWorkItem+0x1ce
5d 01b1f04c 6f178808 clr!ThreadpoolMgr::ExecuteWorkRequest+0x42
5e 01b1f0b4 6f16a909 clr!ThreadpoolMgr::WorkerThreadStart+0x36c
5f 01b1fcd4 7576338a clr!Thread::intermediateThreadProc+0x4d
60 01b1fce0 77a997f2 kernel32!BaseThreadInitThunk+0xe
61 01b1fd20 77a997c5 ntdll!__RtlUserThreadStart+0x70
62 01b1fd38 00000000 ntdll!_RtlUserThreadStart+0x1b
0:032> lmvm RazorEngine
Browse full module list
start end module name
01be0000 01bfa000 RazorEngine (no symbols)
Loaded symbol image file: RazorEngine.dll
Image path: RazorEngine.dll
Image name: RazorEngine.dll
Browse all global symbols functions data
Using CLR debugging support for all symbols
Has CLR image header, track-debug-data flag not set
Timestamp: Fri Jan 17 14:39:29 2014 (52D8F349)
CheckSum: 0002243D
ImageSize: 0001A000
File version: 3.4.1.0
Product version: 3.4.1.0
File flags: 0 (Mask 3F)
File OS: 4 Unknown Win32
File type: 2.0 Dll
File date: 00000000.00000000
Translations: 0000.04b0
CompanyName: RazorEngine Project
ProductName: RazorEngine
InternalName: RazorEngine.dll
OriginalFilename: RazorEngine.dll
ProductVersion: 3.4.1.0
FileVersion: 3.4.1.0
FileDescription: RazorEngine.Core
LegalCopyright: Copyright © RazorEngine Project 2011-2013
Comments: RazorEngine - Core Framework
Closing old issue. Please open a new one if the issue persists. As I said, compiling the first time is slow...
We have used this in a project. It is taking nearly 30 sec for generating HTML of a view. In one request, it is taking more than 60% of request time
Is there any solution for reducing the time?