nGrinder SocketTest.groovy

s

import net.grinder.plugin.http.HTTPRequest
import net.grinder.script.GTest
import net.grinder.scriptengine.groovy.junit.GrinderRunner
import net.grinder.scriptengine.groovy.junit.annotation.BeforeProcess
import net.grinder.scriptengine.groovy.junit.annotation.BeforeThread
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import com.s*****.testwa.taskserver.echo.EchoClient

import static net.grinder.script.Grinder.grinder

// import static net.grinder.util.GrinderUtils.* // You can use this if you're using nGrinder after 3.2.3
/**
 * A simple example using the HTTP plugin that shows the retrieval of a
 * single page via HTTP.
 *
 * This script is automatically generated by ngrinder.
 *
 * @author papapa
 */
@RunWith(GrinderRunner)
class SocketTest {
    public static BufferedReader br
    public static InputStream is
    public static PrintWriter pw
    public static OutputStream os
    public static BufferedWriter bw
    GTest test1 = new GTest(1, "事务1")

    HTTPRequest request

    @BeforeProcess
    public static void beforeProcess() {
        //进程初始化
        grinder.logger.info("beforeProcess.")
    }

    @BeforeThread
    public void beforeThread() {
        test1.record(this, "action")
        grinder.threadNumber
        grinder.logger.info("beforeThread.")
    }

    @Before
    public void before() {
        grinder.logger.info("before.")
    }

    @Test
    public void test1() {
        Socket socket = new Socket("talk8xgc.cns*****.com",6700);
        socket.setSoLinger(true,0)

        while(true){

            sleep(10000)

        }
    }

    public void action() {

        Socket socket = new Socket("10.243.248.185",9100);
        socket.setSoLinger()
        try {

            is = socket.getInputStream();
            os = socket.getOutputStream();
            String s1 = "<packet id="Heartbeat">
" +
                    "<body>
" +
                    "<output>
" +
                    "<ip>10.24.3.246</ip>
" +
                    "<mac>00-0B-00-19-54-F7</mac>
" +
                    "<name>71411810004070</name>
" +
                    "<brand>实达</brand>
" +
                    "<model>TP-200K</model>
" +
                    "</output>
" +
                    "</body>
" +
                    "</packet>
";

            bw = new BufferedWriter(new OutputStreamWriter(os,"GBK"));
            //鍚戞湇鍔″櫒绔�彂閫佷竴鏉℃秷鎭�
            bw.write(s1);

            bw.flush();

            //璇诲彇鏈嶅姟鍣ㄨ繑鍥炵殑娑堟伅
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String mess = br.readLine();

            grinder.logger.info("---------mess={}",mess)

        } catch (IOException e) {
            e.printStackTrace();
        }
        socket.setSoLinger(true,0)
    }

}

end

原文地址:https://www.cnblogs.com/lindows/p/11160729.html