def finalise(self): for _, query in self.queries.items(): query_model = models.SQLQuery.objects.create(**query) query['model'] = query_model for _, profile in self.profiles.items(): profile_query_models = [] if TYP_QUERIES in profile: profile_queries = profile[TYP_QUERIES] del profile[TYP_QUERIES] for query_temp_id in profile_queries: try: query = self.queries[query_temp_id] try: profile_query_models.append(query['model']) except KeyError: raise SilkInternalInconsistency( 'Profile references a query dictionary that has not ' 'been converted into a Django model. This should ' 'never happen, please file a bug report') except KeyError: raise SilkInternalInconsistency( 'Profile references a query temp_id that does not exist. ' 'This should never happen, please file a bug report' ) profile = models.Profile.objects.create(**profile) if profile_query_models: profile.queries = profile_query_models profile.save() self._record_meta_profiling()
def finalise(self): if hasattr(self, 'pythonprofiler'): s = StringIO() ps = pstats.Stats(self.pythonprofiler, stream=s).sort_stats('cumulative') ps.print_stats() profile_text = s.getvalue() profile_text = "\n".join(profile_text.split("\n")[0:256]) # don't record too much because it can overflow the field storage size self.request.pyprofile = profile_text for _, query in self.queries.items(): query_model = models.SQLQuery.objects.create(**query) query['model'] = query_model for _, profile in self.profiles.items(): profile_query_models = [] if TYP_QUERIES in profile: profile_queries = profile[TYP_QUERIES] del profile[TYP_QUERIES] for query_temp_id in profile_queries: try: query = self.queries[query_temp_id] try: profile_query_models.append(query['model']) except KeyError: raise SilkInternalInconsistency('Profile references a query dictionary that has not ' 'been converted into a Django model. This should ' 'never happen, please file a bug report') except KeyError: raise SilkInternalInconsistency('Profile references a query temp_id that does not exist. ' 'This should never happen, please file a bug report') profile = models.Profile.objects.create(**profile) if profile_query_models: profile.queries = profile_query_models profile.save() self._record_meta_profiling()
def finalise(self): if getattr(self.local, 'pythonprofiler', None): s = StringIO() ps = pstats.Stats(self.local.pythonprofiler, stream=s).sort_stats('cumulative') ps.print_stats() profile_text = s.getvalue() profile_text = "\n".join( profile_text.split("\n")[0:256] ) # don't record too much because it can overflow the field storage size self.request.pyprofile = profile_text if SilkyConfig().SILKY_PYTHON_PROFILER_BINARY: file_name = self.request.prof_file.storage.get_available_name( "{}.prof".format(str(self.request.id))) with open(self.request.prof_file.storage.path(file_name), 'w+b') as f: ps.dump_stats(f.name) self.request.prof_file = f.name self.request.save() sql_queries = [] for identifier, query in self.queries.items(): query['identifier'] = identifier sql_query = models.SQLQuery(**query) sql_queries += [sql_query] models.SQLQuery.objects.bulk_create(sql_queries) sql_queries = models.SQLQuery.objects.filter(request=self.request) for sql_query in sql_queries.all(): query = self.queries.get(sql_query.identifier) if query: query['model'] = sql_query for profile in self.profiles.values(): profile_query_models = [] if TYP_QUERIES in profile: profile_queries = profile[TYP_QUERIES] del profile[TYP_QUERIES] for query_temp_id in profile_queries: try: query = self.queries[query_temp_id] try: profile_query_models.append(query['model']) except KeyError: raise SilkInternalInconsistency( 'Profile references a query dictionary that has not ' 'been converted into a Django model. This should ' 'never happen, please file a bug report') except KeyError: raise SilkInternalInconsistency( 'Profile references a query temp_id that does not exist. ' 'This should never happen, please file a bug report' ) profile = models.Profile.objects.create(**profile) if profile_query_models: profile.queries.set(profile_query_models) self._record_meta_profiling()
def finalise(self): if hasattr(self, 'pythonprofiler'): s = StringIO() ps = pstats.Stats(self.pythonprofiler, stream=s).sort_stats('cumulative') ps.print_stats() profile_text = s.getvalue() profile_text = "\n".join( profile_text.split("\n")[0:256] ) # don't record too much because it can overflow the field storage size self.request.pyprofile = profile_text query_models_to_create = [] use_bulk_insert = getattr(connection.features, 'can_return_id_from_bulk_insert', False) for _, query in self.queries.items(): query['request'] = self.request self.request.num_sql_queries += 1 query_model = models.SQLQuery(**query) query['model'] = query_model if use_bulk_insert: query_models_to_create.append(query_model) else: query_model.save() if query_models_to_create: models.SQLQuery.objects.bulk_create(query_models_to_create, set_primary_keys=True) for _, profile in self.profiles.items(): profile_query_models = [] if TYP_QUERIES in profile: profile_queries = profile[TYP_QUERIES] del profile[TYP_QUERIES] for query_temp_id in profile_queries: try: query = self.queries[query_temp_id] try: profile_query_models.append(query['model']) except KeyError: raise SilkInternalInconsistency( 'Profile references a query dictionary that has not ' 'been converted into a Django model. This should ' 'never happen, please file a bug report') except KeyError: raise SilkInternalInconsistency( 'Profile references a query temp_id that does not exist. ' 'This should never happen, please file a bug report' ) profile = models.Profile.objects.create(**profile) if profile_query_models: profile.queries = profile_query_models profile.save() self._record_meta_profiling() # Add the final save() to the queries count. if SilkyConfig().SILKY_META: self.request.meta_num_queries += 1
def finalise(self): if getattr(self.local, 'pythonprofiler', None): s = StringIO() ps = pstats.Stats(self.local.pythonprofiler, stream=s).sort_stats('cumulative') ps.print_stats() profile_text = s.getvalue() profile_text = "\n".join( profile_text.split("\n")[0:256] ) # don't record too much because it can overflow the field storage size self.request.pyprofile = profile_text if SilkyConfig().SILKY_PYTHON_PROFILER_BINARY: file_path = self._get_profile_file_path() content = ContentFile(marshal.dumps(ps.stats)) saved_file_path = self.request.prof_file.storage.save( file_path, content) self.request.prof_file = saved_file_path self.request.save() for _, query in self.queries.items(): query_model = models.SQLQuery.objects.create(**query) query['model'] = query_model for _, profile in self.profiles.items(): profile_query_models = [] if TYP_QUERIES in profile: profile_queries = profile[TYP_QUERIES] del profile[TYP_QUERIES] for query_temp_id in profile_queries: try: query = self.queries[query_temp_id] try: profile_query_models.append(query['model']) except KeyError: raise SilkInternalInconsistency( 'Profile references a query dictionary that has not ' 'been converted into a Django model. This should ' 'never happen, please file a bug report') except KeyError: raise SilkInternalInconsistency( 'Profile references a query temp_id that does not exist. ' 'This should never happen, please file a bug report' ) profile = models.Profile.objects.create(**profile) if profile_query_models: profile.queries.set(profile_query_models) self._record_meta_profiling()