xref: /haiku/src/tests/kits/locale/catalogSpeed.cpp (revision be9a70562e3c6552efb0caa53bd26965e7e1bed7)
1 /*
2 ** Copyright 2003, Oliver Tappe, zooey@hirschkaefer.de. All rights reserved.
3 ** Distributed under the terms of the MIT License.
4 */
5 
6 #include <assert.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <typeinfo>
10 #include <unistd.h>
11 
12 #include <Application.h>
13 #include <StopWatch.h>
14 
15 #include <Catalog.h>
16 #include <HashMapCatalog.h>
17 #include <Entry.h>
18 #include <Locale.h>
19 #include <Path.h>
20 #include <Roster.h>
21 
22 const uint32 kNumStrings = 10000;
23 
24 BString strs[kNumStrings];
25 BString ctxs[kNumStrings];
26 
27 BString trls[kNumStrings];
28 
29 const char *translated;
30 
31 class CatalogSpeed {
32 	public:
33 		void TestCreation();
34 		void TestLookup();
35 		void TestIdCreation();
36 		void TestIdLookup();
37 };
38 
39 #define B_TRANSLATION_CONTEXT "CatalogSpeed"
40 
41 #define catSig "x-vnd.Be.locale.catalogSpeed"
42 #define catName catSig".catalog"
43 
44 
45 void
46 CatalogSpeed::TestCreation()
47 {
48 	for (uint32 i = 0; i < kNumStrings; i++) {
49 		strs[i] << "native-string#" << 1000000+i;
50 		ctxs[i] << B_TRANSLATION_CONTEXT;
51 		trls[i] << "translation#" << 4000000+i;
52 	}
53 
54 	BStopWatch watch("catalogSpeed", true);
55 
56 	status_t res;
57 	assert(be_locale != NULL);
58 	system("mkdir -p ./locale/catalogs/"catSig);
59 
60 	// create an empty catalog of default type...
61 	BPrivate::EditableCatalog cat1("Default", catSig, "klingon");
62 	assert(cat1.InitCheck() == B_OK);
63 
64 	// ...and populate the catalog with some data:
65 	for (uint32 i = 0; i < kNumStrings; i++) {
66 		cat1.SetString(strs[i].String(), trls[i].String(), ctxs[i].String());
67 	}
68 	watch.Suspend();
69 	printf("\tadded %ld strings in           %9Ld usecs\n",
70 		cat1.CountItems(), watch.ElapsedTime());
71 
72 	watch.Reset();
73 	watch.Resume();
74 	res = cat1.WriteToFile("./locale/catalogs/"catSig"/klingon.catalog");
75 	assert(res == B_OK);
76 	watch.Suspend();
77 	printf("\t%ld strings written to disk in %9Ld usecs\n",
78 		cat1.CountItems(), watch.ElapsedTime());
79 }
80 
81 
82 void
83 CatalogSpeed::TestLookup()
84 {
85 	BStopWatch watch("catalogSpeed", true);
86 
87 	BCatalog *cat = be_catalog = new BCatalog(catSig, "klingon");
88 
89 	assert(cat != NULL);
90 	assert(cat->InitCheck() == B_OK);
91 	watch.Suspend();
92 	printf("\t%ld strings read from disk in  %9Ld usecs\n",
93 		cat->CountItems(), watch.ElapsedTime());
94 
95 	watch.Reset();
96 	watch.Resume();
97 	for (uint32 i = 0; i < kNumStrings; i++) {
98 		translated = B_TRANSLATE(strs[i].String());
99 	}
100 	watch.Suspend();
101 	printf("\tlooked up %lu strings in       %9Ld usecs\n",
102 		kNumStrings, watch.ElapsedTime());
103 
104 	delete cat;
105 }
106 
107 
108 void
109 CatalogSpeed::TestIdCreation()
110 {
111 	BStopWatch watch("catalogSpeed", true);
112 	watch.Suspend();
113 
114 	status_t res;
115 	BString s("string");
116 	s << "\x01" << typeid(*this).name() << "\x01";
117 	//size_t hashVal = __stl_hash_string(s.String());
118 	assert(be_locale != NULL);
119 	system("mkdir -p ./locale/catalogs/"catSig);
120 
121 	// create an empty catalog of default type...
122 	BPrivate::EditableCatalog cat1("Default", catSig, "klingon");
123 	assert(cat1.InitCheck() == B_OK);
124 
125 	// ...and populate the catalog with some data:
126 	for (uint32 i = 0; i < kNumStrings; i++) {
127 		trls[i] = BString("id_translation#") << 6000000+i;
128 	}
129 	watch.Reset();
130 	watch.Resume();
131 	for (uint32 i = 0; i < kNumStrings; i++) {
132 		cat1.SetString(i, trls[i].String());
133 	}
134 	watch.Suspend();
135 	printf("\tadded %ld strings by id in     %9Ld usecs\n",
136 		cat1.CountItems(), watch.ElapsedTime());
137 
138 	watch.Reset();
139 	watch.Resume();
140 	res = cat1.WriteToFile("./locale/catalogs/"catSig"/klingon.catalog");
141 	assert( res == B_OK);
142 	watch.Suspend();
143 	printf("\t%ld strings written to disk in %9Ld usecs\n",
144 		cat1.CountItems(), watch.ElapsedTime());
145 }
146 
147 
148 void
149 CatalogSpeed::TestIdLookup()
150 {
151 	BStopWatch watch("catalogSpeed", true);
152 
153 	BCatalog *cat = be_catalog = new BCatalog(catSig, "klingon");
154 
155 	assert(cat != NULL);
156 	assert(cat->InitCheck() == B_OK);
157 	watch.Suspend();
158 	printf("\t%ld strings read from disk in  %9Ld usecs\n",
159 		cat->CountItems(), watch.ElapsedTime());
160 
161 	watch.Reset();
162 	watch.Resume();
163 	for (uint32 i = 0; i < kNumStrings; i++) {
164 		translated = B_TRANSLATE_ID(i);
165 	}
166 	watch.Suspend();
167 	printf("\tlooked up %lu strings in       %9Ld usecs\n",
168 		kNumStrings, watch.ElapsedTime());
169 
170 	delete cat;
171 }
172 
173 
174 int
175 main(int argc, char **argv)
176 {
177 	BApplication* testApp
178 		= new BApplication("application/"catSig);
179 
180 	// change to app-folder:
181 	app_info appInfo;
182 	be_app->GetAppInfo(&appInfo);
183 	BEntry appEntry(&appInfo.ref);
184 	BEntry appFolder;
185 	appEntry.GetParent(&appFolder);
186 	BPath appPath;
187 	appFolder.GetPath(&appPath);
188 	chdir(appPath.Path());
189 
190 	CatalogSpeed catSpeed;
191 	printf("\t------------------------------------------------\n");
192 	printf("\tstring-based catalog usage:\n");
193 	printf("\t------------------------------------------------\n");
194 	catSpeed.TestCreation();
195 	catSpeed.TestLookup();
196 	printf("\t------------------------------------------------\n");
197 	printf("\tid-based catalog usage:\n");
198 	printf("\t------------------------------------------------\n");
199 	catSpeed.TestIdCreation();
200 	catSpeed.TestIdLookup();
201 
202 	delete testApp;
203 
204 	return 0;
205 }
206