在线网络速度测试JAVA程序:思路和控制台主程序

sam 2022年02月24日 352次浏览

事情的缘由

因上级公司的信息化主管部门经常被投诉说是各种业务应用系统反映系统使用慢的问题,而都把问题归结于网速不给力。而业务系统一部份是总公司一级部署的,其它很多都是各个业务部门各自为政建设的,不知道从哪里找来的各种小公司,搞的五法八门的技术路线,没得统一标准、技术平台,所以水平就参差不齐。人少的时候还好点,业务和使用人一多,系统慢得象蜗牛。而网络基础设施这几年来改造、提升得差不多了,都是百兆桌面、千兆骨干了。地市级公司内部虽然办公点分散,但都是使用电力专用光纤建设的千兆骨干的MPLSVPN技术,地市公司与网省公司是千兆或万兆专用光纤。因此其实网络基础设施还是绰绰有余。为了自证清白,上级主管部门就盟生了建一个分层分布式的网速测试系统的想法。你说网速慢,那你在客户端上打开网页,测试一下各级网速,就可能看看到底是网速慢,还是应用系统本身太垃圾。

思路

在网络上给出的参考方法,WEB版的大部份是客户端用JAVASCRIPT下载一个大文件,然后根据速度=总下载量/时间,得到网速;专业的开源测试工具是iperf,是c版本的,和业余的相比,专业的不只一点点。于是,了解了一下iperf的基本思路和方法,于是采用JAVA来写,利用socket直接实现通信,服务端和客户端配合,通过TCP通信的数据量和使用时间来计算网络速度,一方面效率高、减少其它因素(如HTTP协议、JAVASCRIPT加载与执行效率等)的影响;另一方面,利用applet可以实现B/S应用,客户端部署使用简单。程序代码参考了OSCHINA的JGroups项目的JPerf.java。项目名称为jperf,使用eclipse开发和测试,目前实现了基本功能(因目前只是进行验证,因此未进行完整的异常处理、验证等)。

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.text.NumberFormat;
/***
 * Tool to measure TCP throughput, similar to iperf
 * @author Bela Ban
 * @version $Id: JPerf.java,v 1.4 2007/12/28 23:29:17 belaban Exp $
 */
public class jperf implements Runnable{
    boolean client;
    boolean direction;//test direction:true-up test,false-down
    int num;
    InetAddress local_addr;
    int local_port;
    int remote_port;
    InetAddress remote_addr;
    int size;
    int receivebuf=200000;
    int sendbuf=200000;
    static  NumberFormat f;
    Socket client_sock;

    static {
        f=NumberFormat.getNumberInstance();
        f.setGroupingUsed(false);
        f.setMaximumFractionDigits(2);
    }

    public jperf(){
    }

