File: collection.cc

package info (click to toggle)
tagcoll 1.6.3-2
  • links: PTS
  • area: main
  • in suites: etch
  • size: 2,500 kB
  • ctags: 2,062
  • sloc: cpp: 10,930; sh: 8,892; makefile: 201; lex: 54; yacc: 27
file content (348 lines) | stat: -rw-r--r-- 8,123 bytes parent folder | download | duplicates (2)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <bench/Benchmark.h>
#include <tagcoll/stringf.h>
#include <tagcoll/InputMerger.h>
#include <tagcoll/TextFormat.h>
#include <tagcoll/BasicStringDiskIndex.h>
#include <tagcoll/TDBIndexer.h>
#include <tagcoll/TDBReadonlyDiskIndex.h>
#include <tagcoll/CardinalityStore.h>

#include <vector>
#include <iostream>
#include <math.h>

using namespace std;
using namespace stringf;
using namespace Tagcoll;

namespace bench_collection {

/* Some utility random generator functions */

static inline int rnd(int min, int max)
{
	return min + (int) (max * (rand() / (RAND_MAX + 1.0)));
}

static inline double rnd(double min, double max)
{
	return min + (int) (max * (rand() / (RAND_MAX + 1.0)));
}

class CollectionBench : public Benchmark
{
protected:
	// Vector of available tags.  The tags at the beginning are the most
	// popular ones.
	static vector<string> tags;
	// Vector of available items.  The items at the beginning are the ones with
	// most tags.
	static vector<string> items;
	// Sample collection
	static InputMerger<string, string> coll;
	// Precomputed tagsets
	static vector< OpSet<string> > tagsets;
	// Precomputed itemsets
	static vector< OpSet<string> > itemsets;

	// Compute a random number between min and max (inclusive), following a
	// geometric distribution with parameter p
	int rndGeom(int min, int max, double p)
	{
		int res = (int)rint((double)min + ((double)max-(double)min)*(log(1.0 - drand48()) / log(p)));
		if (res > max)
			return max;
		else
			return res;
	}
	
	void outputROCollection(Tagcoll::Consumer<string, string>& cons);
	void benchROCollection(const Tagcoll::ReadonlyCollection<string, string>& coll);
	void benchCollection(Tagcoll::Collection<string, string>& coll);

	OpSet<string> makeTagset(double p)
	{
		OpSet<string> res;
		size_t ntags = rndGeom(1, 20, p);
		for (size_t j = 0; j < ntags; j++)
		{
			int idx = rndGeom(0, tags.size() - 1, 0.01);
			res += tags[idx];
		}
		return res;
	}

	OpSet<string> makeItemset(double p)
	{
		OpSet<string> res;
		size_t nitems = rndGeom(1, 10, p);
		for (size_t j = 0; j < nitems; j++)
		{
			int idx = rndGeom(0, items.size() - 1, 0.001);
			res += items[idx];
		}
		return res;
	}

