spir_test.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <sys/random.h>
  4. #include <sys/time.h>
  5. #include <unistd.h>
  6. #include "spir.hpp"
  7. using std::cout;
  8. using std::cerr;
  9. static inline size_t elapsed_us(const struct timeval *start)
  10. {
  11. struct timeval end;
  12. gettimeofday(&end, NULL);
  13. return (end.tv_sec-start->tv_sec)*1000000 + end.tv_usec - start->tv_usec;
  14. }
  15. int main(int argc, char **argv)
  16. {
  17. if (argc < 2 || argc > 5) {
  18. cerr << "Usage: " << argv[0] << " r [num_threads [num_preproc [num_pirs]]]\n";
  19. cerr << "r = log_2(num_records)\n";
  20. exit(1);
  21. }
  22. uint32_t r, num_threads = 1, num_preproc = 1, num_pirs = 1;
  23. r = strtoul(argv[1], NULL, 10);
  24. size_t num_records = ((size_t) 1)<<r;
  25. size_t num_records_mask = num_records - 1;
  26. if (argc > 2) {
  27. num_threads = strtoul(argv[2], NULL, 10);
  28. }
  29. if (argc > 3) {
  30. num_preproc = strtoul(argv[3], NULL, 10);
  31. }
  32. if (argc > 4) {
  33. num_pirs = strtoul(argv[4], NULL, 10);
  34. } else {
  35. num_pirs = num_preproc;
  36. }
  37. cout << "===== ONE-TIME SETUP =====\n\n";
  38. struct timeval otsetup_start;
  39. gettimeofday(&otsetup_start, NULL);
  40. SPIR::init(num_threads);
  41. string pub_params;
  42. SPIR_Client client(r, pub_params);
  43. SPIR_Server server(r, pub_params);
  44. size_t otsetup_us = elapsed_us(&otsetup_start);
  45. cout << "One-time setup: " << otsetup_us << " µs\n";
  46. cout << "pub_params len = " << pub_params.length() << "\n";
  47. cout << "\n===== PREPROCESSING =====\n\n";
  48. cout << "num_preproc = " << num_preproc << "\n";
  49. struct timeval preproc_client_start;
  50. gettimeofday(&preproc_client_start, NULL);
  51. string preproc_msg = client.preproc(num_preproc);
  52. size_t preproc_client_us = elapsed_us(&preproc_client_start);
  53. cout << "Preprocessing client: " << preproc_client_us << " µs\n";
  54. cout << "preproc_msg len = " << preproc_msg.length() << "\n";
  55. struct timeval preproc_server_start;
  56. gettimeofday(&preproc_server_start, NULL);
  57. string preproc_resp = server.preproc_process(preproc_msg);
  58. size_t preproc_server_us = elapsed_us(&preproc_server_start);
  59. cout << "Preprocessing server: " << preproc_server_us << " µs\n";
  60. cout << "preproc_resp len = " << preproc_resp.length() << "\n";
  61. struct timeval preproc_finish_start;
  62. gettimeofday(&preproc_finish_start, NULL);
  63. client.preproc_finish(preproc_resp);
  64. size_t preproc_finish_us = elapsed_us(&preproc_finish_start);
  65. cout << "Preprocessing client finish: " << preproc_finish_us << " µs\n";
  66. // Create the database
  67. SPIR::DBEntry *db = new SPIR::DBEntry[num_records];
  68. for (size_t i=0; i<num_records; ++i) {
  69. db[i] = i * 10000001;
  70. }
  71. for (size_t i=0; i<num_pirs; ++i) {
  72. cout << "\n===== SPIR QUERY " << i+1 << " =====\n\n";
  73. size_t idx;
  74. if (getrandom(&idx, sizeof(idx), 0) != sizeof(idx)) {
  75. cerr << "Failure in getrandom\n";
  76. exit(1);
  77. }
  78. idx &= num_records_mask;
  79. struct timeval query_client_start;
  80. gettimeofday(&query_client_start, NULL);
  81. string query_msg = client.query(idx);
  82. size_t query_client_us = elapsed_us(&query_client_start);
  83. cout << "Query client: " << query_client_us << " µs\n";
  84. cout << "query_msg len = " << query_msg.length() << "\n";
  85. struct timeval query_server_start;
  86. gettimeofday(&query_server_start, NULL);
  87. string query_resp = server.query_process(query_msg, db, 100, 20);
  88. size_t query_server_us = elapsed_us(&query_server_start);
  89. cout << "Query server: " << query_server_us << " µs\n";
  90. cout << "query_resp len = " << query_resp.length() << "\n";
  91. struct timeval query_finish_start;
  92. gettimeofday(&query_finish_start, NULL);
  93. SPIR::DBEntry entry = client.query_finish(query_resp);
  94. size_t query_finish_us = elapsed_us(&query_finish_start);
  95. cout << "Query client finish: " << query_finish_us << " µs\n";
  96. cout << "idx = " << idx << "; entry = " << entry << "\n";
  97. }
  98. delete[] db;
  99. return 0;
  100. }