Skip to content

Commit

Permalink
feat: Support and test asynchronous calls
Browse files Browse the repository at this point in the history
  • Loading branch information
jannisborn committed Nov 23, 2024
1 parent 10d3968 commit c01d712
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
14 changes: 11 additions & 3 deletions paperscraper/citations/core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import logging
import re
import sys
Expand Down Expand Up @@ -40,19 +41,26 @@ async def self_references(

results: Dict[str, Dict[str, Union[float, int]]] = {}

tasks = []

for sample in inputs:
dois = re.findall(doi_pattern, sample, re.IGNORECASE)
if len(dois) == 1:
# This is a DOI
results[sample] = await self_references_paper(
dois[0], verbose=verbose, relative=relative
tasks.append(
(
sample,
self_references_paper(dois[0], verbose=verbose, relative=relative),
)
)
elif len(dois) == 0:
# TODO: Check that it is a proper name or an ORCID ID
raise NotImplementedError(
"Analyzing self-references of whole authors is not yet implemented."
)
# TODO: Collect all tasks and then run asynchronously
completed_tasks = await asyncio.gather(*[task[1] for task in tasks])
for sample, task_result in zip(tasks, completed_tasks):
results[sample[0]] = task_result

return results

Expand Down
29 changes: 29 additions & 0 deletions paperscraper/citations/tests/test_self_references.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import asyncio
import logging
import time

import pytest

Expand Down Expand Up @@ -53,3 +55,30 @@ def test_multiple_dois(self, dois):
def test_not_implemented_error(self):
with pytest.raises(NotImplementedError):
self_references("John Jumper")

def test_compare_async_and_sync_performance(self, dois):
"""
Compares the execution time of asynchronous and synchronous `self_references`
for a list of DOIs.
"""

start_time = time.perf_counter()
self_references(dois)
async_duration = time.perf_counter() - start_time

# Measure synchronous execution time (three independent calls)
start_time = time.perf_counter()
for doi in dois:
self_references(doi)
sync_duration = time.perf_counter() - start_time

print(f"Asynchronous execution time (batch): {async_duration:.2f} seconds")
print(
f"Synchronous execution time (independent calls): {sync_duration:.2f} seconds"
)

# Assert that async execution (batch) is faster or at least not slower
assert async_duration <= sync_duration, (
f"Async execution ({async_duration:.2f}s) is slower than sync execution "
f"({sync_duration:.2f}s)"
)

0 comments on commit c01d712

Please sign in to comment.