-
Notifications
You must be signed in to change notification settings - Fork 122
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
test: add a small set of ops.testing benchmark tests #1504
Open
tonyandrewmeyer
wants to merge
10
commits into
canonical:main
Choose a base branch
from
tonyandrewmeyer:add-scenario-benchmark-tests
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.
+313
−3
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8c0ae3c
Add a small set of benchmark tests for Scenario.
tonyandrewmeyer 9f9b9f0
Merge branch 'main' into add-scenario-benchmark-tests
tonyandrewmeyer 82ca4e2
Explain the magic fixture.
tonyandrewmeyer b362f13
Move benchmark tests to a dedicated folder, like smoke.
tonyandrewmeyer 34d4d73
With the benchmark tests in a separate folder, no need to change pypr…
tonyandrewmeyer e81ced2
Typo
tonyandrewmeyer c89dbe1
Tweaks, per review.
tonyandrewmeyer f4d020e
Rename, per review.
tonyandrewmeyer bbcc730
Fix the storedstate check in full_state.
tonyandrewmeyer f9463b1
Style fix.
tonyandrewmeyer 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 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 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,24 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Benchmark tests for ops. | ||
|
||
Optimising performance is not a current goal with ops - any gains are | ||
unlikely to be significant compared with ones from Juju or the charm and | ||
its workload. However, we do want to ensure that we do not unknowingly | ||
regress in performance. | ||
|
||
This package is for tests that cover core functionality, to be used for | ||
performance benchmarking. | ||
""" |
This file contains 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,40 @@ | ||
name: benchmark | ||
type: charm | ||
title: ops-benchmark | ||
summary: A simple charm used for benchmark tests | ||
description: Read the summary. | ||
bases: | ||
- build-on: | ||
- name: ubuntu | ||
channel: "22.04" | ||
run-on: | ||
- name: ubuntu | ||
channel: "22.04" | ||
config: | ||
options: | ||
log-level: | ||
description: Configures the log level. | ||
default: "info" | ||
type: string | ||
actions: | ||
act: | ||
description: Do something to the workload. | ||
containers: | ||
foo: | ||
resources: | ||
baz: | ||
type: oci-image | ||
storage: | ||
bar: | ||
type: filesystem | ||
requires: | ||
rel: | ||
interface: qux | ||
peers: | ||
peer: | ||
interface: chat | ||
extra-bindings: | ||
MySpace: null | ||
parts: | ||
charm: | ||
charm-entrypoint: src/bcharm.py |
This file contains 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 @@ | ||
ops ~= 2.17 |
This file contains 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,43 @@ | ||
#!/usr/bin/env python3 | ||
# Copyright 2024 Canonical Ltd. | ||
# See LICENSE file for licensing details. | ||
|
||
"""Basic benchmarking charm. | ||
|
||
Note that this is named benchmark_charm rather than charm as is typical to | ||
avoid conflicts with ops.charm in the testing runs. | ||
""" | ||
|
||
import logging | ||
|
||
import ops | ||
|
||
logger = logging.getLogger('__name__') | ||
|
||
|
||
class BenchmarkCharm(ops.CharmBase): | ||
"""Charm the service.""" | ||
|
||
_stored = ops.StoredState() | ||
|
||
def __init__(self, framework: ops.Framework): | ||
super().__init__(framework) | ||
framework.observe(self.on.update_status, self._on_update_status) | ||
framework.observe(self.on.stop, self._on_stop) | ||
framework.observe(self.on.config_changed, self._on_config_changed) | ||
|
||
def _on_update_status(self, _: ops.UpdateStatusEvent): | ||
# Say a bunch of things. | ||
for level in ('debug', 'info', 'warning', 'error'): | ||
for i in range(50): | ||
getattr(logger, level)('This is message %s', i) | ||
|
||
def _on_stop(self, _: ops.StopEvent): | ||
"""Do nothing - this exists to benchmark having an observer.""" | ||
|
||
def _on_config_changed(self, event: ops.ConfigChangedEvent): | ||
event.defer() | ||
|
||
|
||
if __name__ == '__main__': # pragma: nocover | ||
ops.main(BenchmarkCharm) |
This file contains 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,22 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Benchmark tests for ops-scenario. | ||
|
||
Optimising performance is not a current goal with ops-scenario. However, | ||
we do want to ensure that we do not unknowingly regress in performance. | ||
|
||
This package contains a small set of tests that cover core functionality, | ||
to be used for performance benchmarking. | ||
""" |
This file contains 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,154 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Benchmark tests for ops-scenario.""" | ||
|
||
import dataclasses | ||
import pathlib | ||
import sys | ||
|
||
import ops | ||
from ops import testing | ||
|
||
sys.path.append( | ||
str( | ||
pathlib.Path(__file__).parent.parent.parent.parent | ||
/ "test" | ||
/ "charms" | ||
/ "test_benchmark" | ||
/ "src" | ||
) | ||
) | ||
|
||
from benchmark_charm import BenchmarkCharm | ||
|
||
|
||
# Note: the 'benchmark' argument here is a fixture that pytest-benchmark | ||
# automatically makes available to all tests. | ||
def test_context_explicit_meta(benchmark): | ||
ctx = benchmark(testing.Context, ops.CharmBase, meta={"name": "foo"}) | ||
assert isinstance(ctx, testing.Context) | ||
|
||
|
||
def test_run_no_observer(benchmark): | ||
ctx = testing.Context(BenchmarkCharm) | ||
benchmark(ctx.run, ctx.on.start(), testing.State()) | ||
assert len({e.handle.kind for e in ctx.emitted_events}) == 1 | ||
|
||
|
||
def test_run_observed(benchmark): | ||
ctx = testing.Context(BenchmarkCharm) | ||
benchmark(ctx.run, ctx.on.stop(), testing.State()) | ||
assert len({e.handle.kind for e in ctx.emitted_events}) == 1 | ||
|
||
|
||
def test_context_explicit_meta_config_actions(benchmark): | ||
ctx = benchmark( | ||
testing.Context, | ||
ops.CharmBase, | ||
meta={"name": "foo"}, | ||
actions={"act": {"description": "foo"}}, | ||
config={"options": {"conf": {"type": "int", "description": "bar"}}}, | ||
) | ||
ctx.run(ctx.on.action("act"), testing.State(config={"conf": 10})) | ||
assert len({e.handle.kind for e in ctx.emitted_events}) == 1 | ||
|
||
|
||
def test_context_autoload_meta(benchmark): | ||
ctx = benchmark(testing.Context, BenchmarkCharm) | ||
assert isinstance(ctx, testing.Context) | ||
|
||
|
||
def test_many_tests_explicit_meta(benchmark): | ||
def mock_pytest(): | ||
"""Simulate running multiple tests against the same charm.""" | ||
for event in ("install", "start", "stop", "remove"): | ||
for _ in range(5): | ||
ctx = testing.Context(ops.CharmBase, meta={"name": "foo"}) | ||
ctx.run(getattr(ctx.on, event)(), testing.State()) | ||
assert len({e.handle.kind for e in ctx.emitted_events}) == 1 | ||
|
||
benchmark(mock_pytest) | ||
|
||
|
||
def test_many_tests_autoload_meta(benchmark): | ||
def mock_pytest(): | ||
"""Simulate running multiple tests against the same charm.""" | ||
for event in ("install", "start", "stop", "remove"): | ||
for _ in range(5): | ||
ctx = testing.Context(BenchmarkCharm) | ||
ctx.run(getattr(ctx.on, event)(), testing.State()) | ||
assert len({e.handle.kind for e in ctx.emitted_events}) == 1 | ||
|
||
benchmark(mock_pytest) | ||
|
||
|
||
def test_lots_of_logs(benchmark): | ||
ctx = testing.Context(BenchmarkCharm) | ||
benchmark(ctx.run, ctx.on.update_status(), testing.State()) | ||
assert len(ctx.juju_log) > 200 | ||
|
||
|
||
def test_full_state(benchmark): | ||
def fill_state(): | ||
rel = testing.Relation("rel") | ||
peer = testing.PeerRelation("peer") | ||
network = testing.Network("MySpace") | ||
container = testing.Container("foo") | ||
storage = testing.Storage("bar") | ||
tcp = testing.TCPPort(22) | ||
icmp = testing.ICMPPort() | ||
udp = testing.UDPPort(8000) | ||
secret = testing.Secret({"password": "admin"}) | ||
resource = testing.Resource(name="baz", path=".") | ||
stored_state = testing.StoredState(owner_path="BenchMarkCharm") | ||
state = testing.State( | ||
relations={rel, peer}, | ||
networks={network}, | ||
containers={container}, | ||
storages={storage}, | ||
opened_ports={tcp, icmp, udp}, | ||
secrets={secret}, | ||
resources={resource}, | ||
stored_states={stored_state}, | ||
app_status=testing.ActiveStatus(), | ||
unit_status=testing.BlockedStatus("I'm stuck!"), | ||
) | ||
return state | ||
|
||
ctx = testing.Context(BenchmarkCharm) | ||
state_in = benchmark(fill_state) | ||
state_out = ctx.run(ctx.on.start(), state_in) | ||
# stored_states is complicated: it will contain a stored state the | ||
# framework itself added (counting the number of events), so the | ||
# input and output state doesn't naively match. We strip that out and | ||
# compare it separately. | ||
state_in_dict = dataclasses.asdict(state_in) | ||
state_out_dict = dataclasses.asdict(state_out) | ||
# An owner_path of None means that it's owned by the framework. | ||
assert state_in_dict["stored_states"] == { | ||
ss for ss in state_out_dict["stored_states"] if ss.owner_path is not None | ||
} | ||
del state_in_dict["stored_states"] | ||
del state_out_dict["stored_states"] | ||
assert state_in_dict == state_out_dict | ||
|
||
|
||
def test_deferred_events(benchmark): | ||
ctx = testing.Context(BenchmarkCharm, capture_deferred_events=True) | ||
deferred = ctx.on.stop().deferred(BenchmarkCharm._on_stop) | ||
state_in = testing.State(deferred=[deferred]) | ||
state_out = benchmark(ctx.run, ctx.on.config_changed(), state_in) | ||
assert len(state_out.deferred) == 1 | ||
assert len({e.handle.kind for e in ctx.emitted_events}) == 2 |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use markers instead?
The magic paths are not very ergonomic, e.g. not easy to copy-paste into the terminal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's true, but we are using them extensively throughout tox.ini already. I feel like it's better to remain consistent.
I wouldn't have any objection to a separate PR that removed them entirely, though. I don't think they add much value, and using explicit paths (for e.g. PYTHONPATH) and markers (for e.g. skipping types of test) could be a nice improvement.