[Debtags-commits] [svn] r1593 - in tagcoll/trunk: . bench

Enrico Zini enrico at costa.debian.org
Mon Feb 20 16:49:28 UTC 2006


Author: enrico
Date: Mon Feb 20 16:49:28 2006
New Revision: 1593

Modified:
   tagcoll/trunk/   (props changed)
   tagcoll/trunk/bench/Benchmark.cc
   tagcoll/trunk/bench/Benchmark.h
   tagcoll/trunk/bench/collection.cc
Log:
 r7408 at viaza:  enrico | 2006-02-20 17:49:07 +0100
 Refactored taking times in RAII style


Modified: tagcoll/trunk/bench/Benchmark.cc
==============================================================================
--- tagcoll/trunk/bench/Benchmark.cc	(original)
+++ tagcoll/trunk/bench/Benchmark.cc	Mon Feb 20 16:49:28 2006
@@ -2,12 +2,30 @@
 #include <tagcoll/Exception.h>
 
 #include <stdarg.h>
-#include <unistd.h>
-#include <errno.h>
+#include <time.h>
+#include <iostream>
 
 using namespace std;
 using namespace Tagcoll;
 
+Timer::~Timer()
+{
+	done();
+}
+
+void Timer::done()
+{
+	if (start != -1)
+	{
+		clock_t end = clock();
+
+		cout << desc << ": " << (double)(end - start) / (double)CLOCKS_PER_SEC * 1000.0 << "ms" << endl;
+
+		start = -1;
+	}
+}
+
+
 static Benchmark* _root = 0;
 Benchmark* Benchmark::root()
 {
@@ -17,32 +35,6 @@
 }
 
 
