Here's a example for JS - if you are using C# I can't promise that you can access the same functionality to measure the request.
JS
I found the piece that is creating the request - you can get the string via ctx.get_pendingRequest().$2x_0().toString()
. Afterwards you simply try to get the bytelength via JS (reference).
Working example:
var stringToByte = function(str){
return encodeURI(str).split(/%(?:u[0-9A-F]{2})?[0-9A-F]{2}|./).length - 1;
}
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var webs = web.get_webs();
ctx.load(webs);
alert('Current size is ' + stringToByte(ctx.get_pendingRequest().$2x_0().toString()) + 'bytes');
ctx.load(webs, 'Include(AllProperties, Title)');
alert('Current size is ' + stringToByte(ctx.get_pendingRequest().$2x_0().toString()) + 'bytes');
C#
I managed to get at least some vague Information via the GarbageCollector
. Here's the full example:
using Microsoft.SharePoint.Client;
using System;
using System.Linq;
namespace Tester
{
internal class Program
{
private static void Main(string[] args)
{
try
{
var measure = new GcMeasurer();
ClientContext ctx = new ClientContext("https://sharepoint");
for (var i = 0; i < 1500; i++)
{
measure.StartMonitor();
ctx.Web.AllProperties["_Tester" + i] = RandomString(1000);
measure.EndMonitor();
Console.WriteLine(BytesToString(measure.GetSize()));
}
}
catch (Exception ex)
{
Console.WriteLine("Error:");
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}
Console.Read();
}
private static readonly Random Random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[Random.Next(s.Length)]).ToArray());
}
private static String BytesToString(long byteCount)
{
string[] suf = { "B", "KB", "MB", "GB", "TB", "PB", "EB" }; //Longs run out around EB
if (byteCount == 0)
return "0" + suf[0];
long bytes = Math.Abs(byteCount);
int place = Convert.ToInt32(Math.Floor(Math.Log(bytes, 1024)));
double num = Math.Round(bytes / Math.Pow(1024, place), 1);
return (Math.Sign(byteCount) * num).ToString() + suf[place];
}
}
internal class GcMeasurer
{
private long CurrentSize { get; set; }
private long MonitoredSize { get; set; }
internal GcMeasurer()
{
CurrentSize = 0;
}
internal void StartMonitor()
{
MonitoredSize = GC.GetTotalMemory(true);
}
internal void EndMonitor()
{
CurrentSize += GC.GetTotalMemory(true) - MonitoredSize;
}
internal long GetSize()
{
return CurrentSize;
}
}
}