如何创建一个能够检测用户上网速度并在页面上显示的JavaScript页面?像"你的网速是?? ?? ?? Kb/s".
这是一个受欢迎的问题,它正在得到更多的关注.为了阻止错误信息的传播,请首先阅读以下段落和随附的文章:
速度不应成为决定是使用HTTPS还是HTTP的因素.如果您的站点的任何部分需要 HTTPS (登录,注册,信用卡等),您绝对需要所有这些的HTTPS.
我被认为在https下运行我的整个电子商务网站.我决定运行一个粗略的基准来测量156KB图像的下载时间,通过https vs http,因为我已经读过https负担加密过程带来的额外开销.
当从空缓存下载图像时,使用Firefox的Firebug简单地通过将"等待"和"接收"时间(所有其他时间均为0)转录到Excel中的Excel来执行基准测试.
我的结果出人意料:
http: 11.233 seconds
Waiting Receiving Total
1.56 0.88 2.44
1.55 0.101 1.651
1.53 0.9 2.43
1.71 0.172 1.882
1.9 0.93 2.83
https: 9.936 seconds
Waiting Receiving Total
0.867 1.59 2.457
0.4 1.67 2.07
0.277 1.5 1.777
0.536 1.29 1.826
0.256 1.55 1.806
Run Code Online (Sandbox Code Playgroud)
[明显]基准观察:
任何人都可以解释为什么会这样吗?
你认为文件(html,css,javascript)会给出不同的结果吗?
有没有人有更好的基准测试下载方法?
[删除测试图像]
附加信息:
编辑:以下1px GIF(35字节)的基准:
http: 2.666 seconds
Waiting …
Run Code Online (Sandbox Code Playgroud) 好吧,我有一个QProgressBar,我显示下载进度,但我想设置它显示下载速度的百分比,将其保留为:
百分比%(downloadspeed KB/s)
任何的想法?
我requests
用来下载文件,但是对于大文件我需要每次检查磁盘上文件的大小,因为我无法显示百分比的进度,我也想知道下载速度.我怎么去做呢?这是我的代码:
import requests
import sys
import time
import os
def downloadFile(url, directory) :
localFilename = url.split('/')[-1]
r = requests.get(url, stream=True)
start = time.clock()
f = open(directory + '/' + localFilename, 'wb')
for chunk in r.iter_content(chunk_size = 512 * 1024) :
if chunk :
f.write(chunk)
f.flush()
os.fsync(f.fileno())
f.close()
return (time.clock() - start)
def main() :
if len(sys.argv) > 1 :
url = sys.argv[1]
else :
url = raw_input("Enter the URL : ")
directory = raw_input("Where would you want to …
Run Code Online (Sandbox Code Playgroud) 我使用以下代码来限制java中文件的下载速度:
package org;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
class MainClass {
public static void main(String[] args) {
download("https://speed.hetzner.de/100MB.bin");
}
public static void download(String link) {
try {
URL url = new URL(link);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(5000);
con.setReadTimeout(5000);
InputStream is = con.getInputStream();
CustomInputStream inputStream = new CustomInputStream(is);
byte[] buffer = new byte[2024];
int len;
while ((len = inputStream.read(buffer)) != -1) {
System.out.println("downloaded : " + len);
//save file
}
} catch (IOException e) {
e.printStackTrace();
} …
Run Code Online (Sandbox Code Playgroud) 我正在使用cURL
通过给定的 URL 上传文件。(用户提供 URL,我的服务器下载文件)
对于进度条,我使用该CURLOPT_PROGRESSFUNCTION
选项。我希望进度功能还可以计算下载速度,以及还剩多少时间。
$fp = fopen($temp_file, "w");
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOPROGRESS, false );
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, "curl_progress_callback");
curl_setopt($ch, CURLOPT_FILE, $fp);
$success = curl_exec($ch);
$curl_info = curl_getinfo($ch);
curl_close($ch);
fclose($fp);
function curl_progress_callback ($download_size, $downloaded_size, $upload_size, $uploaded_size) {
global $fileinfo;
if (!$downloaded_size) {
if (!isset($fileinfo->size)) {
$fileinfo->size = $download_size;
event_callback(array("send" => $fileinfo));
}
}
event_callback(array("progress" => array("loaded" => $downloaded_size, "total" => $download_size)));
}
Run Code Online (Sandbox Code Playgroud)
谢谢!对不起我的英语
android ×1
benchmarking ×1
curl ×1
http ×1
https ×1
inputstream ×1
java ×1
javascript ×1
limit ×1
php ×1
progress ×1
python ×1
python-2.7 ×1
qprogressbar ×1
qt ×1
ssl ×1
upload ×1