koraktor / steam-condenser

A multi-language library for querying the Steam Community, Source, GoldSrc servers and Steam master servers
https://koraktor.de/steam-condenser
Other
356 stars 67 forks source link

Long time of initialize() GoldSrc server. #243

Closed ghost closed 10 years ago

ghost commented 10 years ago

Someone knows why the time initialize() server is not directly proportional to the increase of the number of respondents servers? 1 server: 0.100690964 s 2 servers: 4.701786192 s 3 servers: 4.859124065 s 4 servers: 9.422729783 s 5 servers: 14.101701261 s 10 servers: 15.83547205 - 23 s 20 servers: 15.836554095 - 71.335466586 s

Maybe i am made something wrong, or maybe need use multithreading?

I am use a code:

try {
    MasterServer master = new MasterServer("141.101.245.171:27010");
    servers = master.getServers();
} catch (SteamCondenserException e) {
    e.printStackTrace();
    System.out.println("SteamCondenserException e happened");
} catch (TimeoutException e) {
    e.printStackTrace();
    System.out.println("TimeoutException e happened");
}

long start = System.nanoTime();
for (int i = 0; i <= 10 - 1; i++) { // for initialize 10 servers
    try {
        System.out.println("Start initialize...");
        GoldSrcServer temp_server = new GoldSrcServer(servers.get(i).getAddress(), servers.get(i).getPort());
        temp_server.initialize();
    } catch (SteamCondenserException e) {
        //e.printStackTrace();
        System.out.println("SteamCondenserException if initializing the socket fails");
    } catch (TimeoutException e) {
        //e.printStackTrace();
        System.out.println("TimeoutException if too many timeouts occur while querying the server");
    }
}
long end = System.nanoTime();
long traceTime = end - start;
System.out.println(traceTime / 1000000000.0);
}
koraktor commented 10 years ago

initialize() makes several requests to a server. Depending on the response time of an individual server this may vary from server to server. Additionally a server might respond faster or slower from time to time.

Generally, you might leverage better results when using multi-threading, but the times for an individual server might still vary a lot.

ghost commented 10 years ago

Ok, i am using threads for this (pool of 50 threads). But this not so fast as in standard serverbrowser by Steam or in game.

        ExecutorService executor = Executors.newFixedThreadPool(50);
        for (int i = 0; i < servers.size() - 1; i++) {
            Runnable worker = new GetServersInfo(servers.get(i).getAddress().toString().replace("/", ""), servers.get(i).getPort(), "" + i);
            executor.execute(worker);
        }
        executor.shutdown();
        //while (!executor.isTerminated()) {        }
        System.out.println("Finished all threads");
    public class GetServersInfo implements Runnable {

        private String address;
        private int port;
        private String command;

        public GetServersInfo(final String address_arg, int port_arg, String s) {
            this.address = address_arg;
            this.port = port_arg;
            this.command = s;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " Start. Command = " + command);
            try {
                GoldSrcServer temp_server = new GoldSrcServer(address, port);
                temp_server.initialize();
            } catch (SteamCondenserException e) {
                //e.printStackTrace();
                System.out.println("SteamCondenserException if initializing the socket fails");
            } catch (TimeoutException e) {
                //e.printStackTrace();
                System.out.println("TimeoutException if too many timeouts occur while querying the server");
            }
            System.out.println(Thread.currentThread().getName() + " End.");
        }

        @Override
        public String toString() {
            return this.command;
        }
    }
koraktor commented 10 years ago

Like said before initialize() makes several requests. At least the request for a challenge number is not required if you just want to list the servers. So you might be better off with just using the following:

temp_server.updatePing();
temp_server.updateServerInfo();