测试服务器响应时间

简介:
所谓的响应时间,就是从客户端开始发出请求到服务器最后返回请求时所消耗的时间,见代码
package com.speed; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.lang.reflect.Array; 
import java.net.URL; 

public  class TestServer { 
   static  int loopTimes = 500; 

   public Parameter readFromArgFile(String str) { 
    FileInputStream fileInput; 
    BufferedReader br; 
    Parameter param =  new Parameter(); 
     try { 
      fileInput =  new FileInputStream( new File(str)); 
      br =  new BufferedReader( new InputStreamReader(fileInput)); 

      String line; 
       while ((line = br.readLine()) !=  null) { 
         if (line.startsWith( "URL") ==  true && line.indexOf( "=") >= 3) { 
           int f = line.indexOf( "="); 
          String urlstring = line.substring(f + 1); 
          urlstring.trim(); 
          param.url =  new URL(urlstring); 
        }  else  if (line.startsWith( "METHOD") ==  true 
            && line.indexOf( "=") >= 3) { 
           int f = line.indexOf( "="); 
          String method = line.substring(f + 1); 
          method.trim(); 
          param.method = method; 
        }  else  if (line.indexOf( "=") != -1) { 
           int f = line.indexOf( "="); 
          String key = line.substring(0, f - 1); 
          String value = line.substring(f + 1); 
          param.addPair(key.trim(), value.trim()); 
        } 
      } 
      fileInput.close(); 
      br.close(); 
    }  catch (FileNotFoundException e) { 
      System.out.println( "File" + str +  "not found."); 
    }  catch (NullPointerException e) { 

    }  catch (IOException e) { 
      System.out.println(e); 
    } 
     return param; 
  } 

   public  static  void main(String[] args) { 
     int i; 
     int j; 
    Parameter param; 
    TestServer tester =  new TestServer(); 
     for (i = 0; i < Array.getLength(args); i++) { 
      param = tester.readFromArgFile(args[i]); 
       for (j = 0; j < loopTimes; j++) { 
        Thread th =  new Thread( new TestThread(param)); 
        th.start(); 
      } 
    } 
  } 
}
package com.speed; 

import java.lang.reflect.Array; 
import java.net.URL; 

public  class Parameter { 
  URL url; 
  String[] key; 
  String[] value; 
  String method; 
   int length = 0; 

   public  void addPair(String k, String v) { 
    Array.set(key, length, k); 
    Array.set(value, length, v); 
    length++; 
  } 
}
package com.speed; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.util.Date; 

public  class TestThread  implements Runnable { 
  Parameter param; 

  TestThread(Parameter par) { 
    param = par; 
  } 

   public  void run() { 
     long time1 =  new Date().getTime(); 
     try { 
      URL target = param.url; 
      HttpURLConnection conn = (HttpURLConnection) target 
          .openConnection(); 
      conn.setRequestMethod(param.method); 
       int i; 
       for (i = 0; i < param.length; i++) { 
        conn.setRequestProperty(param.key[i], param.value[i]); 
      } 
      conn.connect(); 
      BufferedReader in =  new BufferedReader( new InputStreamReader(conn 
          .getInputStream())); 
      String inputLine; 
       while ((inputLine = in.readLine()) !=  null); 
    }  catch (Exception e) { 

    } 
     long time2 =  new Date().getTime(); 
    System.out.println((time2 - time1)/1000+ "秒"); 
  } 

}
由于这个是在命令行模式下运行的,我的附件是这样写的
URL=http://mail.163.com 
METHOD=GET 
User-Agent=Internet Explorer 
Host=mail.163.com 
Accept=image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-silverlight, */* 
Accept-Language=zh-cn 
Content-Type=application/x-www-form-urlencoded 
Accept-Encoding=gzip, deflate 
User-Agent=Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727;MEGAUPLOAD 1.0) 
Connection=Keep-Alive 
Cache-Control=no-cache
我们来测试一下163的邮件服务器,最后响应时间为14秒,可是单独打开网页的话马上就出现页面了,难道是ajax的过吗?提前展现页面?如果真让用户等待14秒,那还不得疯了,这是个问题,有知道这是为什么的朋友,给我留言告诉我哦!


本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/353618,如需转载请自行联系原作者
相关文章
|
3天前
|
NoSQL 关系型数据库 MySQL
涉及rocketMQ,jemeter等性能测试服务器的安装记录
涉及rocketMQ,jemeter等性能测试服务器的安装记录
20 1
|
3天前
|
JSON 应用服务中间件 Linux
skywalking内部测试服务器安装记录
skywalking内部测试服务器安装记录
13 0
|
3天前
|
监控 测试技术 Apache
如何测试服务器性能?
通过以上步骤,您可以全面评估服务器的性能,找出潜在问题,并采取措施来提高服务器的性能和稳定性。这对于确保服务器在实际生产环境中能够高效运行非常重要。
20 1
|
3天前
|
传感器 Linux 测试技术
xenomai 在X86平台下中断响应时间测试
该文讨论了实时操作系统中断响应时间的重要性,并介绍了x86中断机制和Xenomai的中断管理,包括硬件中断和虚拟中断的处理。Xenomai通过I-Pipe确保实时性,中断优先级高的Xenomai先处理中断。文中还提到了中断响应时间的测试设计,分别针对I-Pipe内核间虚拟中断和硬件中断进行了测试,并给出了在不同负载下的测试结果。
11 0
xenomai 在X86平台下中断响应时间测试
|
3天前
|
弹性计算 运维 监控
|
3天前
|
弹性计算 分布式计算 DataWorks
DataWorks报错问题之ecs自建数据库连通性测试报错如何解决
DataWorks是阿里云提供的一站式大数据开发与管理平台,支持数据集成、数据开发、数据治理等功能;在本汇总中,我们梳理了DataWorks产品在使用过程中经常遇到的问题及解答,以助用户在数据处理和分析工作中提高效率,降低难度。
|
3天前
|
弹性计算 缓存 测试技术
阿里云2核4g服务器(费用价格/性能测试/支持人数)
阿里云2核4g服务器能支持多少人访问?2核4G服务器并发数性能测试,阿小云账号下的2核4G服务器支持20人同时在线访问,然而应用不同、类型不同、程序效率不同实际并发数也不同,2核4G服务器的在线访问人数取决于多个变量因素
|
3天前
|
弹性计算 缓存 测试技术
2核4g服务器能支持多少人访问?阿里云2核4G服务器并发数测试
2核4g服务器能支持多少人访问?阿里云2核4G服务器并发数测试,2核4G服务器并发数性能测试,阿小云账号下的2核4G服务器支持20人同时在线访问,然而应用不同、类型不同、程序效率不同实际并发数也不同,2核4G服务器的在线访问人数取决于多个变量因素
|
3天前
|
存储 负载均衡 监控
epoll服务器百万并发测试
epoll服务器百万并发测试
32 1
|
3天前
|
机器学习/深度学习 数据采集 人工智能
【专栏】AI在软件测试中的应用,如自动执行测试用例、识别缺陷和优化测试设计
【4月更文挑战第27天】本文探讨了AI在软件测试中的应用,如自动执行测试用例、识别缺陷和优化测试设计。AI辅助工具利用机器学习、自然语言处理和图像识别提高效率,但面临数据质量、模型解释性、维护更新及安全性挑战。未来,AI将更注重用户体验,提升透明度,并在保护隐私的同时,通过联邦学习等技术共享知识。AI在软件测试领域的前景广阔,但需解决现有挑战。

热门文章

最新文章