From 1453c4015c71f2fde3971f4632348cc92048fb86 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 8 Jan 2024 15:22:16 +0000 Subject: [PATCH] Show timings for index page data gathering --- agenda/data.py | 63 ++++++++++++++++++++++++++++++-------------- templates/index.html | 6 +++++ 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/agenda/data.py b/agenda/data.py index a75a848..536e5e8 100644 --- a/agenda/data.py +++ b/agenda/data.py @@ -6,6 +6,7 @@ import itertools import os import typing from datetime import date, datetime, timedelta +from time import time import dateutil.rrule import dateutil.tz @@ -353,6 +354,19 @@ def busy_event(e: Event) -> bool: return "rebels" not in lc_title and "south west data social" not in lc_title +async def time_function( + name: str, + func: typing.Callable[..., typing.Coroutine[typing.Any, typing.Any, typing.Any]], + *args, + **kwargs, +) -> tuple[str, typing.Any, float]: + """Time the execution of an asynchronous function.""" + start_time = time() + result = await func(*args, **kwargs) + end_time = time() + return name, result, end_time - start_time + + async def get_data( now: datetime, config: flask.config.Config ) -> typing.Mapping[str, str | object]: @@ -369,28 +383,37 @@ async def get_data( minus_365 = now - timedelta(days=365) plus_365 = now + timedelta(days=365) - ( - gbpusd, - gwr_advance_tickets, - bank_holiday, - rockets, - backwell_bins, - bristol_bins, - ) = await asyncio.gather( - fx.get_gbpusd(config), - gwr.advance_ticket_date(data_dir), - uk_holiday.bank_holiday_list(last_year, next_year, data_dir), - thespacedevs.get_launches(rocket_dir, limit=40), - waste_collection_events(data_dir), - bristol_waste_collection_events(data_dir, today), + t0 = time() + result_list = await asyncio.gather( + time_function("gbpusd", fx.get_gbpusd, config), + time_function("gwr_advance_tickets", gwr.advance_ticket_date, data_dir), + time_function( + "bank_holiday", uk_holiday.bank_holiday_list, last_year, next_year, data_dir + ), + time_function("rockets", thespacedevs.get_launches, rocket_dir, limit=40), + time_function("backwell_bins", waste_collection_events, data_dir), + time_function("bristol_bins", bristol_waste_collection_events, data_dir, today), ) + results = {call[0]: call[1] for call in result_list} + + gwr_advance_tickets = results["gwr_advance_tickets"] + + data_gather_seconds = time() - t0 + t0 = time() + + stock_market_times = stock_market.open_and_close() + stock_market_times_seconds = time() - t0 + reply: dict[str, typing.Any] = { "now": now, - "gbpusd": gbpusd, - "stock_markets": stock_market.open_and_close(), - "rockets": rockets, + "gbpusd": results["gbpusd"], + "stock_markets": stock_market_times, + "rockets": results["rockets"], "gwr_advance_tickets": gwr_advance_tickets, + "data_gather_seconds": data_gather_seconds, + "stock_market_times_seconds": stock_market_times_seconds, + "timings": [(call[0], call[2]) for call in result_list], } my_data = config["PERSONAL_DATA"] @@ -409,7 +432,7 @@ async def get_data( us_hols = us_holidays(last_year, next_year) - holidays: list[Holiday] = bank_holiday + us_hols + holidays: list[Holiday] = results["bank_holiday"] + us_hols for country in ( "at", "be", @@ -441,7 +464,7 @@ async def get_data( events += accommodation_events events += travel.all_events(my_data) events += conference.get_list(os.path.join(my_data, "conferences.yaml")) - events += backwell_bins + bristol_bins + events += results["backwell_bins"] + results["bristol_bins"] events += read_events_yaml(my_data, last_year, next_year) events += subscription.get_events(os.path.join(my_data, "subscriptions.yaml")) events += economist.publication_dates(last_week, next_year) @@ -464,7 +487,7 @@ async def get_data( for market in overlapping_markets: events.remove(market) - for launch in rockets: + for launch in results["rockets"]: dt = None if launch["net_precision"] == "Day": diff --git a/templates/index.html b/templates/index.html index 5dcf8c3..818ec90 100644 --- a/templates/index.html +++ b/templates/index.html @@ -125,6 +125,12 @@
  • Bristol Sunrise: {{ sunrise.strftime("%H:%M:%S") }} / Sunset: {{ sunset.strftime("%H:%M:%S") }}
  • +
  • Data gather took {{ "%.1f" | format(data_gather_seconds) }} seconds
  • +
  • Stock market open/close took + {{ "%.1f" | format(stock_market_times_seconds) }} seconds
  • + {% for name, seconds in timings %} +
  • {{ name }} took {{ "%.1f" | format(seconds) }} seconds
  • + {% endfor %}

    Stock markets