0

我有一个调用网络服务的应用程序。我已经记录了使用和不使用 GZIP 完成此呼叫所需的时间。我使用 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 次运行 = 2923 毫秒

使用 GZIP:平均 5 次运行 = 3179 毫秒

4

1 回答 1

1

在时间安排上至少有两个主要贡献:

  • 客户端:连接速度与解码速度
  • 服务器端:连接速度与编码速度

gzip 编码在服务器端可以是静态的或动态的。对于某些内容,以已经压缩的形式存储查询数据是有意义的。对于某些内容,它无法完成,服务器可能已经占用了“压缩引擎”。

ADSL、WLAN 或直接以太网连接之间的时间可能会发生变化。

于 2012-11-05T16:14:51.753 回答