/// <summary> /// Saves the specified request. /// </summary> /// <param name="request">The request.</param> public void Save(GlimpseRequest request) { if (GlimpseRequests.Count >= BufferSize) { GlimpseRequests.Dequeue(); } GlimpseRequests.Enqueue(request); }
public virtual void Save(GlimpseRequest request) { // There's a race here that could end up with the buffer being over-cleaned, but I think we're OK with that. GlimpseRequest _; while (_requests.Count > _bufferSize) { _requests.TryDequeue(out _); } _requests.Enqueue(request); }
public void GetGlimpseMetadataById() { var requestId = Guid.NewGuid(); var metadata = new GlimpseRequest(requestId, Store.RequestMetadataMock.Object, new Dictionary<string, TabResult>(), TimeSpan.FromMilliseconds(0)); Store.Save(metadata); var result = Store.GetByRequestId(requestId); Assert.Equal(metadata, result); }
public void BeThreadSafe() { var sut = new ApplicationPersistenceStore(new DictionaryDataStoreAdapter(new Dictionary<string, object>())); Action<ApplicationPersistenceStore> addingRequests = store => { var glimpseRequest = new GlimpseRequest( Guid.NewGuid(), new Mock<IRequestMetadata>().Object, new Dictionary<string, TabResult>(), new Dictionary<string, TabResult>(), new TimeSpan(1000)); for (int requestCounter = 0; requestCounter < 200; requestCounter++) { store.Save(glimpseRequest); Thread.Sleep(10); } }; Action<ApplicationPersistenceStore> gettingRequests = store => { for (int requestCounter = 0; requestCounter < 200; requestCounter++) { // gets will never by found with the given GUID, but that is not a problem, it's even a good // thing for this test, since it will enumerate the complete collection, quicker running into // threading issues while the state is being manipulated while enumerating it. store.GetByRequestId(Guid.NewGuid()); store.GetByRequestParentId(Guid.NewGuid()); store.GetByRequestIdAndTabKey(Guid.NewGuid(), "SomeUnknownTabKey"); store.GetTop(10); Thread.Sleep(14); } }; var invokedDelegates = new List<Tuple<Action<ApplicationPersistenceStore>, IAsyncResult>> { new Tuple<Action<ApplicationPersistenceStore>, IAsyncResult>(addingRequests, addingRequests.BeginInvoke(sut, null, null)), new Tuple<Action<ApplicationPersistenceStore>, IAsyncResult>(gettingRequests, gettingRequests.BeginInvoke(sut, null, null)), new Tuple<Action<ApplicationPersistenceStore>, IAsyncResult>(addingRequests, addingRequests.BeginInvoke(sut, null, null)), new Tuple<Action<ApplicationPersistenceStore>, IAsyncResult>(gettingRequests, gettingRequests.BeginInvoke(sut, null, null)), new Tuple<Action<ApplicationPersistenceStore>, IAsyncResult>(addingRequests, addingRequests.BeginInvoke(sut, null, null)), new Tuple<Action<ApplicationPersistenceStore>, IAsyncResult>(gettingRequests, gettingRequests.BeginInvoke(sut, null, null)) }; foreach (var invokedDelegate in invokedDelegates) { invokedDelegate.Item1.EndInvoke(invokedDelegate.Item2); } }
/// <summary> /// Saves the specified request. /// </summary> /// <param name="request">The request.</param> public void Save(GlimpseRequest request) { lock (queueLock) { if (GlimpseRequests.Count >= BufferSize) { GlimpseRequests.Dequeue(); } GlimpseRequests.Enqueue(request); } }
public void Construct() { var guid = Guid.NewGuid(); var metadataMock = new Mock<IRequestMetadata>(); var pluginData = new Dictionary<string, TabResult>(); var expectedDuration = TimeSpan.FromMilliseconds(5); var metadata = new GlimpseRequest(guid, metadataMock.Object, pluginData, expectedDuration); Assert.Equal(guid, metadata.RequestId); Assert.Equal(pluginData, metadata.TabData); Assert.Equal(expectedDuration, metadata.Duration); Assert.Null(metadata.ParentRequestId); }
public void RespectTheBufferSize(int bufferSize, int requestCount) { var sut = ApplicationPersistenceStoreTester.Create(bufferSize); var glimpseRequest = new GlimpseRequest( Guid.NewGuid(), new Mock<IRequestMetadata>().Object, new Dictionary<string, TabResult>(), new Dictionary<string, TabResult>(), new TimeSpan(1000)); for (int i = 0; i < requestCount; i++) { sut.Save(glimpseRequest); } Assert.Equal(Math.Min(bufferSize, requestCount), sut.GlimpseRequests.Count); }
public void ConvertAGlimpseMetadataObject() { var requestMock = new Mock<IRequestMetadata>(); requestMock.Setup(r => r.GetCookie(Constants.ClientIdCookieName)).Returns("Anything"); requestMock.Setup(r => r.RequestHttpMethod).Returns("POST"); requestMock.Setup(r => r.RequestUri).Returns("http://localhost/"); requestMock.Setup(r => r.ResponseContentType).Returns(@"text/html"); requestMock.Setup(r => r.GetHttpHeader(Constants.UserAgentHeaderName)).Returns(@"FireFox"); var metadata = new GlimpseRequest(Guid.NewGuid(), requestMock.Object, new Dictionary<string, TabResult>(), new Dictionary<string, TabResult>(), TimeSpan.FromMilliseconds(55)); var converter = new GlimpseRequestConverter(); var obj = converter.Convert(metadata); var result = obj as IDictionary<string, object>; Assert.NotNull(result); Assert.True(result.ContainsKey("clientId")); Assert.NotNull(result["clientId"]); Assert.True(result.ContainsKey("dateTime")); Assert.NotNull(result["dateTime"]); Assert.True(result.ContainsKey("duration")); Assert.NotNull(result["duration"]); Assert.True(result.ContainsKey("parentRequestId")); Assert.Null(result["parentRequestId"]); Assert.True(result.ContainsKey("requestId")); Assert.NotNull(result["requestId"]); Assert.True(result.ContainsKey("isAjax")); Assert.NotNull(result["isAjax"]); Assert.True(result.ContainsKey("method")); Assert.NotNull(result["method"]); Assert.True(result.ContainsKey("uri")); Assert.NotNull(result["uri"]); Assert.True(result.ContainsKey("contentType")); Assert.NotNull(result["contentType"]); Assert.True(result.ContainsKey("statusCode")); Assert.NotNull(result["statusCode"]); Assert.True(result.ContainsKey("userAgent")); Assert.NotNull(result["userAgent"]); }
// TODO: Add PRG support /// <summary> /// Ends Glimpse's processing a Http request. /// </summary> /// <exception cref="Glimpse.Core.Framework.GlimpseException">Throws an exception if <c>BeginRequest</c> has not yet been called on a given request.</exception> public void EndRequest() { if (HasOffRuntimePolicy(RuntimeEvent.EndRequest)) return; var frameworkProvider = Configuration.FrameworkProvider; var requestStore = frameworkProvider.HttpRequestStore; var executionTimer = requestStore.Get<ExecutionTimer>(Constants.GlobalTimerKey); if (executionTimer != null) { Configuration.MessageBroker.Publish(new RuntimeMessage().AsSourceMessage(typeof(GlimpseRuntime), MethodInfoBeginRequest).AsTimelineMessage("End Request", TimelineCategory.Request).AsTimedMessage(executionTimer.Point())); } ExecuteTabs(RuntimeEvent.EndRequest); ExecuteDisplays(); Guid requestId; Stopwatch stopwatch; try { requestId = requestStore.Get<Guid>(Constants.RequestIdKey); stopwatch = requestStore.Get<Stopwatch>(Constants.GlobalStopwatchKey); stopwatch.Stop(); } catch (NullReferenceException ex) { throw new GlimpseException(Resources.EndRequestOutOfOrderRuntimeMethodCall, ex); } var requestMetadata = frameworkProvider.RequestMetadata; var policy = DetermineAndStoreAccumulatedRuntimePolicy(RuntimeEvent.EndRequest); if (policy.HasFlag(RuntimePolicy.PersistResults)) { var persistenceStore = Configuration.PersistenceStore; var metadata = new GlimpseRequest(requestId, requestMetadata, TabResultsStore, DisplayResultsStore, stopwatch.Elapsed); try { persistenceStore.Save(metadata); } catch (Exception exception) { Configuration.Logger.Error(Resources.GlimpseRuntimeEndRequesPersistError, exception, persistenceStore.GetType()); } } if (policy.HasFlag(RuntimePolicy.ModifyResponseHeaders)) { frameworkProvider.SetHttpResponseHeader(Constants.HttpResponseHeader, requestId.ToString()); if (requestMetadata.GetCookie(Constants.ClientIdCookieName) == null) { frameworkProvider.SetCookie(Constants.ClientIdCookieName, requestMetadata.ClientId); } } if (policy.HasFlag(RuntimePolicy.DisplayGlimpseClient)) { var html = GenerateScriptTags(requestId); frameworkProvider.InjectHttpResponseBody(html); } }
/// <summary> /// Ends Glimpse's processing a Http request. /// </summary> /// <exception cref="Glimpse.Core.Framework.GlimpseException">Throws an exception if <c>BeginRequest</c> has not yet been called on a given request.</exception> public void EndRequest() // TODO: Add PRG support { if (HasOffRuntimePolicy(RuntimeEvent.EndRequest)) { return; } var frameworkProvider = Configuration.FrameworkProvider; var requestStore = frameworkProvider.HttpRequestStore; var executionTimer = requestStore.Get <ExecutionTimer>(Constants.GlobalTimerKey); if (executionTimer != null) { Configuration.MessageBroker.Publish(new RuntimeMessage().AsSourceMessage(typeof(GlimpseRuntime), MethodInfoBeginRequest).AsTimelineMessage("End Request", TimelineCategory.Request).AsTimedMessage(executionTimer.Point())); } ExecuteTabs(RuntimeEvent.EndRequest); ExecuteDisplays(); Guid requestId; Stopwatch stopwatch; try { requestId = requestStore.Get <Guid>(Constants.RequestIdKey); stopwatch = requestStore.Get <Stopwatch>(Constants.GlobalStopwatchKey); stopwatch.Stop(); } catch (NullReferenceException ex) { throw new GlimpseException(Resources.EndRequestOutOfOrderRuntimeMethodCall, ex); } var requestMetadata = frameworkProvider.RequestMetadata; var policy = DetermineAndStoreAccumulatedRuntimePolicy(RuntimeEvent.EndRequest); if (policy.HasFlag(RuntimePolicy.PersistResults)) { var persistenceStore = Configuration.PersistenceStore; var metadata = new GlimpseRequest(requestId, requestMetadata, TabResultsStore, DisplayResultsStore, stopwatch.Elapsed); try { persistenceStore.Save(metadata); } catch (Exception exception) { Configuration.Logger.Error(Resources.GlimpseRuntimeEndRequesPersistError, exception, persistenceStore.GetType()); } } if (policy.HasFlag(RuntimePolicy.ModifyResponseHeaders)) { frameworkProvider.SetHttpResponseHeader(Constants.HttpResponseHeader, requestId.ToString()); if (requestMetadata.GetCookie(Constants.ClientIdCookieName) == null) { frameworkProvider.SetCookie(Constants.ClientIdCookieName, requestMetadata.ClientId); } } if (policy.HasFlag(RuntimePolicy.DisplayGlimpseClient)) { var html = GenerateScriptTags(requestId); frameworkProvider.InjectHttpResponseBody(html); } }
/// <summary> /// Initializes a new instance of the <see cref="GlimpseRequestHeaders" /> class. /// </summary> /// <param name="glimpseRequest">The glimpse request.</param> public GlimpseRequestHeaders(GlimpseRequest glimpseRequest) { GlimpseRequest = glimpseRequest; }
public void GetOnePluginsData() { var key = "theKey"; var value = new TabResult("A Name", 5); var pluginData = new Dictionary<string, TabResult> { {key, value} }; var id = Guid.NewGuid(); var metadata = new GlimpseRequest(id, Store.RequestMetadataMock.Object, pluginData, TimeSpan.FromMilliseconds(0)); Store.Save(metadata); var result = Store.GetByRequestIdAndTabKey(id, key); Assert.Equal(value, result); }
public void GetTopWithMoreThanRequested() { var pluginData = new Dictionary<string, TabResult>(); var metadata1 = new GlimpseRequest(Guid.NewGuid(), Store.RequestMetadataMock.Object, pluginData, TimeSpan.FromMilliseconds(0)); var metadata2 = new GlimpseRequest(Guid.NewGuid(), Store.RequestMetadataMock.Object, pluginData, TimeSpan.FromMilliseconds(0)); var metadata3 = new GlimpseRequest(Guid.NewGuid(), Store.RequestMetadataMock.Object, pluginData, TimeSpan.FromMilliseconds(0)); var metadata4 = new GlimpseRequest(Guid.NewGuid(), Store.RequestMetadataMock.Object, pluginData, TimeSpan.FromMilliseconds(0)); var metadata5 = new GlimpseRequest(Guid.NewGuid(), Store.RequestMetadataMock.Object, pluginData, TimeSpan.FromMilliseconds(0)); Store.Save(metadata1); Store.Save(metadata2); Store.Save(metadata3); Store.Save(metadata4); Store.Save(metadata5); var result = Store.GetTop(3); Assert.Equal(3, result.Count()); Assert.Equal(metadata1, result.First()); }
public void GetOnePluginsDataReturnsNullWithMisMatchId() { var key = "theKey"; var value = new TabResult("A Name", 5); var pluginData = new Dictionary<string, TabResult> { {key,value} }; var id = Guid.NewGuid(); var metadata = new GlimpseRequest(id, Store.RequestMetadataMock.Object, pluginData, TimeSpan.FromMilliseconds(0)); Store.Save(metadata); var result = Store.GetByRequestIdAndTabKey(Guid.Parse("00000000-0000-0000-0000-000000000000"), key); Assert.Null(result); }