APP专项性能测试 TestNG ReportNG

本文介绍了如何使用TestNG进行APP专项性能测试。首先,确保安装了JDK1.7以上的版本并配置好环境变量。接着,下载TestNG的最新版本jar文件,如testng-6.9.11,并将其放在指定文件夹中,然后配置环境变量TESTNG_HOME。通过这些步骤,可以开始使用TestNG进行APP性能测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1.JAVA环境配置

TestNG是一个Java的框架,所以第一个要求是JDK要安装在你的机器上。

安装JDK1.7以上版本,配置环境变量


2.TestNG环境配置

下载最新版本的TestNG的jar文件,详细请点击访问 http://www.testng.org

我的版本是testng-6.9.11-20160301.075434-41.jar

把JRA包放在一个文件夹中F:\testng


配置环境变量:

TESTNG_HOME    F:\testng

CLASSPATH   %TESTNG_HOME%\testng-6.9.11-20160301.075434-41.jar


3. 创建一个Java类

打开Eclipse,新建Java Project ,名称:TestNGTest

右键单击 TestNGTest,新建class,名称:TestNGSimpleTest 


4.安装TestNG插件

点击help--Install New Software--点击ADD--收入网址 http://beust.com/eclipse--
选中TestNG后一路点击Next下去安装即可,直到Finished之后,重启Eclipse完成安装。

5.输入代码

package APPTest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.testng.Reporter;
import org.testng.annotations.Test;

public class TestNGTest_Demo {
	
	
	// 获取PID
		public static String PID(String PackageName) throws IOException {

	        String PID=null;
	        Runtime runtime = Runtime.getRuntime();
	        Process proc = runtime.exec("adb shell ps |grep "+PackageName);
	        try {
	            if (proc.waitFor() != 0) {
	                System.err.println("exit value = " + proc.exitValue());
	            }
	            BufferedReader in = new BufferedReader(new InputStreamReader(
	                    proc.getInputStream()));
	            StringBuffer stringBuffer = new StringBuffer();
	            String line = null;
	            while ((line = in.readLine()) != null) {
	                stringBuffer.append(line+" ");                                  
	            }
	        String str1=stringBuffer.toString();
	        String str2=str1.substring(str1.indexOf(" "+PackageName)-46,str1.indexOf(" "+PackageName));
	        String str3 =str2.substring(0,7);
	        str3 = str3.trim();
	        PID=str3;  
	        } catch (InterruptedException e) {
	            System.err.println(e);
	        }finally{
	            try {
	                proc.destroy();
	            } catch (Exception e2) {
	            }
	        }

	        return PID;
	  }  
		
