Difference in time to create KieContainer vs. KieBase?

737 views
Skip to first unread message

KimJohn Quinn

unread,
Aug 4, 2018, 7:00:10 PM8/4/18
to Drools Usage
This started out by me trying to find the reason why certain requests seemed to be taking longer than usual.  

It seems like if I go to the KieBase from the KieContainer and create the new session it works significantly faster than using the KieContainer alone.  

In my tests I am not doing anything extraordinary, just trying to fire an empty rule, in a batch, from different threads - each representing a new stateful session.  Firing the rule after creating the session seems to be consistent across all my tests (after the initial creation of the KieContainer).

I am using Drools 7.8.0.Final...

It seems like there is a difference between:
  • KieSession kieSession = kieContainer.newKieSession(TEST_KSESSION);

  • KieSession kieSession = kieContainer.getKieBase(TEST_KBASE).newKieSession();

My Results look like:
5 Runs / 1000 Executions / 2 Threads (drop the first value) 
DRL (loads just a DRL file using the KieHelper)
   NonThreaded : [11, 9, 9, 8, 8] --> 7 ms avg
   Threaded    : [30, 17, 18, 17, 19] --> 14 ms avg 
KJar (assumes a classpath container and a named session)
   NonThreaded : [460, 427, 406, 369, 330] --> 306 ms avg
   Threaded    : [188, 171, 159, 159, 150] --> 128 ms avg 
KBase (assumes a classpath container and the default session which is the named sesssion marked as default)
   NonThreaded : [47, 28, 29, 28, 26] --> 22 ms avg
   Threaded    : [19, 5, 5, 4, 5] --> 4 ms avg

My Tests look like this:

 @Test
    public void should_run_drl_not_threaded()
    {
        for (int r = 0; r < RUNS; r++)
        {
            final Stopwatch stopwatch = createStarted();

            for (int i = 0; i < EXECUTIONS; i++)
            {
                KieSession kieSession = kieBase.newKieSession();

                CommandExecutor exec = kieSession;
                Command fireCmd = new FireAllRulesCommand();
                Command batchCmd = new BatchExecutionCommandImpl(newArrayList(fireCmd));
                exec.execute(batchCmd);

                kieSession.dispose();
            }

            //Total time
            final long time = stopwatch.stop().elapsed(MILLISECONDS);
            drlNonThreaded[r] = time;
            log.warn("*************** Time for {} non-threaded drl: {} ms", EXECUTIONS, time);
        }
    }

    @Test
    public void should_run_drl_threaded() throws InterruptedException
    {
        final ExecutorService executorService = newFixedThreadPool(THREADS);

        for (int r = 0; r < RUNS; r++)
        {
            final CountDownLatch latch = new CountDownLatch(EXECUTIONS);
            final Stopwatch stopwatch = createStarted();

            for (int i = 0; i < EXECUTIONS; i++)
            {
                executorService.submit(() -> {
                    KieSession kieSession = kieBase.newKieSession();

                    CommandExecutor exec = kieSession;
                    Command fireCmd = new FireAllRulesCommand();
                    Command batchCmd = new BatchExecutionCommandImpl(newArrayList(fireCmd));
                    exec.execute(batchCmd);

                    kieSession.dispose();
                    latch.countDown();
                });
            }

            //Wait for all threads to finish
            latch.await();

            //Total time
            final long time = stopwatch.stop().elapsed(MILLISECONDS);
            drlThreaded[r] = time;
            log.warn("***************  Time for {} threaded drl: {} ms", EXECUTIONS, time);
        }

        executorService.shutdown();
    }

    @Test
    public void should_run_kbase_not_threaded()
    {
        final KieContainer kieContainer = KieServices.get().newKieClasspathContainer();

        for (int r = 0; r < RUNS; r++)
        {
            final Stopwatch stopwatch = createStarted();

            for (int i = 0; i < EXECUTIONS; i++)
            {
                KieSession kieSession = kieContainer.getKieBase(TEST_KBASE).newKieSession();
                CommandExecutor exec = kieSession;
                Command fireCmd = new FireAllRulesCommand();
                Command batchCmd = new BatchExecutionCommandImpl(newArrayList(fireCmd));
                exec.execute(batchCmd);
                kieSession.dispose();
            }

            //Total time
            final long time = stopwatch.stop().elapsed(MILLISECONDS);
            kbaseNonThreaded[r] = time;
            log.warn("*************** Time for {} non-threaded kbase: {} ms", EXECUTIONS, time);
        }
    }

    @Test
    public void should_run_kbase_threaded() throws InterruptedException
    {
        final ExecutorService executorService = newFixedThreadPool(THREADS);

        final KieContainer kieContainer = KieServices.get().newKieClasspathContainer();

        for (int r = 0; r < RUNS; r++)
        {
            final CountDownLatch latch = new CountDownLatch(EXECUTIONS);
            final Stopwatch stopwatch = createStarted();

            for (int i = 0; i < EXECUTIONS; i++)
            {
                executorService.submit(() -> {
                    KieSession kieSession = kieContainer.getKieBase(TEST_KBASE).newKieSession();
                    CommandExecutor exec = kieSession;
                    Command fireCmd = new FireAllRulesCommand();
                    Command batchCmd = new BatchExecutionCommandImpl(newArrayList(fireCmd));
                    exec.execute(batchCmd);
                    kieSession.dispose();
                    latch.countDown();
                });
            }

            //Wait for all threads to finish
            latch.await();

            //Total time
            final long time = stopwatch.stop().elapsed(MILLISECONDS);
            kbaseThreaded[r] = time;
            log.warn("***************  Time for {} threaded kbase: {} ms", EXECUTIONS, time);
        }

        executorService.shutdown();
    }

    @Test
    public void should_run_kjar_not_threaded()
    {
        final KieContainer kieContainer = KieServices.get().newKieClasspathContainer();

        for (int r = 0; r < RUNS; r++)
        {
            final Stopwatch stopwatch = createStarted();

            for (int i = 0; i < EXECUTIONS; i++)
            {
                KieSession kieSession = kieContainer.newKieSession(TEST_KSESSION);
                CommandExecutor exec = kieSession;
                Command fireCmd = new FireAllRulesCommand();
                Command batchCmd = new BatchExecutionCommandImpl(newArrayList(fireCmd));
                exec.execute(batchCmd);
                kieSession.dispose();
            }

            //Total time
            final long time = stopwatch.stop().elapsed(MILLISECONDS);
            kjarNonThreaded[r] = time;
            log.warn("*************** Time for {} non-threaded kjar: {} ms", EXECUTIONS, time);
        }
    }

    @Test
    public void should_run_kjar_threaded() throws InterruptedException
    {
        final ExecutorService executorService = newFixedThreadPool(THREADS);

        final KieContainer kieContainer = KieServices.get().newKieClasspathContainer();

        for (int r = 0; r < RUNS; r++)
        {
            final CountDownLatch latch = new CountDownLatch(EXECUTIONS);
            final Stopwatch stopwatch = createStarted();

            for (int i = 0; i < EXECUTIONS; i++)
            {
                executorService.submit(() -> {
                    KieSession kieSession = kieContainer.newKieSession(TEST_KSESSION);
                    CommandExecutor exec = kieSession;
                    Command fireCmd = new FireAllRulesCommand();
                    Command batchCmd = new BatchExecutionCommandImpl(newArrayList(fireCmd));
                    exec.execute(batchCmd);
                    kieSession.dispose();
                    latch.countDown();
                });
            }

            //Wait for all threads to finish
            latch.await();

            //Total time
            final long time = stopwatch.stop().elapsed(MILLISECONDS);
            kjarThreaded[r] = time;
            log.warn("***************  Time for {} threaded kjar: {} ms", EXECUTIONS, time);
        }

        executorService.shutdown();
    }

 

