模拟多用户同时下载文件进行压力测试,但是服务端的网路应用总是20%,不知道是什么原因。
package com.http;import java.io.BufferedInputStream;
import java.io.File;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.concurrent.CountDownLatch;import com.TimeUtilty;public class HttpFileDownload {
private static int THREAD_COUNT = 10; private CountDownLatch latch = new CountDownLatch(THREAD_COUNT); private long completeLength = 0; private long fileLength; private static List<Long> actionTimeList = new ArrayList<Long>(); public HttpFileDownload(String clientCount) {
THREAD_COUNT = Integer.parseInt(clientCount);
latch = new CountDownLatch(THREAD_COUNT);
} //进行下载的线程
class DownloadThread extends Thread {
private URL url; private RandomAccessFile file; private long from; private long end; /**
 * @param url 下载的URL
 * @param file 下载完成之后存储的文件
 * @param from 当前线程对应文件的起始位置
 * @param end 当前线程对应文件的结束位置
 */
DownloadThread(URL url, RandomAccessFile file, long from, long end) {
this.url = url;
this.file = file;
this.from = from;
this.end = end; } public void run() {
try { Long file_request_time = System.currentTimeMillis(); long pos = from;
byte[] buf = new byte[1024]; HttpURLConnection cn = (HttpURLConnection) url.openConnection(); //因为有些网站根据它判断是否是盗链接
cn.setRequestProperty("Referer", "http://172.28.131.80:8080"); //设置请求的起始和结束位置。
cn.setRequestProperty("Range", "bytes=" + from + "-" + end); //如果请求错误,重试。
if (cn.getResponseCode() != 200 && cn.getResponseCode() != 206) {
System.out
.println("Responce Code: " + cn.getResponseCode());
run();
return;
} BufferedInputStream bis = new BufferedInputStream(cn
.getInputStream());
int len;
while ((len = bis.read(buf)) > 0) {
synchronized (file) {
file.seek(pos);
file.write(buf, 0, len);
}
pos += len;
completeLength += len;
if (completeLength * 100 / fileLength % 10 == 0) {
//System.out.println(Thread.currentThread().getName() + ":" + completeLength * 100 / fileLength + "%");
}
}
cn.disconnect();
long transfer_time = System.currentTimeMillis(); long downLoadTime = transfer_time - file_request_time;
System.out.println(Thread.currentThread().getName()
+ "--------StartTime--------------- :"
+ milltoString2(file_request_time)); System.out.println(Thread.currentThread().getName()
+ "--------End Time--------------- :"
+ milltoString2(transfer_time)); System.out.println(Thread.currentThread().getName()
+ "--------DownLoad Time----------------- :"
+ milltoString(downLoadTime)); actionTimeList.add(downLoadTime); } catch (Exception e) {
e.printStackTrace();
} latch.countDown();
}
} public static String milltoString(long millseconds) { // convert millionseconds
// into string
DateFormat formatter = new SimpleDateFormat("mm分ss.SSS秒");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millseconds); // connection sum time ;
return formatter.format(calendar.getTime());
} public static String milltoString2(long millseconds) { // convert millionseconds
// into string
DateFormat formatter = new SimpleDateFormat("HH时mm分ss.SSS秒");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(millseconds); // connection sum time ;
return formatter.format(calendar.getTime());
} public void download(String address) throws Exception { //计算每个线程请求的起始位置和结束位置。
long pos = 0;

URL url = new URL(address);
URLConnection cn = url.openConnection();
//        cn.setRequestProperty("Referer", "http://localhost:8080");
fileLength = cn.getContentLength(); File fileDest = new File("C:\\httpTest");
if (!fileDest.exists()) {
fileDest.mkdir();
} for (int i = 0; i < THREAD_COUNT; i++) { RandomAccessFile file = new RandomAccessFile(
"C:\\httpTest\\Example" + i + ".rar", "rw");

new DownloadThread(url, file, pos, fileLength).start();
}
latch.await();
if (latch.getCount() == 0) {
TimeUtilty time = new TimeUtilty();
time.setArrayList(actionTimeList);
System.out.println("MaxTime:" + (time.getMaxTime()));
System.out.println("MinTime:" + (time.getMinTime()));
System.out.println("AverageTime:"
+ (time.getAverageTime()));
}
} public static void main(String[] args) throws Exception {
long time = System.currentTimeMillis();
new HttpFileDownload(args[0])
.download("http://172.28.131.80:8080/test5m.rar");
System.out.println("共用时:"
+ milltoString(System.currentTimeMillis() - time));
}
}