Skip to content

Commit

Permalink
Display object size in bytes on the session tab, along with total
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonrclark committed Jul 28, 2011
1 parent 112d954 commit 89416a7
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
31 changes: 25 additions & 6 deletions source/Glimpse.Core/Plugin/Session.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;
using Glimpse.Core.Extensibility;

Expand All @@ -8,6 +10,8 @@ namespace Glimpse.Core.Plugin
[GlimpsePlugin(SessionRequired = true)]
internal class Session : IGlimpsePlugin, IProvideGlimpseHelp
{
internal static string[] Header = new[] {"Key", "Value", "Type", "Size (bytes)"};

public string Name
{
get { return "Session"; }
Expand All @@ -19,20 +23,23 @@ public object GetData(HttpContextBase context)

if (session == null) return null;

var result = new List<object[]>
{
new[] {"Key", "Value", "Type"}
};
var result = new List<object[]> { Header };

foreach (var key in session.Keys)
{
var keyString = key.ToString();
var value = session[keyString];
var type = value != null ? value.GetType().ToString() : null;
result.Add(new[]{keyString, value, type});
result.Add(new[]{keyString, value, type,GetObjectSize(value)});
}

if (result.Count > 1) return result;
if (result.Count > 1)
{
var sessionSize = result.Sum(o => o[3] as long?);
result.Add(new object[] {"Total Session Size", "", "(calculated)", sessionSize});

return result;
}

return null;
}
Expand All @@ -45,5 +52,17 @@ public string HelpUrl
{
get { return "http://getGlimpse.com/Help/Plugin/Session"; }
}

private static long GetObjectSize(object obj)
{
if (obj == null)
return 0;

using (var stream = new MemoryStream())
{
new BinaryFormatter().Serialize(stream, obj);
return stream.Length;
}
}
}
}
55 changes: 54 additions & 1 deletion source/Glimpse.Test.Core/Plugin/SessionTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using Glimpse.Core.Extensibility;
using Glimpse.Core.Plugin;
Expand Down Expand Up @@ -46,17 +47,69 @@ public void Session_GetData_WithNullSession_ReturnsNull()
[Test]
public void Session_GetData_WithEmptySession_ReturnsNull()
{
//TODO: Mock out session state
var session = new Mock<HttpSessionStateBase>();
session.Setup(s => s.Keys).Returns(new NameValueCollection().Keys);

Context.Setup(ctx => ctx.Session).Returns(session.Object);
var data = Plugin.GetData(Context.Object);

Assert.IsNull(data);
Context.VerifyAll();
}

[Test]
public void Session_GetData_ReturnsData()
{
Mock<HttpSessionStateBase> session = CreateSession(new NameValueCollection { { "test", "value" } });
Context.Setup(ctx => ctx.Session).Returns(session.Object);

var data = Plugin.GetData(Context.Object);

var expected = new List<object[]>
{
Session.Header,
new object[] { "test", "value", "System.String", 29 } ,
new object[] { "Total Session Size", "", "(calculated)", 29 },
};

Assert.AreEqual(expected, data);
Context.VerifyAll();
}

[Test]
public void Session_GetData_ToleratesNullValues()
{
Mock<HttpSessionStateBase> session = CreateSession(new NameValueCollection { { "test", null } });
Context.Setup(ctx => ctx.Session).Returns(session.Object);

var data = Plugin.GetData(Context.Object);

var expected = new List<object[]>
{
Session.Header,
new object[] { "test", null, null, 0 } ,
new object[] { "Total Session Size", "", "(calculated)", 0 },
};

Assert.AreEqual(expected, data);
Context.VerifyAll();
}

private static Mock<HttpSessionStateBase> CreateSession(NameValueCollection values)
{
var session = new Mock<HttpSessionStateBase>();
session.Setup(s => s.Keys).Returns(values.Keys);
foreach (string key in values.Keys)
{
string captureKey = key;
session.Setup(s => s[captureKey]).Returns(values[captureKey]);
}
return session;
}

public IGlimpsePlugin Plugin { get; set; }
public Mock<HttpContextBase> Context { get; set; }

[SetUp]
public void Setup()
{
Expand Down

0 comments on commit 89416a7

Please sign in to comment.