Skip to content

Commit

Permalink
Added performance tests.
Browse files Browse the repository at this point in the history
Call with `export PLONE_APP_REDIRECTOR_PERFORMANCE_NUMBER=100000` to enable.
  • Loading branch information
mauritsvanrees committed Feb 13, 2019
1 parent b754c1b commit 96cf70d
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/xxx.bugfix
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Support using the 'in' operator for paths.
Support using storage[old_path] to get the new path, possibly raising KeyError.
Support using storage[old_path] to set or delete new paths.
Support using len(storage) to get the number of paths.
Added performance tests. Call with ``export PLONE_APP_REDIRECTOR_PERFORMANCE_NUMBER=100000`` to enable.
[maurits]
85 changes: 85 additions & 0 deletions plone/app/redirector/tests/test_performance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-
from plone.app.redirector.storage import RedirectionStorage
from time import time

import os
import unittest


env_name = 'PLONE_APP_REDIRECTOR_PERFORMANCE_NUMBER'
NUMBER = int(os.getenv(env_name, 0))


class TestStoragePerformance(unittest.TestCase):
"""Test the performance of the RedirectionStorage class.
"""

@unittest.skipIf(NUMBER <= 0, '{0} env variable not set'.format(env_name))
def test_storage_performance(self):
"""Test the performance of some of the code.
This is skipped by default, unless you set an environment variable.
If you don't set this, or set this to zero or less, you will see
a skip reason when you run the tests with enough verbosity:
$ bin/test -s plone.app.redirector -m test_performance -vvv
Sample run with one hundred thousand inserts:
$ PLONE_APP_REDIRECTOR_PERFORMANCE_NUMBER=100000 \
bin/test -s plone.app.redirector -m test_performance
...
Running plone.app.redirector storage performance tests.
Inserting 100000 paths...
Inserting: 0.74 seconds
Getting length: 0.00 seconds
Getting iterator: 0.00 seconds
Listing all: 0.01 seconds
Listing and getting each single one: 0.16 seconds
"""
st = RedirectionStorage()

start_time = time()
print('\nRunning plone.app.redirector storage performance tests.')
print('Inserting {0} paths...'.format(NUMBER))
for i in range(NUMBER):
st['/old/{0}'.format(i)] = '/new/{0}'.format(i)
insert_time = time()
insert_seconds = insert_time - start_time
print('Inserting: {0:.2f} seconds'.format(insert_seconds))

# len
self.assertEqual(len(st), NUMBER)
length_time = time()
length_seconds = length_time - insert_time
print('Getting length: {0:.2f} seconds'.format(length_seconds))

# iter
iter(st)
iter_time = time()
iter_seconds = iter_time - length_time
print('Getting iterator: {0:.2f} seconds'.format(iter_seconds))

# list
list(st)
list_time = time()
list_seconds = list_time - iter_time
print('Listing all: {0:.2f} seconds'.format(list_seconds))

# list + get
for key in st:
st[key]
getall_time = time()
getall_seconds = getall_time - list_time
print(
'Listing and getting each single one: {0:.2f} seconds'.format(
getall_seconds
)
)


def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestStoragePerformance))
return suite

0 comments on commit 96cf70d

Please sign in to comment.