Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/regex/performance/command_line.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 13.5 KB
Line 
1/*
2 *
3 * Copyright (c) 2002-2003
4 * John Maddock
5 *
6 * Use, modification and distribution are subject to the
7 * Boost Software License, Version 1.0. (See accompanying file
8 * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 *
10 */
11
12#include <iostream>
13#include <iomanip>
14#include <fstream>
15#include <deque>
16#include <sstream>
17#include <stdexcept>
18#include <iterator>
19#include <boost/regex.hpp>
20#include <boost/version.hpp>
21#include "regex_comparison.hpp"
22
23#ifdef BOOST_HAS_PCRE
24#include "pcre.h" // for pcre version number
25#endif
26
27//
28// globals:
29//
30bool time_boost = false;
31bool time_localised_boost = false;
32bool time_greta = false;
33bool time_safe_greta = false;
34bool time_posix = false;
35bool time_pcre = false;
36bool time_xpressive = false;
37
38bool test_matches = false;
39bool test_code = false;
40bool test_html = false;
41bool test_short_twain = false;
42bool test_long_twain = false;
43
44
45std::string html_template_file;
46std::string html_out_file;
47std::string html_contents;
48std::list<results> result_list;
49
50// the following let us compute averages:
51double greta_total = 0;
52double safe_greta_total = 0;
53double boost_total = 0;
54double locale_boost_total = 0;
55double posix_total = 0;
56double pcre_total = 0;
57double xpressive_total = 0;
58unsigned greta_test_count = 0;
59unsigned safe_greta_test_count = 0;
60unsigned boost_test_count = 0;
61unsigned locale_boost_test_count = 0;
62unsigned posix_test_count = 0;
63unsigned pcre_test_count = 0;
64unsigned xpressive_test_count = 0;
65
66int handle_argument(const std::string& what)
67{
68   if(what == "-b")
69      time_boost = true;
70   else if(what == "-bl")
71      time_localised_boost = true;
72#ifdef BOOST_HAS_GRETA
73   else if(what == "-g")
74      time_greta = true;
75   else if(what == "-gs")
76      time_safe_greta = true;
77#endif
78#ifdef BOOST_HAS_POSIX
79   else if(what == "-posix")
80      time_posix = true;
81#endif
82#ifdef BOOST_HAS_PCRE
83   else if(what == "-pcre")
84      time_pcre = true;
85#endif
86#ifdef BOOST_HAS_XPRESSIVE
87   else if(what == "-xpressive" || what == "-dxpr")
88      time_xpressive = true;
89#endif
90   else if(what == "-all")
91   {
92      time_boost = true;
93      time_localised_boost = true;
94#ifdef BOOST_HAS_GRETA
95      time_greta = true;
96      time_safe_greta = true;
97#endif
98#ifdef BOOST_HAS_POSIX
99      time_posix = true;
100#endif
101#ifdef BOOST_HAS_PCRE
102      time_pcre = true;
103#endif
104   }
105   else if(what == "-test-matches")
106      test_matches = true;
107   else if(what == "-test-code")
108      test_code = true;
109   else if(what == "-test-html")
110      test_html = true;
111   else if(what == "-test-short-twain")
112      test_short_twain = true;
113   else if(what == "-test-long-twain")
114      test_long_twain = true;
115   else if(what == "-test-all")
116   {
117      test_matches = true;
118      test_code = true;
119      test_html = true;
120      test_short_twain = true;
121      test_long_twain = true;
122   }
123   else if((what == "-h") || (what == "--help"))
124      return show_usage();
125   else if((what[0] == '-') || (what[0] == '/'))
126   {
127      std::cerr << "Unknown argument: \"" << what << "\"" << std::endl;
128      return 1;
129   }
130   else if(html_template_file.size() == 0)
131   {
132      html_template_file = what;
133      load_file(html_contents, what.c_str());
134   }
135   else if(html_out_file.size() == 0)
136      html_out_file = what;
137   else
138   {
139      std::cerr << "Unexpected argument: \"" << what << "\"" << std::endl;
140      return 1;
141   }
142   return 0;
143}
144
145int show_usage()
146{
147   std::cout <<
148      "Usage\n"
149      "regex_comparison [-h] [library options] [test options] [html_template html_output_file]\n"
150      "   -h        Show help\n\n"
151      "   library options:\n"
152      "      -b     Apply tests to boost library\n"
153      "      -bl    Apply tests to boost library with C++ locale\n"
154#ifdef BOOST_HAS_GRETA
155      "      -g     Apply tests to GRETA library\n"
156      "      -gs    Apply tests to GRETA library (in non-recursive mode)\n"
157#endif
158#ifdef BOOST_HAS_POSIX
159      "      -posix Apply tests to POSIX library\n"
160#endif
161#ifdef BOOST_HAS_PCRE
162      "      -pcre  Apply tests to PCRE library\n"
163#endif
164#ifdef BOOST_HAS_XPRESSIVE
165      "      -dxpr  Apply tests to dynamic xpressive library\n"
166#endif
167      "      -all   Apply tests to all libraries\n\n"
168      "   test options:\n"
169      "      -test-matches      Test short matches\n"
170      "      -test-code         Test c++ code examples\n"
171      "      -test-html         Test c++ code examples\n"
172      "      -test-short-twain  Test short searches\n"
173      "      -test-long-twain   Test long searches\n"
174      "      -test-all          Test everthing\n";
175   return 1;
176}
177
178void load_file(std::string& text, const char* file)
179{
180   std::deque<char> temp_copy;
181   std::ifstream is(file);
182   if(!is.good())
183   {
184      std::string msg("Unable to open file: \"");
185      msg.append(file);
186      msg.append("\"");
187      throw std::runtime_error(msg);
188   }
189   is.seekg(0, std::ios_base::end);
190   std::istream::pos_type pos = is.tellg();
191   is.seekg(0, std::ios_base::beg);
192   text.erase();
193   text.reserve(pos);
194   std::istreambuf_iterator<char> it(is);
195   std::copy(it, std::istreambuf_iterator<char>(), std::back_inserter(text));
196}
197
198void print_result(std::ostream& os, double time, double best)
199{
200   static const char* suffixes[] = {"s", "ms", "us", "ns", "ps", };
201
202   if(time < 0)
203   {
204      os << "<td>NA</td>";
205      return;
206   }
207   double rel = time / best;
208   bool highlight = ((rel > 0) && (rel < 1.1));
209   unsigned suffix = 0;
210   while(time < 0)
211   {
212      time *= 1000;
213      ++suffix;
214   }
215   os << "<td>";
216   if(highlight)
217      os << "<font color=\"#008000\">";
218   if(rel <= 1000)
219      os << std::setprecision(3) << rel;
220   else
221      os << (int)rel;
222   os << "<BR>(";
223   if(time <= 1000)
224      os << std::setprecision(3) << time;
225   else
226      os << (int)time;
227   os << suffixes[suffix] << ")";
228   if(highlight)
229      os << "</font>";
230   os << "</td>";
231}
232
233std::string html_quote(const std::string& in)
234{
235   static const boost::regex e("(<)|(>)|(&)|(\")");
236   static const std::string format("(?1&lt;)(?2&gt;)(?3&amp;)(?4&quot;)");
237   return regex_replace(in, e, format, boost::match_default | boost::format_all);
238}
239
240void output_html_results(bool show_description, const std::string& tagname)
241{
242   std::stringstream os;
243   if(result_list.size())
244   {
245      //
246      // start by outputting the table header:
247      //
248      os << "<table border=\"1\" cellspacing=\"1\">\n";
249      os << "<tr><td><strong>Expression</strong></td>";
250      if(show_description)
251         os << "<td><strong>Text</strong></td>";
252#if defined(BOOST_HAS_GRETA)
253      if(time_greta == true)
254         os << "<td><strong>GRETA</strong></td>";
255      if(time_safe_greta == true)
256         os << "<td><strong>GRETA<BR>(non-recursive mode)</strong></td>";
257#endif
258      if(time_boost == true)
259         os << "<td><strong>Boost</strong></td>";
260      if(time_localised_boost == true)
261         os << "<td><strong>Boost + C++ locale</strong></td>";
262#if defined(BOOST_HAS_POSIX)
263      if(time_posix == true)
264         os << "<td><strong>POSIX</strong></td>";
265#endif
266#ifdef BOOST_HAS_PCRE
267      if(time_pcre == true)
268         os << "<td><strong>PCRE</strong></td>";
269#endif
270#ifdef BOOST_HAS_XPRESSIVE
271      if(time_xpressive == true)
272         os << "<td><strong>Dynamic Xpressive</strong></td>";
273#endif
274      os << "</tr>\n";
275
276      //
277      // Now enumerate through all the test results:
278      //
279      std::list<results>::const_iterator first, last;
280      first = result_list.begin();
281      last = result_list.end();
282      while(first != last)
283      {
284         os << "<tr><td><code>" << html_quote(first->expression) << "</code></td>";
285         if(show_description)
286            os << "<td>" << html_quote(first->description) << "</td>";
287#if defined(BOOST_HAS_GRETA)
288         if(time_greta == true)
289         {
290            print_result(os, first->greta_time, first->factor);
291            if(first->greta_time > 0)
292            {
293               greta_total += first->greta_time / first->factor;
294               ++greta_test_count;
295            }
296         }
297         if(time_safe_greta == true)
298         {
299            print_result(os, first->safe_greta_time, first->factor);
300            if(first->safe_greta_time > 0)
301            {
302               safe_greta_total += first->safe_greta_time / first->factor;
303               ++safe_greta_test_count;
304            }
305         }
306#endif
307#if defined(BOOST_HAS_POSIX)
308         if(time_boost == true)
309         {
310            print_result(os, first->boost_time, first->factor);
311            if(first->boost_time > 0)
312            {
313               boost_total += first->boost_time / first->factor;
314               ++boost_test_count;
315            }
316         }
317#endif
318         if(time_localised_boost == true)
319         {
320            print_result(os, first->localised_boost_time, first->factor);
321            if(first->localised_boost_time > 0)
322            {
323               locale_boost_total += first->localised_boost_time / first->factor;
324               ++locale_boost_test_count;
325            }
326         }
327         if(time_posix == true)
328         {
329            print_result(os, first->posix_time, first->factor);
330            if(first->posix_time > 0)
331            {
332               posix_total += first->posix_time / first->factor;
333               ++posix_test_count;
334            }
335         }
336#if defined(BOOST_HAS_PCRE)
337         if(time_pcre == true)
338         {
339            print_result(os, first->pcre_time, first->factor);
340            if(first->pcre_time > 0)
341            {
342               pcre_total += first->pcre_time / first->factor;
343               ++pcre_test_count;
344            }
345         }
346#endif
347#if defined(BOOST_HAS_XPRESSIVE)
348         if(time_xpressive == true)
349         {
350            print_result(os, first->xpressive_time, first->factor);
351            if(first->xpressive_time > 0)
352            {
353               xpressive_total += first->xpressive_time / first->factor;
354               ++xpressive_test_count;
355            }
356         }
357#endif
358         os << "</tr>\n";
359         ++first;
360      }
361      os << "</table>\n";
362      result_list.clear();
363   }
364   else
365   {
366      os << "<P><I>Results not available...</I></P>\n";
367   }
368
369   std::string result = os.str();
370
371   std::string::size_type pos = html_contents.find(tagname);
372   if(pos != std::string::npos)
373   {
374      html_contents.replace(pos, tagname.size(), result);
375   }
376}
377
378std::string get_boost_version()
379{
380   std::stringstream os;
381   os << (BOOST_VERSION / 100000) << '.' << ((BOOST_VERSION / 100) % 1000) << '.' << (BOOST_VERSION % 100);
382   return os.str();
383}
384
385std::string get_averages_table()
386{
387   std::stringstream os;
388   //
389   // start by outputting the table header:
390   //
391   os << "<table border=\"1\" cellspacing=\"1\">\n";
392   os << "<tr>";
393#if defined(BOOST_HAS_GRETA)
394   if(time_greta == true)
395   {
396      os << "<td><strong>GRETA</strong></td>";
397   }
398   if(time_safe_greta == true)
399   {
400      os << "<td><strong>GRETA<BR>(non-recursive mode)</strong></td>";
401   }
402
403#endif
404   if(time_boost == true)
405   {
406      os << "<td><strong>Boost</strong></td>";
407   }
408   if(time_localised_boost == true)
409   {
410      os << "<td><strong>Boost + C++ locale</strong></td>";
411   }
412#if defined(BOOST_HAS_POSIX)
413   if(time_posix == true)
414   {
415      os << "<td><strong>POSIX</strong></td>";
416   }
417#endif
418#ifdef BOOST_HAS_PCRE
419   if(time_pcre == true)
420   {
421      os << "<td><strong>PCRE</strong></td>";
422   }
423#endif
424#ifdef BOOST_HAS_XPRESSIVE
425   if(time_xpressive == true)
426   {
427      os << "<td><strong>Dynamic Xpressive</strong></td>";
428   }
429#endif
430   os << "</tr>\n";
431
432   //
433   // Now enumerate through all averages:
434   //
435   os << "<tr>";
436#if defined(BOOST_HAS_GRETA)
437   if(time_greta == true)
438      os << "<td>" << (greta_total / greta_test_count) << "</td>\n";
439   if(time_safe_greta == true)
440      os << "<td>" << (safe_greta_total / safe_greta_test_count) << "</td>\n";
441#endif
442#if defined(BOOST_HAS_POSIX)
443   if(time_boost == true)
444      os << "<td>" << (boost_total / boost_test_count) << "</td>\n";
445#endif
446   if(time_localised_boost == true)
447      os << "<td>" << (locale_boost_total / locale_boost_test_count) << "</td>\n";
448   if(time_posix == true)
449      os << "<td>" << (posix_total / posix_test_count) << "</td>\n";
450#if defined(BOOST_HAS_PCRE)
451   if(time_pcre == true)
452      os << "<td>" << (pcre_total / pcre_test_count) << "</td>\n";
453#endif
454#if defined(BOOST_HAS_XPRESSIVE)
455   if(time_xpressive == true)
456      os << "<td>" << (xpressive_total / xpressive_test_count) << "</td>\n";
457#endif
458   os << "</tr>\n";
459   os << "</table>\n";
460   return os.str();
461}
462
463void output_final_html()
464{
465   if(html_out_file.size())
466   {
467      //
468      // start with search and replace ops:
469      //
470      std::string::size_type pos;
471      pos = html_contents.find("%compiler%");
472      if(pos != std::string::npos)
473      {
474         html_contents.replace(pos, 10, BOOST_COMPILER);
475      }
476      pos = html_contents.find("%library%");
477      if(pos != std::string::npos)
478      {
479         html_contents.replace(pos, 9, BOOST_STDLIB);
480      }
481      pos = html_contents.find("%os%");
482      if(pos != std::string::npos)
483      {
484         html_contents.replace(pos, 4, BOOST_PLATFORM);
485      }
486      pos = html_contents.find("%boost%");
487      if(pos != std::string::npos)
488      {
489         html_contents.replace(pos, 7, get_boost_version());
490      }
491      pos = html_contents.find("%pcre%");
492      if(pos != std::string::npos)
493      {
494#ifdef PCRE_MINOR
495         html_contents.replace(pos, 6, BOOST_STRINGIZE(PCRE_MAJOR.PCRE_MINOR));
496#else
497         html_contents.replace(pos, 6, "N/A");
498#endif
499      }
500      pos = html_contents.find("%averages%");
501      if(pos != std::string::npos)
502      {
503         html_contents.replace(pos, 10, get_averages_table());
504      }
505     //
506      // now right the output to file:
507      //
508      std::ofstream os(html_out_file.c_str());
509      os << html_contents;
510   }
511   else
512   {
513      std::cout << html_contents;
514   }
515}
Note: See TracBrowser for help on using the repository browser.