如何集成JAVA Request Sampler + Red5-client库

时间:2018-07-30 12:41:47

标签: jmeter rtmp red5pro

如何为Java Request Sampler实现Java类

使用JMeter进行RTMP性能测试

Red5-最早的用Java编写的RTMP服务器之一 如何实现到Jmeter到Red5 pro服务器 如何测试视频方案 请帮助我...

我想测试RTMP视频流录制是否正确。

1 个答案:

答案 0 :(得分:0)

假设Clustering partitioned tables的示例Java Request sampler实现如下:

package RTMPSubscriber;

import  org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;


public class App implements JavaSamplerClient {



    @Override
    public void setupTest(JavaSamplerContext javaSamplerContext) {

    }

    @Override
    public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
        String url = javaSamplerContext.getParameter("url");
        int port = Integer.parseInt(javaSamplerContext.getParameter("port"));
        String app = javaSamplerContext.getParameter("app");
        String streamName = javaSamplerContext.getParameter("name");
        int timeout = Integer.parseInt(javaSamplerContext.getParameter("timeout"));


        SampleResult result = new SampleResult();
        result.sampleStart();
        result.setSampleLabel("Test Sample");
        Subscriber sub = new Subscriber( url, port, app, streamName, timeout);
        sub.Subscribe();
        result.sampleEnd();
        if(!sub.exception) {
            result.setResponseMessage("OK");
            result.setSuccessful(true);
        } else {
            result.setResponseMessage("Error. See logs. If the log does not " +
                    "contain information about the error, I forgot about some exceptions and I am really sorry. T_T");
            result.setSuccessful(false);
        }
        return result;
    }

    @Override
    public void teardownTest(JavaSamplerContext javaSamplerContext) {

    }

    @Override
    public Arguments getDefaultParameters() {
        Arguments defaultParameters = new Arguments();
        defaultParameters.addArgument("url","localhost");
        defaultParameters.addArgument("port","1935");
        defaultParameters.addArgument("app","live");
        defaultParameters.addArgument("name","stream");
        defaultParameters.addArgument("timeout","600");

        return defaultParameters;
    }
}

参考的Subscriber类:

package RTMPSubscriber;

import org.red5.client.net.rtmp.ClientExceptionHandler;
import org.red5.client.net.rtmp.INetStreamEventHandler;
import org.red5.client.net.rtmp.RTMPClient;
import org.red5.io.utils.ObjectMap;
import org.red5.server.api.event.IEvent;
import org.red5.server.api.event.IEventDispatcher;
import org.red5.server.api.service.IPendingServiceCall;
import org.red5.server.api.service.IPendingServiceCallback;
import org.red5.server.api.stream.IStreamPacket;
import org.red5.server.net.rtmp.event.Notify;
import org.red5.server.net.rtmp.status.StatusCodes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class Subscriber  {

    private Logger log = LoggerFactory.getLogger(Subscriber.class);
    private final String url;
    private final int port;
    private final String application;
    private final String streamName;
    private int timeout;
    private RTMPClient client;
    public boolean exception = false;

    /**
     * @param url
     * @param port
     * @param application
     * @param streamName
     */
    public Subscriber(String url, int port, String application, String streamName, int timeout) {
        this.url = url;
        this.port = port;
        this.application = application;
        this.streamName = streamName;
        this.timeout = timeout;
    }


    public void Subscribe() {

        client = new RTMPClient();
        client.setServiceProvider(this);

        client.setExceptionHandler(new ClientExceptionHandler() {
            @Override
            public void handleException(Throwable throwable) {
                exception=true;
                throwable.printStackTrace();
            }
        });


        client.setStreamEventDispatcher(new IEventDispatcher() {
            @Override
            public void dispatchEvent(IEvent event) {
               // @SuppressWarnings("unused")
                IStreamPacket data = (IStreamPacket) event;
                System.out.println("dispatchEvent: " + event);
                if(event.toString().contains("Video"))
                {
                    System.out.println("dispatchEvent: " + event);
                }
            }
        });


        final INetStreamEventHandler netStreamEventHandler = new INetStreamEventHandler() {
            @Override
            public void onStreamEvent(Notify notify) {
                System.out.println("ClientStream.onStreamEvent: " + notify);
            }
        };
        client.setStreamEventHandler(netStreamEventHandler);


        final IPendingServiceCallback streamCallback = new IPendingServiceCallback() {
            @Override
            public void resultReceived(IPendingServiceCall call) {
                if (call.getServiceMethodName().equals("createStream")) {
                    Number streamId = (Number) call.getResult();
                    // -2: live then recorded, -1: live, >=0: recorded
                    // streamId, streamName, mode, length
                    System.out.println("streamCallback:resultReceived: (streamId " + streamId + ")");
                    client.play(streamId, streamName, -1, 0);

                }
            }
        };

        IPendingServiceCallback connectCallback = new IPendingServiceCallback() {
            @Override
            public void resultReceived(IPendingServiceCall call) {
                ObjectMap<?, ?> map = (ObjectMap<?, ?>) call.getResult();
                String code = (String) map.get("code");
                System.out.println("connectCallback: (code " + code + ")");
                // Server connection established, but issue in connection.
                if (StatusCodes.NC_CONNECT_FAILED.equals(code) || StatusCodes.NC_CONNECT_REJECTED.equals(code) || StatusCodes.NC_CONNECT_INVALID_APPLICATION.equals(code)) {
                    log.error("Rejected: " + map.get("description"));
                    exception=true;
                    client.disconnect();
                    }
                // If connection successful, establish a stream
                else if (StatusCodes.NC_CONNECT_SUCCESS.equals(code)) {
                    client.createStream(streamCallback);
                } else {
                    exception=true;
                    log.error("ERROR code:" + code);
                }
            }
        };
        client.connect(url, port, client.makeDefaultConnectionParams(url, port, application), connectCallback);

            try {
                Thread.sleep(timeout*1000);
            } catch (InterruptedException e) {
                exception=true;
                log.error("Exception: " + e);
            }

        client.disconnect();
        System.out.println("End");
    }

    public void onBWCheck(Object params) {
        System.out.println("onBWCheck: " + params);
    }

    /**
     * Called when bandwidth has been configured.
     */
    public void onBWDone(Object params) {
        System.out.println("onBWDone: " + params);
    }

    public void onStatus(Object params) {
        System.out.println("onStatus: " +  params);
        String status = params.toString();
        // if stream is stopped or unpublished
        if (status.indexOf("Stop") != -1 || status.indexOf("UnPublish") != -1) {
            client.disconnect();
        }
    }
}

更多信息: