2017-02-27 72 views
2

在Visual Studio中測量Android Xamarin應用程序中數據使用情況的最佳方式是什麼?如何測量Android Xamarin應用程序的數據使用情況?

我想知道,爲每個被調用的請求傳輸了多少數據。 我正在尋找Xamarin Profiler,但沒有任何有關數據使用情況的信息。

謝謝。你可以使用

+0

您使用什麼方法來提出請求?通常您可以檢查Web請求的長度和Web響應的長度。 –

回答

2

一種方法是通過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 
} 

然後在您的輸出中輸入w indow使用VSColorOutput extension它產生一個很好的可讀性報告你的請求/響應,包括時間和大小。如果你所追求的只是請求/響應的大小,你可以簡化這段代碼。

enter image description here

相關問題