$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
From: lists.drrngrvy_at_[hidden]
Date: 2008-03-21 18:06:44
Author: drrngrvy
Date: 2008-03-21 18:06:43 EDT (Fri, 21 Mar 2008)
New Revision: 43786
URL: http://svn.boost.org/trac/boost/changeset/43786
Log:
Fixing amortization examples so they don't use custom boost::high_resolution_timer or custom Google.cTemplate libraries.
Text files modified: 
   sandbox/SOC/2007/cgi/branches/acceptor_work/libs/cgi/example/acgi/amortization/main.cpp |    88 ++++++++++++++------------------------- 
   sandbox/SOC/2007/cgi/branches/acceptor_work/libs/cgi/example/fcgi/amortization/main.cpp |    89 +++++++++------------------------------ 
   2 files changed, 53 insertions(+), 124 deletions(-)
Modified: sandbox/SOC/2007/cgi/branches/acceptor_work/libs/cgi/example/acgi/amortization/main.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/acceptor_work/libs/cgi/example/acgi/amortization/main.cpp	(original)
+++ sandbox/SOC/2007/cgi/branches/acceptor_work/libs/cgi/example/acgi/amortization/main.cpp	2008-03-21 18:06:43 EDT (Fri, 21 Mar 2008)
@@ -21,7 +21,6 @@
 #include <boost/cgi/acgi.hpp>
 #include <boost/algorithm/string/regex.hpp>
 #include <google/template.h>
-#include <boost/high_resolution_timer.hpp>
 
 using namespace boost::acgi;
 
@@ -102,38 +101,53 @@
   google::Template* tmpl
     = google::Template::GetTemplate("example.tpl", google::STRIP_WHITESPACE);
 
-  boost::high_resolution_timer t;
-
   std::string arg(req.GET("arg"));
+  if (arg.empty())
+    arg = "4"; // Make this the default
+
+  // Depending on the value of `arg`, we can write the output in
+  // different ways.
 
   if (arg == "1")
   {
+    // Output the response straight to std::cout. This won't work with
+    // anything but vanilla CGI and isn't recommended. It's useful for
+    // checking things work though (don't forget to write a content type
+    // header - followed by a blank line - first though!).
     std::string output;
     tmpl->Expand(&output, &dict);
     std::cout<< output;
   }else
   if (arg == "2")
   {
+    // Expand the output to a string and copy the string to the response.
+    // Should be expensive, but doesn't seem to impact performance hugely...
     std::string output;
     tmpl->Expand(&output, &dict);
     resp<< output;
   }else
