13 #include <sys/types.h> 20 char const* filename=
"/opt/verylarge/chessgames/ftp.cis.uab.edu/crafty/enormous.pgn";
21 char const* warmupfile=
"/home/carlo/chess/fics2008.pgn";
29 #define CWCHESS_PGN_IMPLEMENTATION 31 #ifdef GFILE_ASYNC_IMPLEMENTATION 35 #ifdef CWCHESS_PGN_IMPLEMENTATION 36 #include <giomm/init.h> 39 #if defined(GFILE_ASYNC_IMPLEMENTATION) || defined(CWCHESS_PGN_IMPLEMENTATION) 40 std::ostream* global_os;
43 struct timeval before, after;
45 inline void start_timer(
void)
47 gettimeofday(&before, NULL);
50 uint64_t stop_timer(
void)
52 gettimeofday(&after, NULL);
53 timersub(&after,& before,& after);
54 uint64_t t = after.tv_sec;
62 #ifdef REFERENCE_IMPLEMENTATION 63 void benchmark_reference(std::ostream& os,
char const* filename)
65 int fd = open(filename, O_RDONLY);
68 while(len < 945950820)
70 int s = read(fd, buf,
sizeof(buf));
80 uint64_t microseconds = stop_timer();
81 os <<
"Reference implementation (buffersize " <<
sizeof(buf) <<
"): " << microseconds <<
" microseconds." << std::endl;
86 #ifdef BUFFERSIZE_IMPLEMENTATION 87 void benchmark_buffersize(std::ostream& os,
char const* filename)
89 size_t const bufsize = 4096;
90 int fd = open(filename, O_RDONLY);
93 while(len < 945950820)
95 int s = read(fd, buf, bufsize);
105 uint64_t microseconds = stop_timer();
106 os <<
"read(2) (buffersize " << bufsize <<
"): " << microseconds <<
" microseconds." << std::endl;
111 #ifdef FSTREAM_READ_IMPLEMENTATION 112 void benchmark_fstream_read(std::ostream& os,
char const* filename)
114 std::ifstream inputfile;
115 inputfile.open(filename);
120 inputfile.read(buf, 4096);
121 len += inputfile.gcount();
123 uint64_t microseconds = stop_timer();
124 os <<
"std::ifstream::read (buffersize 4096): " << microseconds <<
" microseconds. Size read: " << len << std::endl;
129 #ifdef FSTREAM_GETLINE_IMPLEMENTATION 130 void benchmark_fstream_getline(std::ostream& os,
char const* filename)
132 std::ifstream inputfile;
133 inputfile.open(filename);
137 while(getline(inputfile, line))
139 len += line.length() + 1;
141 uint64_t microseconds = stop_timer();
142 os <<
"std::getline(std::ifstream, ...): " << microseconds <<
" microseconds. Size read: " << len << std::endl;
147 #ifdef GFILE_IMPLEMENTATION 148 void benchmark_gfile(std::ostream& os,
char const* filename)
151 GFile* file = g_file_new_for_path(filename);
152 GCancellable* cancellable = g_cancellable_new();
153 GError* error = NULL;
154 GFileInputStream* stream = g_file_read(file, cancellable,& error);
157 std::cerr <<
"g_file_read failed: " << error->message << std::endl;
165 res = g_input_stream_read(G_INPUT_STREAM(stream), buf, 4096, cancellable,& error);
168 std::cerr <<
"g_input_stream_read: " << error->message << std::endl;
174 uint64_t microseconds = stop_timer();
175 os <<
"g_input_stream_read (buffersize 4096): " << microseconds <<
" microseconds. Size read: " << len << std::endl;
176 g_object_unref(stream);
177 g_object_unref(cancellable);
178 g_object_unref(file);
182 #ifdef GFILE_ASYNC_IMPLEMENTATION 183 GMainLoop* gmain_loop;
184 GCancellable* cancellable;
185 void async_ready_callback(GObject* source_object, GAsyncResult* async_res, gpointer user_data);
186 void async_ready_callback2(GObject* source_object, GAsyncResult* async_res, gpointer user_data);
188 void benchmark_gfile_async(std::ostream& os,
char const* filename)
192 GFile* file = g_file_new_for_path(filename);
193 cancellable = g_cancellable_new();
194 g_file_read_async(file, G_PRIORITY_DEFAULT, cancellable, async_ready_callback, file);
195 gmain_loop = g_main_loop_new(NULL,
false);
196 g_main_loop_run(gmain_loop);
197 g_object_unref(cancellable);
198 g_object_unref(file);
202 #ifdef CWCHESS_PGN_IMPLEMENTATION 203 Glib::RefPtr<Glib::MainLoop> main_loop;
204 Glib::RefPtr<cwchess::pgn::Database> pgn_data_base;
205 void open_finished(
size_t len);
207 void benchmark_cwchess_pgn(std::ostream& os,
char const* filename)
212 pgn_data_base = pgn::DatabaseSeekable::open(filename, sigc::ptr_fun(&open_finished));
213 main_loop = Glib::MainLoop::create(
false);
219 void clear_disk_cache(
void)
222 system(
"sudo sh -c \"sync; echo 1 > /proc/sys/vm/drop_caches\"");
225 uint64_t microseconds = stop_timer();
226 std::ofstream dump(
"/dev/null");
228 #ifdef REFERENCE_IMPLEMENTATION 229 benchmark_reference(dump, warmupfile);
231 #ifdef BUFFERSIZE_IMPLEMENTATION 232 benchmark_buffersize(dump, warmupfile);
234 #ifdef FSTREAM_READ_IMPLEMENTATION 235 benchmark_fstream_read(dump, warmupfile);
237 #ifdef FSTREAM_GETLINE_IMPLEMENTATION 238 benchmark_fstream_getline(dump, warmupfile);
240 #ifdef GFILE_IMPLEMENTATION 241 benchmark_gfile(dump, warmupfile);
243 #ifdef GFILE_ASYNC_IMPLEMENTATION 244 benchmark_gfile_async(dump, warmupfile);
246 #ifdef CWCHESS_PGN_IMPLEMENTATION 247 benchmark_cwchess_pgn(dump, warmupfile);
256 if (!Glib::thread_supported())
258 Debug(NAMESPACE_DEBUG::init());
260 #ifdef REFERENCE_IMPLEMENTATION 262 benchmark_reference(std::cout, filename);
265 #ifdef BUFFERSIZE_IMPLEMENTATION 267 benchmark_buffersize(std::cout, filename);
270 #ifdef FSTREAM_READ_IMPLEMENTATION 272 benchmark_fstream_read(std::cout, filename);
275 #ifdef FSTREAM_GETLINE_IMPLEMENTATION 277 benchmark_fstream_getline(std::cout, filename);
280 #ifdef GFILE_IMPLEMENTATION 282 benchmark_gfile(std::cout, filename);
285 #ifdef GFILE_ASYNC_IMPLEMENTATION 287 benchmark_gfile_async(std::cout, filename);
290 #ifdef CWCHESS_PGN_IMPLEMENTATION 292 benchmark_cwchess_pgn(std::cout, filename);
296 #ifdef GFILE_ASYNC_IMPLEMENTATION 299 void async_ready_callback(GObject* source_object, GAsyncResult* async_res, gpointer user_data)
301 GFile* file = G_FILE(user_data);
302 GError* error = NULL;
303 GFileInputStream* stream = g_file_read_finish(file, async_res,& error);
306 std::cerr <<
"g_file_read_finish failed: " << error->message << std::endl;
311 g_input_stream_read_async(G_INPUT_STREAM(stream), buf, cwchess::pgn::DatabaseSeekable::S_buffer_size,
312 G_PRIORITY_DEFAULT, cancellable, async_ready_callback2, stream);
315 void async_ready_callback2(GObject* source_object, GAsyncResult* async_res, gpointer user_data)
317 GInputStream* stream = G_INPUT_STREAM(user_data);
318 GError* error = NULL;
319 gssize res = g_input_stream_read_finish(stream, async_res,& error);
322 std::cerr <<
"g_input_stream_read_finish: " << error->message << std::endl;
327 g_input_stream_read_async(G_INPUT_STREAM(stream), buf, cwchess::pgn::DatabaseSeekable::S_buffer_size,
328 G_PRIORITY_DEFAULT, cancellable, async_ready_callback2, stream);
331 uint64_t microseconds = stop_timer();
332 * global_os <<
"g_input_stream_read_async (buffersize " << cwchess::pgn::DatabaseSeekable::S_buffer_size <<
"): " <<
333 microseconds <<
" microseconds. Size read: " << len << std::endl;
334 g_object_unref(stream);
335 g_main_loop_quit(gmain_loop);
340 #ifdef CWCHESS_PGN_IMPLEMENTATION 341 void open_finished(
size_t len)
343 uint64_t microseconds = stop_timer();
344 * global_os <<
"cwchess::pgn::DatabaseSeekable (buffersize " << cwchess::pgn::DatabaseSeekable::S_buffer_size <<
"): " <<
345 microseconds <<
" microseconds. Size read: " << len <<
"; number of lines: " << pgn_data_base->number_of_lines() <<
"; number of characters: " <<
346 pgn_data_base->number_of_characters() << std::endl;
A namespace for all chess related objects that are not related to the GUI.
This file contains the declaration of class pgn::Database.