目前手头的项目是对几个API接口进行功能和性能测试。下面我简单的介绍一下。
功能很简单,首先有一个产生Access_Token的API,这个token就是用来给其他API做认证用的;其他的API就是很普通的restAPI。
性能测试主要是压力测试,目标是能得到系统的瓶颈在哪里。第一个是看看1分钟能接受多少个request,第2个是看看能接受多少个并发用户,第3个是看看1个小时能接受多少个request。
功能嘛,很简单,POSTMAN就搞定,此处不展开来讲了。
主要来说说性能,考虑过JMETER,因为没怎么用过,没经验,所以就想着自己写代码测吧。 下面我会把用到的代码一并贴出。
--------------------------------------
对于1分钟能接受多少request是极限,我是这么做的,1分钟是60000ms,首先看看200个request系统是否能顶住,那就是每隔300ms发1个请求,总共发200个,也就完成了1分钟200个请求。其实这些都是可以在MAIN函数调整的,比如调整成1个小时发5000个也可以,比如测试并发,就是每隔0ms发送20个请求,就是测试20个并发了。代码奉上,自己消化:
--------------****************---------------
public class RestTest {
private static Queue<Long> accesses = new LinkedList<Long>(); private static int rateLimit = 10;
private static RestTemplate restTemplate = new RestTemplate();
public static void accessTest() {
removeOldAccesses();
if (accesses.size() >= rateLimit) {
System.out.println("Rate limit exceeded.");
} else {
long access = System.currentTimeMillis();
accesses.add(access);
}
} private static synchronized void removeOldAccesses() {
long oneMiniteAgo = System.currentTimeMillis() - 10000;
while (!accesses.isEmpty() && accesses.peek() < oneMiniteAgo) {
long removed = accesses.poll();
System.out.println("Removed access: " + removed);
}
} private static void postCustomer(String server, String token, String body) {
long startTime = System.currentTimeMillis(); String customerUrl = server + "/integrate.customer";
HttpHeaders httpHeaders = createBizHeaders(token);
HttpEntity<String> httpEntity = new HttpEntity<String>(body, httpHeaders);
ResponseEntity<String> response = restTemplate.exchange(customerUrl, HttpMethod.POST, httpEntity, String.class);
System.out.println((System.currentTimeMillis() - startTime) + " ------------"+response.getBody());
} private static String getAccessToken(String server) {
String getTokenUrl = server + "/integrate.getToken?grant_type=client_credentials";
HttpHeaders httpHeaders = createGetTokenHeaders(); HttpEntity<String> httpEntity = new HttpEntity<String>(null, httpHeaders);
ResponseEntity<Map> response = restTemplate.exchange(getTokenUrl, HttpMethod.POST, httpEntity, Map.class);
Map r = response.getBody();
String accessToken = r.get("access_token").toString(); return accessToken;
} private static HttpHeaders createGetTokenHeaders() {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Accept", "application/json");
httpHeaders.set("content-type", "application/x-www-form-urlencoded");
httpHeaders.set("Authorization", "Basic YWN");
return httpHeaders;
} private static HttpHeaders createBizHeaders(String token) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Accept", "application/json");
httpHeaders.set("content-type", "application/json");
httpHeaders.set("Authorization", "OAuth " + token);
return httpHeaders;
} private static String readToString(String fileName) {
String encoding = "UTF-8";
File file = new File(fileName);
Long filelength = file.length();
byte[] filecontent = new byte[filelength.intValue()];
try {
FileInputStream in = new FileInputStream(file);
in.read(filecontent);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
return new String(filecontent, encoding);
} catch (UnsupportedEncodingException e) {
System.err.println("The OS does not support " + encoding);
e.printStackTrace();
return null;
}
} private static void postCustomerNewThread(final String server, final String token, final String body) {
new Thread(new Runnable() {
@Override
public void run() {
postCustomer(server, token, body);
}
}).start();
} public static void main(String[] args) throws Exception {
int requestNumber = 200;
long requestInterval = 300;
final String server = "http://localhost:8080";
final String token = getAccessToken(server);
final String body = readToString("D:customer.json"); for (int i = 1; i <= requestNumber; i++) {
postCustomerNewThread(server, token, body);
Thread.sleep(requestInterval);
}
}}