How to test the performance of an API using Java code

Source: Internet
Author: User

The project at hand is a functional and performance test of several API interfaces. Let me give you a brief introduction.

The function is very simple, first there is an API that generates Access_token, this token is used for authentication to other APIs, the other API is very common RESTAPI.

Performance testing is primarily a stress test, where the goal is to get the bottleneck of the system. The first is to see how many request to accept in 1 minutes, the 2nd is to see how many concurrent users can be accepted, and the 3rd is to see how many request can be accepted in 1 hours.

function, very simple, postman on the deal, here does not expand to talk about.

Mainly say that performance, consider jmeter, because not how to use, no experience, so think about yourself to write code test it. Below I will put the code used to paste out.

--------------------------------------

For 1 minutes to accept how many request is the limit, I do, 1 minutes is 60000ms, first see if 200 request system can withstand, that is, every 300ms sent 1 requests, a total of 200, also completed 1 minutes 200 requests. In fact, these can be adjusted in the main function, such as adjusting to 1 hours of 5,000 can also, such as test concurrency, that is, every 0ms send 20 requests, is to test 20 concurrent. The code of the ministry, self-digestion:

--------------****************---------------

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);
}
}

}

How to test the performance of an API using Java code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.