$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
From: mconsoni_at_[hidden]
Date: 2007-06-12 17:01:13
Author: mconsoni
Date: 2007-06-12 17:01:13 EDT (Tue, 12 Jun 2007)
New Revision: 7016
URL: http://svn.boost.org/trac/boost/changeset/7016
Log:
- New benchmarks directory.
- Added the first benchmark, comparing the plain old dlopen approach with extensions. The difference appears to be 3x favoring plain-old approach. Now it only works on Linux.
Added:
   sandbox/libs/extension/benchmarks/
   sandbox/libs/extension/benchmarks/Jamfile.v2
   sandbox/libs/extension/benchmarks/hello_world_plain_old.cpp
   sandbox/libs/extension/benchmarks/plain_old_approach.cpp
Added: sandbox/libs/extension/benchmarks/Jamfile.v2
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/benchmarks/Jamfile.v2	2007-06-12 17:01:13 EDT (Tue, 12 Jun 2007)
@@ -0,0 +1,26 @@
+import type : change-generated-target-suffix ;
+import type : change-generated-target-prefix ;
+type.change-generated-target-suffix SHARED_LIB : : extension ;
+type.change-generated-target-prefix SHARED_LIB : : lib ;
+import os ;
+
+local BOOST_ROOT = [ os.environ BOOST_ROOT ] ;
+project 
+    : requirements
+      <include>../../../
+      <include>$(BOOST_ROOT)
+      <toolset>gcc:<find-static-library>dl
+    :
+    ;
+
+exe PlainOldApproach : plain_old_approach.cpp ;
+lib PlainOldHelloWorldLib : hello_world_plain_old.cpp : <link>shared ;
+
+install ../bin : 
+  PlainOldApproach PlainOldHelloWorldLib
+  :
+ # <install-dependencies>on 
+ # <install-type>EXE 
+ # <install-type>SHARED_LIB
+ # <install-type>LIB
+  ;
Added: sandbox/libs/extension/benchmarks/hello_world_plain_old.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/benchmarks/hello_world_plain_old.cpp	2007-06-12 17:01:13 EDT (Tue, 12 Jun 2007)
@@ -0,0 +1,31 @@
+/* (C) Copyright Mariano G. Consoni 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#include "../examples/word.hpp"
+
+#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
+#  define EXPORT_DECL __declspec(dllexport)
+#else
+#  define EXPORT_DECL
+#endif
+
+
+class world : public word
+{
+public:
+  virtual const char * get_val(){return "world!";}
+};
+class hello : public word
+{
+public:
+  virtual const char * get_val(){return "hello";}
+};
+
+extern "C" void EXPORT_DECL extension_export_words(word **h, word **w)
+{
+	*h = new hello;
+	*w = new world;
+}
Added: sandbox/libs/extension/benchmarks/plain_old_approach.cpp
==============================================================================
--- (empty file)
+++ sandbox/libs/extension/benchmarks/plain_old_approach.cpp	2007-06-12 17:01:13 EDT (Tue, 12 Jun 2007)
@@ -0,0 +1,85 @@
+/* (C) Copyright Mariano G. Consoni 2007
+ * Distributed under the Boost Software License, Version 1.0. (See
+ * accompanying file LICENSE_1_0.txt or copy at
+ * http://www.boost.org/LICENSE_1_0.txt)
+ */
+
+#include <boost/extension/factory_map.hpp>
+#include <boost/extension/shared_library.hpp>
+#include <boost/extension/convenience.hpp>
+#include <boost/timer.hpp>
+
+#include <iostream>
+#include <dlfcn.h>
+
+#include "../examples/word.hpp"
+
+
+int main(void)
+{
+	using namespace boost::extensions;
+
+	const unsigned int times = 100000;
+
+	// boost.extensions style
+	boost::timer extensions_style;
+	for(unsigned int c = 0; c < times; ++c) {
+
+		shared_library l((std::string("libHelloWorldLib") + ".extension").c_str());
+		l.open();
+		{
+			factory_map fm;
+			functor<void, factory_map &> load_func = l.get_functor<void, factory_map &>("extension_export_word");
+			load_func(fm);
+
+			std::list<factory<word, int> > & factory_list = fm.get<word, int>();  
+			for (std::list<factory<word, int> >::iterator current_word = factory_list.begin();
+			     current_word != factory_list.end(); ++current_word)
+			{
+				std::auto_ptr<word> word_ptr(current_word->create());
+
+				// do something with the word
+				std::string s(word_ptr->get_val());
+				s += "\n";
+			}
+		}
+		l.close();
+	}
+	std::cout << "Boost.extensions style: " << extensions_style.elapsed() << std::endl;
+
+
+	// plain old style
+	boost::timer old_style;
+	for(unsigned int c = 0; c < times; ++c) {
+		void *library = dlopen("libPlainOldHelloWorldLib.extension", RTLD_LAZY);
+		if(library == 0) {
+			std::cerr << "Cannot open Hello World Library." << std::endl;
+			return 1;
+		}
+		void (*export_words)(word **, word **);
+		*(void **) (&export_words) = dlsym(library, "extension_export_words");
+		if(export_words == 0) {
+			std::cerr << "Cannot get exported symbol." << std::endl;
+			return 1;
+		}
+		
+		// retrieve the words
+		word *first_word, *second_word;
+		(*export_words)(&first_word, &second_word);
+
+		// do something with the word
+		std::string f(first_word->get_val());
+		f += "\n";
+
+		std::string s(second_word->get_val());
+		s += "\n";
+
+		delete first_word;
+		delete second_word;
+
+		dlclose(library);
+	}
+	std::cout << "Plain old style: " << old_style.elapsed() << std::endl;
+
+	return 0;
+}