		//获取APP占用的CPU
		public static String GetCpu(String packageName) throws IOException {
			String str3 = "null";
			String s = "";
			Runtime runtime = Runtime.getRuntime();
			Process proc = runtime.exec("adb shell dumpsys cpuinfo  $" + packageName 
					+ "|grep " + PID(packageName));
		try {
			if (proc.waitFor() != 0) {
				System.err.println("exit value = " + proc.exitValue());
			}
			BufferedReader in = new BufferedReader(new InputStreamReader(
					proc.getInputStream()));
			StringBuffer stringBuffer = new StringBuffer();
			String line = null;
			while ((line = in.readLine()) != null) {
				stringBuffer.append(line + " ");
			}
			String str1 = stringBuffer.toString();
			s = str1;
			String str2 = str1.substring(str1.indexOf(packageName),
					str1.indexOf(packageName) + 28);
			str3 = str2.substring(18, 23);


		} catch (InterruptedException e) {
			System.err.println(e);
		} finally {
			try {
				proc.destroy();
			} catch (Exception e2) {
			}
		}
		return s;
		}
		
<span style="white-space:pre">		</span>//获取APP的内存情况
		public static String GetMemory(String packageName) throws IOException, InterruptedException {
			  String s1 = "";
			  String s2 = "";
			  String s3 = "";
			  String s4 = "";
			  String s5 = "";
			  String s6 = "";
			  String s7 = "";
			  
	          String str3=null;
	          String str4=null;
	          String str5=null;
	          String str6=null;
	          String str7=null;
	            Runtime runtime = Runtime.getRuntime();
	            Process proc = runtime.exec("adb shell dumpsys meminfo "+packageName);
	            try {

	                if (proc.waitFor() != 0) {
	                    System.err.println("exit value = " + proc.exitValue());
	                }
	                BufferedReader in = new BufferedReader(new InputStreamReader(
	                        proc.getInputStream()));
	                StringBuffer stringBuffer = new StringBuffer();
	                String line = null;
	                while ((line = in.readLine()) != null) {
	                    stringBuffer.append(line+" ");

	                }
	                String str1=stringBuffer.toString();
	                s1= str1;
	                String str2=str1.substring(str1.indexOf("Objects")-68,str1.indexOf("Objects")); 
	                s2=str2;
	                //s3运行内存,s4私有内存,s5堆内存,s6占用的堆内存,s7空闲堆内存
	                str3=str2.substring(0,10);
	                s3=str3;
	                str4=str2.substring(9,19);
	                s4=str4;
	                str5=str2.substring(36,45);
	                s5=str5;
	                str6=str2.substring(45,55);
	                s6=str6;
	                str7=str2.substring(55,65);
	                s7=str7;
	            } catch (InterruptedException e) {
	                System.err.println(e);
	            }finally{
	                try {
	                    proc.destroy();
	                } catch (Exception e2) {
	                }
	            }
	          return "运行内存:"+s3+" 私有内存:"+s4+" 堆内存:"+s5+" 占用的堆内存:"+s6+" 空闲堆内存:"+s7;
}
		
		
		//获取APP总的下载量
		public static double GetFlow(String PackageName) throws IOException {
		    double FlowSize=0;
		    String Pid=PID(PackageName);
		try{
		    Runtime runtime = Runtime.getRuntime();
		    Process proc = runtime.exec("adb shell cat /proc/"+Pid+"/net/dev");
		    try {
		        if (proc.waitFor() != 0) {
		            System.err.println("exit value = " + proc.exitValue());
		        }
		        BufferedReader in = new BufferedReader(new InputStreamReader(
		                proc.getInputStream()));
		        StringBuffer stringBuffer = new StringBuffer();
		        String line = null;
		        while ((line = in.readLine()) != null) {
		            stringBuffer.append(line+" ");


		        }
		    String str1=stringBuffer.toString();
		    String str2=str1.substring(str1.indexOf("wlan0:"),str1.indexOf("wlan0:")+90);
		    String str4=str2.substring(7,16);
		    str4 = str4.trim();
		    int Flow=Integer.parseInt(str4);
		    FlowSize=Flow/1024;

		    } catch (InterruptedException e) {
		        System.err.println(e);
		    }finally{
		        try {
		            proc.destroy();
		        } catch (Exception e2) {
		        }
		    }
		}
		    catch (Exception StringIndexOutOfBoundsException)
		    {

		    }

		    return FlowSize;
		   }
		
		//获取APP每秒的下载量
		public static double Flow(String PackageName) throws IOException, InterruptedException
	    {

	        double Flow1=GetFlow(PackageName);
	        Thread.sleep(1000);
	        double Flow=GetFlow(PackageName)-Flow1;
	        //System.out.println(GetFlow()-Flow1);
	        return Flow ;

	    }
		

}


添加TestNG用例,如果报错,点击错误点,会自动提示,这是需要添加 TestNG library,排除所有错误后,在代码中右键点击,选择Run As ----TestNG Test,至此TestNG可以应用了。

            @Test
	    public void testadd1() throws IOException, InterruptedException {
	        
	        for(int i =1;i<10;i++){
	        	
	        Reporter.log("第"+i+"次"+"----------------------------------------------------------");
	        Reporter.log("包名 : me.chunyu.ChunyuDoctor");
	        Reporter.log("PID:"+PID("me.chunyu.ChunyuDoctor"));
	        Reporter.log("CPU:" + GetCpu("me.chunyu.ChunyuDoctor"));
	        Reporter.log("Memory:" + GetMemory("me.chunyu.ChunyuDoctor"));
	        Reporter.log("总的下载量:"+GetFlow("me.chunyu.ChunyuDoctor")+"M");
	        Reporter.log("每秒下载量:"+Flow("me.chunyu.ChunyuDoctor")+"M");
	        
	        }
	    }




三.配置reportNG

将4个jar包添加到项目的build path中,其中问了避免中文产生乱码,我直接做好了reportNG-1.1.5.jar替代reportNG-1.1.4.jar,省去了乱码的麻烦
下载包地址: 点击打开链接  http://download.youkuaiyun.com/detail/u011910905/9472603

四.配置TestNG

目的是为了输出reportNG的格式,美观,数据统计

Window--Preferences,按照下面的输入填写内容,大功告成。
输入的内容:
org.testng.internal org.testng.TestRunner org.testng.SuiteRunner org.testng.remote.RemoteTestNG org.testng.TestNG sun.reflect java.lang

org.uncommons.reportng.HTMLReporter




最后,运行代码,要以Run As --TestNG Test的方式运行,最后生出下面的结果


































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值