如何在jmeter post请求中发送字节数组

时间:2017-08-23 11:17:43

标签: automation jmeter performance-testing jmeter-plugins

有一个WCF服务。我必须访问的方法有1个参数作为输入,谁的类型是Stream。

我不知道如何从JMeter向WCF服务提供流对象。

目前我编写了一个C#方法(MVC项目),它接受来自JMeter的字符串,然后将其转换为Stream类型对象,而不是由WCF服务处理。但是这种方法并没有给出不提供WCF服务的实际性能

[WebInvoke(UriTemplate = "", Method = "POST")]
    [OperationContract]
    public Stream ReceiveChirp(Stream fileContents)
    {
        string response = "";
        long ticks = DateTime.Now.Ticks;
        DateTime receiveTime = DateTime.Now;

目前我正在获得对象类型的例外。

以下是例外:

       <p class="heading1">Request Error</p>
    <p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="http://t2vnmeterservicessciexp.cloudapp.net/ReceiveChirp/help">service help page</a> for constructing valid requests to the service. The exception message is 'Object reference not set to an instance of an object.'. See server logs for more details. The exception stack trace is: </p>
      <p>   at Base.VN.Core.Generic.GenericExtensions.ChirpGenerator.ConvertStringToByteArray(String input)
   at T2.VN.Core.MeterServices.MainEndPoint.ReceiveChirp(Stream fileContents)
   at SyncInvokeReceiveChirp(Object , Object[] , Object[] )
   at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
   at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)
   at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)</p>

我的同事用于对其进行单元测试的代码:

public string TestServicePacket(string data)
        {
            var url = "http://test.net/";
            var requestUrl = string.Format("{0}/ReceiveChirp/", url);
            var request = (HttpWebRequest)WebRequest.Create(requestUrl);
            request.Timeout = Timeout.Infinite;
            request.Method = "POST";
            request.ContentType = "text/plain";

            var dataTrailing = "&UserName=eReg&UserPassword=abc123";
            var dataTrailingBytes = Encoding.ASCII.GetBytes(dataTrailing);

            var bChirp = ConvertStringToByteArray(data);

            var ToSend = new byte[dataTrailingBytes.Length + bChirp.Length];
            ToSend = bChirp.Concat(dataTrailingBytes).ToArray();

            request.ContentLength = ToSend.Length;

            using (var requestStream = request.GetRequestStream())
            {
                // Send the file as body request.
                requestStream.Write(ToSend, 0, ToSend.Length);
                requestStream.Close();
            }
            var response = (HttpWebResponse)request.GetResponse();
            var responseStream = response.GetResponseStream();
            var memoryStream = new MemoryStream();
            string resultResponse;

            resultResponse = ConvertStream(responseStream);
            return resultResponse;
        }

2 个答案:

答案 0 :(得分:2)

我认为最简单的方法是使用HTTP Raw RequestRaw Data Source插件,因为JMeter的HTTP请求采样器参数仅限于字符串,而使用这些插件可以发送&#34; raw& #34;字节数组以及HTTP请求。

您可以使用JMeter Plugins Manager

安装两个测试元素

JMeter Plugins Manager HTTP Raw Request

答案 1 :(得分:0)

我们可以在这里使用Java Request Sampler。我们可以编写自定义代码在字节流中创建我们自己的数据并执行GET / POST。 我们可以查看HttpSampler的代码,在post / get方法中,我们可以返回一个字节数组而不是String。

通过Http Sampler,无法发送字节数组。即HTTP Sampler仅适用于String