Wednesday, 27 November 2013

Executing two H2 scripts after compiling them

Sometimes you want to reuse a script that already exists, for example to have multiple copies of it running at the same time (great for Fuzzing of load testing).

Here is a simple example (from the TeamMentor UnitTest/Tools collection) that does exactly that:

   1: //var topPanel = O2Gui.open<Panel>("{name}",700,400);

   2: var file =  PublicDI.CurrentScript.parentFolder()

   3:                                   .pathCombine(@"Util - Browse TeamMentor Libraries v1.0.h2");

   4:  

   5: var assembly =  file.compile_H2Script();

   6: assembly.executeFirstMethod(); 

   7: assembly.executeFirstMethod();   

   8:   

Note the PublicDI.CurrentScript.parentFolder() to get the location of the current script so that we can find the target one (if the target script was part of the O2.Platform.Scripts folder, we could had  just used “{file name}”.local()

The file.compile_H2Script(); does all the heavy lifting of compiling the H2 partial script and returning a compiled assembly.

The assembly.executeFirstMethod() is another helper method that will find the first method from the the first type in the provided assembly and execute it (note: that is the same as executing assembly.types().first().methods().first().executeMethod(); ).

Here is what the script looks like in the O2 REPL environment:

image

And when executed we will have 3 instances of the Util - Browse TeamMentor Libraries v1.0.h2 script running (note that I loaded data from 3 different TeamMentor servers)

image