InputStream转成String考虑高性能

我已经创建了rest-client java 代码并将XML 响应转换为字符串。我找到了三种方法来转换该响应。


HttpURLConnection....

....

InputStream in = new GZIPInputStream(conn.getInputStream());

Gzip使用上面的行将响应隐藏到 InputStream 中。然后我找到了以下3种方法来转换为String。


1 方法


ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] buffer = new byte[4096];

int length = 0;

while ((length = in.read(buffer)) != -1) {

    baos.write(buffer, 0, length);

}


String response =new String(baos.toByteArray());

2 方法


String response = "";

BufferedReader br = new BufferedReader(new InputStreamReader(in));

String output;

while ((output = br.readLine()) != null) {

    response =output;

}

3 方法


StringBuilder sb = new StringBuilder();

for (int c; (c = in.read()) >= 0;)

    sb.append((char) c);

String response = sb.toString();

我需要知道哪种是读取 XML 响应的最佳方式,考虑到高性能/可用性(以减少响应时间)。此代码将在并行进程中每秒使用大约 50-100 个请求。请建议我。


爪哇表现休息


德玛西亚99
浏览 113回答 2
2回答

Smart猫小萌

这是你的答案。我编写了一个简单的代码,可以为您完成所有性能测试:import java.util.*;import java.io.*;import java.time.format.DateTimeFormatter;import java.time.LocalDateTime;public class Main {&nbsp;public static void main(String[] args) throws Exception {&nbsp; // Your code here!&nbsp; String response = "";&nbsp; DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");&nbsp; //STEP 1&nbsp; InputStream in = new ByteArrayInputStream("MYSTREAMASDasdasdSJKDFJASLKFJKSDLFJLKSADJFKSJDKAFJKSDJFLKSDJLKFJLKSDFJLKSDJLKLAKSDJLJKFSDKJADFSLJKLKJSDALJKALJKS adkaslkdajslkdjkaslkdjlaks\nasdasdasdasdasdasdasdasdasdasdasdasdasdas".getBytes());&nbsp; System.out.println("FIRST OPTION");&nbsp; long beginTime = System.nanoTime();&nbsp; //System.out.println("begin -" + beginTime);&nbsp; for (int i = 0; i < 500000; i++) {&nbsp; &nbsp;ByteArrayOutputStream baos = new ByteArrayOutputStream();&nbsp; &nbsp;byte[] buffer = new byte[4096];&nbsp; &nbsp;int length = 0;&nbsp; &nbsp;while ((length = in .read(buffer)) != -1) {&nbsp; &nbsp; baos.write(buffer, 0, length);&nbsp; &nbsp;}&nbsp; &nbsp;response = new String(baos.toByteArray());&nbsp; }&nbsp; long endTime = System.nanoTime();&nbsp; System.out.println(endTime - beginTime);&nbsp; //STEP 2&nbsp; System.out.println("SECOND OPTION");&nbsp; beginTime = System.nanoTime();&nbsp; for (int i = 0; i < 500000; i++) {&nbsp; &nbsp;response = "";&nbsp; &nbsp;BufferedReader br = new BufferedReader(new InputStreamReader( in ));&nbsp; &nbsp;String output;&nbsp; &nbsp;while ((output = br.readLine()) != null) {&nbsp; &nbsp; response = output;&nbsp; &nbsp;}&nbsp; }&nbsp; endTime = System.nanoTime();&nbsp; System.out.println(endTime - beginTime);&nbsp; System.out.println("THIRD OPTION");&nbsp; beginTime = System.nanoTime();&nbsp; for (int i = 0; i < 500000; i++) {&nbsp; &nbsp;StringBuilder sb = new StringBuilder();&nbsp; &nbsp;for (int c;&nbsp; &nbsp; (c = in .read()) >= 0;)&nbsp; &nbsp; sb.append((char) c);&nbsp; &nbsp;response = sb.toString();&nbsp; }&nbsp; endTime = System.nanoTime();&nbsp; System.out.println(endTime - beginTime);&nbsp;}}这里有 500K 循环执行的结果:第一个选项 284684443 毫秒第二个选项 1331922335 毫秒第三个选项 16031272 毫秒正如您将看到的,第三个选项是最快的。这是运行示例

阿晨1998

我已经使用Jemeter - Java Request运行了我的代码,根据统计数据,1 方法显示了与其他方法相比的最佳性能。此测试使用实际的 HTTP 调用运行并将该响应转换为字符串。方法 01&nbsp;成功执行操作:完整:1.393 秒,转换:0.001秒方法 02&nbsp;成功执行操作:完整:1.502 秒,转换:0.021秒方法 03&nbsp;成功执行操作:完整:1.709 秒,转换:0.015秒海滩标记代码在这里:-&nbsp;https://github.com/idthusitha/ride-test
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java