Jscripting rules are slow after 3.4.2

Hi all,

After switching to 3.4.2 from 3.4 my jscripting rules are very slow. If I look at the output of top, I see that the cpu usage spikes when I trigger a rule (goes up to 300% sometimes). Has anyone seen this behaviour?

I am on a raspberry pi 4 4gb and running openhab on docker. When I switched back to 3.4 the problem went away. Is there anything I can do to help fix this problem?

From 3.4.0 to 3.4.2 there were absolutely no changes to the addon, and there were no core changes related to scripting as well. I don’t see any reason for that problem.

Could you try the normal openHAB 3.4.2 install outside of Docker?

I converted some of my own Nashorn JS scripts to JScriptings, turns out manually running those scripts compared to Nashorn JS takes a lot of more CPU.

NashornJS directly writes the output in the openhab logs, JScripting around 3-5 seconds later after I pressed the run action. Also in top i see the same speaks as Mark mentioned.

Running also openhab inside a docker on a pi (normally more than enough cpu power) with openhab 3.4.2.

They should only be slow on the first run. After they ran the first time, they are cached.

However the initial run takes quite some time because in JS Scripting, the openhab-js library is loaded (which are many lines of code), in NashornJS no helper library is loaded. The openhab-js helper library allows easy access to the openHAB Java APIs without having to mess with Java types in a JS environment.

You can maybe speed up the initial run of a script by disabling the „Use Built-In Globals“ option (see JavaScript Scripting - Automation | openHAB) and then manually loading the parts of the helper library you need (using const { items } = require(‘openhab’); as an example).

For openHAB 4, there are some major performance and memory improvements coming. So stay tuned!

On my Raspberry Pi 3 test system, I was able to reduce initial script execution time from 13 to just 2 seconds.

Sounds awesome, eager to try it out. So waiting for openHAB 4, till then i use the old ones.

Sorry to step into this discussion, I have a similar performance issue.
Currently, my rules are written in Jython and I understood that “Ivans Helper Libraries” are no longer maintained. Therefore I am thinking about transferring everything to JS Scripting Rules. I implemented a first rule using JSRule. And it is really slow in two regards:

  1. After having saved the JS script file, it takes 37s after I see the “Adding rule” comment in the log. Jython takes about 11s.
  2. I can compare Jython and JS Scripting rules, both rules are triggered by an item state change and write a log entry: the Jython rule reacts almost instanteously compared to the state change I see in the basicUI. The JS scripting rule comes always ~4s later.

Why is the performance so different?

(system is Raspi 4 with 2 GB RAM, recently upgraded to OH 4.0)