JAVA简单性能检测

领导的需求总是严要求伴随着扯淡,这次让我不能用jmeter,不能用LR,不能用各种性能测试工具,写一个JAVA测性能。哎。。。

需求详细:

读取文本中URL访问,必须以随机读取的方式访问,然后记录下URL的耗时,以及总计时间,平均时间。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class test {
    private static final String String = null;
    public static long totalCost = 0;
    public static void main(String[] args) throws RemoteException, InterruptedException, IOException {
        int threadCount = Integer.parseInt(args[0]);
        int times = Integer.parseInt(args[1]);
//        int threadCount=10;
//        int times=100;
        File f1=new File("./java/urls.txt");
        File f2=new File("./java/A.txt");
        FileWriter writer = null;
        writer = new FileWriter(f2,true);
        BufferedWriter bw = new BufferedWriter(writer);
        FileReader reader = new FileReader(f1);        
        BufferedReader br = new BufferedReader(reader);
        String line="";
        ArrayList al=new ArrayList();
        while((line=br.readLine())!=null){
            al.add(line);
        }
        reader.close();
        int size=al.size();
        Random rnd = new Random();    
        ExecutorService p = Executors.newFixedThreadPool(threadCount);
        long start=System.currentTimeMillis();
        for(int i=0;i<times;i++){
            for(int j=0;j<threadCount;j++){
                int p1=rnd.nextInt(size);
                String l=(java.lang.String) al.get(p1);
                p.execute(new thread(l));
            }
        }
        long total=0;
        while (p.isTerminated() == false){
            p.shutdown();
            total=System.currentTimeMillis() - start;
        }
        bw.write("total: "+total+"ms");
        bw.newLine();
        bw.flush();
        bw.write("avg: "+(total/threadCount)+"ms");
        bw.newLine();
        bw.flush();
    }
}
public class thread extends Thread {
    String url = "";
    public thread(String line) {
        // TODO Auto-generated constructor stub
        this.url = line;
    }
    public void run(){
        javaurl a1=new javaurl();
        a1.getURLContent(url);
    }
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class javaurl {
      public static String getURLContent(String urlStr) {                 
             URL url = null;  /** 网络的url地址 */        
             HttpURLConnection httpConn = null;  /** http连接 */               
             BufferedReader in = null; /**//** 输入流 */ 
             StringBuffer sb = new StringBuffer(); 
             long start= System.currentTimeMillis();
             try{   
                 url = new URL(urlStr);   
                 in = new BufferedReader( new InputStreamReader(url.openStream(),"UTF-8") ); 
                 String str = null;  
                 while((str = in.readLine()) != null) {  
                     sb.append( str );   
                 }   
             } catch (Exception ex) { 
                 ex.printStackTrace();
             } finally{  
                      try{           
                          if(in!=null) {
                              in.close();   
                          }   
                      }catch(IOException ex) {    
                          ex.printStackTrace();
                      }   
             }   
             String result =sb.toString();   
//             System.out.println(result);   
             long ks=System.currentTimeMillis() - start;
             File f2=new File("./A.txt");
                FileWriter writer = null;
                try {
                    writer = new FileWriter(f2,true);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                BufferedWriter bw = new BufferedWriter(writer);
                try {
                    bw.write(url+"  "+ks+"ms");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    bw.newLine();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    bw.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }    
             return result;  
        }  

}

因为时间的关系,没好好把代码整理,应该还能优化很多。

原文地址:https://www.cnblogs.com/leonxiaosi/p/2892933.html