如何测量客户端和服务器之间使用的网络带宽?

时间:2022-04-03 00:45:54

I have a client which GETs a JSON response from a server. I want to calculate how many requests/responses will consume my allotted transfer allowance from a web hosting company (e.g. 100GB per month).

我有一个从服务器得到JSON响应的客户端。我想计算一下有多少请求/回复会消耗我从网络托管公司分配的转账津贴(例如,每月100GB)。

How do I measure/calculate this?

我如何测量/计算这个?

I assume I only need to measure once because the msgs are of a consistent length and format.

我假设我只需要测量一次,因为msg的长度和格式是一致的。

I have control over client/server/network. All can be uniquely dedicated to the task. The client is an IOS App and the server is a PHP REST Web Service (on Windows). Both on my LAN.

我可以控制客户端/服务器/网络。所有这些都可以专门用于任务。客户机是一个IOS应用程序,服务器是一个PHP REST Web服务(在Windows上)。我在两个局域网。

I know nothing about this and so far have just got the size of the JSON using strlen(). Is that even heading in the right direction?

我对此一无所知,到目前为止,我只使用strlen()获得了JSON的大小。这是朝着正确的方向吗?

3 个解决方案

#1


2  

I would recommend using Charles Proxy. It is an invaluable tool for debugging all kinds of information exchanged via HTTP. You may use it for tracing all HTTP/s communication from and to your iPhone/iPod/iPad as well as the simulator.

我建议使用Charles Proxy。它是调试通过HTTP交换的各种信息的宝贵工具。您可以使用它跟踪您的iPhone/iPod/iPad以及模拟器之间的所有HTTP/s通信。

It does unfortunately not work too well with most Android devices as those do not really support configuring a system-wide HTTP proxy. For those cases and non HTTP-based communication, I would recommend using WireShark.

不幸的是,它在大多数Android设备上运行得不是很好,因为这些设备并不真正支持配置系统范围的HTTP代理。对于这些情况和非基于http的通信,我建议使用WireShark。

In some rare cases, for reasons that are still unclear to me, Charles rarely fails on iOS devices for HTTP-based connections - a typical case would be GoogleAnalytics. For those, again I would recommend WireShark.

在一些罕见的案例中,由于某些原因,我还不清楚,Charles在基于http的连接的iOS设备上很少失败——典型的例子就是GoogleAnalytics。对于这些,我还是推荐WireShark。

#2


2  

The length of JSON string gives you only the size of the payload field in the transferred network packets. This data field may be encapsulated within an HTTP packet and HTTP packet should be put into an IP packet before transmission. Each of these packets have header fields which contributes to total transmission length.

JSON字符串的长度只提供传输网络数据包中有效负载字段的大小。这个数据字段可以封装在一个HTTP包中,而HTTP包应该在传输之前放入一个IP包中。每个包都有头字段,这有助于传输长度。

So, for a precise estimate you should first find the real length of the response packet by using Wireshark or an equivalent tool. If this is the only request type for your application you can divide your bandwidth to the response size of your server application to get maximum number of requests to reach the limit. However, this is usually not the case if you have a web application which has several web pages that are accessible from clients, since any access (browsing) will cause a data transfer from server to client.

因此,为了进行精确的估计,您应该首先使用Wireshark或等效工具找到响应包的实际长度。如果这是您的应用程序的唯一请求类型,您可以将您的带宽分配到服务器应用程序的响应大小,以获得最大限度的请求数量。但是,如果您有一个web应用程序,它有几个可从客户端访问的web页面,那么通常情况下不是这样的,因为任何访问(浏览)都会导致数据从服务器传输到客户端。

#3


1  

Your Apache logs will have the number of bytes of each request, but assuming you want a completely PHP solution, add this to the beginning of your scripts:

您的Apache日志将包含每个请求的字节数,但是如果您想要一个完整的PHP解决方案,请在脚本的开头添加以下内容:

