From c9bfccc35345ce58fb5774d4b0b6a9868b262c0a Mon Sep 17 00:00:00 2001 From: giorgio Date: Wed, 5 Sep 2012 08:31:30 +0000 Subject: git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1381061 13f79535-47bb-0310-9956-ffa450edef68 --- .../lightweight-sca/hosting/server/client-test.hpp | 436 +++++++++++++++++++++ 1 file changed, 436 insertions(+) create mode 100644 sca-cpp/branches/lightweight-sca/hosting/server/client-test.hpp (limited to 'sca-cpp/branches/lightweight-sca/hosting/server/client-test.hpp') diff --git a/sca-cpp/branches/lightweight-sca/hosting/server/client-test.hpp b/sca-cpp/branches/lightweight-sca/hosting/server/client-test.hpp new file mode 100644 index 0000000000..7d85fc99a6 --- /dev/null +++ b/sca-cpp/branches/lightweight-sca/hosting/server/client-test.hpp @@ -0,0 +1,436 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/* $Rev$ $Date$ */ + +/** + * Test HTTP client functions. + */ + +#include +#include +#include +#include +#include "stream.hpp" +#include "string.hpp" +#include "parallel.hpp" +#include "perf.hpp" +#include "../../modules/http/http.hpp" + +namespace tuscany { +namespace server { + +string testURI = "http://localhost:8090"; +bool testBlobs = true; + +ostream* curlWriter(const string& s, ostream* os) { + (*os) << s; + return os; +} + +const bool testGetDoc() { + gc_scoped_pool pool; + http::CURLSession ch("", "", "", "", 0); + { + ostringstream os; + const failable > r = http::get(curlWriter, &os, testURI + "/", ch); + assert(hasContent(r)); + assert(contains(str(os), "HTTP/1.1 200") || contains(str(os), "HTTP/1.0 200")); + assert(contains(str(os), "")); + } + { + const failable r = http::getcontent(testURI + "/", ch); + assert(hasContent(r)); + assert(contains(car(list(content(r))), "")); + } + return true; +} + +struct getDocLoop { + http::CURLSession& ch; + getDocLoop(http::CURLSession& ch) : ch(ch) { + } + const bool operator()() const { + const failable r = http::getcontent(testURI + "/", ch); + assert(hasContent(r)); + assert(contains(car(list(content(r))), "")); + return true; + } +}; + +const bool testGetDocPerf() { + gc_scoped_pool pool; + http::CURLSession ch("", "", "", "", 0); + const lambda gl = getDocLoop(ch); + cout << "GET doc test " << time(gl, 10, 50) << " ms" << endl; + return true; +} + +struct getCompositeLoop { + http::CURLSession& ch; + getCompositeLoop(http::CURLSession& ch) : ch(ch) { + } + const bool operator()() const { + const failable r = http::getcontent(testURI + "/r/Editor/composites/test", ch); + assert(hasContent(r)); + return true; + } +}; + +const bool testGetCompositePerf() { + gc_scoped_pool pool; + http::CURLSession ch("", "", "", "", 0); + const lambda gl = getCompositeLoop(ch); + cout << "GET composite test " << time(gl, 10, 50) << " ms" << endl; + return true; +} + +struct getPageLoop { + http::CURLSession& ch; + getPageLoop(http::CURLSession& ch) : ch(ch) { + } + const bool operator()() const { + const failable r = http::getcontent(testURI + "/r/Editor/pages/test", ch); + assert(hasContent(r)); + return true; + } +}; + +const bool testGetPagePerf() { + gc_scoped_pool pool; + http::CURLSession ch("", "", "", "", 0); + const lambda gl = getPageLoop(ch); + cout << "GET page test " << time(gl, 10, 50) << " ms" << endl; + return true; +} + +struct getAppLoop { + http::CURLSession& ch; + getAppLoop(http::CURLSession& ch) : ch(ch) { + } + const bool operator()() const { + const failable r = http::getcontent(testURI + "/r/Editor/apps/test", ch); + assert(hasContent(r)); + return true; + } +}; + +const bool testGetAppPerf() { + gc_scoped_pool pool; + http::CURLSession ch("", "", "", "", 0); + const lambda gl = getAppLoop(ch); + cout << "GET app test " << time(gl, 10, 50) << " ms" << endl; + return true; +} + +const bool testEval() { + gc_scoped_pool pool; + http::CURLSession ch("", "", "", "", 0); + const failable r = http::evalExpr(mklist(string("echo"), string("Hello")), testURI, ch); + assert(hasContent(r)); + assert(content(r) == string("Hello")); + return true; +} + +struct evalLoop { + const string uri; + http::CURLSession& ch; + evalLoop(const string& uri, http::CURLSession& ch) : uri(uri), ch(ch) { + } + const bool operator()() const { + const failable r = http::evalExpr(mklist(string("echo"), string("Hello")), uri, ch); + assert(hasContent(r)); + assert(content(r) == string("Hello")); + return true; + } +}; + +const value blob(string(2048, 'A')); +const list blobs = mklist(blob, blob); + +struct blobEvalLoop { + const string uri; + http::CURLSession& ch; + blobEvalLoop(const string& uri, http::CURLSession& ch) : uri(uri), ch(ch) { + } + const bool operator()() const { + const failable r = content(http::evalExpr(mklist(string("echo"), blobs), uri, ch)); + assert(hasContent(r)); + assert(content(r) == blobs); + return true; + } +}; + +const bool testEvalPerf() { + gc_scoped_pool pool; + http::CURLSession ch("", "", "", "", 0); + const lambda el = evalLoop(testURI, ch); + cout << "JSON-RPC eval echo test " << time(el, 5, 200) << " ms" << endl; + + if (testBlobs) { + const lambda bel = blobEvalLoop(testURI, ch); + cout << "JSON-RPC eval blob test " << time(bel, 5, 200) << " ms" << endl; + } + return true; +} + +bool testPost() { + gc_scoped_pool pool; + const list i = list() + "content" + (list() + "item" + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$2.99"))); + const list a = list() + (list() + "entry" + + (list() + "title" + string("item")) + + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + + i); + http::CURLSession ch("", "", "", "", 0); + const failable id = http::post(a, testURI, ch); + assert(hasContent(id)); + return true; +} + +struct postLoop { + const string uri; + const value val; + http::CURLSession& ch; + postLoop(const string& uri, const value& val, http::CURLSession& ch) : uri(uri), val(val), ch(ch) { + } + const bool operator()() const { + const failable id = http::post(val, uri, ch); + assert(hasContent(id)); + return true; + } +}; + +struct postBlobLoop { + const string uri; + const value val; + http::CURLSession& ch; + postBlobLoop(const string& uri, const value& val, http::CURLSession& ch) : uri(uri), val(val), ch(ch) { + } + const bool operator()() const { + gc_scoped_pool pool; + const failable id = http::post(val, uri, ch); + assert(hasContent(id)); + return true; + } +}; + +const bool testPostPerf() { + gc_scoped_pool pool; + http::CURLSession ch("", "", "", "", 0); + { + const list i = list() + "content" + (list() + "item" + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$2.99"))); + const list val = list() + (list() + "entry" + + (list() + "title" + string("item")) + + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + + i); + const lambda pl = postLoop(testURI, val, ch); + cout << "ATOMPub POST small test " << time(pl, 5, 200) << " ms" << endl; + } + if (testBlobs) { + const list i = list() + "content" + (list() + "item" + + (list() + "name" + string("Apple")) + + (list() + "blob1" + blob) + + (list() + "blob2" + blob) + + (list() + "price" + string("$2.99"))); + const list val = list() + (list() + "entry" + + (list() + "title" + string("item")) + + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + + i); + const lambda pl = postBlobLoop(testURI, val, ch); + cout << "ATOMPub POST blob test " << time(pl, 5, 200) << " ms" << endl; + } + return true; +} + +#ifdef WANT_THREADS + +const bool postThread(const string& uri, const int count, const value& val) { + gc_scoped_pool pool; + http::CURLSession ch("", "", "", "", 0); + const lambda pl = postLoop(uri, val, ch); + time(pl, 0, count); + return true; +} + +const list > startPost(worker& w, const int threads, const lambda& l) { + if (threads == 0) + return list >(); + return cons(submit(w, l), startPost(w, threads - 1, l)); +} + +const bool checkPost(const list >& r) { + if (isNil(r)) + return true; + assert(car(r) == true); + return checkPost(cdr(r)); +} + +struct postThreadLoop { + const lambda l; + worker& w; + const int threads; + postThreadLoop(const lambda& l, worker& w, const int threads) : l(l), w(w), threads(threads) { + } + const bool operator()() const { + list > r = startPost(w, threads, l); + checkPost(r); + return true; + } +}; + +const bool testPostThreadPerf() { + gc_scoped_pool pool; + const int count = 50; + const int threads = 10; + worker w(threads); + + const list i = list() + "content" + (list() + "item" + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$2.99"))); + const value val = list() + (list() + "entry" + + (list() + "title" + string("item")) + + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + + i); + + const lambda pl= curry(lambda(postThread), testURI, count, val); + const lambda ptl = postThreadLoop(pl, w, threads); + double t = time(ptl, 0, 1) / (threads * count); + cout << "ATOMPub POST thread test " << t << " ms" << endl; + + return true; +} + +#else + +const bool postProc(const string& uri, const int count, const value& val) { + gc_scoped_pool pool; + http::CURLSession ch("", "", "", "", 0); + const lambda pl = postLoop(uri, val, ch); + time(pl, 0, count); + return true; +} + +const list startPost(const int procs, const lambda& l) { + if (procs == 0) + return list(); + pid_t pid = fork(); + if (pid == 0) { + assert(l() == true); + exit(0); + } + return cons(pid, startPost(procs - 1, l)); +} + +const bool checkPost(const list& r) { + if (isNil(r)) + return true; + int status; + waitpid(car(r), &status, 0); + assert(status == 0); + return checkPost(cdr(r)); +} + +struct postForkLoop { + const lambda l; + const int procs; + postForkLoop(const lambda& l, const int procs) : l(l), procs(procs) { + } + const bool operator()() const { + list r = startPost(procs, l); + checkPost(r); + return true; + } +}; + +const bool testPostForkPerf() { + gc_scoped_pool pool; + const int count = 50; + const int procs = 10; + + const list i = list() + "content" + (list() + "item" + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$2.99"))); + const value val = list() + (list() + "entry" + + (list() + "title" + string("item")) + + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + + i); + + const lambda pl= curry(lambda(postProc), testURI, count, val); + const lambda ptl = postForkLoop(pl, procs); + double t = time(ptl, 0, 1) / (procs * count); + cout << "ATOMPub POST fork test " << t << " ms" << endl; + + return true; +} + +#endif + +const bool testPut() { + gc_scoped_pool pool; + const list i = list() + "content" + (list() + "item" + + (list() + "name" + string("Apple")) + + (list() + "price" + string("$2.99"))); + const list a = list() + (list() + "entry" + + (list() + "title" + string("item")) + + (list() + "id" + string("cart-53d67a61-aa5e-4e5e-8401-39edeba8b83b")) + + i); + http::CURLSession ch("", "", "", "", 0); + value rc = content(http::put(a, testURI + "/111", ch)); + assert(rc == value(true)); + return true; +} + +const bool testDel() { + gc_scoped_pool pool; + http::CURLSession ch("", "", "", "", 0); + value rc = content(http::del(testURI + "/111", ch)); + assert(rc == value(true)); + return true; +} + +const bool testServer() { + tuscany::server::testGetDoc(); +#ifdef FOO + tuscany::server::testPost(); + tuscany::server::testPut(); + tuscany::server::testDel(); + tuscany::server::testEval(); +#endif + tuscany::server::testGetDocPerf(); + tuscany::server::testGetCompositePerf(); + tuscany::server::testGetPagePerf(); + tuscany::server::testGetAppPerf(); +#ifdef FOO + tuscany::server::testPostPerf(); +#ifdef WANT_THREADS + tuscany::server::testPostThreadPerf(); +#else + tuscany::server::testPostForkPerf(); +#endif + tuscany::server::testEvalPerf(); +#endif + return true; +} + +} +} -- cgit v1.2.3