java 写http压测
2016-06-27 10:40
459 查看
本文只是写个简单的小程序
第一个类制作自己的实例,并在自己的实例中创建线程和第二个类的实例,线程中不断循环调用第二个类的方法
第二个类负责访问http连接,判断返回状态
第二个类 :
第一个类制作自己的实例,并在自己的实例中创建线程和第二个类的实例,线程中不断循环调用第二个类的方法
第二个类负责访问http连接,判断返回状态
public class HttpTest { private ConcurrentLinkedQueue<String> concurrentLinkedQueue = new ConcurrentLinkedQueue<String>(); private static int count_up = 0; private static int count_get = 0; private static boolean isRun = true; private static final int threadSize = 2; public static void main(String[] arg){ for(int i = 0; i<threadSize ;i++){ HttpTest httpTest = new HttpTest(); httpTest.input(); httpTest.output(); } } private void input(){ getInputThread().start(); } private void output(){ getOutputThread().start(); } private Thread getInputThread(){ return new Thread(new Runnable() { public void run() { while (isRun){ long start = System.currentTimeMillis(); String key = Test.getkey(); new PYDeepLink().callUp(key); // inputList.add(key); concurrentLinkedQueue.add(key); count_up++; if(count_up%1000 == 0){ System.out.println("up:" + count_up); System.out.println("------------ 输入 : " + (System.currentTimeMillis() - start)); } } } }); } private Thread getOutputThread(){ return new Thread(new Runnable() { public void run() { while (isRun){ long start = System.currentTimeMillis(); String key = concurrentLinkedQueue.poll(); if(null != key){ new PYDeepLink().callGet(key); count_get++; if(count_get%1000 == 0) { System.out.println("gt : " + count_get); System.out.println("------------ 获取 : " + (System.currentTimeMillis() - start)); } } } } }); } }
第二个类 :
import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; class PYDeepLink { private static final String URL_BASE_UP = "http://.../up?v="; private static final String URL_BASE_GET = "http://.../get?v="; private static final int TIME_OUT = 2000; void callUp(String key){ httpUp(URL_BASE_UP + key); } void callGet(String key){ httpGet(URL_BASE_GET + key); } private void httpUp(final String httpUrl) { HttpURLConnection httpURLConnection = null; InputStream inputStream = null; try { URL url = new URL(httpUrl); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoOutput(false); httpURLConnection.setDoInput(true); httpURLConnection.setUseCaches(false); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(TIME_OUT); inputStream = httpURLConnection.getInputStream(); byte[] str = new byte[1]; int readValue = inputStream.read(str); // if (readValue == -1 || !new String(str).equals("t") || httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_OK) { System.out.println("Up server return error"); } } catch (Exception e) { System.out.println("connect error"); } finally { if(null != inputStream) try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } if (null != httpURLConnection) httpURLConnection.disconnect(); } } private void httpGet(final String httpUrl) { HttpURLConnection httpURLConnection = null; InputStream inputStream = null; try { URL url = new URL(httpUrl); httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoInput(true); httpURLConnection.setDoOutput(false); httpURLConnection.setUseCaches(false); httpURLConnection.setRequestMethod("GET"); httpURLConnection.setConnectTimeout(TIME_OUT); inputStream = httpURLConnection.getInputStream(); byte[] str = new byte[18]; int readValue = inputStream.read(str); // if (readValue == -1 || !new String(str).equals("t") || httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_OK) { System.out.println("Get server return error"); } } catch (Exception e) { System.out.println("connect error"); } finally { if (null != inputStream) try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } if (null != httpURLConnection) httpURLConnection.disconnect(); } } }
相关文章推荐
- 大端法、小端法、网络字节序
- 浅谈HTTP中Get与Post的区别
- laravel HTTP路由实例教程(三)—— CSRF攻击原理及其防护
- 用 Java 实现断点续传 (HTTP)
- java web 学习-网络资源
- OKHttp源码解析(二)
- OKHttp源码解析(一)
- 最核心的是要熟悉和掌握HTTP协议
- CRAFT:级联的区域建议网络与快速RCNN结合
- 《HTTP指南》读书笔记
- The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
- 4.网络操作
- “HTTPS”安全在哪里?
- p2p传输
- iOS网络相关知识总结
- 在Android 6.0下继续使用HttpClient
- 已经重写,源码和文章请跳转http://www.cnblogs.com/ymnets/p/5621706.html
- Pull解析网络请求文件
- SOCKET,TCP/UDP,HTTP,FTP网络协议之间的关系
- 欢迎使用CSDN-markdown编辑器