Giải quyết vấn đề profile PHP memory với Drupal 7

Giải quyết vấn đề profile PHP memory với Drupal 7

Why is this important

How to size the PHP setting max_memory is actually really important for the health of your Drupal application. Size this too small, and you risk getting PHP fatals due to not enough memory allocated. Size this too large, and you are essentially under-utilising your hardware, which in turn can lead to more cost.

>> Hướng dẫn Debug: Stop Thinking and Look năm 2015

>> Sam Boyer cho chúng ta cái nhìn web serivces trong Drupal 8

How to record every Drupal requests PHP max memory usage

Tim Hillard created this really nice module called Memory profiler, which probably wins some sort of award for being around one of the smallest modules on drupal.org. Essentially this module registers a shutdown function that gets called at the end of every normal Drupal request.

The module is lightweight enough to run on production and only produces an extra syslog line per request.

Analyse the data

The data for memory profiler flows into watchdog, so if you run syslog (which you should), you can use CLI tools to analyse the data.

What does a single request look like

$ grep "memory profiler" drupal-watchdog.log | head -n 1

May 26 06:25:21 10.212.4.16 sitename: https://www.sitename.com|1432621521|memory profiler|1.152.97.17|https://www.sitename.com/|https://www.sitename.com/home|0||4.75 MB - home request_id="v-fc9573dc-036f-11e5-a8c0-22000af91462"

This comes from your syslog format (which can be changed on a per site basis):

$ drush vget syslog_format

syslog_format: '!base_url|!timestamp|!type|!ip|!request_uri|!referer|!uid|!link|!message'

Extract the data from syslog

From here you can tokenise the parts you actually care about, in other words the:

  • URL requested (part 5)
  • PHP max memory (part 9)

Using more bash foo

$ grep "memory profiler" drupal-watchdog.log | head -n 1 | awk -F'|' -v OFS=',' '{print $5, $9}'

https://www.sitename.com/,4.75 MB - home request_id="v-fc9573dc-036f-11e5-a8c0-22000af91462"

On Acquia Cloud a request ID is added to all requests, we don’t need this. Also having the string ‘MB’ there is superfluous.

$ grep "memory profiler" drupal-watchdog.log | head -n 1 | awk -F'|' -v OFS=',' '{print $5, $9}' | sed 's/ MB.*//'

https://www.sitename.com/,4.75

Perfect.

So in order to create a CSV for analysing in a spreadsheet you could do:

$ echo "request_uri,max_memory" > /tmp/memory.csv && grep "memory profiler" drupal-watchdog.log | awk -F'|' -v OFS=',' '{print $5, $9}' | sed 's/ MB.*//' >> /tmp/memory.csv

And then you can make pretty graphs if you want:

Graph showing PHP memory usage sorted by smallest to largest

Or if you just want to find the top requests to your application by memory you can do

$ grep "memory profiler" drupal-watchdog.log | awk -F'|' -v OFS=',' '{print $5, $9}' | sed 's/ MB.*//' | sort -t, -k+2 -n -r | head -n 20

Conclusions

Based on your findings in the logs, you should be able to come up with:

  • A better understanding of your request memory profile
  • Better max memory settings for your Drupal application
  • Potentially identify poor performing pages (memory wise) and can look to optimise them

Gotchas

This module will only work if:

  • hook_boot() is called (which might not be the case if you run custom lightweight PHP scripts that do not bootstrap Drupal)
  • The Drupal request is not terminated with a SIGTERM or SIGKILL signal

Comments

Let me know if you found this helpful, or if you have any changes to my bash foo. If you have profiled your Drupal application recently, what methods and tools did you use?

Bạn thấy bài viết này như thế nào?: 
No votes yet
Ảnh của Tommy Tran

Tommy owner Express Magazine

Drupal Developer having 9+ year experience, implementation and having strong knowledge of technical specifications, workflow development. Ability to perform effectively and efficiently in team and individually. Always enthusiastic and interseted to study new technologies

  • Skype ID: tthanhthuy

Tìm kiếm bất động sản

 

Advertisement

 

jobsora

Dich vu khu trung tphcm

Dich vu diet chuot tphcm

Dich vu diet con trung

Quảng Cáo Bài Viết

 
Các nhà nghiên cứu phá thành công khóa mã hóa an toàn bằng âm thanh

Các nhà nghiên cứu phá thành công khóa mã hóa an toàn bằng âm thanh

Mới đây, các nhà nghiên cứu tại đại học Tel Aviv và viện khoa học Weizmann (Israel) đã thành công trong việc giải mã một trong những thuật toán mã hóa an toàn nhất hiện nay là RSA 4096 bit bằng cách dùng microphone để lắng nghe một chiếc máy tính khi nó giải mã dữ liệu đã mã hóa.

Kiếm tiền từ Android - Google phải làm gì để có lợi nhất?

Kiếm tiền từ Android - Google phải làm gì để có lợi nhất?

Cho rằng Android hiện tại vẫn chưa đem lại hiệu quả kinh doanh cho Google, cây bút công nghệ Farhad Manjoo đã đề xuất những ý tưởng kinh doanh trên Android thông qua bài viết dưới đây:

Hướng dẫn Rich Video Snippets cho Drupal sites

Hướng dẫn Rich Video Snippets cho Drupal sites

Rich video snippets can make the search engine results more appealing for your website pages that contain video.

Công ty diệt chuột T&C

 

Diet con trung