def test_performance(self): print('\n' + ('-' * 80)) print('react-render performance test') print('-' * 80) render_component_times = [] render_watched_component_times = [] rendered_components = [] iteration_count = 25 for i in range(iteration_count): start = time.time() rendered_components.append( render_component(path_to_component, props={'name': 'world'}, to_static_markup=True)) end = time.time() render_component_times.append(end - start) for i in range(iteration_count): start = time.time() rendered_components.append( render_component(path_to_component, props={'name': 'world'}, to_static_markup=True)) end = time.time() render_watched_component_times.append(end - start) for component in rendered_components: self.assertEqual(str(component), '<span>Hello world</span>') print( 'Total time taken to render a component {iteration_count} times: {value}' .format(iteration_count=iteration_count, value=sum(render_component_times))) print('Times: {value}'.format(value=render_component_times)) print('Max: {value}'.format(value=max(render_component_times))) print('Min: {value}'.format(value=min(render_component_times))) print('Mean: {value}'.format(value=sum(render_component_times) / len(render_component_times))) print('Median: {value}'.format(value=median(render_component_times))) print( '\nTotal time taken to render a watched component {iteration_count} times: {value}' .format(iteration_count=iteration_count, value=sum(render_watched_component_times))) print('Times: {value}'.format(value=render_watched_component_times)) print('Max: {value}'.format(value=max(render_watched_component_times))) print('Min: {value}'.format(value=min(render_watched_component_times))) print( 'Mean: {value}'.format(value=sum(render_watched_component_times) / len(render_watched_component_times))) print('Median: {value}'.format( value=median(render_watched_component_times)))
def test_performance(self): print('\n' + ('-' * 80)) print('react-render performance test') print('-' * 80) render_component_times = [] render_watched_component_times = [] rendered_components = [] iteration_count = 25 for i in range(iteration_count): start = time.time() rendered_components.append( render_component(path_to_component, props={'name': 'world'}, to_static_markup=True) ) end = time.time() render_component_times.append(end - start) for i in range(iteration_count): start = time.time() rendered_components.append( render_component(path_to_component, props={'name': 'world'}, to_static_markup=True) ) end = time.time() render_watched_component_times.append(end - start) for component in rendered_components: self.assertEqual(str(component), '<span>Hello world</span>') print('Total time taken to render a component {iteration_count} times: {value}'.format( iteration_count=iteration_count, value=sum(render_component_times) )) print('Times: {value}'.format(value=render_component_times)) print('Max: {value}'.format(value=max(render_component_times))) print('Min: {value}'.format(value=min(render_component_times))) print('Mean: {value}'.format(value=sum(render_component_times) / len(render_component_times))) print('Median: {value}'.format(value=median(render_component_times))) print('\nTotal time taken to render a watched component {iteration_count} times: {value}'.format( iteration_count=iteration_count, value=sum(render_watched_component_times) )) print('Times: {value}'.format(value=render_watched_component_times)) print('Max: {value}'.format(value=max(render_watched_component_times))) print('Min: {value}'.format(value=min(render_watched_component_times))) print('Mean: {value}'.format(value=sum(render_watched_component_times) / len(render_watched_component_times))) print('Median: {value}'.format(value=median(render_watched_component_times)))
def index(request): # Param flag to deactivate client-side Javascript no_js = 'no-js' in request.GET # Render the CommentBox component down to HTML comment_box = render_component( # The path to the component is resolved via Django's # static-file finders 'js/main.server.js', # We can pass data along to the component which will be # accessible from the component via its `this.props` property props={ 'comments': comments, 'url': reverse('comment'), 'pollInterval': 2000, }, # If we intend to use React on the client-side, React will # add extra attributes to the HTML so that the initial mount # is faster, however these extra attributes are unnecessary # if there is no JS on the client-side. to_static_markup=no_js ) context = { 'comment_box': comment_box, 'no_js': no_js, } return render(request, 'example_app/index.html', context)
def index(request): # Properties to pass into react component props = { 'products': [model_to_dict(m) for m in PRODUCTS] } context = { # Render the CommentBox component down to HTML 'react_component': render_component('js/server/Component.js', props), } return render(request, 'index.html', context)