From b1e78aacaed22d27ca72ea6419b313796463171e Mon Sep 17 00:00:00 2001 From: lideming Date: Wed, 29 Nov 2023 15:51:00 +0800 Subject: [PATCH] perf: logs batch render --- neetbox/frontend/src/services/projects.ts | 20 ++++++-- tests/client/snake.py | 56 ++++++++++------------- tests/client/test.py | 8 ++++ 3 files changed, 48 insertions(+), 36 deletions(-) diff --git a/neetbox/frontend/src/services/projects.ts b/neetbox/frontend/src/services/projects.ts index b5d763d3..5c771af6 100644 --- a/neetbox/frontend/src/services/projects.ts +++ b/neetbox/frontend/src/services/projects.ts @@ -87,13 +87,23 @@ export class Project { }); const projectData = { ...this.status.value }; projectData.current = data; - projectData.history = slideWindow(projectData.history, data, 70); + projectData.history = slideWindow(projectData.history, [data], 70); this.status.value = projectData; }); } + private _logQueue: LogData[] | null = null; + private _logFlush = () => { + this.logs.value = slideWindow(this.logs.value, this._logQueue!, 1000); // TODO + this._logQueue = null; + }; + handleLog(log: LogData) { - this.logs.value = slideWindow(this.logs.value, log, 200); // TODO + if (!this._logQueue) { + this._logQueue = []; + setTimeout(this._logFlush, 60); + } + this._logQueue.push(log); } sendAction(action: string, args: Record, onReply?: (result: any) => void) { @@ -140,8 +150,8 @@ export function startBackgroundTasks() { }; } -function slideWindow(arr: T[], item: T, max: number) { - arr = arr.slice(arr.length + 1 > max ? arr.length + 1 - max : 0); - arr.push(item); +function slideWindow(arr: T[], items: T[], max: number) { + arr = arr.slice(arr.length + items.length > max ? arr.length + items.length - max : 0); + arr.push(...items); return arr; } diff --git a/tests/client/snake.py b/tests/client/snake.py index 268948d3..df8025b8 100644 --- a/tests/client/snake.py +++ b/tests/client/snake.py @@ -13,15 +13,13 @@ MAP_WIDTH, MAP_HEIGHT = 20, 10 -cur_direction = (1, 0) - game_running = False # logger.info("starting") @action(blocking=False) -def start_game(): +def snake_game(): global game_running if game_running: logger.err("game already running") @@ -34,36 +32,32 @@ def start_game(): game_running = False -@action() -def left(): - global cur_direction - cur_direction = (-1, 0) - - -@action() -def down(): - global cur_direction - cur_direction = (0, 1) - - -@action() -def up(): - global cur_direction - cur_direction = (0, -1) - - -@action() -def right(): - global cur_direction - cur_direction = (1, 0) - - def game(): - global cur_direction cur_direction = (1, 0) gamemap = [[EMPTY for _ in range(MAP_WIDTH)] for _ in range(MAP_HEIGHT)] bodies = [] cur_head = 3, 3 + cur_direction = (1, 0) + + @action() + def left(): + nonlocal cur_direction + cur_direction = (-1, 0) + + @action() + def down(): + nonlocal cur_direction + cur_direction = (0, 1) + + @action() + def up(): + nonlocal cur_direction + cur_direction = (0, -1) + + @action() + def right(): + nonlocal cur_direction + cur_direction = (1, 0) def main(): logger.info("===================snake game===================") @@ -124,6 +118,6 @@ def print_map(): main() -while True: - sleep(1000) - game_running = True +if __name__ == "__main__": + while True: + sleep(1000) diff --git a/tests/client/test.py b/tests/client/test.py index c5e9dd4d..48d119a7 100644 --- a/tests/client/test.py +++ b/tests/client/test.py @@ -7,6 +7,8 @@ from neetbox.logging import logger from neetbox.pipeline import listen, watch +import snake + @watch("train", initiative=True) def train(epoch): @@ -27,6 +29,12 @@ def log_with_some_prefix(): logger.warn("some warn") logger.err("some error") +@action() +def log_perf_test(interval: int, count: int): + for i in range(count): + sleep(interval) + logger.info(f'log_perf_test {i + 1}/{count}') + @action(name="action-1") def action_1(text: str):