0

我试图对 Kryonet RMI 进行性能测试,测试结果并不令人信服。但是,我认为我可能没有以正确的方式做事。我可以得到一些关于下面代码的反馈吗?

服务器

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Connection;
import com.esotericsoftware.kryonet.Listener;
import com.esotericsoftware.kryonet.Server;
import com.esotericsoftware.kryonet.rmi.ObjectSpace;

public class Razor {      

    static int port = 2551;
    static String send = null;

    // Data Creator, creates data of size s.getBytes().length * 10
    static String createMsg(String s){

        StringBuilder sb = new StringBuilder();

        for (int i = 0 ; i < 10; i++){
            sb.append(s);
        }

        System.out.println(sb.toString().getBytes().length);

        return sb.toString();

    }    

    // Test Interface
    public static interface TestInterface {

        public String getName (String name);

    }    

    //Class implementing the test interface.
    static private class TestImpl implements TestInterface {  

        public String getName (String name) {

            return send;

        }

    }    

    public static void main (String[] args) throws Exception {

        // Creating data of size 160 bytes
        send = createMsg("FooAndBarAndLazy");

        Server server = new Server();

        Kryo kryo = server.getKryo();

        ObjectSpace.registerClasses(kryo);

        kryo.register(TestInterface.class);

        server.start();

        server.bind(port);

        System.out.println("Server started on " + port);        

        TestImpl test = new TestImpl();

        final ObjectSpace objectSpace = new ObjectSpace();

        objectSpace.register(123, test);

        server.addListener(new Listener() {

            public void connected (final Connection connection) {

                objectSpace.addConnection(connection);

            }

        });

    }

}

客户

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryonet.Client;
import com.esotericsoftware.kryonet.rmi.ObjectSpace;

public class TBone {

    static int port = 2551;

    // Bytes to MB converter
    public static float b2mb(long bytes){

        float bb = bytes;

        float ll = 1024 * 1024;

        return bb/ll;

    }

    // Nono Seconds to seconds converter
    public static float ns2s(long nsecs){

        float f = nsecs;
        float t = 1000000000;

        return f/t;

    }



    public static void main (String[] args) throws Exception {

        Client client = new Client();

        Kryo kryo = client.getKryo();

        ObjectSpace.registerClasses(kryo);

        kryo.register(Razor.TestInterface.class);


        client.start();

        client.connect(50000, "localhost", port);


        Razor.TestInterface test = ObjectSpace.getRemoteObject(client, 123, Razor.TestInterface.class);

        String profile = null;

        int bytes = 0;

        long stime = System.nanoTime();

        for (int  i = 0; i < 1000; i++){

            profile = test.getName("");

            bytes = bytes + profile.getBytes().length;

            if (i %100 == 0) System.out.println("Done : " + i);

        }

        long etime = System.nanoTime();

        System.out.println("Total bytes(MB) : " + b2mb(bytes)+" , " + "Total time : " + ns2s((etime-stime))+" seconds");

        client.stop();

    }

}

结果

Done : 0
Done : 100
Done : 200
Done : 300
Done : 400
Done : 500
Done : 600
Done : 700
Done : 800
Done : 900

Total bytes(MB) : 0.15258789 , Total time : 26.139627 seconds

我会想象更多的表现。这是一个有效的测试吗?

4

1 回答 1

0

这不是有效的性能测试,因为即使在 localhost 请求中,您也有很长的延迟,并且您的测试程序在发送下一个请求之前等待服务器响应(因为 rmi 方法调用本质上是阻塞的)。

由于您这样做了 900 次,即使没有任何 java 处理,您也必须等待:number_of_request * travel_time(latency) * 2(一次旅行用于请求,一次旅行用于响应)。

因此,在您的情况下:900 * 0.03 秒(ping localhost 以查看您的本地延迟)~= 27 秒

于 2014-02-06T20:13:32.820 回答