-  if (arg == "3")
-  {
-    std::string s;
-    std::vector<boost::asio::const_buffer> out;
-
-    tmpl->Expand(&s, &out, &dict);
-    write(req.client(), out);
-  }else
+//  if (arg == "3")
+//  {
+//    // Expand the string to a vector<const_buffer>, which should minimise any
+//    // copying of data. This requires a modified version of Google.cTemplate, but
+//    // doesn't seem to add anything to performance. Will have to check if there's a
+//    // better way to do it. 
+//    std::string s;
+//    std::vector<boost::asio::const_buffer> out;
+//
+//   tmpl->Expand(&s, &out, &dict);
+//    write(req.client(), out);
+//  }else
   if (arg == "4")
   {
+    // Write the output directly to the request's client.
     std::string output;
     tmpl->Expand(&output, &dict);
     write(req.client(), buffer(output));
   }else
    if (arg == "5")
   {
+    // An alternative to { arg == "1" }, which seems to be slightly faster.
     std::string output;
     tmpl->Expand(&output, &dict);
     std::cout.write(output.c_str(), output.size());
@@ -141,58 +155,20 @@
   {
     resp<< "Error!";
   }
-   //output.clear();
-      //<< "<b>" << t.elapsed() << "</b><p />"
-      //<< output << "<p /><p />"
-      //<< "<b>" << t.elapsed() << "</b><p />";
-  //}
- 
-  //std::cout<<
-  //  "<pre>\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nSTARTING OTHER THINGY\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n</pre>"
-  //  ;
-/*
-    std::string s;
-    std::vector<boost::asio::const_buffer> out;
-for(int i=0; i < 50000; ++i) {
-   // boost::high_resolution_timer t;
-
-    tmpl->Expand(&s, &out, &dict);
-    out.clear();
-    s.clear();
-    //std::cout<< "<b>" << t.elapsed() << "</b><p />";
-
-      //<< "<p /><p />"
-      //<< "<b>" << t.elapsed() << "</b><p />";
-  }
- 
-
-  std::cerr<< "a took " << std::setiosflags(std::ios::fixed) << std::setprecision(5) << a/100000 << " secs<p />"
-           << "b took " << b/100000 << " secs<p />";
-
-  
-  std::cerr
-    << "Content-type: text/html\r\n\r\n"
-    << output << "<p /><p />";
-  
-  resp<< content_type("text/html");
-  resp.flush(req.client());
-  write(req.client(), buffer(output));
-  */
 }
 
 int main()
 {
   try{
-  std::cout
-    << "Content-type: text/html\r\n\r\n";
-  service s;
-  request req(s);
-  req.load(true);
-  response resp;
+    std::cout<< "Content-type: text/html\r\n\r\n"; // just for debugging
+    service s;
+    request req(s);
+    req.load(true);
+    response resp;
 
-  write_amortization_template(req, resp);
+    write_amortization_template(req, resp);
   
-  return_(resp, req, 0);
+    return_(resp, req, 0);
   }catch(...){
     std::cout<< "Content-type: text/html\r\n\r\n"
       << "ERROR!! BOOM!";
Modified: sandbox/SOC/2007/cgi/branches/acceptor_work/libs/cgi/example/fcgi/amortization/main.cpp
==============================================================================
--- sandbox/SOC/2007/cgi/branches/acceptor_work/libs/cgi/example/fcgi/amortization/main.cpp	(original)
+++ sandbox/SOC/2007/cgi/branches/acceptor_work/libs/cgi/example/fcgi/amortization/main.cpp	2008-03-21 18:06:43 EDT (Fri, 21 Mar 2008)
@@ -11,8 +11,8 @@
 // -----------------------
 //
 // This file uses Google cTemplate to show the benefits of using an
-// HTML template engine. The code isn't commented, but should be 
-// reasonably self-explanatory.
+// HTML template engine. The code isn't commented yet, but should be 
+// *reasonably* self-explanatory.
 //
 // It is a very basic amortization calculator.
 //
@@ -21,7 +21,6 @@
 #include <boost/cgi/fcgi.hpp>
 #include <boost/algorithm/string/regex.hpp>
 #include <google/template.h>
-#include <boost/high_resolution_timer.hpp>
 
 using namespace boost::fcgi;
 
@@ -103,13 +102,14 @@
   google::Template* tmpl
     = google::Template::GetTemplate("amortization.tpl", google::STRIP_WHITESPACE);
 
-  boost::high_resolution_timer t;
-
   std::string h("Content-type: text/html\r\n\r\n");
   write(req.client(), buffer(h));
 
   std::string arg(req.GET("arg"));
+  if (arg.empty())
+    arg = "2"; // set this as default (for no particular reason).
 
+  // Different, but equivalent ways of writing the output.
   if (arg == "1")
   {
     std::string output;
@@ -122,55 +122,20 @@
     tmpl->Expand(&output, &dict);
     write(req.client(), buffer(output));
   }else
-  if (arg == "3")
-  {
-    std::string s;
-    std::vector<boost::asio::const_buffer> out;
-
-    tmpl->Expand(&s, &out, &dict);
-    write(req.client(), out);
-  }else
+//  if (arg == "3")
+//  {
+//    // This requires a modified version of Google.cTemplate, so it won't work.
+//    std::string s;
+//    std::vector<boost::asio::const_buffer> out;
+//
+//    tmpl->Expand(&s, &out, &dict);
+//    write(req.client(), out);
+//  }else
   {
     resp<< "Error!";
     return 1;
   }
-   //output.clear();
-      //<< "<b>" << t.elapsed() << "</b><p />"
-      //<< output << "<p /><p />"
-      //<< "<b>" << t.elapsed() << "</b><p />";
-  //}
- 
-  //std::cout<<
-  //  "<pre>\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nSTARTING OTHER THINGY\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n</pre>"
-  //  ;
-/*
-    std::string s;
-    std::vector<boost::asio::const_buffer> out;
-for(int i=0; i < 50000; ++i) {
-   // boost::high_resolution_timer t;
-
-    tmpl->Expand(&s, &out, &dict);
-    out.clear();
-    s.clear();
-    //std::cout<< "<b>" << t.elapsed() << "</b><p />";
-
-      //<< "<p /><p />"
-      //<< "<b>" << t.elapsed() << "</b><p />";
-  }
- 
 
-  std::cerr<< "a took " << std::setiosflags(std::ios::fixed) << std::setprecision(5) << a/100000 << " secs<p />"
-           << "b took " << b/100000 << " secs<p />";
-
-  
-  std::cerr
-    << "Content-type: text/html\r\n\r\n"
-    << output << "<p /><p />";
-  
-  resp<< content_type("text/html");
-  resp.flush(req.client());
-  write(req.client(), buffer(output));
-  */
   return 0;
 }
 
@@ -178,11 +143,6 @@
 {
   boost::system::error_code ec;
 
-  //std::ofstream of("/var/www/log/fcgi_reaccept.txt");
-  using std::endl;
-  //std::cerr<< "Eh?" << endl;
-  //of<< "Opening request" << endl;
-
   request req(a.protocol_service());
  
   int ret = 0;
@@ -191,24 +151,21 @@
   {
     response resp;
     ++num;
-    //std::cerr<< endl << endl << "request num := " << num << endl << endl;
-    //of<< "Accepting now" << endl;
+
+    // Accepting on a closed request is fine (and more efficient than constantly
+    // creating/destructing request objects). You must call close() first though!
     a.accept(req);
-    //of<< "Loading" << endl;
+
     req.load(true);
 
     resp<< content_type("text/html")
         << "map size := " << req.POST().size() << "<p>";
   
-    //of<< "Writing template" << endl;
     ret = write_amortization_template(req, resp);
 
-    //of<< "Sending" << endl;
     resp.send(req.client(), ec);
-    //of<< "Closing" << endl;
+
     ret = ret ? ret : req.close(resp.status(), 0,  ec);
-    //of<< "ok. ec := " << ec.message() << endl;
-    //return 1;
   }
   return ret;
 }
@@ -217,6 +174,7 @@
 {
   for(;;)
   {
+    // Keep handling requests until something goes wrong.
     if (handle_request(a))
       break;
   }
@@ -225,14 +183,9 @@
 int main()
 {
   try{
-  //std::cout
-  //  << "Content-type: text/html\r\n\r\n";
-  using std::endl;
+
     service s;
-  std::cerr<< "Eh1?" << endl;
     acceptor a(s, true);
-    //a.assign(boost::asio::ip::tcp::v4(), 0);
-  std::cerr<< "Eh2?" << endl;
 
     accept_requests(a);