def TransferredDataRevisit(trace, after_time_s, assume_validation_ok=False):
  """Returns the amount of data transferred for a revisit.

  Args:
    trace: (LoadingTrace) loading trace.
    after_time_s: (float) Time in s after which the site is revisited.
    assume_validation_ok: (bool) Assumes that the resources to validate return
                          304s.

  Returns:
    (uploaded_bytes, downloaded_bytes)
  """
  uploaded_bytes = 0
  downloaded_bytes = 0
  for request in trace.request_track.GetEvents():
    caching_policy = CachingPolicy(request)
    policy = caching_policy.PolicyAtDate(request.wall_time + after_time_s)
    request_bytes = _RequestTransferSize(request)
    if policy == CachingPolicy.VALIDATION_NONE:
      continue
    uploaded_bytes += request_bytes['get'] + request_bytes['request_headers']
    if (policy in (CachingPolicy.VALIDATION_SYNC,
                   CachingPolicy.VALIDATION_ASYNC)
        and caching_policy.HasValidators() and assume_validation_ok):
      downloaded_bytes += len('HTTP/1.1 304 NOT MODIFIED\r\n')
      continue
    downloaded_bytes += (len('HTTP/1.1 200 OK\r\n')
                         + request_bytes['response_headers']
                         + request_bytes['body'])
  return (uploaded_bytes, downloaded_bytes)
Beispiel #2
0
 def testHasValidators(self):
     r = self._MakeRequest()
     self.assertFalse(CachingPolicy(r).HasValidators())
     r.response_headers['Last-Modified'] = 'Yesterday all my troubles'
     self.assertTrue(CachingPolicy(r).HasValidators())
     r = self._MakeRequest()
     r.response_headers['ETAG'] = 'ABC'
     self.assertTrue(CachingPolicy(r).HasValidators())
Beispiel #3
0
 def testLastModifiedHeuristic(self):
     r = self._MakeRequest()
     # 8 hours ago.
     r.response_headers['Last-Modified'] = 'Fri, 22 Apr 2016 00:56:19 -0200'
     del r.response_headers['Age']
     self.assertEqual(CachingPolicy.VALIDATION_NONE,
                      CachingPolicy(r).PolicyAtDate(r.wall_time + 60))
     self.assertEqual(CachingPolicy.VALIDATION_SYNC,
                      CachingPolicy(r).PolicyAtDate(r.wall_time + 3600))
Beispiel #4
0
 def testStaleWhileRevalidate(self):
     r = self._MakeRequest()
     r.response_headers['Cache-Control'] = (
         'whatever,max-age=1000,stale-while-revalidate=2000')
     self.assertEqual(CachingPolicy.VALIDATION_ASYNC,
                      CachingPolicy(r).PolicyAtDate(r.wall_time + 200))
     self.assertEqual(CachingPolicy.VALIDATION_ASYNC,
                      CachingPolicy(r).PolicyAtDate(r.wall_time + 2000))
     self.assertEqual(CachingPolicy.VALIDATION_SYNC,
                      CachingPolicy(r).PolicyAtDate(r.wall_time + 3100))
     # must-revalidate overrides stale-while-revalidate.
     r.response_headers['Cache-Control'] += ',must-revalidate'
     self.assertEqual(CachingPolicy.VALIDATION_SYNC,
                      CachingPolicy(r).PolicyAtDate(r.wall_time + 200))
Beispiel #5
0
 def testPolicyExpires(self):
     r = self._MakeRequest()
     # Already expired
     r.response_headers['Expires'] = 'Thu, 21 Apr 2016 00:00:00 -0200'
     self.assertEqual(CachingPolicy.VALIDATION_SYNC,
                      CachingPolicy(r).PolicyAtDate(r.wall_time))
     r.response_headers['Expires'] = 'Thu, 25 Apr 2016 00:00:00 -0200'
     self.assertEqual(
         CachingPolicy.VALIDATION_NONE,\
         CachingPolicy(r).PolicyAtDate(r.wall_time))
     self.assertEqual(CachingPolicy.VALIDATION_NONE,
                      CachingPolicy(r).PolicyAtDate(r.wall_time + 86400))
     self.assertEqual(
         CachingPolicy.VALIDATION_SYNC,
         CachingPolicy(r).PolicyAtDate(r.wall_time + 86400 * 5))
Beispiel #6
0
 def testPolicyMaxAge(self):
     r = self._MakeRequest()
     r.response_headers[
         'Cache-Control'] = 'whatever,max-age=  1000,whatever'
     self.assertEqual(CachingPolicy.VALIDATION_NONE,
                      CachingPolicy(r).PolicyAtDate(r.wall_time))
     self.assertEqual(CachingPolicy.VALIDATION_SYNC,
                      CachingPolicy(r).PolicyAtDate(r.wall_time + 10000))
     # Take current age into account.
     self.assertEqual(CachingPolicy.VALIDATION_SYNC,
                      CachingPolicy(r).PolicyAtDate(r.wall_time + 500))
     # Max-Age before Expires.
     r.response_headers['Expires'] = 'Thu, 21 Apr 2016 00:00:00 -0200'
     self.assertEqual(CachingPolicy.VALIDATION_NONE,
                      CachingPolicy(r).PolicyAtDate(r.wall_time))
     # Max-Age < age
     r.response_headers[
         'Cache-Control'] = 'whatever,max-age=100crap,whatever'
     self.assertEqual(CachingPolicy.VALIDATION_SYNC,
                      CachingPolicy(r).PolicyAtDate(r.wall_time + 2))
Beispiel #7
0
 def test301NeverExpires(self):
     r = self._MakeRequest()
     r.status = 301
     self.assertEqual(CachingPolicy.VALIDATION_NONE,
                      CachingPolicy(r).PolicyAtDate(r.wall_time + 2000))
Beispiel #8
0
 def testPolicyNoStore(self):
     r = self._MakeRequest()
     r.response_headers['Cache-Control'] = 'Whatever,no-store'
     self.assertEqual(CachingPolicy.FETCH, CachingPolicy(r).PolicyAtDate(0))
Beispiel #9
0
 def testIsCacheable(self):
     r = self._MakeRequest()
     self.assertTrue(CachingPolicy(r).IsCacheable())
     r.response_headers['Cache-Control'] = 'Whatever,no-store'
     self.assertFalse(CachingPolicy(r).IsCacheable())