summaryrefslogtreecommitdiffstats
path: root/libktorrent/util/profiler.cpp
blob: 4da9e7fb78ce800d06d3d439b3a04ea4d9aaca31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/***************************************************************************
 *   Copyright (C) 2005 by Joris Guisson                                   *
 *   [email protected]                                               *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/
#ifdef KT_PROFILE
#include <tqfile.h>
#include <tqtextstream.h>
#include <sys/time.h>
#include "profiler.h"

namespace bt
{
	Profile::Profile(Profile* parent,const TQString & name) : parent(parent),name(name)
	{
		min = max = avg = 0.0;
		count = 0;
		start_time = 0.0;
		children.setAutoDelete(true);
	}
	
	Profile::~Profile()
	{
	}
		
	void Profile::start()
	{
		struct timeval tv;
		gettimeofday(&tv,0);
		start_time = tv.tv_sec * 1000.0 + tv.tv_usec * 0.001;
	}
		
	void Profile::end()
	{
		struct timeval tv;
		gettimeofday(&tv,0);
		double end_time = tv.tv_sec * 1000.0 + tv.tv_usec * 0.001;
		double d = end_time - start_time;
		// update stuff
		
		if (d < min || count == 0)
			min = d;
		if (d > max || count == 0)
			max = d;
		
		avg = (avg * count + d) / (count + 1);
		count++;
	}
		
	Profile* Profile::child(const TQString & name)
	{
		TQPtrList<Profile>::iterator i = children.begin();
		while (i != children.end())
		{
			Profile* p = *i;
			if (p->name == name)
				return p;
			i++;
		}
		
		Profile* p = new Profile(this,name);
		children.append(p);
		return p;
	}
	
	void Profile::save(TQTextStream & out,const TQString & base)
	{
		TQString nb = base + "/" + name;
		
		out.precision(5);
		out << qSetW(60) << nb << qSetW(10) << min << qSetW(10) << max << qSetW(10) << avg << qSetW(10) << count << endl;
		
		TQPtrList<Profile>::iterator i = children.begin();
		while (i != children.end())
		{
			Profile* p = *i;
			p->save(out,nb);
			i++;
		} 
	}
	
	/////////////////////
	
	Profiler Profiler::inst;

	Profiler::Profiler() : curr(0),root(0)
	{
		root = new Profile(0,"root");
		curr = root;
	}


	Profiler::~Profiler()
	{
		delete root;
	}

	void Profiler::start(const TQString & s)
	{
		curr = curr->child(s);
		curr->start();
	}
	
	void Profiler::end()
	{
		curr->end();
		curr = curr->getParent();
	}

	void Profiler::saveToFile(const TQString & fn)
	{
		TQFile fptr(fn);
		if (!fptr.open(IO_WriteOnly))
			return;
		
		TQTextStream out(&fptr);
		
		out << qSetW(60) << "code" << qSetW(10) << "min" << qSetW(10) << "max" << qSetW(10) << "avg" << qSetW(10) << "count" << endl;
		out << endl; 
		
		root->save(out,TQString());
	}
}
#endif