<?php
    function log_input() {
        # get raw post data
        $length = strlen(file_get_contents('php://input'));

        if (function_exists('apache_request_headers')) {
            # quick, but not accurate
            $length += strlen(serialize(apache_request_headers()));
        } else {
            # add fudge for HTTP/1.1 etc
            $length += strlen($_SERVER['QUERY_STRING']) + 14;
            foreach ($_SERVER as $k => $v) {
                if (preg_match('/^HTTP/i', $k)) {
                    $length += strlen($k) + strlen($v);
                }
            }
        }

        $s = sprintf("%s\t%s\n", date('c'), $length);
        file_put_contents('/tmp/input.log', $s, FILE_APPEND);
    }           

    function log_output() {
        $s = sprintf("%s\t%s\n", date('c'), ob_get_length());
        file_put_contents('/tmp/output.log', $s, FILE_APPEND);
    }

    log_input();
    register_shutdown_function('log_output'); 
    ob_start();

?>
<html> ....

#1


2  

I would recommend using Charles Proxy. It is an invaluable tool for debugging all kinds of information exchanged via HTTP. You may use it for tracing all HTTP/s communication from and to your iPhone/iPod/iPad as well as the simulator.

我建议使用Charles Proxy。它是调试通过HTTP交换的各种信息的宝贵工具。您可以使用它跟踪您的iPhone/iPod/iPad以及模拟器之间的所有HTTP/s通信。

It does unfortunately not work too well with most Android devices as those do not really support configuring a system-wide HTTP proxy. For those cases and non HTTP-based communication, I would recommend using WireShark.

不幸的是,它在大多数Android设备上运行得不是很好,因为这些设备并不真正支持配置系统范围的HTTP代理。对于这些情况和非基于http的通信,我建议使用WireShark。

In some rare cases, for reasons that are still unclear to me, Charles rarely fails on iOS devices for HTTP-based connections - a typical case would be GoogleAnalytics. For those, again I would recommend WireShark.

在一些罕见的案例中,由于某些原因,我还不清楚,Charles在基于http的连接的iOS设备上很少失败——典型的例子就是GoogleAnalytics。对于这些,我还是推荐WireShark。

#2


2  

The length of JSON string gives you only the size of the payload field in the transferred network packets. This data field may be encapsulated within an HTTP packet and HTTP packet should be put into an IP packet before transmission. Each of these packets have header fields which contributes to total transmission length.

JSON字符串的长度只提供传输网络数据包中有效负载字段的大小。这个数据字段可以封装在一个HTTP包中,而HTTP包应该在传输之前放入一个IP包中。每个包都有头字段,这有助于传输长度。

So, for a precise estimate you should first find the real length of the response packet by using Wireshark or an equivalent tool. If this is the only request type for your application you can divide your bandwidth to the response size of your server application to get maximum number of requests to reach the limit. However, this is usually not the case if you have a web application which has several web pages that are accessible from clients, since any access (browsing) will cause a data transfer from server to client.

因此,为了进行精确的估计,您应该首先使用Wireshark或等效工具找到响应包的实际长度。如果这是您的应用程序的唯一请求类型,您可以将您的带宽分配到服务器应用程序的响应大小,以获得最大限度的请求数量。但是,如果您有一个web应用程序,它有几个可从客户端访问的web页面,那么通常情况下不是这样的,因为任何访问(浏览)都会导致数据从服务器传输到客户端。

#3


1  

Your Apache logs will have the number of bytes of each request, but assuming you want a completely PHP solution, add this to the beginning of your scripts:

您的Apache日志将包含每个请求的字节数,但是如果您想要一个完整的PHP解决方案,请在脚本的开头添加以下内容:

<?php
    function log_input() {
        # get raw post data
        $length = strlen(file_get_contents('php://input'));

        if (function_exists('apache_request_headers')) {
            # quick, but not accurate
            $length += strlen(serialize(apache_request_headers()));
        } else {
            # add fudge for HTTP/1.1 etc
            $length += strlen($_SERVER['QUERY_STRING']) + 14;
            foreach ($_SERVER as $k => $v) {
                if (preg_match('/^HTTP/i', $k)) {
                    $length += strlen($k) + strlen($v);
                }
            }
        }

        $s = sprintf("%s\t%s\n", date('c'), $length);
        file_put_contents('/tmp/input.log', $s, FILE_APPEND);
    }           

    function log_output() {
        $s = sprintf("%s\t%s\n", date('c'), ob_get_length());
        file_put_contents('/tmp/output.log', $s, FILE_APPEND);
    }

    log_input();
    register_shutdown_function('log_output'); 
    ob_start();

?>
<html> ....