Imperfect, but working perf profiling

This commit is contained in:
Jim Miller 2026-04-10 16:49:17 -05:00
parent 405c37aeb5
commit 08bae8d9be
4 changed files with 64 additions and 24 deletions

View file

@ -10,27 +10,6 @@ __docformat__ = 'restructuredtext en'
import fanficfare.six as six
from fanficfare.six import ensure_text, string_types, text_type as unicode
# from io import StringIO
# import cProfile, pstats
# from pstats import SortKey
# def do_cprofile(func):
# def profiled_func(*args, **kwargs):
# profile = cProfile.Profile()
# try:
# profile.enable()
# result = func(*args, **kwargs)
# profile.disable()
# return result
# finally:
# # profile.print_stats()
# s = StringIO()
# sortby = SortKey.CUMULATIVE
# ps = pstats.Stats(profile, stream=s).sort_stats(sortby)
# ps.print_stats(20)
# print(s.getvalue())
# return profiled_func
import logging
logger = logging.getLogger(__name__)
@ -91,6 +70,8 @@ from fanficfare.geturls import (
get_urls_from_page, get_urls_from_text,get_urls_from_imap,
get_urls_from_mime)
from fanficfare.fff_profile import do_cprofile
from calibre_plugins.fanficfare_plugin.fff_util import (
get_fff_adapter, get_fff_config, get_fff_personalini,
get_common_elements)
@ -1304,7 +1285,7 @@ class FanFicFarePlugin(InterfaceAction):
# let other exceptions percolate up.
return adapter.getStoryMetadataOnly(get_cover=False)
# @do_cprofile
@do_cprofile
def prep_download_loop(self,book,
options={'fileform':'epub',
'collision':ADDNEW,
@ -1900,7 +1881,7 @@ class FanFicFarePlugin(InterfaceAction):
else:
return None
# @do_cprofile
@do_cprofile
def update_books_loop(self,book,db=None,
options={'fileform':'epub',
'collision':ADDNEW,

View file

@ -24,12 +24,34 @@ try:
except NameError:
pass # load_translations() added in calibre 1.9
from io import StringIO
import cProfile, pstats
from pstats import SortKey
def do_cprofile(func):
def profiled_func(*args, **kwargs):
profile = cProfile.Profile()
try:
profile.enable()
result = func(*args, **kwargs)
profile.disable()
return result
finally:
# profile.print_stats()
s = StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(profile, stream=s).sort_stats(sortby)
ps.print_stats(20)
print(s.getvalue())
return profiled_func
# ------------------------------------------------------------------------------
#
# Functions to perform downloads using worker jobs
#
# ------------------------------------------------------------------------------
@do_cprofile
def do_download_worker_single(site,
book_list,
options,

View file

@ -27,7 +27,6 @@ import pprint
import string
import os, sys, platform
version="4.56.1"
os.environ['CURRENT_VERSION_ID']=version
@ -51,6 +50,8 @@ from fanficfare.geturls import get_urls_from_page, get_urls_from_imap
from fanficfare.six.moves import configparser
from fanficfare.six import text_type as unicode
from fanficfare.fff_profile import do_cprofile
def write_story(config, adapter, writeformat,
metaonly=False, nooutput=False,
outstream=None):
@ -346,6 +347,7 @@ def main(argv=None,
dispatch(options, urls, passed_defaultsini, passed_personalini, warn, fail)
# make rest a function and loop on it.
@do_cprofile
def do_download(arg,
options,
passed_defaultsini,

35
fanficfare/fff_profile.py Normal file
View file

@ -0,0 +1,35 @@
# Copyright 2026 FanFicFare team
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from io import StringIO
import cProfile, pstats
from pstats import SortKey
def do_cprofile(func):
def profiled_func(*args, **kwargs):
profile = cProfile.Profile()
try:
profile.enable()
result = func(*args, **kwargs)
profile.disable()
return result
finally:
# profile.print_stats()
s = StringIO()
sortby = SortKey.CUMULATIVE
ps = pstats.Stats(profile, stream=s).sort_stats(sortby)
ps.print_stats(20)
print(s.getvalue())
return profiled_func