超速! Webページ速度改善ガイド ── 使いやすさは
Django で
結果を
前提
以下の
OS
cat /etc/centos-release CentOS Linux release 7.6.1810 (Core)
Django version
pip list | grep Django Django 1.10.8
作成した middleware の 説明
Show page generation time in Django - Stavros’ Stuff をStatsMiddleware
を
middleware.py
import time class StatsMiddleware(object): def process_request(self, request): "Store the start time when the request comes in." request.start_time = time.time() def process_response(self, request, response): "Calculate and output the page generation duration" # Get the start time from the request and calculate how long # the response took. duration = time.time() - request.start_time # Add the header. response["Server-Timing"] = 'res;desc="Server app response time";dur=' + str(int(duration * 1000)) return response
Headerの
設定値の 説明
以下の形式で、 設定されます。
res;desc="Server app response time";dur=20
res
サーバーのメトリック名称を 示します。 desc
サーバーのメトリックの 説明です。
この設定値が、 Chrome の 開発者ツールに 表示されます。 dur
duration の略でかかった 時間を 示します。
単位はms で、 Python の timeモジュールで 取得できる duration は 秒に なるため 1000 倍する 必要が あります。
settings.py
MIDDLEARE_CLASSES の一番上に 作成した middleware を 記載します。 MIDDLEWARE_CLASSES = ( "mezzanine_extentions.middleware.StatsMiddleware",
Google Analytics に 送信する
Google Analytics に
当サイトは、
W3C
のexample JavaScript
そもそもどのようにして、 JavaScript から アクセスできるかわからず、 まず W3C
のexample JavaScript を 確認しました。
navigation
で現在開いている ページの HTML の Server-Timing 、 resource
で、サブリソースの Server-Timingが 取得できます。 // serverTiming entries can live on 'navigation' and 'resource' entries for (const entryType of ['navigation', 'resource']) { for (const {name: url, serverTiming} of performance.getEntriesByType(entryType)) { // iterate over the serverTiming array for (const {name, duration, description} of serverTiming) { // we only care about "slow" ones if (duration > 200) { console.info('Slow server-timing entry =', JSON.stringify({url, entryType, name, duration, description}, null, 2)) } } } }
if (duration > 200) {
のif文を 削除して コードを 実行すると、 以下のような 情報が 取得できます。 Slow server-timing entry = { "url": "https://www.monotalk.xyz/blog/Deal-with-http-response-headers-warnings-in-security-headers/", "entryType": "navigation", "name": "res", "duration": 26, "description": "Server app response time" } VM1355:7 Slow server-timing entry = { "url": "https://www.monotalk.xyz/report/", "entryType": "resource", "name": "res", "duration": 119, "description": "Server app response time" } VM1355:7 Slow server-timing entry = { "url": "https://www.monotalk.xyz/report/", "entryType": "resource", "name": "res", "duration": 105, "description": "Server app response time" } VM1355:7 Slow server-timing entry = { "url": "https://www.monotalk.xyz/report/", "entryType": "resource", "name": "res", "duration": 108, "description": "Server app response time" } VM1355:7 Slow server-timing entry = { "url": "https://www.monotalk.xyz/report/", "entryType": "resource", "name": "res", "duration": 120, "description": "Server app response time" } VM1355:7 Slow server-timing entry = { "url": "https://www.monotalk.xyz/blog/qualys-ssl-server-test-%E3%82%92%E5%AE%9F%E8%A1%8C%E3%81%97%E5%87%BA%E5%8A%9B%E3%81%95%E3%82%8C%E3%82%8B%E8%AD%A6%E5%91%8A%E3%81%AB%E5%AF%BE%E5%87%A6%E3%81%99%E3%82%8B/", "entryType": "resource", "name": "res", "duration": 21, "description": "Server app response time" }
severTiming.js
Google Tag Manager では、以下のような コードを カスタム JavaScript 変数と して 設定しました。 function () { var serverTiming = performance.getEntriesByType('navigation')[0].serverTiming; for (var i = 0; i < serverTiming.length; i++) { if (serverTiming[i].name == "res") { return serverTiming[i].duration; } } return undefined; }
Google Analytics 変数に、
カスタムディメンションを 追加
カスタムディメンションの20番に、 serverTiming
を設定しました。
django-server-timing に ついて
Server Timing をServer-Timing
header が
計測対象の
参考
以下、
以上です。
コメント