/** * author : lipan * filename : HttpUtils.java * create_time : 2014年8月28日 下午5:33:40 */ package com.sets.speedtest.http; import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.Socket; import java.net.URL; import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.Random; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import android.os.AsyncTask; import android.test.AndroidTestCase; import android.util.Log; import com.sets.speedtest.utils.CollectionUtils; /** * @author : lipan * @create_time : 2014年8月28日 下午5:33:40 * @desc : Http Test * @update_person: * @update_time : * @update_desc : * */ public class HttpUtils extends AndroidTestCase { public static final String TAG = HttpUtils.class.getSimpleName(); public static final String URL_ = "http://101test.com/AppWeb/test.bin"; public void testSpeed() { URL url; try { url = new URL("http://cesu.qscm.net/speedtestsvr/test.bin"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5 * 1000); if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) { Long a = System.currentTimeMillis(); InputStream inputStream = connection.getInputStream(); getSpeed(inputStream); Long b = System.currentTimeMillis(); // Log.i("HttpUtils",(b-a)+""); // Log.i("HttpUtils",bytes.length+""); Log.i(TAG, "HOLLY" + (b - a)); } else { Log.i(TAG, "error"); } } catch (Exception e) { e.printStackTrace(); } } /** * 输入流转字节数组 * * @param in * @return * @throws Exception */ public static Integer getSpeed(InputStream in) throws Exception { // 600*1000/30 int readNum = 600 * 1000; // 600k LinkedList<Long> logs = new LinkedList<Long>(); byte[] b = new byte[readNum]; long timeSum = 0; long dataSum = 0; int n; in.read(); long lastTime = System.currentTimeMillis(); while ((n = in.read(b)) != -1) { long nowTime = System.currentTimeMillis(); long cost = nowTime - lastTime; // timeSum += cost; dataSum += n; if (cost >= 30) // 30毫秒 { logs.add(dataSum / cost); dataSum = 0; lastTime = nowTime; } } in.close(); Object[] array = logs.toArray(); Arrays.sort(array); int size = array.length; // 去掉最快的10%和最慢的30% int min = size * 30 / 100; int max = size * 90 / 100; List<Integer> speeds = new ArrayList<Integer>(); for (int i = min; i < max; i++) { speeds.add(((Long) array[i]).intValue()); } return CollectionUtils.getIntListAvg(speeds); } public void testM() { try { Log.i(TAG, "Hello:" + new DownloadCall().call()); } catch (Exception e) { e.printStackTrace(); } } private static final int parallels = 20; private class DownloadCall implements Callable<Integer> { @Override public Integer call() throws Exception { try { Socket[] sockets = new Socket[parallels]; InputStream[] ins = new InputStream[parallels]; for (int i = 0; i < parallels; i++) { sockets[i] = new Socket("www.ixisu.com", 80); Socket s = sockets[i]; s.setReceiveBufferSize(1024000); s.setSendBufferSize(1024000); OutputStreamWriter osw = new OutputStreamWriter( s.getOutputStream()); StringBuffer sb = new StringBuffer(); sb.append("GET /speedtest/test.bin?t="+new Random().nextInt(1000)+" HTTP/1.1\r\n"); sb.append("Host: www.ixisu.com:80\r\n"); sb.append("Connection: Keep-Alive\r\n"); sb.append("\r\n"); osw.write(sb.toString()); osw.flush(); ins[i] = s.getInputStream(); } for (int i = 0; i < parallels; i++) { InputStream in = ins[i]; in.read(); } for (int i = 0; i < parallels; i++) { InputStream in = ins[i]; in.skip(in.available()); } long begin = System.currentTimeMillis(); int total = 0; while (true) { Thread.sleep(100); int size = 0; for (int i = 0; i < parallels; i++) { InputStream in = ins[i]; int available = in.available(); size += available; in.skip(available); } if (size == 0 && total > 0) break; total += size; // TextView result = (TextView) findViewById(R.id.result); // result.setText("size=" + total + ", speed=" // + (total / (System.currentTimeMillis() - begin))); Log.i(TAG,"size=" + total + ", speed=" + (total / (System.currentTimeMillis() - begin))); } for (int i = 0; i < parallels; i++) { sockets[i].close(); } } catch (Exception e) { Log.e(TAG,e.toString()); e.printStackTrace(); } return 0; } } public void testCallable() { try { Callable<Integer> callable1 = new Callable<Integer>() { @Override public Integer call() throws Exception { Thread.sleep(3000); Log.i(TAG, "call1"); return null; } }; Callable<Integer> callable2 = new Callable<Integer>() { @Override public Integer call() throws Exception { Thread.sleep(3000); Log.i(TAG, "call2"); return null; } }; Callable<Integer> callable3 = new Callable<Integer>() { @Override public Integer call() throws Exception { Thread.sleep(3000); Log.i(TAG, "call3"); return null; } }; Callable<Integer> callable4 = new Callable<Integer>() { @Override public Integer call() throws Exception { Thread.sleep(3000); Log.i(TAG, "call4"); return null; } }; Callable<Integer> callable5 = new Callable<Integer>() { @Override public Integer call() throws Exception { Thread.sleep(3000); Log.i(TAG, "call5"); return null; } }; ExecutorService pool = Executors.newCachedThreadPool(); Future<Integer> futrue1 = pool.submit(callable1); Future<Integer> futrue2 = pool.submit(callable2); Future<Integer> futrue3 = pool.submit(callable3); Future<Integer> futrue4 = pool.submit(callable4); Future<Integer> futrue5 = pool.submit(callable5); futrue1.get(); futrue2.get(); futrue3.get(); futrue4.get(); futrue5.get(); } catch (Exception e) { e.printStackTrace(); } } public class Download extends AsyncTask<Object, Object, Object> { @Override protected Object doInBackground(Object... params) { try { URL url = new URL(URL_); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5 * 1000); if (HttpURLConnection.HTTP_OK == connection.getResponseCode()) { int speed = getSpeed(connection.getInputStream()); Log.i(TAG, "xxxx"+speed); return speed; } } catch (Exception e) { e.printStackTrace(); } return null; } } }