Commits

Andriy Kornatskyy committed 75d46d8

Added gzip transform to caching benchmark.

  • Participants
  • Parent commits e050cbc

Comments (0)

Files changed (5)

File 07-caching/benchmark.py

     return wrapper
 
 
-if __name__ == '__main__':
+def run_batch(name):
+    print('\n%s' % name)
     run('welcome', build_wrapper('/welcome'))
     run('memory', build_wrapper('/memory'))
     run('pylibmc', build_wrapper('/pylibmc'))
     run('memcache', build_wrapper('/memcache'))
+
+
+if __name__ == '__main__':
+    run_batch('no gzip')
+    environ['HTTP_ACCEPT_ENCODING'] = 'gzip,deflate,sdch'
+    run_batch('gzip')

File 07-caching/django/helloworld/views.py

 
 from django.http import HttpResponse
 from django.views.decorators.cache import cache_page
+from django.views.decorators.gzip import gzip_page
 
-hello = 'Hello World!' * 350
+
+hello = ''.join(['%s. Hello World! ' % i for i in xrange(500)])
 
 
 def welcome(request):
 
 
 @cache_page(60 * 15, cache="memory")
+@gzip_page
 def memory(request):
     return HttpResponse(hello)
 
 
 @cache_page(60 * 15, cache="pylibmc")
+@gzip_page
 def pylibmc(request):
     return HttpResponse(hello)
 
 
 @cache_page(60 * 15, cache="memcache")
+@gzip_page
 def memcache(request):
     return HttpResponse(hello)

File 07-caching/samples.py

     'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;'
                    'q=0.9,*/*;q=0.8',
     'HTTP_ACCEPT_CHARSET': 'ISO-8859-1,utf-8;q=0.7,*;q=0.3',
-    'HTTP_ACCEPT_ENCODING': 'gzip,deflate,sdch',
     'HTTP_ACCEPT_LANGUAGE': 'uk,en-US;q=0.8,en;q=0.6',
     'HTTP_CONNECTION': 'keep-alive',
     'HTTP_HOST': 'vm0.dev.local:8080',

File 07-caching/wheezy.web/config.py

 # cache profiles
 
 memory_profile = CacheProfile(
-    'server', namespace='memory', duration=timedelta(minutes=15))
+    'server', namespace='memory', duration=timedelta(minutes=15),
+    vary_environ=['HTTP_ACCEPT_ENCODING'])
+pylibmc_profile = CacheProfile(
+    'server', namespace='pylibmc', duration=timedelta(minutes=15),
+    vary_environ=['HTTP_ACCEPT_ENCODING'])
 memcache_profile = CacheProfile(
-    'server', namespace='memcache', duration=timedelta(minutes=15))
-pylibmc_profile = CacheProfile(
-    'server', namespace='pylibmc', duration=timedelta(minutes=15))
+    'server', namespace='memcache', duration=timedelta(minutes=15),
+    vary_environ=['HTTP_ACCEPT_ENCODING'])

File 07-caching/wheezy.web/views.py

 
 import config
 
+from wheezy.core.comp import ntob
 from wheezy.http import HTTPResponse
+from wheezy.http.transforms import gzip_transform
 from wheezy.web import handler_cache
 from wheezy.web.handlers import BaseHandler
+from wheezy.web.transforms import handler_transforms
 
-hello = 'Hello World!' * 350
+
+hello = ntob(''.join(['%s. Hello World! ' % i for i in xrange(500)]),
+             'utf8')
 
 
 class WelcomeHandler(BaseHandler):
 
+    @handler_transforms(gzip_transform(compress_level=6))
     def get(self):
         response = HTTPResponse()
-        response.write(hello)
+        response.write_bytes(hello)
         return response
 
 
 class MemoryHandler(BaseHandler):
 
     @handler_cache(config.memory_profile)
+    @handler_transforms(gzip_transform(compress_level=6))
     def get(self):
         response = HTTPResponse()
-        response.write(hello)
+        response.write_bytes(hello)
         return response
 
 
 class PylibmcHandler(BaseHandler):
 
     @handler_cache(config.pylibmc_profile)
+    @handler_transforms(gzip_transform(compress_level=6))
     def get(self):
         response = HTTPResponse()
-        response.write(hello)
+        response.write_bytes(hello)
         return response
 
 
 class MemcacheHandler(BaseHandler):
 
     @handler_cache(config.memcache_profile)
+    @handler_transforms(gzip_transform(compress_level=6))
     def get(self):
         response = HTTPResponse()
-        response.write(hello)
+        response.write_bytes(hello)
         return response