def testMulti(self): assert_pformat_equal(len(LocalSyncCache.CACHES), 0) c1 = LocalSyncCache(id="test1") assert_pformat_equal(len(LocalSyncCache.CACHES), 1) c2 = LocalSyncCache(id="test1", unique_ids=False) assert_pformat_equal(len(LocalSyncCache.CACHES), 2) c1["c1"] = "foo" c2["c2"] = "bar" assert_pformat_equal(c1, {"c1": "foo"}) assert_pformat_equal(c2, {"c2": "bar"}) c1.check_state() c2.check_state() assert_pformat_equal(c1, {"c1": "foo"}) assert_pformat_equal(c2, {"c2": "bar"}) c1.clear() assert_pformat_equal(c1, {}) # In a "new request" all the same caches should be cleared c2.check_state() assert_pformat_equal(c2, {}) cache_information = LocalSyncCache.get_cache_information() assert_pformat_equal(len(cache_information), 2)
def testMulti(self): self.assertEqual(len(LocalSyncCache.CACHES), 0) c1 = LocalSyncCache(id="test1") self.assertEqual(len(LocalSyncCache.CACHES), 1) c2 = LocalSyncCache(id="test1", unique_ids=False) self.assertEqual(len(LocalSyncCache.CACHES), 2) c1["c1"] = "foo" c2["c2"] = "bar" self.assertEqual(c1, {'c1': 'foo'}) self.assertEqual(c2, {'c2': 'bar'}) c1.check_state() c2.check_state() self.assertEqual(c1, {'c1': 'foo'}) self.assertEqual(c2, {'c2': 'bar'}) c1.clear() self.assertEqual(c1, {}) # In a "new request" all the same caches should be cleared c2.check_state() self.assertEqual(c2, {}) cache_information = LocalSyncCache.get_cache_information() self.assertEqual(len(cache_information), 2)
def show_internals(request): apps_info = [] for app in get_apps(): model_info = [] for model in get_models(app): model_info.append({ "name":model._meta.object_name, }) apps_info.append({ "app_name": app.__name__, "app_models": model_info, }) # from http://www.djangosnippets.org/snippets/1434/ # generate a list of (pattern-name, pattern) tuples resolver = urlresolvers.get_resolver(None) urlpatterns = sorted([ (key, value[0][0][0]) for key, value in resolver.reverse_dict.items() if isinstance(key, basestring) ]) context = { "title": "Show internals", "pid": os.getpid(), "cache_information": LocalSyncCache.get_cache_information(), "permissions": Permission.objects.all(), "urlpatterns": urlpatterns, "settings": hightlighter.make_html( pformat(get_safe_settings()), source_type="py", django_escape=True ), "db_backend_name": backend.Database.__name__, "db_backend_module": backend.Database.__file__, "db_backend_version": getattr(backend.Database, "version", "?"), "apps_info": apps_info, "db_table_names": sorted(connection.introspection.table_names()), "django_tables": sorted(connection.introspection.django_table_names()), "request_meta": hightlighter.make_html( pformat(request.META), source_type="py", django_escape=True ), "request_session": hightlighter.make_html( pformat(dict(request.session)), source_type="py", django_escape=True ), "sys_path": sys.path, "os_environ": os.environ, } return context
def testBasic(self): c = LocalSyncCache(id="test1") c["key1"] = "value1" assert_pformat_equal(c, {"key1": "value1"}) cache.set("test1", time.time()) c.check_state() assert_pformat_equal(c, {}) cache_information = LocalSyncCache.get_cache_information() assert_pformat_equal(len(cache_information), 1)
def testBasic(self): c = LocalSyncCache(id="test1") c["key1"] = "value1" self.assertEqual(c, {'key1': 'value1'}) cache.set("test1", time.time()) c.check_state() self.assertEqual(c, {}) cache_information = LocalSyncCache.get_cache_information() self.assertEqual(len(cache_information), 1)
def show_internals(request): apps_info = [] for app in get_apps(): model_info = [] for model in get_models(app): model_info.append({ "name":model._meta.object_name, }) apps_info.append({ "app_name": app.__name__, "app_models": model_info, }) # Information about the current used url patterns urlpatterns = UrlPatternInfo().get_url_info() context = { "title": "Show internals", "pid": os.getpid(), "cache_information": LocalSyncCache.get_cache_information(), "permissions": Permission.objects.all(), "urlpatterns": urlpatterns, "settings": hightlighter.make_html( pformat(get_safe_settings()), source_type="py", django_escape=True ), "db_backend_name": backend.Database.__name__, "db_backend_module": backend.Database.__file__, "db_backend_version": getattr(backend.Database, "version", "?"), "apps_info": apps_info, "db_table_names": sorted(connection.introspection.table_names()), "django_tables": sorted(connection.introspection.django_table_names()), "request_meta": hightlighter.make_html( pformat(request.META), source_type="py", django_escape=True ), "request_session": hightlighter.make_html( pformat(dict(request.session)), source_type="py", django_escape=True ), "sys_path": sys.path, "os_environ": os.environ, } return context
def testLocalSyncCacheMiddleware(self): middleware = LocalSyncCacheMiddleware() c1 = LocalSyncCache(id="testLocalSyncCacheMiddleware1") c2 = LocalSyncCache(id="testLocalSyncCacheMiddleware1", unique_ids=False) c3 = LocalSyncCache(id="testLocalSyncCacheMiddleware2") assert_pformat_equal(len(LocalSyncCache.CACHES), 3) c1["c1"] = "foo" c2["c2"] = "bar" c3["foo"] = "bar" middleware.process_request(None) assert_pformat_equal(c1, {"c1": "foo"}) assert_pformat_equal(c2, {"c2": "bar"}) assert_pformat_equal(c3, {"foo": "bar"}) c1.clear() assert_pformat_equal(c1, {}) # In a "new request" all the same caches should be cleared middleware.process_request(None) assert_pformat_equal(c2, {}) # Other caches should be not affected by clear() assert_pformat_equal(c3, {"foo": "bar"}) c1["c1"] = "foo2" c2["c2"] = "bar2" middleware.process_request(None) assert_pformat_equal(c1, {"c1": "foo2"}) assert_pformat_equal(c2, {"c2": "bar2"}) assert_pformat_equal(c3, {"foo": "bar"}) c2.clear() assert_pformat_equal(c2, {}) # In a "new request" all the same caches should be cleared middleware.process_request(None) assert_pformat_equal(c1, {}) # print LocalSyncCache.pformat_cache_information() cache_information = LocalSyncCache.get_cache_information() assert_pformat_equal(len(cache_information), 3) for item in cache_information: instance = item["instance"] assert_pformat_equal(instance.request_counter, 4) if instance.id == "testLocalSyncCacheMiddleware2": assert_pformat_equal(instance.own_clear_counter, 0) assert_pformat_equal(instance.ext_clear_counter, 0) assert_pformat_equal(instance, {"foo": "bar"}) else: assert_pformat_equal(instance.own_clear_counter, 1) assert_pformat_equal(instance.ext_clear_counter, 1) assert_pformat_equal(instance, {})
def testLocalSyncCacheMiddleware(self): middleware = LocalSyncCacheMiddleware() c1 = LocalSyncCache(id="testLocalSyncCacheMiddleware1") c2 = LocalSyncCache(id="testLocalSyncCacheMiddleware1", unique_ids=False) c3 = LocalSyncCache(id="testLocalSyncCacheMiddleware2") self.assertEqual(len(LocalSyncCache.CACHES), 3) c1["c1"] = "foo" c2["c2"] = "bar" c3["foo"] = "bar" middleware.process_request(None) self.assertEqual(c1, {'c1': 'foo'}) self.assertEqual(c2, {'c2': 'bar'}) self.assertEqual(c3, {'foo': 'bar'}) c1.clear() self.assertEqual(c1, {}) # In a "new request" all the same caches should be cleared middleware.process_request(None) self.assertEqual(c2, {}) # Other caches should be not affected by clear() self.assertEqual(c3, {'foo': 'bar'}) c1["c1"] = "foo2" c2["c2"] = "bar2" middleware.process_request(None) self.assertEqual(c1, {'c1': 'foo2'}) self.assertEqual(c2, {'c2': 'bar2'}) self.assertEqual(c3, {'foo': 'bar'}) c2.clear() self.assertEqual(c2, {}) # In a "new request" all the same caches should be cleared middleware.process_request(None) self.assertEqual(c1, {}) # print LocalSyncCache.pformat_cache_information() cache_information = LocalSyncCache.get_cache_information() self.assertEqual(len(cache_information), 3) for item in cache_information: instance = item["instance"] self.assertEqual(instance.request_counter, 4) if instance.id == "testLocalSyncCacheMiddleware2": self.assertEqual(instance.own_clear_counter, 0) self.assertEqual(instance.ext_clear_counter, 0) self.assertEqual(instance, {'foo': 'bar'}) else: self.assertEqual(instance.own_clear_counter, 1) self.assertEqual(instance.ext_clear_counter, 1) self.assertEqual(instance, {})
def show_internals(request): apps_info = [] for app in get_apps(): model_info = [] for model in get_models(app): model_info.append({ "name":model._meta.object_name, }) apps_info.append({ "app_name": app.__name__, "app_models": model_info, }) # Information about the current used url patterns urlpatterns = UrlPatternInfo().get_url_info() # Create a dict from RequestContext request_context = RequestContext(request) keys = set() for context_dict in request_context.dicts: keys = keys.union(set(context_dict.keys())) request_context_info = {} for key in keys: request_context_info[key] = request_context[key] try: cnonce_size = sys.getsizeof(CNONCE_CACHE) # New in version 2.6 except (AttributeError, TypeError): # PyPy raised a TypeError cnonce_size = None context = { "title": "Show internals", "pid": os.getpid(), "cache_information": LocalSyncCache.get_cache_information(), "permissions": Permission.objects.all(), "urlpatterns": urlpatterns, "request_context":hightlighted_pformat(request_context_info), "settings": hightlighted_pformat(get_safe_settings()), "db_backend_name": backend.Database.__name__, "db_backend_module": backend.Database.__file__, "db_backend_version": getattr(backend.Database, "version", "?"), "apps_info": apps_info, "db_table_names": sorted(connection.introspection.table_names()), "django_tables": sorted(connection.introspection.django_table_names()), "request_meta": hightlighter.make_html( pformat(request.META), source_type="py", django_escape=True ), "request_session": hightlighter.make_html( pformat(dict(request.session)), source_type="py", django_escape=True ), "sys_path": sys.path, "os_environ": os.environ, # Information of the cache usage # from FetchFromCacheMiddleware (if settings.COUNT_FETCH_FROM_CACHE != True: all values are None): "local_cache_requests": LOCAL_CACHE_INFO["requests"], "local_cache_request_hits": LOCAL_CACHE_INFO["request hits"], "global_cache_requests": cache.get(CACHE_REQUESTS), "global_cache_request_hits": cache.get(CACHE_REQUEST_HITS), # from UpdateCacheMiddleware (if settings.COUNT_UPDATE_CACHE != True: all values are None): "local_cache_responses": LOCAL_CACHE_INFO["responses"], "local_cache_response_hits": LOCAL_CACHE_INFO["response hits"], "global_cache_responses": cache.get(CACHE_RESPONSES), "global_cache_response_hits": cache.get(CACHE_RESPONSE_HITS), # Information about auth cnonce usage: "cnonce_count": len(CNONCE_CACHE), "cnonce_size": cnonce_size, "used_cnonces": tuple(sorted(CNONCE_CACHE.keys())), } return context