Monday, February 19, 2007

Many rule engines compile the rules to achieve better performance. Drools uses Janino to compile the rules dynamically. Other rules engines use proprietary approaches. With code for javac being made public, rules in java syntax can also be compiled. Since the advent of Mustang, there's been a lot of talk about scripting support in java and I thought why not try something with a scripting language for instance. A benchmark I saw on the net revealed that groovy was by far the fastest next only to Rhino. My inherent suspicion of javascript made me to advise my team to use Groovy and this blog contains the results of our findings. I asked my team to to beat the timings of a fictitious evaluator that would evaluate the rules in 30ms or so.
To be honest, they were circumspect from the beginning but put up a brave front to me anyways. Later they told me it was the time taken to initialize the scripting engine, the binding of the params etc that worried them. So I asked them to try a lot of techniques like pre-cooking (a term I coined for initializing everything beforehand and leaving only the binding and evaluation for the last minute) for achieving better performance.

Now for some Groovy lessons. Groovy can be integrated in your java program in multiple ways.

1. GroovyShell approach
You could use the Groovy shell which wouldn't be much unlike a call to system in c or Runtime.exec in java. This is by far the slowest and I gave up this approach soon as I tried it.


2. GroovyClassLoader approach
The second approach was using the GroovyClassLoader. The groovy class is loaded into the ‘GroovyClassLoader’ and the ‘evaluate’ method invoked using reflection. The ‘GroovyClassLoader’ provides a ‘loadClass’ method for this purpose. However, this method only accepts a source file of a groovy script as input which it then compiles and returns a ‘GroovyClass’ object. The code for this approach is given below:

ClassLoader parent = ScriptRunnerLoader.class.getClassLoader();
GroovyClassLoader loader = new GroovyClassLoader(parent);

Object [] ps = vect.toArray(); // parameters to the method
File groovyFile = new File(ruleDir + File.separator + rname + ".groovy");
// Get an groovyClass object
Class groovyClass = loader.parseClass(groovyFile);

//create instance of class
groovyObject = (GroovyObject) groovyClass.newInstance();
//call method
Object obj = groovyObject.invokeMethod("evaluate", ps);


3. Groovy class approach
The last approach was using compiled groovy class. This is the most straightforward way of calling the groovy class and therefore the most efficient. The groovy class is directly imported into the application and the ‘evaluate’ method is called, similar to function calls made on other java objects in the application. This approach yields drastic improvement in performance, as there is no overhead of classloading or from interpreting the rules. The code for this is as below:

Vector vc = (Vector) vec.get(s);
String rname = (String) vc.get(0);

int rno = Integer.parseInt(rname.substring(4));

if(skipListContains(rno))
continue;

Vector vect = (Vector) vc.get(2);
String cond = (String) vc.get(1);

Object [] ps = vect.toArray();
Class groovyClass = Class.forName("rules." + rname);
groovyObject = (GroovyObject) groovyClass.newInstance();


Object res = groovyObject.invokeMethod("evaluate", ps);

Moral:
Don't expect some benefit when the real benefit lies elsewhere. In this case, the benefit of groovy was in its rich syntax and closure support and not in terms of cpu cycles.

Conclusion:
Groovy is great. But it is yet another java syntax. For someone who works with java day in and day out and looking for something new, give Ruby, beanshell, python and F3 a test drive. Forget such things as benchmarks for the time being and do what your heart tells you.

52 comments:

Anonymous said...

