Ejemplo n.º 1
0
 def test_extra_columns_admin_index(self, profiler_mock):
     profiler_mock.storage.fetch_all.return_value = [
         ViewProfiler(view_name="app.view_name",
                      method="GET",
                      extra="Value"),
     ]
     response = self.client.get(
         reverse("admin:speedinfo_viewprofiler_changelist"))
     self.assertEqual(response.status_code, 200)
Ejemplo n.º 2
0
    def fetch_all(self, ordering=None):
        results = [
            ViewProfiler(**self._cache.get(index))
            for index in self.indexes()
        ]

        order_fields = [
            (field[1:], True) if field.startswith("-") else (field, False)
            for field in ordering or []
        ]

        return sorted(results, key=cmp_to_key(comparator(order_fields)))
Ejemplo n.º 3
0
    def fetch_all(self, ordering=None):
        qs = Storage.objects.annotate(
            anon_calls_ratio=ExpressionWrapper(100.0 * F("anon_calls") / F("total_calls"), output_field=FloatField()),
            cache_hits_ratio=ExpressionWrapper(100.0 * F("cache_hits") / F("total_calls"), output_field=FloatField()),
            sql_count_per_call=ExpressionWrapper(F("sql_total_count") / F("total_calls"), output_field=IntegerField()),
            sql_time_ratio=ExpressionWrapper(100.0 * F("sql_total_time") / F("total_time"), output_field=FloatField()),
            time_per_call=ExpressionWrapper(F("total_time") / F("total_calls"), output_field=FloatField()),
        )

        if ordering:
            qs = qs.order_by(*ordering)

        return [ViewProfiler(**model_to_dict(item)) for item in qs]
Ejemplo n.º 4
0
    def test_extra_columns_export(self, profiler_mock):
        profiler_mock.storage.fetch_all.return_value = [
            ViewProfiler(view_name="app.view_name",
                         method="GET",
                         extra="Value"),
        ]
        response = self.client.get(reverse("admin:speedinfo-profiler-export"))
        output = response.content.decode()

        self.assertEqual(
            output,
            "View name,HTTP method,Extra\r\n"
            "app.view_name,GET,Value\r\n",
        )
Ejemplo n.º 5
0
    def test_partial_export(self, profiler_mock):
        profiler_mock.storage.fetch_all.return_value = [
            ViewProfiler(
                view_name="app.view_name",
                method="GET",
                anon_calls=8,
                cache_hits=3,
                sql_total_time=40,
                sql_total_count=20,
                total_calls=10,
                total_time=50,
            ),
        ]
        response = self.client.get(reverse("admin:speedinfo-profiler-export"))
        output = response.content.decode()

        self.assertEqual(
            output,
            "View name,HTTP method,Total calls,Time per call,Total time\r\n"
            "app.view_name,GET,10,5.0000,50.00\r\n",
        )
Ejemplo n.º 6
0
    def test_export(self, profiler_mock):
        profiler_mock.storage.fetch_all.return_value = [
            ViewProfiler(
                view_name="app.view_name",
                method="GET",
                anon_calls=8,
                cache_hits=3,
                sql_total_time=40,
                sql_total_count=20,
                total_calls=10,
                total_time=50,
            ),
        ]
        response = self.client.get(reverse("admin:speedinfo-profiler-export"))
        self.assertEquals(response.get("Content-Disposition"),
                          "attachment; filename=profiler.csv")

        output = response.content.decode()
        self.assertEqual(
            output,
            "View name,HTTP method,Anonymous calls,Cache hits,SQL queries per call,"
            "SQL time,Total calls,Time per call,Total time\r\n"
            "app.view_name,GET,80.0%,30.0%,2,80.0%,10,5.00000000,50.0000\r\n",
        )
Ejemplo n.º 7
0
 def test_extra_fields(self):
     vp = ViewProfiler(extra="Value")
     self.assertEqual(getattr(vp, "extra", None), "Value")