故意减慢 HTML/PHP 页面加载速度进行测试

发布于 2024-12-04 12:39:43 字数 272 浏览 0 评论 0原文

我很好奇是否存在一种方法可以故意减慢页面加载速度?

我正在测试我的 HTML 和PHP 页面现在在我的本地主机上,我想看看当页面加载速度较慢时我的加载 gif 等将如何执行。

我意识到这是一个罕见的请求,因为大多数开发人员只关心加快页面加载速度,但我认为可能有一种方法,使用 javascript/jQuery 或 PHP 来执行类似的测试目的。

感谢您的帮助!

注意:我正在 MAMP 上进行测试,因此这是在 Mac OS 10.7 上运行的 Apache 服务器

I'm curious if there exists a method to intentionally slow the page load?

I'm testing my HTML & PHP pages on my local host right now, and I want to see how my loading gif etc will perform when the page load is slower.

I realize this is a rare request as most developers are only concerned with speeding up page loads, but I thought there might be a method, using either javascript/jQuery or PHP, to do something like this for testing purposes.

Thanks for any help!

Note: I'm testing on MAMP, so that's Apache server running on Mac OS 10.7

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

神爱温柔 2024-12-11 12:39:43

您可以使用php的 sleep($seconds) 函数减慢页面加载速度。但是,您需要将 隐式输出缓冲区刷新设置为“on” ob_implicit_flush(true); 如果您希望在页面处理完成之前将任何内容发送到用户的浏览器。否则,您的页面在加载完成之前不会有任何内容。仅仅调用 sleep 并不能解决问题。

You can use php's sleep($seconds) function to slow down a page load. However, you would need to turn implicit output buffer flushing to "on" with ob_implicit_flush(true); if you want anything to be sent to the user's browser before the page is done being processed. Otherwise your page won't have ANY contents until it's done loading. Calling sleep alone won't do the trick.

拍不死你 2024-12-11 12:39:43

在 Chrome 中,您可以使用开发人员工具模拟慢速互联网连接。在最右侧的“网络”选项卡下。您可以使用“Fast G3”等预设,也可以使用精确的上传、下载和 ping 数字创建自己的预设。

输入图像描述此处

参考:https://helpdeskgeek.com/networking/simulate-slow-internet-connection-testing/

In Chrome, you can simulate a slow Internet connection with the developer tools. under the "Network" Tab on the far right. You can use a preset like "Fast G3" or can create your own with exact numbers for upload, download and ping.

enter image description here

Reference: https://helpdeskgeek.com/networking/simulate-slow-internet-connection-testing/

享受孤独 2024-12-11 12:39:43

穆萨有正确的想法。测试缓慢页面加载的最佳方法是使用 Chrome 的开发人员工具。选择网络选项卡,然后单击显示“无限制”的下拉列表。然后将页面更改为您想要的速度。

这种方式优于使用 sleep 函数,因为您不必弄乱任何代码然后记得将其取出。如果您想更改速度,只需更改节流级别即可。

有关限制的更多信息查看文档

Moussa has the right idea. The best way to test a slow page load is to use Chrome's developer tools. Select the network tab, and then click the dropdown that says "No throttling". Then change the page to your desired speed.

This way is superior to using the sleep function because you don't have to mess with any code and then remember to take it out. If you want to change the speed, just change the throttling level and you're set.

For more information on throttling check out the docs.

装纯掩盖桑 2024-12-11 12:39:43

这就是我要尝试的:
使用 php 资源作为图像源:

<img src="images/gifLoager.php" />

在 gifLoader.php 中,读取图像文件,在循环中延迟逐字节输出。

$fp = fopen( $path, 'rb');
while(!feof($fp)) {
        print(fread($fp, 1024));
        flush();
        Sleep(1);
     }
fclose($fp);

在输出二进制数据之前,不要忘记适当设置标头。

参考文献:

http://stackoverflow.com/questions/1563069/stream-binary-file-from-mysql-to-download-with-php
http://php.net/manual/en/function.sleep.php
http://www.gamedev.net/topic/427622-php-and-file-streaming-script-with-resume-capability/

更新2015-04-09
使用 Chrome“设备模式”:
该工具具有网络限制功能,可让您查看页面在网络带宽较慢的设备上的呈现方式。它具有许多其他功能,允许您模拟各种设备上的功能,例如屏幕尺寸和触摸。

https://developer.chrome.com/devtools/docs/device-mode

This is what I would try:
Use a php resource as source of the image:

<img src="images/gifLoager.php" />

in gifLoader.php, read your image file, output it byte by byte with a delay in the loop.

$fp = fopen( $path, 'rb');
while(!feof($fp)) {
        print(fread($fp, 1024));
        flush();
        Sleep(1);
     }
fclose($fp);

Don't forget to appropriately set the headers before outputting the binary data.

Referrences:

http://stackoverflow.com/questions/1563069/stream-binary-file-from-mysql-to-download-with-php
http://php.net/manual/en/function.sleep.php
http://www.gamedev.net/topic/427622-php-and-file-streaming-script-with-resume-capability/

UPDATE 2015-04-09
Use Chrome 'Device Mode':
This tool has a network throttling feature that allows you to see how your page may render on a device with a slow network bandwidth. It has many other features that allow you to emulate features on various devices such as screen size and touch.

https://developer.chrome.com/devtools/docs/device-mode

野鹿林 2024-12-11 12:39:43

一点点 JS setTimeout 就可以解决这个问题

setTimeout(function()
{
    // Delayed code in here
    alert('You waited 5 seconds to see me'); // Waits 5 seconds, then alerts this
}, 5000); // 5000 = 5 seconds

A little bit of JS setTimeout can do the trick

setTimeout(function()
{
    // Delayed code in here
    alert('You waited 5 seconds to see me'); // Waits 5 seconds, then alerts this
}, 5000); // 5000 = 5 seconds
难如初 2024-12-11 12:39:43

您可以使用 sleep()


<?php
// Delays for 10 seconds.
sleep(10);
?>

...
html here
...

You can use sleep():


<?php
// Delays for 10 seconds.
sleep(10);
?>

...
html here
...

硬不硬你别怂 2024-12-11 12:39:43

在 PHP 代码中调用 sleep() 来延迟向服务器发出请求。

Call sleep() in your PHP code to delay the request to the server.

夜未央樱花落 2024-12-11 12:39:43

您可以使用 sloppy 本地网络代理 来减慢您的连接速度(它是 JAVA 语言,所以它会可能在您的开发机器上运行,如果您希望浏览器如何响应缓慢的 HTML 加载(例如,动态大小的 HTML 表格等),您可能还需要关闭 mod_deflate 来进行测试。

另外,请参阅 此问题位于 webmasters.stackexchange.com 上

you can use sloppy local web proxy to slow down your connection (it's in JAVA, so it'll probably run on your devel machine. You might also want to turn off mod_deflate for testing purposes, if you want so how your browser responds to slow HTML load (for example, dynamicly sized HTML tables, etc)

Also, see this question on webmasters.stackexchange.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文