Skip to content

Instantly share code, notes, and snippets.

@JonnyWong16
Last active August 23, 2021 05:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonnyWong16/7ee64bd8edf31bb277cf617d57c164a6 to your computer and use it in GitHub Desktop.
Save JonnyWong16/7ee64bd8edf31bb277cf617d57c164a6 to your computer and use it in GitHub Desktop.
Test Plex API performance
from plexapi.server import PlexServer
import time
SERVER_URL = 'http://localhost:32400'
SERVER_TOKEN = 'xxxxxxxxxxxxxxxxxxxx'
LIBRARY_NAME = 'Movies'
plex = PlexServer(SERVER_URL, token=SERVER_TOKEN)
movies = plex.library.section(LIBRARY_NAME)
totalItems = movies.totalSize
librarySectionID = movies.key
print(f'Total items: {totalItems}')
print('/library/sections/<librarySectionID>/all')
startTime = time.process_time()
results = plex.fetchItems(f'/library/sections/{librarySectionID}/all')
elapsedTime = time.process_time() - startTime
print(f'{elapsedTime:.2f} seconds')
allRatingKeys = [str(m.ratingKey) for m in results]
print('/library/metadata/<commaSeparatedRatingKeys>')
commaSeparatedRatingKeys = ','.join(allRatingKeys)
startTime = time.process_time()
results = plex.fetchItems(f'/library/metadata/{commaSeparatedRatingKeys}')
elapsedTime = time.process_time() - startTime
print(f'{elapsedTime:.2f} seconds')
step = 50
print('/library/sections/<librarySectionID>/all with pagination')
results = []
startTime = time.process_time()
for start in range(0, totalItems, step):
results.extend(plex.fetchItems(f'/library/sections/{librarySectionID}/all?X-Plex-Container-Start={start}&X-Plex-Container-Size={step}'))
elapsedTime = time.process_time() - startTime
print(f'{elapsedTime:.2f} seconds')
print('/library/metadata/<commaSeparatedRatingKeys> with batched rating keys')
results = []
startTime = time.process_time()
for start in range(0, totalItems, step):
batchCommaSeparatedRatingKeys = ','.join(allRatingKeys[start:start + step])
results.extend(plex.fetchItems(f'/library/metadata/{batchCommaSeparatedRatingKeys}'))
elapsedTime = time.process_time() - startTime
print(f'{elapsedTime:.2f} seconds')
print('/library/metadata/<ratingKey> individually')
results = []
startTime = time.process_time()
for ratingKey in allRatingKeys:
results.extend(plex.fetchItems(f'/library/metadata/{ratingKey}'))
elapsedTime = time.process_time() - startTime
print(f'{elapsedTime:.2f} seconds')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment