Skip to content

Commit 8aa45bf

Browse files
committed
Re-added the perf tests
1 parent 9092a13 commit 8aa45bf

File tree

2 files changed

+79
-68
lines changed

2 files changed

+79
-68
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,4 @@ docs/_build/
5454
target/
5555

5656
node_modules
57-
example/static
57+
tests/static_root

tests/test_performance.py

Lines changed: 78 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,78 @@
1-
# import os
2-
# import time
3-
# import unittest
4-
# from django_react.render import render_component
5-
#
6-
# path_to_component = os.path.abspath(os.path.join(os.path.dirname(__file__), 'components', 'PerfTestComponent.jsx'))
7-
#
8-
#
9-
# def median(l):
10-
# half = int(len(l) / 2)
11-
# l.sort()
12-
# if len(l) % 2 == 0:
13-
# return (l[half-1] + l[half]) / 2.0
14-
# else:
15-
# return l[half]
16-
#
17-
#
18-
# class TestDjangoReactPerformance(unittest.TestCase):
19-
# def test_performance(self):
20-
# print('\n' + ('-' * 80))
21-
# print('django-react performance test')
22-
# print('-' * 80)
23-
#
24-
# render_component_times = []
25-
# render_watched_component_times = []
26-
# rendered_components = []
27-
#
28-
# iteration_count = 25
29-
#
30-
# for i in range(iteration_count):
31-
# start = time.time()
32-
# rendered_components.append(
33-
# render_component(path_to_component, props={'name': 'world'}, to_static_markup=True)
34-
# )
35-
# end = time.time()
36-
# render_component_times.append(end - start)
37-
#
38-
# for i in range(iteration_count):
39-
# start = time.time()
40-
# rendered_components.append(
41-
# render_component(path_to_component, props={'name': 'world'}, watch_source=True, to_static_markup=True)
42-
# )
43-
# end = time.time()
44-
# render_watched_component_times.append(end - start)
45-
#
46-
# for component in rendered_components:
47-
# self.assertEqual(str(component), '<span>Hello world</span>')
48-
#
49-
# print('Total time taken to render a component {iteration_count} times: {value}'.format(
50-
# iteration_count=iteration_count,
51-
# value=sum(render_component_times)
52-
# ))
53-
# print('Times: {value}'.format(value=render_component_times))
54-
# print('Max: {value}'.format(value=max(render_component_times)))
55-
# print('Min: {value}'.format(value=min(render_component_times)))
56-
# print('Mean: {value}'.format(value=sum(render_component_times) / len(render_component_times)))
57-
# print('Median: {value}'.format(value=median(render_component_times)))
58-
#
59-
# print('\nTotal time taken to render a watched component {iteration_count} times: {value}'.format(
60-
# iteration_count=iteration_count,
61-
# value=sum(render_watched_component_times)
62-
# ))
63-
# print('Times: {value}'.format(value=render_watched_component_times))
64-
# print('Max: {value}'.format(value=max(render_watched_component_times)))
65-
# print('Min: {value}'.format(value=min(render_watched_component_times)))
66-
# print('Mean: {value}'.format(value=sum(render_watched_component_times) / len(render_watched_component_times)))
67-
# print('Median: {value}'.format(value=median(render_watched_component_times)))
1+
import os
2+
import time
3+
import unittest
4+
from django_react.render import render_component
5+
6+
path_to_component = os.path.abspath(os.path.join(os.path.dirname(__file__), 'components', 'PerfTestComponent.jsx'))
7+
8+
9+
def median(l):
10+
half = int(len(l) / 2)
11+
l.sort()
12+
if len(l) % 2 == 0:
13+
return (l[half-1] + l[half]) / 2.0
14+
else:
15+
return l[half]
16+
17+
18+
class TestDjangoReactPerformance(unittest.TestCase):
19+
def test_performance(self):
20+
print('\n' + ('-' * 80))
21+
print('django-react performance test')
22+
print('-' * 80)
23+
24+
render_component_times = []
25+
render_watched_component_times = []
26+
rendered_components = []
27+
28+
iteration_count = 25
29+
30+
for i in range(iteration_count):
31+
start = time.time()
32+
rendered_components.append(
33+
render_component(
34+
path_to_component,
35+
props={'name': 'world'},
36+
translate=True,
37+
to_static_markup=True
38+
)
39+
)
40+
end = time.time()
41+
render_component_times.append(end - start)
42+
43+
for i in range(iteration_count):
44+
start = time.time()
45+
rendered_components.append(
46+
render_component(
47+
path_to_component,
48+
props={'name': 'world'},
49+
translate=True,
50+
watch_source=True,
51+
to_static_markup=True
52+
)
53+
)
54+
end = time.time()
55+
render_watched_component_times.append(end - start)
56+
57+
for component in rendered_components:
58+
self.assertEqual(str(component), '<span>Hello world</span>')
59+
60+
print('Total time taken to render a component {iteration_count} times: {value}'.format(
61+
iteration_count=iteration_count,
62+
value=sum(render_component_times)
63+
))
64+
print('Times: {value}'.format(value=render_component_times))
65+
print('Max: {value}'.format(value=max(render_component_times)))
66+
print('Min: {value}'.format(value=min(render_component_times)))
67+
print('Mean: {value}'.format(value=sum(render_component_times) / len(render_component_times)))
68+
print('Median: {value}'.format(value=median(render_component_times)))
69+
70+
print('\nTotal time taken to render a watched component {iteration_count} times: {value}'.format(
71+
iteration_count=iteration_count,
72+
value=sum(render_watched_component_times)
73+
))
74+
print('Times: {value}'.format(value=render_watched_component_times))
75+
print('Max: {value}'.format(value=max(render_watched_component_times)))
76+
print('Min: {value}'.format(value=min(render_watched_component_times)))
77+
print('Mean: {value}'.format(value=sum(render_watched_component_times) / len(render_watched_component_times)))
78+
print('Median: {value}'.format(value=median(render_watched_component_times)))

0 commit comments

Comments
 (0)