	CollectionBench(const std::string& name)
		: Benchmark(name)
	{
		if (tags.empty())
		{
			// Create the tag vocabulary
			for (int i = 0; i < 500; i++)
				tags.push_back("tag" + fmt(i));

			// Create the package set
			for (int i = 0; i < 10000; i++)
				items.push_back("pkg" + fmt(i));

			// Create the test collection
			for (size_t i = 0; i < items.size(); i++)
				coll.consume(
						items[i],
						makeTagset((((double)items.size() - (double)i) / (double)items.size())/2)
						);
			
			// Precompute tagsets used for benchmarking
			for (size_t i = 0; i < 100; i++)
				tagsets.push_back(makeTagset(0.02));
			
			// Precompute itemsets used for benchmarking
			for (size_t i = 0; i < 100; i++)
				itemsets.push_back(makeItemset(0.02));
		}
	}
};

vector<string> CollectionBench::tags;
vector<string> CollectionBench::items;
InputMerger<string, string> CollectionBench::coll;
vector< OpSet<string> > CollectionBench::tagsets;
vector< OpSet<string> > CollectionBench::itemsets;

void CollectionBench::outputROCollection(Tagcoll::Consumer<string, string>& cons)
{
	coll.output(cons);
}

void CollectionBench::benchROCollection(const Tagcoll::ReadonlyCollection<string, string>& coll)
{
	{
		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;
	{
		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)
{
}

class benchInputMerger : public CollectionBench
{
protected:
	virtual void main()
	{
		Timer main = mktimer("total");

		Timer t1 = mktimer("instantiating collection");
		InputMerger<string, string> coll;
		t1.done();

		Timer t2 = mktimer("populating collection");
		outputROCollection(coll);
		t2.done();

		{
			Timer t = mktimer("read only collection");
			benchROCollection(coll);
		}
	}

public:
	benchInputMerger() : CollectionBench("InputMerger") {}
};

class benchBasicStringDiskIndex : public CollectionBench
{
protected:
	virtual void main()
	{
		Timer main = mktimer("total");
		{
			BasicStringDiskIndexer indexer;

			Timer t1 = mktimer("indexing collection");
			outputROCollection(indexer);
			t1.done();
			
			Timer t2 = mktimer("writing indexed collection");
			indexer.write("bench-basicstringdiskindex.tmp");
			t2.done();
		}

		Timer t3 = mktimer("instantiating indexed collection");
		BasicStringDiskIndex coll("bench-basicstringdiskindex.tmp");
		t3.done();

		{
			Timer t = mktimer("read only collection");
			benchROCollection(coll);
		}
	}

public:
	benchBasicStringDiskIndex() : CollectionBench("BasicStringDiskIndex") {}
	~benchBasicStringDiskIndex() {
		unlink("bench-basicstringdiskindex.tmp");
	}
};


class benchTDBReadonlyDiskIndex : public CollectionBench
{
protected:
	virtual void main()
	{
		Timer main = mktimer("total");
		TrivialConverter<string, string> conv;
		{
			TDBIndexer<string, string> indexer;

			Timer t1 = mktimer("indexing collection");
			outputROCollection(indexer);
			t1.done();
			
			Timer t2 = mktimer("writing indexed collection");
			indexer.writeIndex(conv, conv,
					"bench-TDBReadonlyDiskIndex1.tmp",
					"bench-TDBReadonlyDiskIndex2.tmp");
			t2.done();
		}

		Timer t3 = mktimer("instantiating indexed collection");
		TDBReadonlyDiskIndex<string, string> coll(
				"bench-TDBReadonlyDiskIndex1.tmp",
				"bench-TDBReadonlyDiskIndex2.tmp",
				conv, conv, conv, conv);
		t3.done();

		{
			Timer t = mktimer("read only collection");
			benchROCollection(coll);
		}
	}

public:
	benchTDBReadonlyDiskIndex() : CollectionBench("TDBReadonlyDiskIndex") {}
	~benchTDBReadonlyDiskIndex() {
		unlink("bench-TDBReadonlyDiskIndex1.tmp");
		unlink("bench-TDBReadonlyDiskIndex2.tmp");
	}
};

class benchTDBIndexer : public CollectionBench
{
protected:
	virtual void main()
	{
		Timer main = mktimer("total");
		TDBIndexer<string, string> coll;

		Timer t1 = mktimer("indexing collection");
		outputROCollection(coll);
		t1.done();
			
		{
			Timer t = mktimer("read only collection");
			benchROCollection(coll);
		}
	}

public:
	benchTDBIndexer() : CollectionBench("TDBIndexer") {}
};


class benchCardinalityStore : public CollectionBench
{
protected:
	virtual void main()
	{
		Timer main = mktimer("total");
		CardinalityStore<string, string> coll;

		Timer t1 = mktimer("indexing collection");
		outputROCollection(coll);
		t1.done();
			
		{
			Timer t = mktimer("read only collection");
			benchROCollection(coll);
		}
	}

public:
	benchCardinalityStore() : CollectionBench("CardinalityStore") {}
};

class top : public Benchmark
{
public:
	top() : Benchmark("collection")
	{
		addChild(new benchInputMerger());
		addChild(new benchBasicStringDiskIndex());
		addChild(new benchTDBReadonlyDiskIndex());
		addChild(new benchTDBIndexer());
		addChild(new benchCardinalityStore());
	}
};

static RegisterRoot r(new top());

}

/* vim:set ts=4 sw=4: */