-
-
Notifications
You must be signed in to change notification settings - Fork 465
Monitor rule execution duration #5358
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
Open
querdenker2k
wants to merge
8
commits into
openhab:main
Choose a base branch
from
querdenker2k:monitor-rule-execution-duration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b5814c1
add rule execution duration
querdenker2k 183640a
add description
querdenker2k e2700a9
do not use asterisk imports
querdenker2k 51b1dda
add test
querdenker2k 56e2e61
add test
querdenker2k c59b0cc
add test
querdenker2k aa14380
add test
querdenker2k 4a09b39
add test
querdenker2k File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
...io.monitor/src/test/java/org/openhab/core/io/monitor/internal/metrics/RuleMetricTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| /* | ||
| * Copyright (c) 2010-2026 Contributors to the openHAB project | ||
| * | ||
| * See the NOTICE file(s) distributed with this work for additional | ||
| * information. | ||
| * | ||
| * This program and the accompanying materials are made available under the | ||
| * terms of the Eclipse Public License 2.0 which is available at | ||
| * http://www.eclipse.org/legal/epl-2.0 | ||
| * | ||
| * SPDX-License-Identifier: EPL-2.0 | ||
| */ | ||
| package org.openhab.core.io.monitor.internal.metrics; | ||
|
|
||
| import static org.mockito.Mockito.mock; | ||
| import static org.openhab.core.io.monitor.internal.metrics.RuleMetric.*; | ||
|
|
||
| import java.time.Duration; | ||
| import java.util.List; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import org.eclipse.jdt.annotation.NonNullByDefault; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import org.openhab.core.automation.RuleRegistry; | ||
| import org.openhab.core.automation.RuleStatus; | ||
| import org.openhab.core.automation.RuleStatusInfo; | ||
| import org.openhab.core.automation.events.RuleStatusInfoEvent; | ||
| import org.osgi.framework.BundleContext; | ||
|
|
||
| import io.micrometer.core.instrument.Meter; | ||
| import io.micrometer.core.instrument.MockClock; | ||
| import io.micrometer.core.instrument.Timer; | ||
| import io.micrometer.core.instrument.simple.SimpleConfig; | ||
| import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
|
|
||
| /** | ||
| * Tests for RuleMetric class | ||
| * | ||
| * @author Robert Delbrück - Initial contribution | ||
| */ | ||
| @ExtendWith(MockitoExtension.class) | ||
| class RuleMetricTest { | ||
|
|
||
| public static final String RULE1 = "any"; | ||
| public static final String RULE2 = "anything-else"; | ||
|
|
||
| @Test | ||
| void testRuleExecution() { | ||
| MockClock clock = new MockClock(); | ||
| SimpleMeterRegistry meterRegistry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, clock); | ||
|
|
||
| RuleMetric ruleMetric = new RuleMetric(mock(BundleContext.class), List.of(), mock(RuleRegistry.class)); | ||
| ruleMetric.bindTo(meterRegistry); | ||
| fireRule(ruleMetric, clock, RULE1); | ||
| assertMeters(meterRegistry, 1); | ||
| assertMeter(meterRegistry, RULE1, 1, 1); | ||
|
|
||
| fireRule(ruleMetric, clock, RULE2); | ||
| assertMeters(meterRegistry, 2); | ||
| assertMeter(meterRegistry, RULE2, 1, 1); | ||
|
|
||
| // fire again, use the same metric | ||
| fireRule(ruleMetric, clock, RULE2); | ||
| assertMeters(meterRegistry, 2); | ||
| assertMeter(meterRegistry, RULE2, 2, 2); | ||
| } | ||
|
querdenker2k marked this conversation as resolved.
|
||
|
|
||
| private static void fireRule(RuleMetric ruleMetric, MockClock clock, String ruleName) { | ||
| ruleMetric.receive(new RuleStatusInfoEvent(RULES_TOPIC_PREFIX + ruleName + RULES_TOPIC_SUFFIX, | ||
| RuleStatus.RUNNING.name(), ruleName, mock(RuleStatusInfo.class), ruleName)); | ||
| clock.add(Duration.ofSeconds(1)); | ||
| ruleMetric.receive(new RuleStatusInfoEvent(RULES_TOPIC_PREFIX + ruleName + RULES_TOPIC_SUFFIX, | ||
| RuleStatus.IDLE.name(), ruleName, mock(RuleStatusInfo.class), ruleName)); | ||
|
querdenker2k marked this conversation as resolved.
|
||
| } | ||
|
querdenker2k marked this conversation as resolved.
|
||
|
|
||
| private static void assertMeter(SimpleMeterRegistry meterRegistry, String ruleName, int count, int totalTime) { | ||
| var meter = getMeters(meterRegistry).stream().filter(m -> ruleName.equals(m.getId().getTag("rule"))) | ||
| .map(m -> (Timer) m).findFirst().orElseThrow(); | ||
| Assertions.assertEquals(count, meter.count()); | ||
| Assertions.assertTrue(meter.totalTime(TimeUnit.SECONDS) >= totalTime); | ||
| } | ||
|
|
||
| private static void assertMeters(SimpleMeterRegistry meterRegistry, int size) { | ||
| List<Meter> durationMeters = getMeters(meterRegistry); | ||
| Assertions.assertEquals(size, durationMeters.size()); | ||
| } | ||
|
|
||
| private static @NonNullByDefault List<Meter> getMeters(SimpleMeterRegistry meterRegistry) { | ||
| List<Meter> meters = meterRegistry.getMeters(); | ||
| return meters.stream().filter(m -> m.getId().getName().equals(METRIC_DURATION_NAME)).toList(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.