Open dandjo opened 19 hours ago
Does this improve with more executions? I wonder if dynamic optimisation/Graal‘s script compilation improves anything here.
The performance problem is unfortunately nothing new with JS-Joda — it definitely is not the fastest library. If you run into such performance bottlenecks, consider importing the Java counterparts. I doubt we can do anything here on the openhab-js side of things, but it would be nice if you could try the different installation methods of the library:
BTW, you don’t need to create your own logger, just use console log — it automatically uses a logger name based on your rule UID.
Injection caching does not change the behavior. It's the call to state.timestamp
which costs a lot of time.
state.timestamp
internally uses
to convert the Java ZDT to a JS-Joda ZDT.
I have benchmarked this method when it was initially added in #267, just repeated the benchmark with 10000 iterations:
21:55:08.803 [INFO ] [enhab.automation.script.ui.scratchpad] - Benchmarking Instant.parse conversion ...
21:55:15.110 [INFO ] [enhab.automation.script.ui.scratchpad] - Benchmark finshed, took 6306 ms
21:55:15.111 [INFO ] [enhab.automation.script.ui.scratchpad] - Benchmarking advanced Instant conversion ...
21:55:15.170 [INFO ] [enhab.automation.script.ui.scratchpad] - Benchmark finshed, took 59 ms
21:55:15.171 [INFO ] [enhab.automation.script.ui.scratchpad] - Benchmarking ZonedDateTime.parse conversion ...
21:55:33.975 [INFO ] [enhab.automation.script.ui.scratchpad] - Benchmark finshed, took 18804 ms
21:55:33.977 [INFO ] [enhab.automation.script.ui.scratchpad] - Benchmarking advanced ZonedDateTime conversion ...
21:55:35.151 [INFO ] [enhab.automation.script.ui.scratchpad] - Benchmark finshed, took 1172 ms
Now the question is, how many datapoints do you handle? Anyway, seems like it would be better to work with Instant, and IIRC core supports that since a few days or weeks.
I am handling between 80.000 and 160.000 datapoints. As you can read on the linked discussion, I tried with Java imports. This accelerates it by a factor of 14.
I will add support for getting the instant from the PersistedItem, Java to JS Instant conversion is much faster:
2024-11-04 22:17:48.018 [INFO ] [nhab.automation.script.ui.scratchpad] - Running benchmarks for 1477 states ---------------------------------------------------
2024-11-04 22:17:48.021 [INFO ] [nhab.automation.script.ui.scratchpad] - Benchmarking Java getTimestamp() ...
2024-11-04 22:17:48.038 [INFO ] [nhab.automation.script.ui.scratchpad] - Benchmark finshed, took 17 ms
2024-11-04 22:17:48.043 [INFO ] [nhab.automation.script.ui.scratchpad] - Benchmarking Java getTimestamp() converted to JS-Joda ...
2024-11-04 22:17:49.008 [INFO ] [nhab.automation.script.ui.scratchpad] - Benchmark finshed, took 965 ms
2024-11-04 22:17:49.010 [INFO ] [nhab.automation.script.ui.scratchpad] - Benchmarking Java getInstant() ...
2024-11-04 22:17:49.019 [INFO ] [nhab.automation.script.ui.scratchpad] - Benchmark finshed, took 10 ms
2024-11-04 22:17:49.021 [INFO ] [nhab.automation.script.ui.scratchpad] - Benchmarking Java getInstant() converted to JS-Joda ...
2024-11-04 22:17:49.103 [INFO ] [nhab.automation.script.ui.scratchpad] - Benchmark finshed, took 82 ms
These benchmarks work on the raw Java Persistence Extensions as I need access to the raw Java stuff which is not exposed by the library, as you can see the Java ZDT to JS ZDT conversion is what is time consuming.
That's what I suspected, since I read that js-joda has performance issues in general.
So you would offer state.instant
in addition to state.timestamp
?
Here's also a feature request for providing an implementation which would solve my requirement. Maybe think about exposing a persistence function in openhab-js like item.persistence.riemannSumBetween()
. This would make such calculations obsolete.
openhab-js ItemPersistence
class wraps anything that https://www.openhab.org/javadoc/latest/org/openhab/core/persistence/extensions/persistenceextensions provides.
So if it gets added there, I will provide a JS wrapper for it.
I'm using unstable builds. Thanks!
Expected Behavior
When using ZonedDateTime wrappers via js-joda in ECMAScript (Rules and Scripts), I would expect a similar performance as with all other properties.
Current Behavior
Looping over a large set of states and using js-joda wrappers within the loop, the loop gets extremely slow. Imagine this example:
The output with a state every 5 seconds leads to more than 14 seconds, whereas the following function costs 0.5 seconds.
Context
I'm trying to get a "Riemann Sum" implementation of measured power. See discussion here:
Arithmetic mean vs average linear/step interpolation
Your Environment