mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-01-12 00:04:03 +08:00
Page:
HTTP文件服务器优化:获取文件大小
Pages
Benchmark
Dependency and Copyright
GB28181 SIP信令抓包
GB28181怎么用设备ID作为流ID
GB28181推流
HTTP文件服务器优化:获取文件大小
Home
How to create a live steam
MediaServer支持的HTTP API
MediaServer支持的HTTP HOOK API
Playing URL Rules
Quick Start
RTMP对H265和OPUS的支持
RTMP播放兼容性问题
RTSP性能优化
RTSP推流流程
Starting and Stopping the Server
VideoStack多视频流拼接宫格功能
Windows 版编译说明
ZLMediaKit实现按需拉流
ZLMediaKit实现推流鉴权
ZLMediaKit实现播放鉴权
ZLMediaKit推流测试
ZLMediaKit高并发实现原理
ZLMediakit独家特性介绍
postman 自动生成的restful api接口
rtmp拉流性能测试
rtmp推流性能测试
rtsp拉流性能测试
rtsp推流性能测试
vcpkg方式安装zlmediakit
webrtc信令交互格式
zlmediakit的hls高性能之旅
zlm启用webrtc编译指南
为什么不建议QQ私聊咨询问题?
代码依赖与版权声明
代码篇之onceToken
使用ZLMediaKit实现按需推流
在线测试
延时测试
快速开始
怎么开启https相关功能
怎么测试ZLMediaKit的延时?
性能测试
播放url规则
时序图
服务器的启动与关闭
流媒体相关技术介绍
生成SSL自签名证书并测试
直播延时的本质
视频会议相关资源
配置文件详解
Clone
This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
在优化Http文件服务器时,遇到了一些疑惑,就是怎么获取一个文件的大小? 目前我知道有两种方式,分别是:
- 通过stat函数:
uint64_t getFileLength(const char *file){
struct stat tFileStat;
if (0 != stat(file, &tFileStat)) {
//文件不存在
return 0;
}
return tFileStat.st_size;
}
- 通过ftell方式:
uint64_t getFileLength(FILE *fp){
auto current = ftell(fp);
fseek(fp,0L,SEEK_END); /* 定位到文件末尾 */
auto end = ftell(fp); /* 得到文件大小 */
fseek(fp,current,SEEK_SET);
return end - current;
}
前不久,我在修改ZLMediaKit的http文件服务器时,在实现mmap映射文件的同时,为了精简代码,把代码由stat方式改成了ftell方式, 但是后面不仅就收到了hls直播延时增加的反馈,我怀疑可能是这个改动导致的,所以今天特意测试了两者的性能对比,测试代码如下:
#include <sys/stat.h>
#include <cstdint>
#include <cstdio>
#include <sys/time.h>
#include <string>
using namespace std;
#define MAX_COUNT 1 * 1000000
uint64_t getFileLength(FILE *fp){
auto current = ftell(fp);
fseek(fp,0L,SEEK_END); /* 定位到文件末尾 */
auto end = ftell(fp); /* 得到文件大小 */
fseek(fp,current,SEEK_SET);
return end - current;
}
uint64_t getFileLength(const char *file){
struct stat tFileStat;
if (0 != stat(file, &tFileStat)) {
//文件不存在
return 0;
}
return tFileStat.st_size;
}
static inline uint64_t getCurrentMicrosecondOrigin() {
#if !defined(_WIN32)
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec * 1000000LL + tv.tv_usec;
#else
return std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
#endif
}
class TimePrinter {
public:
TimePrinter(const string &str){
_str = str;
_start_time = getCurrentMicrosecondOrigin();
}
~TimePrinter(){
printf("%s占用时间:%d ms\n",_str.data(),(int)((getCurrentMicrosecondOrigin() - _start_time) / 1000));
}
private:
string _str;
uint64_t _start_time;
};
int main(int argc, char *argv[]) {
const char *file = "/Users/xzl/git/ZLMediaKit/release/mac/Debug/0ca86b00ccd5783194b13be6b0940c43.jpg";
FILE *fp = fopen(file,"rb");
{
TimePrinter printer("stat方式获取文件大小:");
for(int i = 0; i < MAX_COUNT ; ++i){
getFileLength(file);
}
}
{
TimePrinter printer("ftell方式获取文件大小:");
for(int i = 0; i < MAX_COUNT ; ++i){
getFileLength(fp);
}
}
return 0;
}
在我的电脑上,测试获取一个7KB的小文件大小,调用100万次,对比耗时如下:
stat方式获取文件大小:占用时间:1691 ms
ftell方式获取文件大小:占用时间:2590 ms
然后我有测试了一个600MB的大文件,调用100万次,对比耗时如下:
stat方式获取文件大小:占用时间:1682 ms
ftell方式获取文件大小:占用时间:2551 ms
我的电脑是2018款的256GB版本的macbook pro,我这里特意指出容量大小,是因为不同容量大小的ssd读写速度差别很大。 之后我又在一台linux虚拟机(宿主机是机械硬盘)上运行了同样的测试,结果也几乎一致。
从上述测试结果看,得出的结论是:
- 文件的大小并不会影响速度,所以可以肯定的是,这两种方式都没有文件io的操作。
- ftell和stat方式每次执行时间都很短,也没有拉开差距,fseek耗时多点应该是系统调用次数更多的原因。
测试文档
使用教程
- 代码依赖与版权声明
- 快速开始
- vcpkg安装zlmediakit
- 服务器的启动与关闭
- GB28181教程
- 推流播放测试
- RESTful 接口
- RESTful 接口 postman自动生成
- Web Hook 接口
- 配置文件详解
- 播放URL规则
- 按需拉流
- 按需推流
- 播放鉴权
- 推流鉴权
- 怎样创建直播流
- webrtc编译与使用
- webrtc信令交互格式
- 怎么开启https相关功能
相关文档和资源
- zlmediakit独家特性
- zlmediakit的hls高性能之旅
- 高并发实现原理
- RTSP推流流程
- 流媒体相关技术介绍
- 直播延时的本质
- rtmp对H265/opus的支持
- ssl自签名证书测试
- 视频会议相关资源