From 1b211f107dfbadef56d74526d0708b70ae42bce4 Mon Sep 17 00:00:00 2001 From: Lan Hui <1348141770@qq.com> Date: Fri, 11 Aug 2023 11:59:48 +0800 Subject: [PATCH] Speed up loading next article The key change is replacing "d1 = load_freq_history(path_prefix + 'static/frequency/ferquency.p)" with "d1 = load_freq_history(user_word_list)" in function get_today_article() from Article.py. Now, with a user_word_list of size about 500, the next article can be loaded within 100ms. The new d1 is much smaller than the old one, therefore the following computation "d3 = get_difficulty_level_for_user(d1, d2)" is much faster. The students did not feel that loading next article is slow; this is because their frequency.p is quite small. Also log information in app/log.txt --- .gitignore | 3 ++- app/Article.py | 10 ++++++++-- app/main.py | 3 --- app/user_service.py | 5 +++++ 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index c83d46b..39b4e3a 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ app/wordfreqapp.db app/db/wordfreqapp.db app/static/donate-the-author.jpg app/static/donate-the-author-hidden.jpg -app/model/__pycache__/ \ No newline at end of file +app/model/__pycache__/ +app/log.txt diff --git a/app/Article.py b/app/Article.py index c7e87d3..467224a 100644 --- a/app/Article.py +++ b/app/Article.py @@ -8,7 +8,7 @@ import hashlib from datetime import datetime from flask import Flask, request, redirect, render_template, url_for, session, abort, flash, get_flashed_messages from difficulty import get_difficulty_level_for_user, text_difficulty_level, user_difficulty_level - +import logging path_prefix = './' db_path_prefix = './db/' # comment this line in deployment @@ -51,14 +51,20 @@ def get_today_article(user_word_list, visited_articles): random.shuffle(result) # Choose article according to reader's level - d1 = load_freq_history(path_prefix + 'static/frequency/frequency.p') + logging.debug('* get_today_article(): start d1 = ... ') + d1 = load_freq_history(user_word_list) d2 = load_freq_history(path_prefix + 'static/words_and_tests.p') + logging.debug(' ... get_today_article(): get_difficulty_level_for_user() start') d3 = get_difficulty_level_for_user(d1, d2) + logging.debug(' ... get_today_article(): done') d = None result_of_generate_article = "not found" + d_user = load_freq_history(user_word_list) + logging.debug('* get_today_article(): user_difficulty_level() start') user_level = user_difficulty_level(d_user, d3) # more consideration as user's behaviour is dynamic. Time factor should be considered. + logging.debug('* get_today_article(): done') text_level = 0 if visited_articles["index"] > len(visited_articles["article_ids"])-1: # 生成新的文章 amount_of_visited_articles = len(visited_articles["article_ids"]) diff --git a/app/main.py b/app/main.py index f43b36b..19bd889 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,3 @@ -#! /usr/bin/python3 -# -*- coding: utf-8 -*- - ########################################################################### # Copyright 2019 (C) Hui Lan # Written permission must be obtained from the author for commercial uses. diff --git a/app/user_service.py b/app/user_service.py index 5d601e0..6c208dc 100644 --- a/app/user_service.py +++ b/app/user_service.py @@ -15,6 +15,9 @@ from wordfreqCMD import sort_in_descending_order import pickle_idea import pickle_idea2 +import logging +logging.basicConfig(filename='log.txt', format='%(asctime)s %(message)s', level=logging.DEBUG) + # 初始化蓝图 userService = Blueprint("user_bp", __name__) @@ -32,7 +35,9 @@ def get_next_article(username): else: # 当前不为“null”,直接 index+=1 visited_articles["index"] += 1 session["visited_articles"] = visited_articles + logging.debug('/get_next_article: start calling get_today_arcile()') visited_articles, today_article, result_of_generate_article = get_today_article(user_freq_record, session.get('visited_articles')) + logging.debug('/get_next_arcile: done.') data = { 'visited_articles': visited_articles, 'today_article': today_article,