From aad951e2ab553a47338cc4a8badb0fa57a38739b Mon Sep 17 00:00:00 2001 From: Jan Steemann Date: Fri, 28 Sep 2012 15:11:14 +0200 Subject: [PATCH] added collection dropping & creation --- arangosh/Benchmark/BenchmarkOperation.h | 12 +++++ arangosh/Benchmark/BenchmarkThread.h | 56 +++++++++++++++++++++- arangosh/Benchmark/arangob.cpp | 62 +++++++++++++++++++++++-- 3 files changed, 126 insertions(+), 4 deletions(-) diff --git a/arangosh/Benchmark/BenchmarkOperation.h b/arangosh/Benchmark/BenchmarkOperation.h index 62ad5c3615..df05c37604 100644 --- a/arangosh/Benchmark/BenchmarkOperation.h +++ b/arangosh/Benchmark/BenchmarkOperation.h @@ -68,6 +68,18 @@ namespace triagens { virtual ~BenchmarkOperation () { } +//////////////////////////////////////////////////////////////////////////////// +/// @brief the name of the underlying collection +//////////////////////////////////////////////////////////////////////////////// + + virtual string collectionName () = 0; + +//////////////////////////////////////////////////////////////////////////////// +/// @brief whether or not the test uses a collection +//////////////////////////////////////////////////////////////////////////////// + + virtual const bool useCollection () const = 0; + //////////////////////////////////////////////////////////////////////////////// /// @brief return the URL of the operation to execute //////////////////////////////////////////////////////////////////////////////// diff --git a/arangosh/Benchmark/BenchmarkThread.h b/arangosh/Benchmark/BenchmarkThread.h index 9ef345883e..8804246198 100644 --- a/arangosh/Benchmark/BenchmarkThread.h +++ b/arangosh/Benchmark/BenchmarkThread.h @@ -72,7 +72,9 @@ namespace triagens { //////////////////////////////////////////////////////////////////////////////// BenchmarkThread (BenchmarkOperation* operation, - ConditionVariable* condition, + ConditionVariable* condition, + void (*callback) (), + int threadNumber, const unsigned long batchSize, BenchmarkCounter* operationsCounter, Endpoint* endpoint, @@ -81,6 +83,8 @@ namespace triagens { : Thread("arangob"), _operation(operation), _startCondition(condition), + _callback(callback), + _threadNumber(threadNumber), _batchSize(batchSize), _operationsCounter(operationsCounter), _endpoint(endpoint), @@ -139,6 +143,8 @@ namespace triagens { _client = new SimpleHttpClient(_connection, 10.0, true); _client->setUserNamePassword("/", _username, _password); + + // test the connection map headerFields; SimpleHttpResult* result = _client->request(HttpRequest::HTTP_REQUEST_GET, "/_api/version", @@ -155,6 +161,42 @@ namespace triagens { } delete result; + + + // if we're the first thread, wipe the existing collection + if (_threadNumber == 0 && _operation->useCollection()) { + result = _client->request(HttpRequest::HTTP_REQUEST_DELETE, + "/_api/collection/" + _operation->collectionName(), + "", + 0, + headerFields); + + if (result == 0 || (result->getHttpReturnCode() != 200 && result->getHttpReturnCode() != 404)) { + cerr << "could not wipe existing collection " << _operation->collectionName() << endl; + exit(EXIT_FAILURE); + } + else { + delete result; + } + + // now create the collection + string payload = "{\"name\":\"" + _operation->collectionName() + "\"}"; + result = _client->request(HttpRequest::HTTP_REQUEST_POST, + "/_api/collection", + payload.c_str(), + payload.size(), + headerFields); + + if (result == 0 || (result->getHttpReturnCode() != 200 && result->getHttpReturnCode() != 201)) { + cerr << "could not create collection " << _operation->collectionName() << endl; + exit(EXIT_FAILURE); + } + else { + delete result; + } + } + + _callback(); // wait for start condition to be broadcasted { @@ -353,6 +395,18 @@ namespace triagens { ConditionVariable* _startCondition; +//////////////////////////////////////////////////////////////////////////////// +/// @brief start callback function +//////////////////////////////////////////////////////////////////////////////// + + void (*_callback) (); + +//////////////////////////////////////////////////////////////////////////////// +/// @brief our thread number +//////////////////////////////////////////////////////////////////////////////// + + int _threadNumber; + //////////////////////////////////////////////////////////////////////////////// /// @brief batch size //////////////////////////////////////////////////////////////////////////////// diff --git a/arangosh/Benchmark/arangob.cpp b/arangosh/Benchmark/arangob.cpp index 33178f1635..bee89d05d0 100644 --- a/arangosh/Benchmark/arangob.cpp +++ b/arangosh/Benchmark/arangob.cpp @@ -31,6 +31,8 @@ #include "build.h" #include "ArangoShell/ArangoClient.h" +#include "Basics/Mutex.h" +#include "Basics/MutexLocker.h" #include "Basics/ProgramOptions.h" #include "Basics/ProgramOptionsDescription.h" #include "Basics/StringUtils.h" @@ -71,6 +73,18 @@ using namespace triagens::arangob; ArangoClient BaseClient; +//////////////////////////////////////////////////////////////////////////////// +/// @brief started counter +//////////////////////////////////////////////////////////////////////////////// + +static volatile int Started = 0; + +//////////////////////////////////////////////////////////////////////////////// +/// @brief mutex for start counter +//////////////////////////////////////////////////////////////////////////////// + +Mutex StartMutex; + //////////////////////////////////////////////////////////////////////////////// /// @brief concurrency //////////////////////////////////////////////////////////////////////////////// @@ -126,6 +140,14 @@ struct VersionTest : public BenchmarkOperation { ~VersionTest () { } + string collectionName () { + return ""; + } + + const bool useCollection () const { + return false; + } + const string& url () { static string url = "/_api/version"; @@ -170,6 +192,14 @@ struct SmallDocumentCreationTest : public BenchmarkOperation { ~SmallDocumentCreationTest () { } + + string collectionName () { + return Collection; + } + + const bool useCollection () const { + return true; + } const string& url () { return _url; @@ -232,6 +262,14 @@ struct BigDocumentCreationTest : public BenchmarkOperation { ~BigDocumentCreationTest () { TRI_Free(TRI_UNKNOWN_MEM_ZONE, _buffer); } + + string collectionName () { + return Collection; + } + + const bool useCollection () const { + return true; + } const string& url () { return _url; @@ -271,6 +309,21 @@ struct BigDocumentCreationTest : public BenchmarkOperation { /// @{ //////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////// +/// @brief update the number of ready threads. this is a callback function +/// that is called by each thread after it is created +//////////////////////////////////////////////////////////////////////////////// + +static void UpdateStartCounter () { + MUTEX_LOCKER(StartMutex); + ++Started; +} + +static int GetStartCounter () { + MUTEX_LOCKER(StartMutex); + return Started; +} + //////////////////////////////////////////////////////////////////////////////// /// @brief parses the program options //////////////////////////////////////////////////////////////////////////////// @@ -368,7 +421,9 @@ int main (int argc, char* argv[]) { endpoints.push_back(endpoint); BenchmarkThread* thread = new BenchmarkThread(testCase, - &startCondition, + &startCondition, + &UpdateStartCounter, + i, (unsigned long) BatchSize, &operationsCounter, endpoint, @@ -381,8 +436,9 @@ int main (int argc, char* argv[]) { } // give all threads a chance to start so they will not miss the broadcast - usleep(500000); - + while (GetStartCounter() < Concurrency) { + usleep(5000); + } Timing timer(Timing::TI_WALLCLOCK);