test_benchmark.cpp 2.36 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
xiongziliang committed
6 7 8
 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
xiongziliang committed
9
 */
xiongziliang committed
10

xzl committed
11 12 13 14 15 16 17 18 19
#include <signal.h>
#include <atomic>
#include <iostream>
#include <list>
#include "Util/logger.h"
#include "Util/onceToken.h"
#include "Rtsp/UDPServer.h"
#include "Network/sockutil.h"
#include "Poller/EventPoller.h"
xiongziliang committed
20
#include "Player/PlayerProxy.h"
xzl committed
21 22

using namespace std;
xiongziliang committed
23 24
using namespace toolkit;
using namespace mediakit;
xzl committed
25

xiongziliang committed
26 27
int main(int argc, char *argv[]) {
    //设置退出信号处理函数
28 29
    static semaphore sem;
    signal(SIGINT, [](int) { sem.post(); });// 设置退出信号
30

xiongziliang committed
31
    //设置日志
32
    Logger::Instance().add(std::make_shared<ConsoleChannel>());
xiongziliang committed
33
    Logger::Instance().setWriter(std::make_shared<AsyncLogWriter>());
xzl committed
34

xiongziliang committed
35
    if (argc != 5) {
xiongziliang committed
36
        ErrorL << "\r\n测试方法:./test_benchmark player_count play_interval rtxp_url rtp_type\r\n"
xiongziliang committed
37 38 39 40
               << "例如你想每隔50毫秒启动共计100个播放器(tcp方式播放rtsp://127.0.0.1/live/0 )可以输入以下命令:\r\n"
               << "./test_benchmark 100 50 rtsp://127.0.0.1/live/0 0\r\n"
               << endl;
        return 0;
xzl committed
41

xiongziliang committed
42
    }
43 44 45
    list<MediaPlayer::Ptr> playerList;
    auto playerCnt = atoi(argv[1]);//启动的播放器个数
    atomic_int alivePlayerCnt(0);
46

47
    //每隔若干毫秒启动一个播放器(如果一次性全部启动,服务器和客户端可能都承受不了)
48
    Timer timer0(atoi(argv[2])/1000.0f,[&]() {
49 50 51 52 53
        MediaPlayer::Ptr player(new MediaPlayer());
        player->setOnPlayResult([&](const SockException &ex) {
            if (!ex) {
                ++alivePlayerCnt;
            }
xiongziliang committed
54
        });
55 56
        player->setOnShutdown([&](const SockException &ex) {
            --alivePlayerCnt;
xiongziliang committed
57
        });
58
        (*player)[kBenchmarkMode] = true;
xiongziliang committed
59
        (*player)[kRtpType] = atoi(argv[4]);
60 61 62
        player->play(argv[3]);
        playerList.push_back(player);
        return playerCnt--;
63 64
    }, nullptr);

xzl committed
65

66
    Timer timer1(1,[&]() {
67 68
        InfoL << "存活播放器个数:" << alivePlayerCnt.load();
        return true;
69
    }, nullptr);
70

71
    sem.wait();
xiongziliang committed
72
    return 0;
xzl committed
73 74
}