I have been testing various techniques to run "script" code which will
be stored in a SQL database and executed dynamically at runtime. The
scripts are essentially business rules that result in a report being
generated based on test parameters input by a user - 4 batches of
scripts containing multiple functions will be run each time a user runs
a new test. The reason the scripts are run in 4 batches is due to the
fact that each batch is built upon the results of the prior batch. I
have been leaning towards a VB type language since it is well known and
supports the type of functionality required.
Since the application is being written in .Net my initial thought was
to use VSA. Using this technique I was running into serious memory
leaks (on the order of 1M for each batch of scripts) on each test run.
Next I switched to manually using CodeDom and creating a new AppDomain
for each batch of scripts using a class factory approach. This worked
much better, but still results in leaking about 400 bytes for each
batch of scripts run. Plus I could only run about 6 batches of scripts
per second. Following are the compiler parameters being set which are
meant to be as efficient as possible:
CompilerParameters cParams = new CompilerParameters();
cParams.CompilerOptions = "/target:library /optimize";
cParams.GenerateExecutable = false;
cParams.GenerateInMemory = true;
cParams.IncludeDebugInformation = false;
After a lot of testing and debate I decided to test out using the
Microsoft Script Control. The results were over 300 times faster
(2,887 batches of scripts per second versus 6) with no memory leaks.
Following is a sample of what I put into a test loop:
MSScriptControl.ScriptControl sc = new
MSScriptControl.ScriptControlClass();
sc.Language = "VBScript";
sc.AddCode(TestCOMScript);
Object[] oParams = new Object[2] { new Random().Next(1000), new
Random().Next(1000) };
sc.Run("GetNumber", ref oParams).ToString();
System.Runtime.InteropServices.Marshal.ReleaseComO bject(sc);
The decision seems to be a no-brainer, but I wanted to see if anyone
has an idea of how this can be accomplished using only managed code but
without such a large performance hit or the memory leaks. I really
hate to use an ActiveX object on an otherwise purely managed
application.