Home Experience & Tips Sharing Standard Post

Standard Post

Display Database Queries, Generation Time, and Memory Usage in WordPress

This article shares a code that displays the number of database queries, page generation time, and memory usage of the current page at the bottom of any PHP website. Pure code, no need to insert code into the function file. Add the following code to the theme footer file (footer.php). The effect is shown in the image below: The specific code is as follows: This page is <?php timer_st...

Updated on July 23, 2020 About 2 minutes read
在WordPress显示数据库查询、生成时间和内存占用

This article shares a code that displays the number of database queries, page generation time, and memory usage of the current page at the bottom of any PHP website. Pure code, no need to insert code into the function file.

Add the following code to the theme footer file (footer.php).

The effect is shown in the image below:

页面数据库查询内存使用信息

The specific code is as follows:

本页面在 <?php timer_stop(1); ?> 秒里勾搭了 <?php echo get_num_queries(); ?> 次数据库,消耗了 <?php echo memory_get_peak_usage()/1000000; ?>  MB的内存。

Explain the meaning of /1000000: by default, memory_get_peak_usage returns a value in Bytes. To calculate the value in MB, you need to divide by 1024/1024, but the result will have a long tail, so directly divide by 1 million to truncate the tail.

If you only want to display generation time and database queries, there is also the following method:

<?php printf(__('%d queries. %s seconds.', 'kubrick'), get_num_queries(), timer_stop(0, 3)); ?> –>

If you want the above code to only display in the source code, then add comment tags as follows:

<!-- 这里是上面那些代码 -->

If you only want to display it to logged-in users, then write it as follows:

<?php if (current_user_can('level_10')) {echo '<!-- ' . get_num_queries() . ' queries in ' . timer_stop(3) . ' seconds -->';} ?>
5/5 - (1 vote)
Previous How to Reduce the Impact on SEO When Changing a Website's Theme Template Continue reading content around the same timeline. Next What are Windows revisions? How to disable autosave View the next related tutorial or experience.

AI Website Building Assistant

🤖
Hello! I am the Naibabiji AI Assistant. How can I help you?
Quick Consultation: