Skip to content

Commit

Permalink
Adding standard deviation for all load times
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin Crown committed Mar 28, 2015
1 parent 80db8bd commit 17ae18c
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 7 deletions.
36 changes: 36 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,42 @@ file (see example above) with `har_data=har_data`::
# We could do this with 'css', 'js', 'html', 'audio', or 'video'


MultiHarParser
+++++++++

The ``MutliHarParser`` takes a ``list`` of ``dict``, each of which represents the JSON
of a full HAR file. The concept here is that you can provide multiple HAR files of the
same page (representing multiple test runs) and the ``MultiHarParser`` will provide
aggregate results for load times::

import json
from haralyzer import HarParser, HarPage

test_runs = []
with open('har_data1.har', 'r') as f1:
test_runs.append( (json.loads( f1.read() ) )
with open('har_data2.har', 'r') as f2:
test_runs.append( (json.loads( f2.read() ) )

multi_har_parser = MultiHarParser(har_data=test_runs)

# Get the mean for the time to first byte of all runs in MS
print multi_har_parser.time_to_first_byte
# 70

# Get the load time mean for all runs in MS
print multi_har_parser.load_time
# 150

# Get the javascript load time mean for all runs in MS
print multi_har_parser.js_load_time
# 50

# You can get the standard deviation for any of these as well
# Let's get the standard deviation for javascript load time
print multi_har_parser.js_load_time_stdev
# 5

Advanced Usage
==============

Expand Down
74 changes: 69 additions & 5 deletions haralyzer/assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,19 @@ def get_load_times(self, asset_type):
load_times.append(val)
return load_times

def get_stdev(self, asset_type):
"""
Small helper function that returns the standard deviation for a set
of a certain asset type.
:param asset_type: ``str`` of the asset type to calculate standard
deviation for.
:returns: A ``int`` of ``float`` of standard deviation, depending on
the DECIMAL_PRECISION
"""
load_times = self.get_load_times(asset_type)
return round(statistics.stdev(load_times), DECIMAL_PRECISION)

@property
def pages(self):
"""
Expand All @@ -206,7 +219,7 @@ def pages(self):
pages.append(har_parser.pages[0])
return pages

@property
@cached_property
def time_to_first_byte(self):
"""
The aggregate time to first byte for all pages.
Expand All @@ -217,7 +230,17 @@ def time_to_first_byte(self):
return round(statistics.mean(ttfb), DECIMAL_PRECISION)

@cached_property
def load_time_mean(self):
def time_to_first_byte_stdev(self):
"""
The aggregate time to first byte for all pages.
"""
ttfb = []
for page in self.pages:
ttfb.append(page.time_to_first_byte)
return round(statistics.stdev(ttfb), DECIMAL_PRECISION)

@cached_property
def load_time(self):
"""
The average total load time for all runs (not weighted).
"""
Expand All @@ -227,10 +250,9 @@ def load_time_mean(self):
@cached_property
def load_time_stdev(self):
"""
Standard deviations of the load times for all pages
Standard deviation of the total load times for all pages
"""
load_times = self.get_load_times('page')
return round(statistics.stdev(load_times), DECIMAL_PRECISION)
return self.get_stdev('page')

@cached_property
def js_load_time(self):
Expand All @@ -243,6 +265,13 @@ def js_load_time(self):
load_times = self.get_load_times('js')
return round(statistics.mean(load_times), DECIMAL_PRECISION)

@cached_property
def js_load_time_stdev(self):
"""
Standard deviation of the javascript load times for all pages
"""
return self.get_stdev('js')

@cached_property
def css_load_time(self):
"""
Expand All @@ -251,6 +280,13 @@ def css_load_time(self):
load_times = self.get_load_times('css')
return round(statistics.mean(load_times), DECIMAL_PRECISION)

@cached_property
def css_load_time_stdev(self):
"""
Standard deviation of the css load times for all pages
"""
return self.get_stdev('css')

@cached_property
def image_load_time(self):
"""
Expand All @@ -259,6 +295,13 @@ def image_load_time(self):
load_times = self.get_load_times('image')
return round(statistics.mean(load_times), DECIMAL_PRECISION)

@cached_property
def image_load_time_stdev(self):
"""
Standard deviation of the image load times for all pages
"""
return self.get_stdev('image')

@cached_property
def html_load_time(self):
"""
Expand All @@ -267,6 +310,13 @@ def html_load_time(self):
load_times = self.get_load_times('html')
return round(statistics.mean(load_times), DECIMAL_PRECISION)

@cached_property
def html_load_time_stdev(self):
"""
Standard deviation of the html load times for all pages
"""
return self.get_stdev('js')

@cached_property
def audio_load_time(self):
"""
Expand All @@ -275,6 +325,13 @@ def audio_load_time(self):
load_times = self.get_load_times('audio')
return round(statistics.mean(load_times), DECIMAL_PRECISION)

@cached_property
def audio_load_time_stdev(self):
"""
Standard deviations of the audio load times for all pages
"""
return self.get_stdev('audio')

@cached_property
def video_load_time(self):
"""
Expand All @@ -283,6 +340,13 @@ def video_load_time(self):
load_times = self.get_load_times('video')
return round(statistics.mean(load_times), DECIMAL_PRECISION)

@cached_property
def video_load_time_stdev(self):
"""
Standard deviations of the video load times for all pages
"""
return self.get_stdev('video')


class HarPage(object):
"""
Expand Down
11 changes: 9 additions & 2 deletions tests/test_multi_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,25 @@ def test_load_times(har_data):
# Test the default of it only caring about one page
har_parser = MultiHarParser(har_data=data)
# Test total load time averages
assert har_parser.load_time_mean == 519
assert har_parser.load_time == 519
assert har_parser.load_time_stdev == 11
# Test time to first byte
assert har_parser.time_to_first_byte == 70
# Test specific asset type load averages
assert har_parser.time_to_first_byte_stdev == 10
# Test specific asset type load averages and standard deviation
assert har_parser.js_load_time == 149
assert har_parser.js_load_time_stdev == 6
assert har_parser.css_load_time == 74
assert har_parser.css_load_time_stdev == 4
assert har_parser.image_load_time == 379
assert har_parser.image_load_time_stdev == 4
assert har_parser.html_load_time == 70
assert har_parser.html_load_time_stdev == 6
# TODO - Get audio/video load time data
assert har_parser.video_load_time == 0
assert har_parser.video_load_time_stdev == 0
assert har_parser.audio_load_time == 0
assert har_parser.audio_load_time_stdev == 0


def _load_test_data(har_data):
Expand Down

0 comments on commit 17ae18c

Please sign in to comment.