Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[items] ItemPersistence: Add PersistedItem::instant #396

Merged
merged 1 commit into from
Nov 4, 2024

Conversation

florian-h05
Copy link
Contributor

Refs openhab/openhab-core#4384.
Requires openHAB 4.3.0.M3 or newer.

Also provide access to the raw Java types wrapped by PersistedState and PersistedItem.

@florian-h05 florian-h05 requested a review from a team as a code owner November 4, 2024 21:42
@florian-h05
Copy link
Contributor Author

florian-h05 commented Nov 4, 2024

I did some benchmarking, as Java Instant to JS Instant is much faster than Java ZonedDateTime to JS ZonedDateTime,
it seems that calling PersistedItem::instant is by at least factor 10 faster for approx. 1400 states than PersistedState::timestamp:

Running benchmarks for 1477 states ...
Benchmarking Java getTimestamp() ...
Benchmark finshed, took 17 ms
Benchmarking Java getTimestamp() converted to JS-Joda ...
Benchmark finshed, took 965 ms
Benchmarking Java getInstant() ...
Benchmark finshed, took 10 ms
Benchmarking Java getInstant() converted to JS-Joda ...
Benchmark finshed, took 82 ms
Benchmarking Script
var PersistenceExtensions = Java.type('org.openhab.core.persistence.extensions.PersistenceExtensions');
var midnight = time.toZDT('2024-11-01T00:00:00');
var item = items.getItem('Aussentemperatur');
var start, end;

var states = PersistenceExtensions.getAllStatesSince(item.rawItem, midnight);
console.info(`Running benchmarks for ${states.length} states ---------------------------------------------------`)

start = time.Instant.now().toEpochMilli();
console.log('Benchmarking Java getTimestamp() ...')
for (var i = 0; i < states.length; i++) {
  var state = states[i];
  var ts = state.getTimestamp();
}
var end = time.Instant.now().toEpochMilli();
console.log('Benchmark finshed, took ' + (end - start) + ' ms');

start = time.Instant.now().toEpochMilli();
console.log('Benchmarking Java getTimestamp() converted to JS-Joda ...')
for (var i = 0; i < states.length; i++) {
  var state = states[i];
  var ts = time.javaZDTToJsZDT(state.getTimestamp());
}
var end = time.Instant.now().toEpochMilli();
console.log('Benchmark finshed, took ' + (end - start) + ' ms');

start = time.Instant.now().toEpochMilli();
console.log('Benchmarking Java getInstant() ...')
for (var i = 0; i < states.length; i++) {
  var state = states[i];
  var ts = state.getInstant();
}
var end = time.Instant.now().toEpochMilli();
console.log('Benchmark finshed, took ' + (end - start) + ' ms');

start = time.Instant.now().toEpochMilli();
console.log('Benchmarking Java getInstant() converted to JS-Joda ...')
for (var i = 0; i < states.length; i++) {
  var state = states[i];
  var ts = time.javaInstantToJsInstant(state.getInstant());
}
var end = time.Instant.now().toEpochMilli();
console.log('Benchmark finshed, took ' + (end - start) + ' ms');


states = item.persistence.getAllStatesSince(midnight);

start = time.Instant.now().toEpochMilli();
console.log('Benchmarking JS timestamp ...')
for (var i = 0; i < states.length; i++) {
  var state = states[i];
  var ts = state.timestamp;
}
var end = time.Instant.now().toEpochMilli();
console.log('Benchmark finshed, took ' + (end - start) + ' ms');

start = time.Instant.now().toEpochMilli();
console.log('Benchmarking JS instant ...')
for (var i = 0; i < states.length; i++) {
  var state = states[i];
  var ts = state.instant;
}
var end = time.Instant.now().toEpochMilli();
console.log('Benchmark finshed, took ' + (end - start) + ' ms');

@florian-h05 florian-h05 added the enhancement New feature or request label Nov 4, 2024
@florian-h05 florian-h05 added this to the to be released milestone Nov 4, 2024
@florian-h05 florian-h05 merged commit 1d6bbb4 into openhab:main Nov 4, 2024
4 checks passed
@florian-h05 florian-h05 deleted the itempersistence-instant branch November 4, 2024 21:49
florian-h05 pushed a commit that referenced this pull request Nov 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant