Beispiel #1
0
        private void FindMethod()
        {
            currentElement = FindStartedMethod(Thread.CurrentThread.ManagedThreadId);

            try
            {
                StackTrace stackTrace = new StackTrace();
                StackFrame frame;

                for (int i = 0; i < stackTrace.FrameCount; i++)
                {
                    frame = stackTrace.GetFrame(i);
                    Type declaringType = frame.GetMethod().DeclaringType;
                    if (declaringType != null)
                    {
                        string name = declaringType.Name;
                        if (name != typeof(Tracer).Name)
                        {
                            TraceResultElement tmpElement = new TraceResultElement("method", -1, frame.GetMethod().GetParameters().Count(), frame.GetMethod().Name, frame.GetMethod().Module.Name);
                            currentElement.AddElement(tmpElement);
                            currentElement = tmpElement;
                            currentElement.StartTimer();
                            break;
                        }
                    }
                }
            }
            catch
            {
                Console.WriteLine("Ошибка");
            }
        }
Beispiel #2
0
        private void BuildConsoleTree(TraceResultElement pointer, int d)
        {
            LinkedListNode <TraceResultElement> tmpNode;
            int depth = d;

            for (tmpNode = pointer.childs.First; tmpNode != null; tmpNode = tmpNode.Next)
            {
                for (int i = 0; i < depth; i++)
                {
                    Console.Write("-");
                }
                switch (tmpNode.Value.type)
                {
                case "thread":
                    Console.WriteLine("thread id={0}, time={1}ms", tmpNode.Value.id, tmpNode.Value.time);
                    break;

                case "method":
                    Console.WriteLine("method name={0}, time={1}ms, package={2}, paramscount={3}", tmpNode.Value.name, tmpNode.Value.time, tmpNode.Value.package, tmpNode.Value.paramsCount);
                    break;
                }
                if (tmpNode.Value.childs != null)
                {
                    depth += 2;
                    BuildConsoleTree(tmpNode.Value, depth);
                    depth -= 2;
                }
            }
        }
Beispiel #3
0
        private XElement BuildXMLTree(TraceResultElement pointer, XElement e)
        {
            LinkedListNode <TraceResultElement> tmpNode;
            XElement element = e;
            XElement subj    = null;

            for (tmpNode = pointer.childs.First; tmpNode != null; tmpNode = tmpNode.Next)
            {
                switch (tmpNode.Value.type)
                {
                case "thread":
                    subj = new XElement("thread");
                    subj.Add(new XAttribute("time", tmpNode.Value.time));
                    subj.Add(new XAttribute("id", tmpNode.Value.id));
                    break;

                case "method":
                    subj = new XElement("method");
                    subj.Add(new XAttribute("time", tmpNode.Value.time));
                    subj.Add(new XAttribute("package", tmpNode.Value.package));
                    subj.Add(new XAttribute("paramscount", tmpNode.Value.paramsCount));
                    subj.Add(new XAttribute("name", tmpNode.Value.name));
                    break;
                }
                if (tmpNode.Value.childs != null)
                {
                    element.Add(BuildXMLTree(tmpNode.Value, subj));
                }
                else
                {
                    element.Add(subj);
                }
            }
            return(element);
        }
Beispiel #4
0
 public Tracer()
 {
     lock (thisLock)
     {
         root           = new TraceResultElement("root");
         currentElement = root;
         Console.WriteLine("Начало работы");
     }
 }
Beispiel #5
0
 internal void AddElement(TraceResultElement element)
 {
     if (childs == null)
     {
         childs = new LinkedList <TraceResultElement> {
         }
     }
     ;
     childs.AddLast(element);
     element.parent = this;
 }
Beispiel #6
0
        private TraceResultElement FindStartedMethod(int id)
        {
            TraceResultElement tmpThread = GetPointer(id);

            if (tmpThread.childs == null)
            {
                return(tmpThread);
            }
            else
            {
                return(GetRecentMethod(tmpThread, null));
            }
        }
Beispiel #7
0
        private TraceResultElement GetPointer(int id)
        {
            if (CheckForThreads(id) == true)
            {
                LinkedListNode <TraceResultElement> tmpNode;

                for (tmpNode = root.childs.First; tmpNode != null; tmpNode = tmpNode.Next)
                {
                    if (tmpNode.Value.id == id)
                    {
                        return(tmpNode.Value);
                    }
                }
            }

            currentElement = new TraceResultElement("thread", id);
            root.AddElement(currentElement);
            return(currentElement);
        }
Beispiel #8
0
        // метод должен быть вызван после вызова замеряемого метода
        public void StopTrace()
        {
            try
            {
                lock (thisLock)
                {
                    runningElement = FindStartedMethod(Thread.CurrentThread.ManagedThreadId);
                    runningElement.stopTimer();

                    long tmpTime = runningElement.time;
                    runningElement = runningElement.parent;
                    if (runningElement.parent == root)
                    {
                        runningElement.time += tmpTime;
                    }
                }
            }
            catch
            {
                Console.WriteLine("Ошибка остановки");
            }
        }
Beispiel #9
0
        private TraceResultElement GetRecentMethod(TraceResultElement pointer, TraceResultElement found)
        {
            LinkedListNode <TraceResultElement> tmpNode;
            TraceResultElement foundElement = found;

            for (tmpNode = pointer.childs.First; tmpNode != null; tmpNode = tmpNode.Next)
            {
                if (tmpNode.Value.time == 0 && (tmpNode.Next == null || tmpNode.Next.Value.time != 0))
                {
                    foundElement = tmpNode.Value;
                    if (tmpNode.Value.childs == null)
                    {
                        break;
                    }
                    else
                    {
                        foundElement = GetRecentMethod(tmpNode.Value, foundElement);
                    }
                }
            }

            return(foundElement);
        }