Skip to content

Commit

Permalink
Add a CLI to get the stored build durations
Browse files Browse the repository at this point in the history
Signed-off-by: Aurélien Bompard <[email protected]>
  • Loading branch information
abompard committed Apr 24, 2023
1 parent 43fd2ea commit 8ea0ff8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions fmn/cache/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ def __init__(self):
async def compute_value(self, db: "AsyncSession"):
log.debug(f"Building the {self.name} cache")
before = monotonic()
now = datetime.utcnow().isoformat()
now = datetime.utcnow().replace(microsecond=0).isoformat()
value = await self._compute_value(db=db)
after = monotonic()
duration = after - before
log.debug(f"Built the {self.name} cache in %.2f seconds", duration)
await cache.set(key=f"duration:{self.name}:{now}", value=duration, expire="31d")
await cache.set(key=f"duration:{self.name}:{now}", value=int(duration), expire="31d")
return value

async def _compute_value(self, db: "AsyncSession"):
Expand Down
12 changes: 12 additions & 0 deletions fmn/cache/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,15 @@ async def _doit():
click.echo(f"The {cache_value.name} cache is recent enough.")

asyncio.run(_doit())


@cache_cmd.command("get-build-durations")
def get_build_durations():
async def _do_it():
configure_cache()
async for key in cache.scan("duration:*"):
duration = await cache.get(key)
_, name, when = key.split(":", 2)
click.echo(f"Built {name} on {when} in {duration:.02f}s")

asyncio.run(_do_it())
18 changes: 18 additions & 0 deletions tests/cache/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,21 @@ async def _set_cache():

assert result.exit_code == 0, result.output
assert result.output == "Deleted lock for foo\nDeleted lock for bar\nCache locks deleted.\n"


@pytest.mark.cashews_cache(enabled=True)
def test_get_build_durations(mocker, cli_runner):
async def _set_cache():
await cache.set("duration:foo:2022-01-01T01:01:01", value=42.42424242)
await cache.set("duration:bar:2023-02-01T01:01:01", value=12.345678)

mocker.patch("fmn.cache.cli.configure_cache")
asyncio.run(_set_cache())
result = cli_runner.invoke(cli, ["cache", "get-build-durations"])

assert result.exit_code == 0, result.output
expected_msg = (
"Built foo on 2022-01-01T01:01:01 in 42.42s\n"
"Built bar on 2023-02-01T01:01:01 in 12.35s\n"
)
assert result.output == expected_msg

0 comments on commit 8ea0ff8

Please sign in to comment.