-double Benchmark::tps = sysconf(_SC_CLK_TCK);
-
-void Benchmark::timing(const char* fmt, ...)
-{
-    struct tms curtms;
-    if (times(&curtms) == -1)
-		throw SystemException(errno, "reading timing informations");
-
-	string fn = fullName();
-	if (!fn.empty())
-		fprintf(stdout, "%.*s: ", fn.size(), fn.data());
-
-    va_list ap;
-    va_start(ap, fmt);
-    vfprintf(stdout, fmt, ap);
-    va_end(ap);
-
-    fprintf(stdout, ": %.2f user, %.2f system, %.2f total\n",
-				    (curtms.tms_utime - lasttms.tms_utime)/tps,\
-				    (curtms.tms_stime - lasttms.tms_stime)/tps,\
-				    ((curtms.tms_utime - lasttms.tms_utime) + (curtms.tms_stime - lasttms.tms_stime))/tps); \
-
-    if (times(&lasttms) == -1)
-		throw SystemException(errno, "reading timing informations");
-}
-
 // Run only the subtest at the given path
 void Benchmark::run(const std::string& path)
 {
@@ -68,16 +60,13 @@
 // Run all subtests and this test
 void Benchmark::run()
 {
-	if (times(&lasttms) == -1)
-		throw SystemException(errno, "reading timing informations");
-
 	// First, run children
 	if (! children.empty())
 	{
+		Timer t = mktimer("total");
 		for (std::vector<Benchmark*>::iterator i = children.begin();
 				i != children.end(); i++)
 			(*i)->run();
-		timing("total");
 	}
 
 	// Then, run self

Modified: tagcoll/trunk/bench/Benchmark.h
==============================================================================
--- tagcoll/trunk/bench/Benchmark.h	(original)
+++ tagcoll/trunk/bench/Benchmark.h	Mon Feb 20 16:49:28 2006
@@ -1,17 +1,39 @@
 #ifndef WIBBLE_BENCHMARK_H
 #define WIBBLE_BENCHMARK_H
 
-#include <sys/times.h>
+#include <time.h>
 
 #include <vector>
 #include <string>
 
+class Timer
+{
+protected:
+	clock_t start;
+	std::string desc;
+
+public:
+	Timer(const Timer& t) : start(t.start), desc(t.desc)
+	{
+		((Timer*)&t)->start = -1;
+	}
+	Timer(const std::string& desc) : start(clock()), desc(desc) {}
+	~Timer();
+	const Timer& operator=(const Timer& t)
+	{
+		start = t.start;
+		desc = t.desc;
+		((Timer*)&t)->start = -1;
+		return *this;
+	}
+	
+	void done();
+};
+
 class Benchmark
 {
 private:
-	static double tps;
 	std::string tag;
-	struct tms lasttms;
 	Benchmark* parent;
 	std::vector<Benchmark*> children;
 	
@@ -19,10 +41,13 @@
 	// Main function with the benchmarks
 	virtual void main() {}
 
-	void timing(const char* fmt, ...);
-
 	void setParent(Benchmark* parent) { this->parent = parent; }
 
+	Timer mktimer(const std::string& desc)
+	{
+		return Timer(fullName() + "/" + desc);
+	}
+
 	std::string name() const { return tag; }
 
 	std::string fullName() const

Modified: tagcoll/trunk/bench/collection.cc
==============================================================================
--- tagcoll/trunk/bench/collection.cc	(original)
+++ tagcoll/trunk/bench/collection.cc	Mon Feb 20 16:49:28 2006
@@ -125,50 +125,52 @@
 
 void CollectionBench::benchROCollection(const Tagcoll::ReadonlyCollection<string, string>& coll)
 {
-	for (size_t i = 0; i < tags.size(); i++)
-		coll.hasTag(tags[i]);
-	timing("hasTag");
-
-	for (size_t i = 0; i < items.size(); i++)
-		coll.getTags(items[i]);
-	timing("getTags[item]");
-
-	for (size_t i = 0; i < itemsets.size(); i++)
-		coll.getTags(itemsets[i]);
-	timing("getTags[items]");
-
-	for (size_t i = 0; i < tags.size(); i++)
-		coll.getItems(tags[i]);
-	timing("getItems[tag]");
-
-	for (size_t i = 0; i < tagsets.size(); i++)
-		coll.getItems(tagsets[i]);
-	timing("getItems[tags]");
-
-	coll.getTaggedItems();
-	timing("getTaggedItems");
-
-	coll.getAllTags();
-	timing("getAllTags");
-
-	for (size_t i = 0; i < tags.size(); i++)
-		coll.getCardinality(tags[i]);
-	timing("getCardinality");
-
-	for (size_t i = 0; i < tagsets.size(); i++)
-		coll.getCompanionTags(tagsets[i]);
-	timing("getCompanionTags");
-
+	{
+		Timer t = mktimer("hasTag");
+		for (size_t i = 0; i < tags.size(); i++)
+			coll.hasTag(tags[i]);
+	}{
+		Timer t = mktimer("getTags[item]");
+		for (size_t i = 0; i < items.size(); i++)
+			coll.getTags(items[i]);
+	}{
+		Timer t = mktimer("getTags[items]");
+		for (size_t i = 0; i < itemsets.size(); i++)
+			coll.getTags(itemsets[i]);
+	}{
+		Timer t = mktimer("getItems[tag]");
+		for (size_t i = 0; i < tags.size(); i++)
+			coll.getItems(tags[i]);
+	}{
+		Timer t = mktimer("getItems[tags]");
+		for (size_t i = 0; i < tagsets.size(); i++)
+			coll.getItems(tagsets[i]);
+	}{
+		Timer t = mktimer("getTaggedItems");
+		coll.getTaggedItems();
+	}{
+		Timer t = mktimer("getAllTags");
+		coll.getAllTags();
+	}{
+		Timer t = mktimer("getCardinality");
+		for (size_t i = 0; i < tags.size(); i++)
+			coll.getCardinality(tags[i]);
+	}{
+		Timer t = mktimer("getCompanionTags");
+		for (size_t i = 0; i < tagsets.size(); i++)
+			coll.getCompanionTags(tagsets[i]);
+	}
 	// TODO: getRelatedItems
-
 	Sink<string, string> sink;
-	for (size_t i = 0; i < tagsets.size(); i++)
-		coll.output(sink);
-	timing("output");
-
-	for (size_t i = 0; i < tagsets.size(); i++)
-		coll.outputHavingTags(tagsets[i], sink);
-	timing("outputHavingTags");
+	{
+		Timer t = mktimer("output");
+		for (size_t i = 0; i < tagsets.size(); i++)
+			coll.output(sink);
+	}{
+		Timer t = mktimer("outputHavingTags");
+		for (size_t i = 0; i < tagsets.size(); i++)
+			coll.outputHavingTags(tagsets[i], sink);
+	}
 }
 
 void CollectionBench::benchCollection(Tagcoll::Collection<string, string>& coll)
@@ -180,11 +182,13 @@
 protected:
 	virtual void main()
 	{
+		Timer t1 = mktimer("instantiating collection");
 		InputMerger<string, string> coll;
-		timing("instantiating collection");
+		t1.done();
 
+		Timer t2 = mktimer("populating collection");
 		outputROCollection(coll);
-		timing("populating collection");
+		t2.done();
 
 		benchROCollection(coll);
 	}
@@ -201,15 +205,18 @@
 		{
 			BasicStringDiskIndexer indexer;
 
+			Timer t1 = mktimer("indexing collection");
 			outputROCollection(indexer);
-			timing("indexing collection");
+			t1.done();
 			
+			Timer t2 = mktimer("writing indexerd collection");
 			indexer.write("bench-basicstringdiskindex.tmp");
-			timing("writing indexerd collection");
+			t2.done();
 		}
 
+		Timer t3 = mktimer("instantiating indexed collection");
 		BasicStringDiskIndex coll("bench-basicstringdiskindex.tmp");
-		timing("instantiating indexed collection");
+		t3.done();
 
 		benchROCollection(coll);
 	}



More information about the Debtags-commits mailing list