diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-01-02 10:27:26 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2010-01-02 10:27:26 +0000 |
commit | 95fa76f5f3208d913320c13a05171ecdcd7134c2 (patch) | |
tree | 872e101cd2fb1505baf313940e48c6b615fd6725 /sca-cpp/trunk/components/cache | |
parent | 1d04916fda43146fb62488c20ba03b7b3006c8e9 (diff) |
Performance improvements when running both in multi-threaded and pre-forked HTTPD. Changed memory management to use Apache APR pools instead of ref counting pointers as it's much faster and easier to integrate with the Python and Ruby interpreters. Changed to use simple pool-based string and stream implementations instead of the STL ones, which cause a lots of mutex locks in a multi-threaded environment. Added build options to compile with threading and profiling.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@895165 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-cpp/trunk/components/cache')
-rw-r--r-- | sca-cpp/trunk/components/cache/mcache-client-test.cpp | 46 | ||||
-rw-r--r-- | sca-cpp/trunk/components/cache/mcache-test.cpp | 23 | ||||
-rw-r--r-- | sca-cpp/trunk/components/cache/mcache.cpp | 26 | ||||
-rw-r--r-- | sca-cpp/trunk/components/cache/mcache.hpp | 63 | ||||
-rwxr-xr-x | sca-cpp/trunk/components/cache/memcached-server-test | 2 | ||||
-rwxr-xr-x | sca-cpp/trunk/components/cache/memcached-test | 2 |
6 files changed, 79 insertions, 83 deletions
diff --git a/sca-cpp/trunk/components/cache/mcache-client-test.cpp b/sca-cpp/trunk/components/cache/mcache-client-test.cpp index 2e444250c7..e3bf5f7a2e 100644 --- a/sca-cpp/trunk/components/cache/mcache-client-test.cpp +++ b/sca-cpp/trunk/components/cache/mcache-client-test.cpp @@ -24,8 +24,8 @@ */ #include <assert.h> -#include <iostream> -#include <string> +#include "stream.hpp" +#include "string.hpp" #include "list.hpp" #include "value.hpp" @@ -36,46 +36,46 @@ namespace tuscany { namespace cache { -const std::string url("http://localhost:8090/mcache"); +const string url("http://localhost:8090/mcache"); bool testCache() { http::CURLSession cs; const list<value> i = list<value>() - << (list<value>() << "name" << std::string("Apple")) - << (list<value>() << "price" << std::string("$2.99")); - const list<value> a = mklist<value>(std::string("item"), std::string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); + + (list<value>() + "name" + string("Apple")) + + (list<value>() + "price" + string("$2.99")); + const list<value> a = mklist<value>(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); - const failable<value, std::string> id = http::post(a, url, cs); + const failable<value> id = http::post(a, url, cs); assert(hasContent(id)); { - const failable<value, std::string> val = http::get(url + "/" + std::string(content(id)), cs); + const failable<value> val = http::get(url + "/" + content(id), cs); assert(hasContent(val)); assert(content(val) == a); } const list<value> j = list<value>() - << (list<value>() << "name" << std::string("Apple")) - << (list<value>() << "price" << std::string("$3.55")); - const list<value> b = mklist<value>(std::string("item"), std::string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), j); + + (list<value>() + "name" + string("Apple")) + + (list<value>() + "price" + string("$3.55")); + const list<value> b = mklist<value>(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), j); { - const failable<value, std::string> r = http::put(b, url + "/" + std::string(content(id)), cs); + const failable<value> r = http::put(b, url + "/" + content(id), cs); assert(hasContent(r)); assert(content(r) == value(true)); } { - const failable<value, std::string> val = http::get(url + "/" + std::string(content(id)), cs); + const failable<value> val = http::get(url + "/" + content(id), cs); assert(hasContent(val)); assert(content(val) == b); } { - const failable<value, std::string> r = http::del(url + "/" + std::string(content(id)), cs); + const failable<value> r = http::del(url + "/" + content(id), cs); assert(hasContent(r)); assert(content(r) == value(true)); } { - const failable<value, std::string> val = http::get(url + "/" + std::string(content(id)), cs); + const failable<value> val = http::get(url + "/" + content(id), cs); assert(!hasContent(val)); } @@ -89,7 +89,7 @@ struct getLoop { getLoop(const value& id, const value& entry, http::CURLSession cs) : id(id), entry(entry), cs(cs) { } const bool operator()() const { - const failable<value, std::string> val = http::get(url + "/" + std::string(id), cs); + const failable<value> val = http::get(url + "/" + id, cs); assert(hasContent(val)); assert(content(val) == entry); return true; @@ -98,16 +98,16 @@ struct getLoop { bool testGetPerf() { const list<value> i = list<value>() - << (list<value>() << "name" << std::string("Apple")) - << (list<value>() << "price" << std::string("$4.55")); - const value a = mklist<value>(std::string("item"), std::string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); + + (list<value>() + "name" + string("Apple")) + + (list<value>() + "price" + string("$4.55")); + const value a = mklist<value>(string("item"), string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b"), i); http::CURLSession cs; - const failable<value, std::string> id = http::post(a, url, cs); + const failable<value> id = http::post(a, url, cs); assert(hasContent(id)); const lambda<bool()> gl = getLoop(content(id), a, cs); - std::cout << "Cache get test " << time(gl, 5, 200) << " ms" << std::endl; + cout << "Cache get test " << time(gl, 5, 200) << " ms" << endl; return true; } @@ -116,12 +116,12 @@ bool testGetPerf() { } int main() { - std::cout << "Testing..." << std::endl; + tuscany::cout << "Testing..." << tuscany::endl; tuscany::cache::testCache(); tuscany::cache::testGetPerf(); - std::cout << "OK" << std::endl; + tuscany::cout << "OK" << tuscany::endl; return 0; } diff --git a/sca-cpp/trunk/components/cache/mcache-test.cpp b/sca-cpp/trunk/components/cache/mcache-test.cpp index dc286687c0..90f17ffd48 100644 --- a/sca-cpp/trunk/components/cache/mcache-test.cpp +++ b/sca-cpp/trunk/components/cache/mcache-test.cpp @@ -24,8 +24,8 @@ */ #include <assert.h> -#include <iostream> -#include <string> +#include "stream.hpp" +#include "string.hpp" #include "perf.hpp" #include "mcache.hpp" @@ -35,10 +35,10 @@ namespace cache { bool testMemCached() { MemCached ch; - assert(hasContent(post("a", std::string("AAA"), ch))); - assert(get("a", ch) == value(std::string("AAA"))); - assert(hasContent(put("a", std::string("aaa"), ch))); - assert(get("a", ch) == value(std::string("aaa"))); + assert(hasContent(post("a", string("AAA"), ch))); + assert((get("a", ch)) == value(string("AAA"))); + assert(hasContent(put("a", string("aaa"), ch))); + assert((get("a", ch)) == value(string("aaa"))); assert(hasContent(del("a", ch))); assert(!hasContent(get("a", ch))); @@ -50,18 +50,17 @@ struct getLoop { getLoop(MemCached& ch) : ch(ch) { } const bool operator()() const { - assert(get("c", ch) == value(std::string("CCC"))); + assert((get("c", ch)) == value(string("CCC"))); return true; } }; bool testGetPerf() { MemCached ch; - assert(hasContent(post("c", std::string("CCC"), ch))); + assert(hasContent(post("c", string("CCC"), ch))); const lambda<bool()> gl = getLoop(ch); - std::cout << "Memcached get test " << time(gl, 5, 200) << " ms" << std::endl; - + cout << "Memcached get test " << time(gl, 5, 200) << " ms" << endl; return true; } @@ -69,12 +68,12 @@ bool testGetPerf() { } int main() { - std::cout << "Testing..." << std::endl; + tuscany::cout << "Testing..." << tuscany::endl; tuscany::cache::testMemCached(); tuscany::cache::testGetPerf(); - std::cout << "OK" << std::endl; + tuscany::cout << "OK" << tuscany::endl; return 0; } diff --git a/sca-cpp/trunk/components/cache/mcache.cpp b/sca-cpp/trunk/components/cache/mcache.cpp index 926fb66674..3e8fdeb294 100644 --- a/sca-cpp/trunk/components/cache/mcache.cpp +++ b/sca-cpp/trunk/components/cache/mcache.cpp @@ -25,7 +25,7 @@ #include <apr_uuid.h> -#include <string> +#include "string.hpp" #include "function.hpp" #include "list.hpp" @@ -41,7 +41,7 @@ cache::MemCached ch; /** * Get an item from the cache. */ -const failable<value, std::string> get(const list<value>& params) { +const failable<value> get(const list<value>& params) { return cache::get(car(params), ch); } @@ -53,34 +53,34 @@ const value uuidValue() { apr_uuid_get(&uuid); char buf[APR_UUID_FORMATTED_LENGTH]; apr_uuid_format(buf, &uuid); - return value(std::string(buf, APR_UUID_FORMATTED_LENGTH)); + return value(string(buf, APR_UUID_FORMATTED_LENGTH)); } -const failable<value, std::string> post(const list<value>& params) { +const failable<value> post(const list<value>& params) { const value id = uuidValue(); - const failable<bool, std::string> val = cache::post(id, car(params), ch); + const failable<bool> val = cache::post(id, car(params), ch); if (!hasContent(val)) - return mkfailure<value, std::string>(reason(val)); + return mkfailure<value>(reason(val)); return id; } /** * Put an item into the cache. */ -const failable<value, std::string> put(const list<value>& params) { - const failable<bool, std::string> val = cache::put(car(params), cadr(params), ch); +const failable<value> put(const list<value>& params) { + const failable<bool> val = cache::put(car(params), cadr(params), ch); if (!hasContent(val)) - return mkfailure<value, std::string>(reason(val)); + return mkfailure<value>(reason(val)); return value(content(val)); } /** * Delete an item from the cache. */ -const failable<value, std::string> del(const list<value>& params) { - const failable<bool, std::string> val = cache::del(car(params), ch); +const failable<value> del(const list<value>& params) { + const failable<bool> val = cache::del(car(params), ch); if (!hasContent(val)) - return mkfailure<value, std::string>(reason(val)); + return mkfailure<value>(reason(val)); return value(content(val)); } @@ -99,7 +99,7 @@ const tuscany::value eval(const tuscany::list<tuscany::value>& params) { return tuscany::cache::put(cdr(params)); if (func == "delete") return tuscany::cache::del(cdr(params)); - return tuscany::mkfailure<tuscany::value, std::string>(std::string("Function not supported: ") + std::string(func)); + return tuscany::mkfailure<tuscany::value>(tuscany::string("Function not supported: ") + func); } } diff --git a/sca-cpp/trunk/components/cache/mcache.hpp b/sca-cpp/trunk/components/cache/mcache.hpp index d1fe182946..3f32a47964 100644 --- a/sca-cpp/trunk/components/cache/mcache.hpp +++ b/sca-cpp/trunk/components/cache/mcache.hpp @@ -34,8 +34,7 @@ #include "apr_memcache.h" #include "apr_network_io.h" -#include <string> -#include <sstream> +#include "string.hpp" #include "list.hpp" #include "value.hpp" #include "monad.hpp" @@ -56,7 +55,7 @@ public: init("localhost", 11211); } - MemCached(const std::string host, const int port) { + MemCached(const string host, const int port) { apr_pool_create(&pool, NULL); apr_memcache_create(pool, 1, 0, &mc); init(host, port); @@ -70,22 +69,22 @@ private: apr_pool_t* pool; apr_memcache_t* mc; - friend const failable<bool, std::string> post(const value& key, const value& val, const MemCached& cache); - friend const failable<bool, std::string> put(const value& key, const value& val, const MemCached& cache); - friend const failable<value, std::string> get(const value& key, const MemCached& cache); - friend const failable<bool, std::string> del(const value& key, const MemCached& cache); + friend const failable<bool> post(const value& key, const value& val, const MemCached& cache); + friend const failable<bool> put(const value& key, const value& val, const MemCached& cache); + friend const failable<value> get(const value& key, const MemCached& cache); + friend const failable<bool> del(const value& key, const MemCached& cache); /** * Initialize the memcached context. */ - const failable<bool, std::string> init(const std::string& host, const int port) { + const failable<bool> init(const string& host, const int port) { apr_memcache_server_t *server; - const apr_status_t sc = apr_memcache_server_create(pool, host.c_str(), (apr_port_t)port, 0, 1, 1, 60, &server); + const apr_status_t sc = apr_memcache_server_create(pool, c_str(host), (apr_port_t)port, 0, 1, 1, 60, &server); if (sc != APR_SUCCESS) - return mkfailure<bool, std::string>("Could not create server"); + return mkfailure<bool>("Could not create server"); const apr_status_t as = apr_memcache_add_server(mc, server); if (as != APR_SUCCESS) - return mkfailure<bool, std::string>("Could not add server"); + return mkfailure<bool>("Could not add server"); return true; } @@ -94,15 +93,15 @@ private: /** * Post a new item to the cache. */ -const failable<bool, std::string> post(const value& key, const value& val, const MemCached& cache) { +const failable<bool> post(const value& key, const value& val, const MemCached& cache) { debug(key, "cache::post::key"); debug(val, "cache::post::value"); - const std::string ks(eval::writeValue(key)); - const std::string vs(eval::writeValue(val)); - const apr_status_t rc = apr_memcache_add(cache.mc, ks.c_str(), const_cast<char*>(vs.c_str()), vs.size(), 0, 27); + const string ks(eval::writeValue(key)); + const string vs(eval::writeValue(val)); + const apr_status_t rc = apr_memcache_add(cache.mc, c_str(ks), const_cast<char*>(c_str(vs)), length(vs), 0, 27); if (rc != APR_SUCCESS) - return mkfailure<bool, std::string>("Could not add entry"); + return mkfailure<bool>("Could not add entry"); debug(true, "cache::post::result"); return true; @@ -111,15 +110,15 @@ const failable<bool, std::string> post(const value& key, const value& val, const /** * Update an item in the cache. If the item doesn't exist it is added. */ -const failable<bool, std::string> put(const value& key, const value& val, const MemCached& cache) { +const failable<bool> put(const value& key, const value& val, const MemCached& cache) { debug(key, "cache::put::key"); debug(val, "cache::put::value"); - const std::string ks(eval::writeValue(key)); - const std::string vs(eval::writeValue(val)); - const apr_status_t rc = apr_memcache_set(cache.mc, ks.c_str(), const_cast<char*>(vs.c_str()), vs.size(), 0, 27); + const string ks(eval::writeValue(key)); + const string vs(eval::writeValue(val)); + const apr_status_t rc = apr_memcache_set(cache.mc, c_str(ks), const_cast<char*>(c_str(vs)), length(vs), 0, 27); if (rc != APR_SUCCESS) - return mkfailure<bool, std::string>("Could not add entry"); + return mkfailure<bool>("Could not add entry"); debug(true, "cache::put::result"); return true; @@ -128,25 +127,24 @@ const failable<bool, std::string> put(const value& key, const value& val, const /** * Get an item from the cache. */ -const failable<value, std::string> get(const value& key, const MemCached& cache) { +const failable<value> get(const value& key, const MemCached& cache) { debug(key, "cache::get::key"); - const std::string ks(eval::writeValue(key)); - + const string ks(eval::writeValue(key)); apr_pool_t* vpool; const apr_status_t pc = apr_pool_create(&vpool, cache.pool); if (pc != APR_SUCCESS) - return mkfailure<value, std::string>("Could not allocate memory"); + return mkfailure<value>("Could not allocate memory"); char *data; apr_size_t size; - const apr_status_t rc = apr_memcache_getp(cache.mc, cache.pool, ks.c_str(), &data, &size, NULL); + const apr_status_t rc = apr_memcache_getp(cache.mc, cache.pool, c_str(ks), &data, &size, NULL); if (rc != APR_SUCCESS) { apr_pool_destroy(vpool); - return mkfailure<value, std::string>("Could not get entry"); + return mkfailure<value>("Could not get entry"); } - const value val(eval::readValue(std::string(data, size))); + const value val(eval::readValue(string(data, size))); apr_pool_destroy(vpool); debug(val, "cache::get::result"); @@ -156,14 +154,13 @@ const failable<value, std::string> get(const value& key, const MemCached& cache) /** * Delete an item from the cache */ -const failable<bool, std::string> del(const value& key, const MemCached& cache) { +const failable<bool> del(const value& key, const MemCached& cache) { debug(key, "cache::delete::key"); - std::ostringstream kos; - kos << key; - const apr_status_t rc = apr_memcache_delete(cache.mc, kos.str().c_str(), 0); + const string ks(eval::writeValue(key)); + const apr_status_t rc = apr_memcache_delete(cache.mc, c_str(ks), 0); if (rc != APR_SUCCESS) - return mkfailure<bool, std::string>("Could not delete entry"); + return mkfailure<bool>("Could not delete entry"); debug(true, "cache::delete::result"); return true; diff --git a/sca-cpp/trunk/components/cache/memcached-server-test b/sca-cpp/trunk/components/cache/memcached-server-test index 0dc32f6613..f3c4287dc8 100755 --- a/sca-cpp/trunk/components/cache/memcached-server-test +++ b/sca-cpp/trunk/components/cache/memcached-server-test @@ -35,7 +35,7 @@ $mc & sleep 2 # Test -./mcache-client-test 2>/dev/null +./mcache-client-test rc=$? # Cleanup diff --git a/sca-cpp/trunk/components/cache/memcached-test b/sca-cpp/trunk/components/cache/memcached-test index 0160188f65..df21d32a57 100755 --- a/sca-cpp/trunk/components/cache/memcached-test +++ b/sca-cpp/trunk/components/cache/memcached-test @@ -23,7 +23,7 @@ $mc & sleep 1 # Test -./mcache-test 2>/dev/null +./mcache-test rc=$? # Cleanup |