如何衡量Android Xamarin应用程序的数据使用情况?

时间:2017-02-27 15:48:22

标签: android visual-studio xamarin xamarin.android

在Visual Studio中测量Android Xamarin应用中的数据使用情况的最佳方法是什么?

我想知道,为每个被叫请求传输了多少数据。 我在寻找Xamarin Profiler,但没有关于数据使用的任何信息。

感谢。

1 个答案:

答案 0 :(得分:3)

您可以使用的一种方法是通过Android设备监视器来监视网络流量

enter image description here

enter image description here

或者,如果您在自定义处理程序中使用HttpClient并记录请求有效负载的大小,则可以包装您的请求:

public class RequestLoggerHandler : HttpClientHandler
{

#if DEBUG
    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var stopwatch = Stopwatch.StartNew();
        HttpResponseMessage response = null;
        var headers = request.Headers;
        var responseString = string.Empty;
        var requestString = string.Empty;
        var outputStringBuilder = new StringBuilder();

        const string LINE_ENDING = "===================================================================================================";
        const string SECTION_ENDING = "---------------------------------------------------------------------------------------------------";

        try
        {
            if (request.Content != null) requestString = await request.Content?.ReadAsStringAsync();
            response = await base.SendAsync(request, cancellationToken);
            responseString = await response.Content?.ReadAsStringAsync();

            outputStringBuilder.AppendLine(LINE_ENDING);

            // Headers
            outputStringBuilder.AppendLine("REQUEST HEADERS:");
            foreach (var header in headers)
                outputStringBuilder.AppendLine($"HEADER: {header.Key}: {header.Value?.ToList()?.FirstOrDefault()}");
            outputStringBuilder.AppendLine(SECTION_ENDING);

            // Parameters
            outputStringBuilder.AppendLine("REQUEST PARAMS:");
            outputStringBuilder.AppendLine(requestString);
            outputStringBuilder.AppendLine(SECTION_ENDING);

            // Response
            outputStringBuilder.AppendLine("RESPONSE:");
            outputStringBuilder.AppendLine(responseString);
            outputStringBuilder.AppendLine(SECTION_ENDING);

            return response;
        }
        finally
        {
            stopwatch.Stop();
            var totalSize = 0L;

            if (response != null)
            {
                var bodylength = response.Content.Headers.ContentLength;
                var headerlength = response.Headers.ToString().Length;
                totalSize = bodylength.GetValueOrDefault() + headerlength;
            }

            outputStringBuilder.AppendLine(string.Format("REQUEST [{0}:{1}] Time:{2}| Size:{3}| HTTP-CODE:{4}",
                request.Method.ToString(),
                request.RequestUri,
                stopwatch.Elapsed.ToString("ss\\.fff"),
                totalSize.ToPrettyByteSize(),
                response?.StatusCode.ToString() ?? "No Internet Connectivity"));

            outputStringBuilder.AppendLine(LINE_ENDING);

            Debug.WriteLine("\n" + outputStringBuilder);
        }
    }
#endif
}

然后在使用VSColorOutput extension的输出窗口中,它会生成一个很好的可读报告,包括您的请求/响应,包括时间和大小。如果你所追求的只是请求/响应大小,你可以简化这段代码。

enter image description here