paulbartrum / jurassic

A .NET library to parse and execute JavaScript code.
MIT License
874 stars 121 forks source link

Performance issue during first script execution on Azure cloud service #47

Open genna opened 8 years ago

genna commented 8 years ago

Hi,

I have a big JS script which looks like

function executeScript(param1, param2) {
    // It's about 1000 lines of code here
}

During the first run I compile the script and put FunctionInstance to a cache to be able to reuse it in future:

var engine = new ScriptEngine
{
    EnableExposedClrTypes = true
};
var compiledScript = engine.Compile(new StringScriptSource(script));
compiledScript.Execute();
var executeScriptFunc = (FunctionInstance)engine.GetGlobalValue("executeScript");

cache.Add('SCRIPT_ID', executeScriptFunc);

After that I execute the function like this:

Trace.TraceInformation($"{DateTime.Now:T} => Before");

var result = executeScriptFunc.Call(null, "Some value 1", "Some value 2");

Trace.TraceInformation($"{DateTime.Now:T} => After");

On any local environment, the first run takes about 1.5 sec to start executing the first line of the script. Further runs don't have this lag at all

But if I deploy the same code to Azure cloud service, the first run takes about 5 minutes. It doesn't depend on how powerful Azure cloud service is. It's always about 5 minutes. Second and future runs are immediate.

For second and further runs I use cached FunctionInstance

Don't you guys know why it happens? Where could I look at to fix this?

paulbartrum commented 8 years ago

I have no idea, sorry. Possibly it doesn't like the way Jurassic does code generation? Try setting EnableDebugging = true (before you call Compile()) and see if it makes a difference.