PageRenderTime 1373ms CodeModel.GetById 31ms RepoModel.GetById 7ms app.codeStats 0ms

/src/maidsafe/tests/benchmark/operations.cc

http://maidsafe-dht.googlecode.com/
C++ | 316 lines | 259 code | 27 blank | 30 comment | 34 complexity | 5f1dca913979e71edc4369bb1c1f1185 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /* Copyright (c) 2010 maidsafe.net limited
  2. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without modification,
  4. are permitted provided that the following conditions are met:
  5. * Redistributions of source code must retain the above copyright notice,
  6. this list of conditions and the following disclaimer.
  7. * Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation
  9. and/or other materials provided with the distribution.
  10. * Neither the name of the maidsafe.net limited nor the names of its
  11. contributors may be used to endorse or promote products derived from this
  12. software without specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  14. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  17. FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  21. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  22. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include "maidsafe/tests/benchmark/operations.h"
  25. #include <boost/format.hpp>
  26. #include <boost/filesystem.hpp>
  27. #include <boost/filesystem/fstream.hpp>
  28. #include <boost/thread.hpp>
  29. #include <boost/tokenizer.hpp>
  30. #include <boost/lexical_cast.hpp>
  31. #include <cassert>
  32. #include <iomanip>
  33. #include <iostream> // NOLINT
  34. #include <string>
  35. #include <vector>
  36. #include "maidsafe/protobuf/kademlia_service_messages.pb.h"
  37. #include "maidsafe/maidsafe-dht.h"
  38. namespace benchmark {
  39. Operations::Operations(kad::KNode *node)
  40. : node_(node), cryobj_(), private_key_(), public_key_(),
  41. public_key_signature_() {
  42. cryobj_.set_symm_algorithm(crypto::AES_256);
  43. cryobj_.set_hash_algorithm(crypto::SHA_512);
  44. crypto::RsaKeyPair kp;
  45. kp.GenerateKeys(4096);
  46. public_key_ = kp.public_key();
  47. private_key_ = kp.private_key();
  48. public_key_signature_ = cryobj_.AsymSign(public_key_, "", private_key_,
  49. crypto::STRING_STRING);
  50. }
  51. void Operations::TestFindAndPing(const std::vector<kad::KadId> &nodes,
  52. const int &iterations) {
  53. std::vector<kad::Contact> contacts;
  54. {
  55. printf("Finding %d nodes...\n", nodes.size());
  56. base::Stats<boost::uint64_t> stats;
  57. boost::shared_ptr<CallbackData> data(new CallbackData());
  58. boost::mutex::scoped_lock lock(data->mutex);
  59. for (size_t i = 0; i < nodes.size(); ++i) {
  60. boost::uint64_t t = base::GetEpochMilliseconds();
  61. node_->GetNodeContactDetails(nodes[i], boost::bind(
  62. &Operations::GetNodeContactDetailsCallback, this, _1, data), false);
  63. while (static_cast<size_t>(data->returned_count) <= i)
  64. data->condition.wait(lock);
  65. stats.Add(base::GetEpochMilliseconds() - t);
  66. kad::Contact ctc;
  67. ctc.ParseFromString(data->content);
  68. contacts.push_back(ctc);
  69. }
  70. printf("Done: total %.2f s, min/avg/max %.2f/%.2f/%.2f s\n",
  71. stats.Sum() / 1000.0,
  72. stats.Min() / 1000.0,
  73. stats.Mean() / 1000.0,
  74. stats.Max() / 1000.0);
  75. }
  76. if (contacts.size() > 0) {
  77. printf("Pinging %d contacts, %d iterations...\n",
  78. contacts.size(), iterations);
  79. base::Stats<boost::uint64_t> stats;
  80. for (size_t i = 0; i < contacts.size(); ++i) {
  81. base::Stats<boost::uint64_t> it_stats;
  82. boost::shared_ptr<CallbackData> data(new CallbackData());
  83. boost::mutex::scoped_lock lock(data->mutex);
  84. for (int j = 0; j < iterations; ++j) {
  85. boost::uint64_t t = base::GetEpochMilliseconds();
  86. node_->Ping(contacts[i], boost::bind(
  87. &Operations::PingCallback, this, _1, data));
  88. while (data->returned_count <= j)
  89. data->condition.wait(lock);
  90. it_stats.Add(base::GetEpochMilliseconds() - t);
  91. }
  92. stats.Add(it_stats.Mean());
  93. printf(" Pinged contact %d, %02d/%02d times "
  94. "(total %.2f s, min/avg/max %.2f/%.2f/%.2f s)\n", i + 1,
  95. data->succeeded_count, data->returned_count,
  96. it_stats.Sum() / 1000.0,
  97. it_stats.Min() / 1000.0,
  98. it_stats.Mean() / 1000.0,
  99. it_stats.Max() / 1000.0);
  100. }
  101. printf("Done: min/avg/max %.2f/%.2f/%.2f s\n",
  102. stats.Min() / 1000.0,
  103. stats.Mean() / 1000.0,
  104. stats.Max() / 1000.0);
  105. } else {
  106. printf("No contacts for nodes found.\n");
  107. }
  108. }
  109. void Operations::TestStoreAndFind(const std::vector<kad::KadId> &nodes,
  110. const int &iterations, const bool &sign) {
  111. for (int val = 0; val < 4; ++val) {
  112. std::string size, value;
  113. switch (val) {
  114. case 0:
  115. value = base::RandomString(1 << 4);
  116. size = "16 byte";
  117. break;
  118. case 1:
  119. value = base::RandomString(1 << 10);
  120. size = "1 KB";
  121. break;
  122. case 2:
  123. value = base::RandomString(1 << 17);
  124. size = "128 KB";
  125. break;
  126. case 3:
  127. value = base::RandomString(1 << 20);
  128. size = "1 MB";
  129. break;
  130. }
  131. printf("Storing %s value on %d * k closest nodes, %d iterations...\n",
  132. size.c_str(), nodes.size(), iterations);
  133. base::Stats<boost::uint64_t> store_stats;
  134. for (size_t i = 0; i < nodes.size(); ++i) {
  135. base::Stats<boost::uint64_t> it_stats;
  136. boost::shared_ptr<CallbackData> data(new CallbackData());
  137. boost::mutex::scoped_lock lock(data->mutex);
  138. for (int j = 0; j < iterations; ++j) {
  139. kad::KadId mod =
  140. GetModId(val * iterations * nodes.size() + i * iterations + j);
  141. kad::KadId key(nodes[i] ^ mod);
  142. kad::SignedValue sig_val;
  143. kad::SignedRequest sig_req;
  144. if (sign) {
  145. std::string req_sig, ser_sig_val;
  146. req_sig = cryobj_.AsymSign(cryobj_.Hash(public_key_ +
  147. public_key_signature_ + key.String(), "",
  148. crypto::STRING_STRING, false), "", private_key_,
  149. crypto::STRING_STRING);
  150. sig_val.set_value(value);
  151. sig_val.set_value_signature(cryobj_.AsymSign(value, "",
  152. private_key_, crypto::STRING_STRING));
  153. ser_sig_val = sig_val.SerializeAsString();
  154. sig_req.set_signer_id(node_->node_id().String());
  155. sig_req.set_public_key(public_key_);
  156. sig_req.set_signed_public_key(public_key_signature_);
  157. sig_req.set_signed_request(req_sig);
  158. }
  159. boost::uint64_t t = base::GetEpochMilliseconds();
  160. if (sign) {
  161. node_->StoreValue(key, sig_val, sig_req, 86400, boost::bind(
  162. &Operations::StoreCallback, this, _1, data));
  163. } else {
  164. node_->StoreValue(key, value, 86400, boost::bind(
  165. &Operations::StoreCallback, this, _1, data));
  166. }
  167. while (data->returned_count <= j)
  168. data->condition.wait(lock);
  169. it_stats.Add(base::GetEpochMilliseconds() - t);
  170. }
  171. store_stats.Add(it_stats.Mean());
  172. printf(" Stored close to %d, %02d/%02d times "
  173. "(total %.2f s, min/avg/max %.2f/%.2f/%.2f s)\n", i + 1,
  174. data->succeeded_count, data->returned_count,
  175. it_stats.Sum() / 1000.0,
  176. it_stats.Min() / 1000.0,
  177. it_stats.Mean() / 1000.0,
  178. it_stats.Max() / 1000.0);
  179. }
  180. printf("Done: min/avg/max %.2f/%.2f/%.2f s\n",
  181. store_stats.Min() / 1000.0,
  182. store_stats.Mean() / 1000.0,
  183. store_stats.Max() / 1000.0);
  184. printf("Loading %s value from %d closest nodes, %d iterations...\n",
  185. size.c_str(), nodes.size(), iterations);
  186. base::Stats<boost::uint64_t> load_stats;
  187. for (size_t i = 0; i < nodes.size(); ++i) {
  188. base::Stats<boost::uint64_t> it_stats;
  189. boost::shared_ptr<CallbackData> data(new CallbackData());
  190. boost::mutex::scoped_lock lock(data->mutex);
  191. for (int j = 0; j < iterations; ++j) {
  192. kad::KadId mod =
  193. GetModId(val * iterations * nodes.size() + i * iterations + j);
  194. boost::uint64_t t = base::GetEpochMilliseconds();
  195. node_->FindValue(nodes[i] ^ mod, false, boost::bind(
  196. &Operations::FindValueCallback, this, _1, data));
  197. while (data->returned_count <= j)
  198. data->condition.wait(lock);
  199. it_stats.Add(base::GetEpochMilliseconds() - t);
  200. }
  201. load_stats.Add(it_stats.Mean());
  202. printf(" Loaded from %d, %02d/%02d times "
  203. "(total %.2f s, min/avg/max %.2f/%.2f/%.2f s)\n", i + 1,
  204. data->succeeded_count, data->returned_count,
  205. it_stats.Sum() / 1000.0,
  206. it_stats.Min() / 1000.0,
  207. it_stats.Mean() / 1000.0,
  208. it_stats.Max() / 1000.0);
  209. }
  210. printf("Done: min/avg/max %.2f/%.2f/%.2f s\n",
  211. load_stats.Min() / 1000.0,
  212. load_stats.Mean() / 1000.0,
  213. load_stats.Max() / 1000.0);
  214. }
  215. }
  216. void Operations::PingCallback(const std::string &result,
  217. boost::shared_ptr<CallbackData> data) {
  218. boost::mutex::scoped_lock lock(data->mutex);
  219. data->content.clear();
  220. ++data->returned_count;
  221. kad::PingResponse msg;
  222. if (msg.ParseFromString(result) && msg.result() == kad::kRpcResultSuccess)
  223. ++data->succeeded_count;
  224. data->condition.notify_one();
  225. }
  226. void Operations::GetNodeContactDetailsCallback(const std::string &result,
  227. boost::shared_ptr<CallbackData> data) {
  228. boost::mutex::scoped_lock lock(data->mutex);
  229. data->content.clear();
  230. ++data->returned_count;
  231. kad::FindNodeResult msg;
  232. if (msg.ParseFromString(result) && msg.result() == kad::kRpcResultSuccess) {
  233. ++data->succeeded_count;
  234. data->content = msg.contact();
  235. }
  236. data->condition.notify_one();
  237. }
  238. void Operations::StoreCallback(const std::string &result,
  239. boost::shared_ptr<CallbackData> data) {
  240. boost::mutex::scoped_lock lock(data->mutex);
  241. data->content.clear();
  242. ++data->returned_count;
  243. kad::StoreResponse msg;
  244. if (msg.ParseFromString(result) && msg.result() == kad::kRpcResultSuccess)
  245. ++data->succeeded_count;
  246. data->condition.notify_one();
  247. }
  248. void Operations::FindValueCallback(const std::string &result,
  249. boost::shared_ptr<CallbackData> data) {
  250. boost::mutex::scoped_lock lock(data->mutex);
  251. data->content.clear();
  252. ++data->returned_count;
  253. kad::FindResponse msg;
  254. if (msg.ParseFromString(result) && msg.result() == kad::kRpcResultSuccess &&
  255. (msg.values_size() > 0 || msg.signed_values_size() > 0))
  256. ++data->succeeded_count;
  257. data->condition.notify_one();
  258. }
  259. /**
  260. * Calculates a Kademlia ID with smallest possible distance from 000..000,
  261. * with a unique value for each (positive) iteration number.
  262. */
  263. kad::KadId Operations::GetModId(int iteration) {
  264. int bits = kad::kKeySizeBits - 1;
  265. kad::KadId id;
  266. while (iteration > bits) {
  267. id = id ^ kad::KadId(bits);
  268. iteration -= (bits + 1);
  269. --bits;
  270. }
  271. return id ^ kad::KadId(iteration);
  272. }
  273. void Operations::PrintRpcTimings(const rpcprotocol::RpcStatsMap &rpc_timings) {
  274. std::cout << boost::format("Calls RPC Name %40t% min/avg/max\n");
  275. for (rpcprotocol::RpcStatsMap::const_iterator it = rpc_timings.begin();
  276. it != rpc_timings.end();
  277. ++it) {
  278. std::cout << boost::format("%1% : %2% %40t% %3% / %4% / %5% \n")
  279. % it->second.Size()
  280. % it->first.c_str()
  281. % it->second.Min() // / 1000.0
  282. % it->second.Mean() // / 1000.0
  283. % it->second.Max(); // / 1000.0;
  284. }
  285. }
  286. } // namespace benchmark