    //实现Thread类的接口Runnable, 用于支持服务端连接的多线程
    @Override
    public void run() {
        // TODO Auto-generated method stub
        try {
            server_accept_data();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    //用于applet和GUI调用
    public jperf(String remote_addr,int remote_port,int num,int size,boolean direction) throws UnknownHostException{

         this.remote_addr=InetAddress.getByName(remote_addr);
         this.remote_port=remote_port;
         this.num=num;
         this.size=size;
         this.direction=direction;
     }

    private void start(boolean client, boolean direction,int num, int size, String local_addr, int local_port,
                       String remote_addr, int remote_port,int receivebuf, int sendbuf) throws IOException, ClassNotFoundException {
        this.client=client;
        this.direction=direction;
        this.num=num;
        this.size=size;
        this.local_addr=InetAddress.getByName(local_addr);
        this.local_port=local_port;
        this.remote_addr=InetAddress.getByName(remote_addr);
        this.remote_port=remote_port;
        this.receivebuf=receivebuf;
        this.sendbuf=sendbuf;

        if(client) {
           client();
        }
        else {
            server();
        }
    }
    //客户端调用
    public String client() throws IOException{

        System.out.println("-- creating socket to " + this.remote_addr + ":" + this.remote_port);
        client_sock=new Socket(this.remote_addr, remote_port);

        String result="";
        if(sendExchangeData()==true){
            if(direction==true) result=sendData(num,size);
            else result=receiveData(num,size);
        }
        else{
            result="connect to server and exchange data fail!";
        }
        System.out.println(result);
        client_sock.close();

        return result;
    }
    //客户端向服务端发送测试 的数据参数
    private boolean sendExchangeData() throws IOException{
        boolean ret=true;

        client_sock.setReceiveBufferSize(receivebuf);
        client_sock.setSendBufferSize(sendbuf);
        ObjectOutputStream write=new ObjectOutputStream(new BufferedOutputStream(client_sock.getOutputStream()));
        write.writeObject(num);
        write.flush();
        write.writeObject(size);
        write.flush();
        write.writeObject(direction);
        write.flush();

        return ret;
    }
    //服务端调用
    private void server() throws IOException, ClassNotFoundException
    {
         ServerSocket srv_sock=new ServerSocket(local_port, 10, this.local_addr);
         System.out.println("-- waiting for client on " + srv_sock.getLocalSocketAddress());
         while(true){
             //wait for a client connect
             Socket client_sock=srv_sock.accept();
             //start a new thread deal this connection:
             jperf thread_client=new jperf();
             thread_client.client_sock=client_sock;
             thread_client.sendbuf=sendbuf;
             thread_client.receivebuf=receivebuf;
             //每一个客户端单独一个线程,支持多个客户端同时连接
             Thread thread=new Thread(thread_client);
             thread.start();
         }

    }
    //服务器接收和发送测试 数据
    private void server_accept_data() throws IOException, ClassNotFoundException{
        client_sock.setReceiveBufferSize(receivebuf);
        client_sock.setSendBufferSize(sendbuf);
        System.out.println("-- accepted data connection from " + client_sock.getRemoteSocketAddress());
        ObjectInputStream in=new ObjectInputStream(new BufferedInputStream(client_sock.getInputStream()));

        int num=0,size=0;
        boolean direction=false;
        num=(Integer)in.readObject();
        size=(Integer)in.readObject();
        direction=(Boolean)in.readObject();

        if(num>0 && size>0) {
            String result;
            if(direction==true) result=receiveData(num,size);
            else result=sendData(num,size);

            System.out.println(result);
         }
         else{
            System.out.println("-- invalid exchange data! ");
         }
         client_sock.close();
    }
    //发送数据,并计算测试结果
    private String sendData(int num,int size) throws IOException
    {
        System.out.println("-- sending data to "+client_sock.getRemoteSocketAddress().toString()+ " total "+num + " messages");
        DataOutputStream out=new DataOutputStream(new BufferedOutputStream(client_sock.getOutputStream()));
        byte[] buf=new byte[size];
        for(int i=0;i<buf.length;i++) buf[i]=(byte)(i%128);
        long start=0, stop;
        int cnt=1;
        int incr=num/10;
        start=System.currentTimeMillis();
        for(int i=0; i < num; i++) {
            out.write(buf, 0, buf.length);
            out.flush();
            if(cnt % incr == 0)
                System.out.println("-- sent " + cnt + " messages");
            cnt++;
        }
        stop=System.currentTimeMillis();
        long diff=stop-start;
        String result=report("发送报文至 "+client_sock.getRemoteSocketAddress().toString(),(long)num*(long)size,diff);

        return result;
    }

    //接收数据,并计算测试结果
    private String receiveData(int num,int size) throws IOException{
        System.out.println("-- accepted data from " + client_sock.getRemoteSocketAddress().toString()+" total "+num+" messages");
        DataInputStream in=new DataInputStream(new BufferedInputStream(client_sock.getInputStream()));
        byte[] buf=new byte[size];
        long counter=0;
        int incr=num/10;
        long start=0, stop;
        while(true) {
            int len=in.read(buf, 0, buf.length);
            if(len<=0) break;
            if(start == 0)
                start=System.currentTimeMillis();
            counter+=len;
            if((counter/size) % incr == 0)
                System.out.println("-- received " + counter/size);
        }
        stop=System.currentTimeMillis();
        long diff=stop-start;
        String result=report("接收报文来自 "+client_sock.getRemoteSocketAddress().toString(),counter,diff);

        return result;
    }
    //计算测试结果
    private String report(String direction,long totalbyte, double diff)
    {
        StringBuilder sb=new StringBuilder();
        double tbs=totalbyte/(1024*1024);
        if(tbs<1000)  sb.append("\n"+direction+",测试数据总数" + f.format(tbs) + "Mbyte"+" ,用时 " + diff + "毫秒 ");
        else{
            tbs=tbs/1024;
            sb.append("\n"+direction+",测试数据总数" + f.format(tbs)+ "Gbyte"+" ,用时 " + diff + "毫秒 ");
        }
         //tcp throughput:
         double throughput=totalbyte / (diff / 1000.0) / 1024.0;
         if(throughput < 1000)
             sb.append("\n网络吞吐量: " + f.format(throughput) + "KB/秒");
         else {
             throughput/=1024.0;
             sb.append("\n网络吞吐量: " + f.format(throughput) + "MB/秒");
         }
         //bandwidth
         double bandwidth=totalbyte / (diff / 1000.0) / 1024.0*8;
         if(bandwidth < 1000){
             sb.append("\n网络带宽: " + f.format(bandwidth) + "Kb/秒");
         }
         else {
            bandwidth/=1024.0;
             if(bandwidth>1000){
                bandwidth/=1024;
                sb.append("\n网络带宽: " + f.format(bandwidth) + "Gb/秒");
             }
             else sb.append("\n网络带宽: " + f.format(bandwidth) + "Mb/秒");
         }
         return sb.toString();
    }
    static void help() {
        System.out.println("JPerf [-help] [-client] [-direction <up|down>] [-num <number of msgs] [-size <bytes>] [-local_addr <interface>] [-local_port <port]" +
                "[-remote_addr <IP addr>] [-remote_port <port>] [-receivebuf <bytes>] [-sendbuf <bytes>]");
    }
    //主程序执行入口
    public static void main(String[] args) throws UnknownHostException {
        boolean client=false;
        boolean direction=false;//test direction:true-up test,false-down test
        int num=10000;
        int size=8192;
        String local_addr=InetAddress.getLocalHost().getHostAddress();
        String remote_addr=local_addr;
        int local_port=5000;
        int remote_port=5000;
        int receivebuf=200000, sendbuf=200000;
        for(int i=0; i < args.length; i++) {
            if(args[i].equals("-client")) {
                client=true;
                continue;
            }
            if(args[i].equals("-num")) {
                num=Integer.parseInt(args[++i]);
                continue;
            }
            if(args[i].equals("-size")) {
                size=Integer.parseInt(args[++i]);
                continue;
            }
            if(args[i].equals("-local_addr")) {
                local_addr=args[++i];
                continue;
            }
            if(args[i].equals("-remote_addr")) {
                remote_addr=args[++i];
                continue;
            }
            if(args[i].equals("-local_port")) {
                local_port=Integer.parseInt(args[++i]);
                continue;
            }
            if(args[i].equals("-remote_port")) {
                remote_port=Integer.parseInt(args[++i]);
                continue;
            }
            if(args[i].equals("-receivebuf")) {
                receivebuf=Integer.parseInt(args[++i]);
                continue;
            }
            if(args[i].equals("-sendbuf")) {
                sendbuf=Integer.parseInt(args[++i]);
                continue;
            }
            if(args[i].equals("-direction")) {
                 String value=args[++i];
                 if(value.toLowerCase().equals("up")) direction=true;
                 else direction=false;

                 continue;
            }
            help();
            return;
        }
        try {
            new jperf().start(client, direction,num, size, local_addr, local_port, remote_addr, remote_port, receivebuf, sendbuf);
        }
        catch(IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

编译代码:javac jperf.java

在服务端执行:java jperf,启动后默认在5000端口上进行监听。

在客户端执行:java jperf -client -remote_addr x.x.x.x,默认参数为数据报文数10000,数据包大小8192(针对默认的WINDOWS系统的TCP窗口),端口5000,方向为测试下行。

Java jperf -client -remote_addr x.x.x.x -direction up测试上行。

经过测试,两台通过百兆口交换机连接的计算机,吞吐量为11.3MB/s,带宽为90.4Mb/s左右。