KimJohn Quinn

unread,
Aug 4, 2018, 8:56:47 PM8/4/18
to Drools Usage
I am about to try this but i wonder if it is related?

KimJohn Quinn

unread,
Aug 4, 2018, 9:22:33 PM8/4/18
to Drools Usage
Definitely following the attached link seems to make a huge difference for me when creating sessions

My test now looks like this:

final ExecutorService executorService = newFixedThreadPool(THREADS);

        final KieContainer kieContainer = KieServices.get().newKieClasspathContainer();
        Environment kieEnvironment = KieServices.get().newEnvironment();
        KieSessionConfiguration kieSessionConfig = KieServices.get().newKieSessionConfiguration();

        for (int r = 0; r < RUNS; r++)
        {
            final CountDownLatch latch = new CountDownLatch(EXECUTIONS);
            final Stopwatch stopwatch = createStarted();

            for (int i = 0; i < EXECUTIONS; i++)
            {
                executorService.submit(() -> {
                    KieSession kieSession = kieContainer.newKieSession(TEST_KSESSION, kieEnvironment, kieSessionConfig);
                    //KieSession kieSession = kieContainer.newKieSession(TEST_KSESSION);
                    CommandExecutor exec = kieSession;
                    Command fireCmd = new FireAllRulesCommand();
                    Command batchCmd = new BatchExecutionCommandImpl(newArrayList(fireCmd));
                    exec.execute(batchCmd);
                    kieSession.dispose();
                    latch.countDown();
                });
            }

            //Wait for all threads to finish
            latch.await();

            //Total time
            final long time = stopwatch.stop().elapsed(MILLISECONDS);
            kjarThreaded[r] = time;
            log.warn("***************  Time for {} threaded kjar: {} ms", EXECUTIONS, time);
        }

        executorService.shutdown();

On Saturday, August 4, 2018 at 7:00:10 PM UTC-4, KimJohn Quinn wrote:
Reply all
Reply to author
Forward
0 new messages