在流媒体服务器中,需要存储直播数据,发送给客户端,由于客户端的网速不一定都是很快,所以需要一个数据结构来存储。
由于直播数据是视频和音频包,实际上是有时间序列的,那么用list最合适了,list需要频繁的在尾部插入,在头部删除。那么就用std::list。
后来发现系统出现问题,大约30%的时间都用在了list的operator++上面:
Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls ms/call ms/call name 25.19 0.33 0.33 33373363 0.00 0.00 std::_List_const_iterator<stream::IMediaBlock*>::operator++()
性能测试不能猜测,绝对不能猜测,必须实测。
用gprof进行测试,主要的步骤是:
编译时,需要加入编译参数,gcc -g -lc_p -c xxx -o xxx.o 链接时,需要加入链接选项,否则无法工作:gcc -pg -o server xxxx.o 编译并启动程序:./server -f conf/main.conf 退出程序,按CTRL+C,可以看到生成了gmon.out,这个就是性能的统计数据。 用gprof生成报表:gprof -b ./server > ~/t.log 将报表生成图片:./gprof2dot.py ~/t.log| dot -Tpng -o ~/out.png
[winlin@dev6 server]$ make clean [winlin@dev6 server]$ make g++ -c -std=c++98 -Wall -Wextra -g -O0 -pg -fPIC -I src/core -o objs/src/core/smt_public.o src/core/smt_public.cpp g++ -pg -o objs/server objs/src/core/smt_public.o -lssl [winlin@dev6 server]$ ./server -f conf/main.conf [winlin@dev6 server]$ gprof -b ./server gmon.out > ~/t.log [winlin@dev6 server]$ ./3rdparty/gprof2dot.py ~/t.log | dot -Tpng -o ~/out.png
tar xf graphviz-2.18.tar.gz cd graphviz-2.18 ./configure make sudo make install echo "we test in Centos6.0, it's ok"
结果发现vectory没有这些问题,不过发现有些地方是性能瓶颈,有些地方不是:
Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls ms/call ms/call name 7.02 0.04 0.04 230519 0.00 0.00 stream::LiveMessageQueue::GetStartTime() 5.26 0.07 0.03 st_writev_resid 5.26 0.10 0.03 heap_insert 3.51 0.12 0.02 256162 0.00 0.00 protocol::ChunkPacketFarm::Flush()
Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls ms/call ms/call name 25.19 0.33 0.33 33373363 0.00 0.00 std::_List_const_iterator<stream::IMediaBlock*>::operator++() 6.11 0.41 0.08 35423959 0.00 0.00 std::_List_const_iterator<stream::IMediaBlock*>::operator!=(std::_List_const_iterator<stream::IMediaBlock*> const&) const 3.82 0.46 0.05 513934 0.00 0.00 stream::LiveMessageQueue::GetStartTime()