/* * Copyright (c) 2005-2006 Erik Tigerholm * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ using System; using System.Collections.Generic; using System.Text; using System.Xml.Serialization; namespace OpenTraffic { [Serializable] public class TrafficProfile { private long startTicks; [Serializable] private class ProfileInformation { public ProfileInformation(string key, ProfileInformation parent) { this.key = key; ticks = 0; childs = new Dictionary(); this.parent = parent; } public string Key { get { return key; } } private string key; public long ticks; private long lastStartTick; private long number; public ProfileInformation parent; public Dictionary childs; public ProfileInformation Start(string key1) { if (!childs.ContainsKey(key1)) { childs.Add(key1, new ProfileInformation(key1, this)); } childs[key1].lastStartTick = Environment.TickCount; childs[key1].number++; return childs[key1]; } public ProfileInformation Stop() { ticks += Environment.TickCount - lastStartTick; return this.parent; } public override string ToString() { return ToString("", 0); } public string ToString(string tabs, long parentTicks) { string str = tabs + key + ": #" + number + "\t" + ticks; str += "\t" + ticks / 1000 + "s"; if (number != 0) { str += "\t" + (int)(ticks / number); } if (parentTicks != 0) { str += "\t" + (100 * ticks / parentTicks) + "%"; } str += System.Environment.NewLine; foreach (KeyValuePair child in childs) { str += child.Value.ToString(tabs + " ", ticks); } return str; } } private ProfileInformation activeProfileInformation; private ProfileInformation rootProfileInformation; public TrafficProfile() { rootProfileInformation = new ProfileInformation("root", null); activeProfileInformation = rootProfileInformation; startTicks = Environment.TickCount; } public void Start(string name) { activeProfileInformation = activeProfileInformation.Start(name); } public void SStart(string name) { Stop(); Start(name); } public void Stop() { activeProfileInformation = activeProfileInformation.Stop(); } public void Stop(string key) { while (!activeProfileInformation.Key.Equals(key)) { activeProfileInformation = activeProfileInformation.Stop(); } activeProfileInformation = activeProfileInformation.Stop(); } public override string ToString() { rootProfileInformation.ticks = Environment.TickCount - startTicks; return rootProfileInformation.ToString(); } private static TrafficProfile profile = null; // Singleton // This library is not thread safe. Bewara (Pretty hard to be in this case. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")] public static TrafficProfile GetProfile() { if (profile == null) { profile = new TrafficProfile(); } return profile; } } }