Skip to content

Commit

Permalink
perf: logs batch render
Browse files Browse the repository at this point in the history
  • Loading branch information
lideming committed Nov 29, 2023
1 parent 7224c9f commit b1e78aa
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
20 changes: 15 additions & 5 deletions neetbox/frontend/src/services/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>, onReply?: (result: any) => void) {

Check warning on line 109 in neetbox/frontend/src/services/projects.ts

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
Expand Down Expand Up @@ -140,8 +150,8 @@ export function startBackgroundTasks() {
};
}

function slideWindow<T>(arr: T[], item: T, max: number) {
arr = arr.slice(arr.length + 1 > max ? arr.length + 1 - max : 0);
arr.push(item);
function slideWindow<T>(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;
}
56 changes: 25 additions & 31 deletions tests/client/snake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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===================")
Expand Down Expand Up @@ -124,6 +118,6 @@ def print_map():
main()


while True:
sleep(1000)
game_running = True
if __name__ == "__main__":
while True:
sleep(1000)
8 changes: 8 additions & 0 deletions tests/client/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from neetbox.logging import logger
from neetbox.pipeline import listen, watch

import snake


@watch("train", initiative=True)
def train(epoch):
Expand All @@ -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):
Expand Down

0 comments on commit b1e78aa

Please sign in to comment.