diff options
author | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2011-09-05 23:30:31 +0000 |
---|---|---|
committer | jsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68> | 2011-09-05 23:30:31 +0000 |
commit | c298b01922ab03d078f33ec1ceb3e7471277f48c (patch) | |
tree | fbeebb3c73b23b841ebe3d1be9cab397e80bf0fd /sca-cpp/trunk/modules/python/python-test.cpp | |
parent | 31ed46ac8dab9abaee7c74b8d8fb53bde49a317e (diff) |
Support multithreaded execution of Python components with the HTTPD event MPM.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1165452 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-cpp/trunk/modules/python/python-test.cpp')
-rw-r--r-- | sca-cpp/trunk/modules/python/python-test.cpp | 178 |
1 files changed, 173 insertions, 5 deletions
diff --git a/sca-cpp/trunk/modules/python/python-test.cpp b/sca-cpp/trunk/modules/python/python-test.cpp index 41889e6d0e..bc275f27c7 100644 --- a/sca-cpp/trunk/modules/python/python-test.cpp +++ b/sca-cpp/trunk/modules/python/python-test.cpp @@ -27,6 +27,8 @@ #include "stream.hpp" #include "string.hpp" #include "driver.hpp" +#include "parallel.hpp" +#include "perf.hpp" namespace tuscany { namespace python { @@ -40,14 +42,15 @@ bool testEvalExpr() { PythonRuntime py; istringstream is(testPythonAdd); - failable<PyObject*> script = readScript("script", "script.py", is); + failable<PyObject*> script = readScript("script", "script.py", is, py); assert(hasContent(script)); const value exp = mklist<value>("add", 2, 3); - const failable<value> r = evalScript(exp, content(script)); + const failable<value> r = evalScript(exp, content(script), py); assert(hasContent(r)); assert(content(r) == value(5)); + releaseScript(content(script), py); return true; } @@ -74,7 +77,7 @@ bool testEvalLambda() { const value trl = mklist<value>("testReturnLambda"); istringstream trlis(testReturnLambda); - const failable<value> trlv = evalScript(trl, trlis); + const failable<value> trlv = evalScript(trl, trlis, py); assert(hasContent(trlv)); assert(isLambda(content(trlv))); @@ -83,18 +86,179 @@ bool testEvalLambda() { istringstream tclis(testCallLambda); const value tcl = mklist<value>("testCallLambda", content(trlv), 2, 3); - const failable<value> tclv = evalScript(tcl, tclis); + const failable<value> tclv = evalScript(tcl, tclis, py); assert(hasContent(tclv)); assert(content(tclv) == value(6)); istringstream tcelis(testCallLambda); const value tcel = mklist<value>("testCallLambda", lambda<value(const list<value>&)>(mult), 3, 4); - const failable<value> tcelv = evalScript(tcel, tcelis); + const failable<value> tcelv = evalScript(tcel, tcelis, py); assert(hasContent(tcelv)); assert(content(tcelv) == value(12)); return true; } +struct testEvalReadAdd { + PythonRuntime& py; + testEvalReadAdd(PythonRuntime& py) : py(py) { + } + const bool operator()() const { + istringstream is(testPythonAdd); + failable<PyObject*> script = readScript("script", "script.py", is, py); + assert(hasContent(script)); + + const value exp = mklist<value>("add", 2, 3); + const failable<value> r = evalScript(exp, content(script), py); + assert(hasContent(r)); + assert(content(r) == value(5)); + + releaseScript(content(script), py); + return true; + } +}; + +struct testEvalAdd { + PyObject* script; + PythonRuntime& py; + testEvalAdd(PyObject* script, PythonRuntime& py) : script(script), py(py) { + } + const bool operator()() const { + const value exp = mklist<value>("add", 2, 3); + const failable<value> r = evalScript(exp, script, py); + assert(hasContent(r)); + assert(content(r) == value(5)); + return true; + } +}; + +bool testEvalPerf() { + gc_scoped_pool pool; + PythonRuntime py; + + const lambda<bool()> erl = lambda<bool()>(testEvalReadAdd(py)); + cout << "Python read + eval test " << time(erl, 5, 10000) << " ms" << endl; + + istringstream is(testPythonAdd); + failable<PyObject*> script = readScript("script", "script.py", is, py); + assert(hasContent(script)); + + const lambda<bool()> el = lambda<bool()>(testEvalAdd(content(script), py)); + cout << "Python eval test " << time(el, 5, 10000) << " ms" << endl; + + releaseScript(content(script), py); + return true; +} + +#ifdef WANT_THREADS + +struct testReadEvalAddLoop { + PythonRuntime& py; + testReadEvalAddLoop(PythonRuntime& py) : py(py) { + } + const bool operator()() const { + for (int i = 0; i < 100; i++) { + istringstream is(testPythonAdd); + failable<PyObject*> script = readScript("script", "script.py", is, py); + assert(hasContent(script)); + + const value exp = mklist<value>("add", 2, 3); + const failable<value> r = evalScript(exp, content(script), py); + assert(hasContent(r)); + assert(content(r) == value(5)); + + releaseScript(content(script), py); + } + return true; + } +}; + +struct testEvalAddLoop { + PyObject* script; + PythonRuntime& py; + testEvalAddLoop(PyObject* script, PythonRuntime& py) : script(script), py(py) { + } + const bool operator()() const { + for (int i = 0; i < 100; i++) { + const value exp = mklist<value>("add", 2, 3); + const failable<value> r = evalScript(exp, script, py); + assert(hasContent(r)); + assert(content(r) == value(5)); + } + return true; + } +}; + +const list<future<bool> > submitReadEvals(worker& w, const int max, const int i, PythonRuntime& py) { + if (i == max) + return list<future<bool> >(); + const lambda<bool()> func = lambda<bool()>(testReadEvalAddLoop(py)); + return cons(submit(w, func), submitReadEvals(w, max, i + 1, py)); +} + +const list<future<bool> > submitEvals(worker& w, const int max, const int i, PyObject* script, PythonRuntime& py) { + if (i == max) + return list<future<bool> >(); + const lambda<bool()> func = lambda<bool()>(testEvalAddLoop(script, py)); + return cons(submit(w, func), submitEvals(w, max, i + 1, script, py)); +} + +bool checkEvalResults(const list<future<bool> > r) { + if (isNil(r)) + return true; + assert(car(r) == true); + return checkEvalResults(cdr(r)); +} + +struct testReadEvalThreads { + worker& w; + const int max; + PythonRuntime& py; + testReadEvalThreads(worker& w, const int max, PythonRuntime& py) : w(w), max(max), py(py) { + } + const bool operator()() const { + const list<future<bool> > r(submitReadEvals(w, max, 0, py)); + checkEvalResults(r); + return true; + } +}; + +struct testEvalThreads { + worker& w; + const int max; + PyObject* script; + PythonRuntime& py; + testEvalThreads(worker& w, const int max, PyObject* script, PythonRuntime& py) : w(w), max(max), script(script), py(py) { + } + const bool operator()() const { + const list<future<bool> > r(submitEvals(w, max, 0, script, py)); + checkEvalResults(r); + return true; + } +}; + +bool testThreads() { + gc_scoped_pool pool; + PythonRuntime py; + + const int max = 100; + worker w(max); + + const lambda<bool()> elr = lambda<bool()>(testReadEvalThreads(w, max, py)); + cout << "Python eval + read thread test " << time(elr, 1, 1) / 10000.0 << " ms" << endl; + + istringstream is(testPythonAdd); + failable<PyObject*> script = readScript("script", "script.py", is, py); + assert(hasContent(script)); + + const lambda<bool()> el = lambda<bool()>(testEvalThreads(w, max, content(script), py)); + cout << "Python eval thread test " << time(el, 1, 1) / 10000.0 << " ms" << endl; + + releaseScript(content(script), py); + return true; +} + +#endif + } } @@ -103,6 +267,10 @@ int main() { tuscany::python::testEvalExpr(); tuscany::python::testEvalLambda(); + tuscany::python::testEvalPerf(); +#ifdef WANT_THREADS + tuscany::python::testThreads(); +#endif tuscany::cout << "OK" << tuscany::endl; return 0; |