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