我有一个叫做网络服务的应用。我已经记录了使用和不使用GZIP完成此调用所需的时间。我运行应用程序5次,没有GZIP 5次,实际上GZIP耗时更长。所以我只能认为GZIP没有效果,或者我已经严重执行了它。任何想法为什么没有变化?
public String connect(String url) {
HttpClient httpclient = new DefaultHttpClient();
// Prepare a request object
HttpGet httpget = new HttpGet(url);
httpget.addHeader("Accept-Encoding", "gzip");
// Execute the request
HttpResponse response;
try {
long start = System.currentTimeMillis();
response = httpclient.execute(httpget);
// Examine the response status
Log.i(TAG, response.getStatusLine().toString());
// Get hold of the response entity
HttpEntity entity = response.getEntity();
// If the response does not enclose an entity, there is no need
// to worry about connection release
if (entity != null) {
InputStream instream = response.getEntity().getContent();
Header contentEncoding = response.getFirstHeader("Content-Encoding");
if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
instream = new GZIPInputStream(instream);
}
// A Simple JSON Response Read
//InputStream instream = entity.getContent();
result = convertStreamToString(instream);
Log.i(TAG, result);
// A Simple JSONObject Creation
//json = new JSONObject(result);
// Closing the input stream will trigger connection release
instream.close();
long end = System.currentTimeMillis();
long elapsed = (end - start);
Log.e(TAG, "web call took ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" + elapsed);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
结果:
没有GZIP:平均5次运行= 2923ms
使用GZIP:平均5次运行= 3179ms
答案 0 :(得分:1)
时间安排至少有两个主要因素:
gzip编码在服务器端可以是静态的也可以是动态的。对于某些内容,以已压缩的形式存储查询数据是有意义的。对于某些内容,它无法完成,服务器可能会占用“压缩引擎”。
ADSL,WLAN或直接以太网连接之间的时序可能会发生变化。