kictixAxoft, clomid online waydaynerrurb, [url=http://www.webjam.com/clomidonline]buy clomid[/url] phaviolaffisa
23

Anonymous said...

I have found it on this website called [url=http://tipswift.com]tip swift[/url]. You can find it there.
cheers
edit: wrong post

Anonymous said...

woodsesty, levitra online, DinoEndorgo, http://virb.com/levitraonline levitra online, twepitiencisy, order levitra, thaccumence, http://www.protopage.com/orderlevitra levitra

Anonymous said...

chararorylarp, buy zyrtec, Carardykats, [url=http://www.webjam.com/zyrteconline]zyrtec[/url], Glaccerve

Anonymous said...

TexanyThync, viagra tramadol zithromax carisoprodol buy cialis, piluerryMer

Anonymous said...

Verduekssem, propecia online, spancenus, [url=http://virb.com/propeciaonline]propecia[/url]

Anonymous said...

Nice post as for me. It would be great to read a bit more about that theme. Thanx for sharing this information.

Anonymous said...

Hello,

When ever I surf on web I never forget to visit this website[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url]You have really contiributed very good info here ovisvana.blogspot.com. Do you pay attention towards your health?. In plain english I must warn you that, you are not serious about your health. Recent Scientific Research shows that closely 70% of all United States adults are either obese or weighty[url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips].[/url] Hence if you're one of these citizens, you're not alone. Its true that we all can't be like Brad Pitt, Angelina Jolie, Megan Fox, and have sexy and perfect six pack abs. Now the question is how you are planning to have quick weight loss? [url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips]Quick weight loss[/url] is not like piece of cake. If you improve some of your daily diet habbits then, its like piece of cake to quickly lose weight.

About me: I am author of [url=http://www.weightrapidloss.com/lose-10-pounds-in-2-weeks-quick-weight-loss-tips]Quick weight loss tips[/url]. I am also health expert who can help you lose weight quickly. If you do not want to go under painful training program than you may also try [url=http://www.weightrapidloss.com/acai-berry-for-quick-weight-loss]Acai Berry[/url] or [url=http://www.weightrapidloss.com/colon-cleanse-for-weight-loss]Colon Cleansing[/url] for effortless weight loss.

Anonymous said...

Approvingly your article helped me terribly much in my college assignment. Hats incorrect to you send, will look ahead for more cognate articles promptly as its sole of my choice topic to read.

Anonymous said...

turbulence training -
turn your photos into cash -
vincedelmontefitness -
vince del monte fitness -
warp speed fat loss -
wedding speech 4u -
win clear -
xbox 360 red light fix -
xp repair pro -
1st promotion -
3 redlight fix -
500 love making tips -
acid alkaline diet -
advanced defrag -
anti spyware -
anti spyware bot -
art of approaching -
banish rosacea -
blog lock down -
brad yates -
building a chicken coop -
burnthefat -
burn the fat -
carb rotation diet -
combat the fat -
conversationalhypnosis -
conversational hypnosis -
copy n profit -
credit secrets bible -
cure angular cheilitis -
cure morning sickness -
dirty talking guide -
domain tweak system -

Anonymous said...

turbulence training -
turn your photos into cash -
vincedelmontefitness -
vince del monte fitness -
warp speed fat loss -
wedding speech 4u -
win clear -
xbox 360 red light fix -
xp repair pro -
1st promotion -
3 redlight fix -
500 love making tips -
acid alkaline diet -
advanced defrag -
anti spyware -
anti spyware bot -
art of approaching -
banish rosacea -
blog lock down -
brad yates -
building a chicken coop -
burnthefat -
burn the fat -
carb rotation diet -
combat the fat -
conversationalhypnosis -
conversational hypnosis -
copy n profit -
credit secrets bible -
cure angular cheilitis -
cure morning sickness -
dirty talking guide -
domain tweak system -

Anonymous said...

error smart -
evidence eraser -
evidence smart -
fatburningfurnace -
fat burning furnace -
fatloss4idiots -
fat loss 4 idiots -
fitnessmodelprogram -
fitness model program -
fit yummy yummy -
flattenyourabs -
flatten your abs -
flat to fab -
forex derivative -
gas 4 free -
get better grades -
golf swing guru -
governmentregistry -
government registry -
heartburn no more -
homemadeenergy -
home made energy -
hyper vre -
i software tv -
legit online jobs -
linden method -
london forex rush -
master word smith -
maternityacupressure -
maternity acupressure -
max pro system -
meet your sweet -
membership gold rush -

Anonymous said...

It was extremely interesting for me to read this article. Thanks the author for it. I like such themes and anything connected to this matter. I definitely want to read a bit more soon.

Hilary Smith

Anonymous said...

Sildenafil citrate, sold as Viagra, Revatio and beneath different other trade names, is a medicament toughened to premium erectile dysfunction and pulmonary arterial hypertension (PAH). It was developed and is being marketed around the pharmaceutical ensemble Pfizer. It acts by inhibiting cGMP individual phosphodiesterase paradigm 5, an enzyme that regulates blood go in the penis. Since becoming readily obtainable in 1998, sildenafil has been the prime treatment an eye to erectile dysfunction; its primary competitors on the supermarket are tadalafil (Cialis) and vardenafil (Levitra).

Anonymous said...

Wow.
Well thought out
stuff:) Found this here on ovisvana.blogspot.com [url=http://easyrvoutdoors.com]RV[/url]

Bez Depozytu said...

This is my great pleasure to visit your website and to enjoy your excellent A CEO's blog on the world at large post. I found many valuable things to learn in this A CEO's blog on the world at large named blog.
--------------------------
Our www: Bonusy Bez Depozytu & Poker Bez Depozytu

Anonymous said...

[url=http://buyviagraonlinest.com/]viagra online us pharmacy[/url] - viagra online

[url=http://buycheapviagrast.com/#buy]cheap viagra 50 mg[/url] - buy cheap viagra

[url=http://buygenericviagrast.com/]generic viagra[/url] - generic viagra online canadian pharmacy

[url=http://orderviagraonlinest.com/]where to order viagra online[/url] - viagra online order

[url=http://viagradiscountonlinest.com/#discount]viagra discount[/url] - viagra discount

[url=http://viagraonlinepharmacyst.com/]viagra online pharmacy[/url] - viagra online us pharmacy

[url=http://buyviagrawithoutprescriptionst.com/#prescription]buy viagra without prescription[/url] - viagra without prescription australia

[url=http://viagralowestpricest.com/#prices]viagra prices[/url] - viagra price in australia

Anonymous said...

When we look at the concise explaination the word appreciate, installing relation to a romantic marriage using a different, however , as the sense which is engendered when you've got miltchmonkey the best relationship with yourself way too : as well as for a a feeling of more significant unity spouse and children or maybe human beings , that gets to be all the more magnificent that all anyone is looking to get to have will be enjoy.

Anonymous said...

generic viagra viagra buy in singapore - viagra dosage info

Anonymous said...

viagra online without prescription viagra online pharmacy - viagra online reputable

Anonymous said...

cheap generic viagra viagra online london - viagra for women drug

Anonymous said...

sildenafil citrate buy viagra online lloyds pharmacy - viagra online prescription free

Anonymous said...

buy soma soma bras comfortable - soma drug strengths

Anonymous said...

buy soma generic soma jaipur - much does soma cost

Anonymous said...

buy somas online soma no prescription saturday delivery - soma free shipping

Anonymous said...

buy soma online buy watson soma online - soma show on drug test

Anonymous said...

soma 350 mg buy soma florida - soma 456 bars

Anonymous said...

carisoprodol soma cheap sheraton soma bay - buy soma 350 mg online

Anonymous said...

buy cialis online cheap cialis pills - generic cialis mexico

Anonymous said...

buy cialis online is it legal to buy cialis online in usa - cialis tolerance

Anonymous said...

alprazolam mg xanax to buy online no prescription - what is xanax classified as

Anonymous said...

generic xanax generic xanax markings - xanax effects unborn baby

Anonymous said...

xanax online cheapest xanax online no prescription - generic xanax strong

Anonymous said...

buy tramadol tramadol can you snort - tramadol cheap

Anonymous said...

alprazolam 2mg xanax compared to 2mg klonopin - xanax pictures pills generic

Anonymous said...

buy cialis online buy cialis vegas - cialis price canadian pharmacy

Anonymous said...

cialis online cialis online satis - can you order cialis online for usa

Anonymous said...

xanax alprazolam generic xanax used - taking xanax recreational use

Anonymous said...

buy tramadol tramadol 100 mg extended release - buy tramadol cheap online no prescription

Anonymous said...

learn how to buy tramdadol tramadol 50mg for dogs dosage - tramadol dosage chart for dogs

Anonymous said...

cheap tramadol tramadol clorhidrato 100mg gotas - tramadol 50mg prescription

Anonymous said...

http://www.integrativeonc.org/adminsio/buyklonopinonline/#cheap klonopin under tongue high - klonopin 0.5 mg tablets

Anonymous said...

order tramadol overnight buy tramadol cod overnight delivery - can you buy tramadol over the counter in usa

Anonymous said...

http://www.integrativeonc.org/adminsio/buyklonopinonline/#6817 2mg klonopin enough - generic for clonazepam

Anonymous said...

http://landvoicelearning.com/#21906 buy generic tramadol online - tramadol hcl obat apa

Anonymous said...

buy tramadol ultram side effects itching - can you buy tramadol over the counter usa

Anonymous said...

http://buytramadolonlinecool.com/#56411 tramadol hcl prescribed - where to buy tramadol online

Anonymous said...

buy tramadol next day buy tramadol 50mg - tramadol 50 mg prn

Anonymous said...

klonopin for sale 2mg klonopin lot - generic form of klonopin

Anonymous said...

order zithromax online [url=http://viagra-prix.webs.com/#2d3c]viagra pilules[/url] [url=http://zithromax-prix.webs.com/#6h3j]zithromax france[/url] where can i buy drugs USA hap medication pharmacy without prescription jmk acheter zithromax cialis prescrire medicaments generique levitra zithromax tablets zrv

Anonymous said...

order zithromax online [url=http://viagra-prix.webs.com/#2d3c]viagra pilules[/url] [url=http://zithromax-prix.webs.com/#6h3j]zithromax france[/url] where can i buy drugs USA hap medication pharmacy without prescription jmk acheter zithromax cialis prescrire medicaments generique levitra zithromax tablets zrv

Anonymous said...

Why people still make use of to read news papers when in this
technological world everything is available on web?

Take a look at my webpage weight loss centers