内网搭建的workerman http 服务上传文件接口,我这边测试了,上传71kb的图片,一共花费10多秒?
这边查看TcpConnnection类baseRead方法那边的代码打印,发现卡在触发 event 读取socket的事件发生很慢,但是baseRead方法执行很快的, 就是触发执行者方法的事件很慢;网速方面,都是局域网,这边测试了网速都有 每秒3M的传输速度,不是卡在上传速度,为什么触发 event 读的事件很慢? 大神walkor帮忙解答一下哈?
代码都是用框架workerman的代码,没动过的。
我截一下我打印的日志,如下图
如上图,上传70多kb的图片,触发baseRead 10多次,且很慢触发。
运行命令 tcpdump -Ans 512 -iany port 8787 看下数据从开始传输到传输完毕用了多久,如果很慢的话就是网络传输问题
我刚刚测了一下,用curl传输图片很快,然后用浏览器 ajax传输很慢,都是走局域网,很奇怪
var data = new FormData();
let objThis = document.getElementById('file_album');
let tFileArr = [];
for (i = 0; i < objThis.files.length; i++) {
//tFileArr.push(objThis.files[i]);
data.append('sys_file_' + i, objThis.files[i]);
}
$.ajax({
url:"XXXXXXXXXXXXXXXXXX",
timeout : 1000 * 300,
contentType:false, //jQuery不要去设置Content-Type请求头
processData:false, //jQuery不要去处理发送的数据
type:"post",
cache:false, //不缓存
dataType:'json', //返回类型json
data: data, //表单数据
success:function (res) {
console.log(res);
},
error:function (error) {
console.log(error);
}
});
php curl 代码模拟上传图片如下: 很快
<?php
function upload_image( $url , $file =null, $header =null) {
$curl= curl_init ();
curl_setopt ( $curl, CURLOPT_URL, $url);
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, FALSE );
//携带cookie文件
// if(! empty($cookie)){
// curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie); //读取cookie
// }
//携带头
if(!empty($header)){
$newHeader = [];
foreach ($header as $k => $v){
$newHeader[] = $k.': '.$v;
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $newHeader);
curl_setopt($curl, CURLOPT_HEADER, 1);//返回response头部信息
}
//curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
//$body = new \CURLFile('@'.realpath($file['path']));
$body = curl_file_create($file['path'],$file['type'],$file['name']);
//return $body;
$fileData = ['upfile'=>$body];
//发送post数据
curl_setopt ( $curl, CURLOPT_POST, 1 );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $fileData);
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($curl, CURLOPT_ACCEPT_ENCODING, "gzip,deflate");
$output= curl_exec ( $curl);
}
$header = [
'timeout' => 300,
'verify' => false,
'proxy' => '',
'headers' => [
'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36',
'Referer' => 'https://www.xxxx.com/',
'Accept-Encoding' => 'gzip, deflate, br',
'Cookie' => "shopeasy_PHPSESSID=PKoxz8",
"Connection" => "Keep-Alive",
"Keep-Alive" => "300",
]
];
//发布文章
$header['headers']['Referer'] = 'http://192.168.0.7:5003';
//判断是否有图片需要上传
//$fielPath = public_path('uploads').'/'.$data->image;
$fielPath = realpath("./testa.jpg");
//b.jfif
$fielPath = realpath("./b.jfif");
$header['headers']['Host'] = '192.168.0.7:5003';
$header['headers']['Origin'] = 'http://192.168.0.7';
$filename = basename($fielPath);
$imageArr = getimagesize($fielPath);
$fileData = [
// 'media' => '@'.$fielPath,
'path'=>$fielPath,
'type'=>$imageArr['mime'],
'name'=>$filename
];
$uploadUrl = 'http://192.168.0.7:5003/?ct=file&ac=upload';
$header['headers']['Content-Type'] = 'multipart/form-data';
$header['headers']['Content-Disposition'] = 'multipart/form-data;name="upfile"; filename="'.$filename.'"';
$imageRes = upload_image($uploadUrl,$fileData,$header['headers']);
print_r($imageRes);