Merge pull request #3386 from alexanderkyte/nunit_lite_return_status
[mono.git] / mcs / class / referencesource / System.Web / Util / Profiler.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="Profiler.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //------------------------------------------------------------------------------
6
7 /*
8  * Profiler.cs
9  *
10  * Copyright (c) 2000 Microsoft Corporation
11  */
12
13 namespace System.Web.Util {
14
15     using System;
16     using System.Web;
17     using System.Web.SessionState;
18     using System.Web.UI;
19     using System.Threading;
20     using System.Collections;
21
22     internal class Profiler {
23         private int             _requestsToProfile;
24         private Queue           _requests;
25         private bool            _pageOutput;
26         private bool            _isEnabled;
27         private bool            _oldEnabled;
28         private bool            _localOnly;
29         private bool            _mostRecent;
30         private TraceMode       _outputMode;
31
32
33         internal Profiler() {
34             _requestsToProfile = 10;
35             _outputMode = TraceMode.SortByTime;
36             _localOnly = true;
37             _mostRecent = false;
38             _requests = new Queue(_requestsToProfile);
39         }
40
41         internal bool IsEnabled {
42             get { return _isEnabled;}
43             set {
44                _isEnabled = value;
45                _oldEnabled = value;
46             }
47         }
48
49         internal bool PageOutput {
50             get {
51                 // calling HttpContext.Current is slow, but we'll only get there if _pageOutput is true.
52                 return (_pageOutput && !(_localOnly && !HttpContext.Current.Request.IsLocal));
53             }
54             set {
55                 _pageOutput = value;
56             }
57         }
58
59         internal TraceMode OutputMode {
60             get { return _outputMode;}
61             set { _outputMode = value;}
62         }
63
64         internal bool LocalOnly {
65             get { return _localOnly;}
66             set { _localOnly = value; }
67         }
68
69         internal bool MostRecent {
70             get { return _mostRecent; }
71             set { _mostRecent = value; }
72         }
73
74         internal bool IsConfigEnabled {
75             get { return _oldEnabled; }
76         }
77
78         internal int RequestsToProfile {
79             get { return _requestsToProfile;}
80             set {
81                 // VSWhidbey195368 Silently cap request limit at 10,000
82                 if (value > 10000) {
83                     value = 10000;
84                 }
85                 _requestsToProfile = value;
86             }
87         }
88
89         internal int RequestsRemaining {
90             get { return _requestsToProfile - _requests.Count;}
91         }
92
93         internal void Reset() {
94             // start profiling and clear the current log of requests
95             _requests = new Queue(_requestsToProfile);
96
97             if (_requestsToProfile != 0)
98                 _isEnabled = _oldEnabled;
99             else
100                 _isEnabled = false;
101         }
102
103         internal void StartRequest(HttpContext context) {
104             context.Trace.VerifyStart();
105         }
106
107         internal void EndRequest(HttpContext context) {
108             context.Trace.EndRequest();
109
110             // Don't add the trace data if we aren't enabled
111             if (!IsEnabled) return;
112
113             // grab trace data and add it to the list
114             lock (_requests) {
115                 _requests.Enqueue(context.Trace.GetData());
116
117                 // If we are storing the most recent, we may need to kick out the first request
118                 if (MostRecent) {
119                     if (_requests.Count > _requestsToProfile) _requests.Dequeue();
120                 }
121             }
122
123             // Turn off profiling if we are only tracking the first N requests and we hit the limit.
124             if (!MostRecent && _requests.Count >= _requestsToProfile) EndProfiling();
125         }
126
127         internal void EndProfiling() {
128             _isEnabled = false;
129         }
130
131         internal IList GetData() {
132             return  _requests.ToArray();
133         }
134
135     }
136 }