Skip to content

Commit

Permalink
Use time.perf_counter instead of time.time in benchmarking
Browse files Browse the repository at this point in the history
  • Loading branch information
Itz-fork committed Jul 31, 2022
1 parent 1f85765 commit 54a5bdc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
6 changes: 3 additions & 3 deletions unzipper/client/pyro_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# ===================================================================== #

import logging
from time import time
from time import perf_counter
from asyncio import sleep
from typing import Callable
from os import path, remove, stat
Expand Down Expand Up @@ -106,7 +106,7 @@ async def send_file(self, c_id: int, doc_f: str, query: CallbackQuery, lang: str
return

tgupmsg = await self.send_message(c_id, STRINGS[lang]["processing"])
stm = time()
stm = perf_counter()

# Uplaod type: Video
if cum == "video":
Expand All @@ -131,7 +131,7 @@ async def send_file(self, c_id: int, doc_f: str, query: CallbackQuery, lang: str
thumb=sthumb,
progress=progress_for_pyrogram,
progress_args=("**Trying to upload 😇** \n", tgupmsg, stm))
etm = time()
etm = perf_counter()

# Delete or edit the progress message
await tgupmsg.delete() if del_status else await tgupmsg.edit(STRINGS[lang]["ok_upload"].format(path.basename(doc_f), TimeFormatter(round(etm - stm))))
Expand Down
4 changes: 2 additions & 2 deletions unzipper/helpers_nexa/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# Credits: SpEcHiDe's AnyDL-Bot for progress_for_pyrogram, humanbytes and TimeFormatter

from re import sub
from time import time
from time import perf_counter
from math import floor
from json import loads
from os import path, walk
Expand All @@ -23,7 +23,7 @@


async def progress_for_pyrogram(current, total, ud_type, message, start):
now = time()
now = perf_counter()
diff = now - start
speed = current / diff
if total:
Expand Down
4 changes: 2 additions & 2 deletions unzipper/lib/downloader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# ===================================================================== #

from re import match
from time import time
from time import perf_counter
from config import Config
from aiohttp import ClientSession
from pyrogram.types import Message
Expand Down Expand Up @@ -70,7 +70,7 @@ async def from_direct_link(self, url: str, path: str, message: Message = None, c
if total and int(total) > Config.MAX_DOWNLOAD_SIZE:
raise FileTooLarge
curr = 0
st = time()
st = perf_counter()
async with openfile(path, mode="wb") as file:
async for chunk in resp.content.iter_chunked(Config.CHUNK_SIZE):
# Raise FileTooLarge if the content size exceeds Config.MAX_DOWNLOAD_SIZE
Expand Down
18 changes: 9 additions & 9 deletions unzipper/modules/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# ===================================================================== #

import logging
from time import time
from time import perf_counter
from shutil import rmtree
from os import makedirs, path

Expand Down Expand Up @@ -82,10 +82,10 @@ async def unzipper_cb(_, query: CallbackQuery, texts):
makedirs(download_path)
# Send logs
await unzip_client.send_message(Config.LOGS_CHANNEL, texts["log"].format(user_id, url, fsize))
s_time = time()
s_time = perf_counter()
arc_name = f"{download_path}/archive_from_{user_id}_{path.basename(url)}"
await Downloader().from_direct_link(url, arc_name, query.message)
e_time = time()
e_time = perf_counter()

elif splitted_data[1] == "tg_file":
rdoc = r_message.document
Expand All @@ -94,14 +94,14 @@ async def unzipper_cb(_, query: CallbackQuery, texts):
# Send Logs
log_msg = await r_message.forward(Config.LOGS_CHANNEL)
await log_msg.reply(texts["log"].format(user_id, rdoc.file_name, humanbytes(rdoc.file_size)))
s_time = time()
s_time = perf_counter()
arc_name = f"{download_path}/archive_from_{user_id}_{rdoc.file_name}"
await r_message.download(
file_name=arc_name,
progress=progress_for_pyrogram, progress_args=(
"**Trying to Download!** \n", query.message, s_time)
)
e_time = time()
e_time = perf_counter()

else:
return await unzip_client.answer_query(query, "Can't Find Details! Please contact support group!", answer_only=True)
Expand All @@ -122,13 +122,13 @@ async def unzipper_cb(_, query: CallbackQuery, texts):
exter = Extractor()
if splitted_data[2] == "with_pass":
password = (await unzip_client.ask(query.message.chat.id, texts["ask_password"])).text
ext_s_time = time()
ext_s_time = perf_counter()
await exter.extract(arc_name, ext_files_dir, password)
ext_e_time = time()
ext_e_time = perf_counter()
else:
ext_s_time = time()
ext_s_time = perf_counter()
await exter.extract(arc_name, ext_files_dir)
ext_e_time = time()
ext_e_time = perf_counter()

await unzip_client.answer_query(query, texts["ok_extract"].format(TimeFormatter(round(ext_e_time-ext_s_time) * 1000)))

Expand Down
10 changes: 5 additions & 5 deletions unzipper/modules/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# ===================================================================== #

from re import match
from time import time
from time import perf_counter
from os import path, remove

from config import Config
Expand Down Expand Up @@ -48,7 +48,7 @@ async def extract_dis_archive(_, message: Message, texts):
if path.isfile(arc_name):
return await unzip_msg.edit(texts["alert_part_exists"])
# Download the file
s_time = time()
s_time = perf_counter()
if is_url:
async with ClientSession() as ses:
cleng = (await ses.head(message.text)).headers.get("Content-Length")
Expand All @@ -64,7 +64,7 @@ async def extract_dis_archive(_, message: Message, texts):
progress=progress_for_pyrogram, progress_args=(
"**Trying to Download!** \n", unzip_msg, s_time)
)
e_time = time()
e_time = perf_counter()
await unzip_msg.edit(texts["ok_download"].format(file_name, TimeFormatter(round(e_time-s_time) * 1000)))
return

Expand Down Expand Up @@ -97,11 +97,11 @@ async def extracted_dis_spl_archive(_, message: Message, texts):
await del_split_arc_user(user_id)
# Extract the archive
ext = Extractor()
s_time = time()
s_time = perf_counter()
await ext.extract(lfn, ext_path, ps, True)
extdarc = f"{ext_path}/{path.splitext(path.basename(lfn))[0]}"
await ext.extract(extdarc, ext_path, ps)
e_time = time()
e_time = perf_counter()
await spl_umsg.edit(texts["ok_extract"].format(TimeFormatter(round(e_time-s_time) * 1000)))
# Try to remove merged archive
try:
Expand Down

0 comments on commit 54a5bdc

Please sign in to comment.