main.rs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. use std::env;
  2. use std::time::Instant;
  3. use rand::RngCore;
  4. use spiral_spir::client::Client;
  5. use spiral_spir::init;
  6. use spiral_spir::server::Server;
  7. use spiral_spir::DbEntry;
  8. fn main() {
  9. let args: Vec<String> = env::args().collect();
  10. if args.len() < 2 || args.len() > 5 {
  11. println!(
  12. "Usage: {} r [num_threads [num_preproc [num_pirs]]]\nr = log_2(num_records)",
  13. args[0]
  14. );
  15. return;
  16. }
  17. let r: usize = args[1].parse().unwrap();
  18. let mut num_threads = 1usize;
  19. let mut num_preproc = 1usize;
  20. let num_pirs: usize;
  21. if args.len() > 2 {
  22. num_threads = args[2].parse().unwrap();
  23. }
  24. if args.len() > 3 {
  25. num_preproc = args[3].parse().unwrap();
  26. }
  27. if args.len() > 4 {
  28. num_pirs = args[4].parse().unwrap();
  29. } else {
  30. num_pirs = num_preproc;
  31. }
  32. let num_records = 1 << r;
  33. let num_records_mask = num_records - 1;
  34. println!("===== ONE-TIME SETUP =====\n");
  35. let otsetup_start = Instant::now();
  36. init(num_threads);
  37. let (client, pub_params) = Client::new(r);
  38. let pub_params_len = pub_params.len();
  39. let server = Server::new(r, pub_params);
  40. let otsetup_us = otsetup_start.elapsed().as_micros();
  41. println!("One-time setup: {} µs", otsetup_us);
  42. println!("pub_params len = {}", pub_params_len);
  43. println!("\n===== PREPROCESSING =====\n");
  44. println!("num_preproc = {}", num_preproc);
  45. let preproc_client_start = Instant::now();
  46. let preproc_msg = client.preproc(num_preproc);
  47. let preproc_client_us = preproc_client_start.elapsed().as_micros();
  48. println!("Preprocessing client: {} µs", preproc_client_us);
  49. println!("preproc_msg len = {}", preproc_msg.len());
  50. let preproc_server_start = Instant::now();
  51. let preproc_resp = server.preproc_process(&preproc_msg);
  52. let preproc_server_us = preproc_server_start.elapsed().as_micros();
  53. println!("Preprocessing server: {} µs", preproc_server_us);
  54. println!("preproc_resp len = {}", preproc_resp.len());
  55. let preproc_finish_start = Instant::now();
  56. client.preproc_finish(&preproc_resp);
  57. let preproc_finish_us = preproc_finish_start.elapsed().as_micros();
  58. println!("Preprocessing client finish: {} µs", preproc_finish_us);
  59. // Create a database with recognizable contents
  60. let db: Vec<DbEntry> = ((0 as DbEntry)..(num_records as DbEntry))
  61. .map(|x| 10000001 * x)
  62. .collect();
  63. let dbptr = db.as_ptr();
  64. let mut rng = rand::thread_rng();
  65. for i in 1..num_pirs + 1 {
  66. println!("\n===== SPIR QUERY {} =====\n", i);
  67. let idx = (rng.next_u64() as usize) & num_records_mask;
  68. let query_client_start = Instant::now();
  69. let query_msg = client.query(idx);
  70. let query_client_us = query_client_start.elapsed().as_micros();
  71. println!("Query client: {} µs", query_client_us);
  72. println!("query_msg len = {}", query_msg.len());
  73. let query_server_start = Instant::now();
  74. let query_resp = server.query_process(&query_msg, dbptr, 100, 20);
  75. let query_server_us = query_server_start.elapsed().as_micros();
  76. println!("Query server: {} µs", query_server_us);
  77. println!("query_resp len = {}", query_resp.len());
  78. let query_finish_start = Instant::now();
  79. let entry = client.query_finish(&query_resp);
  80. let query_finish_us = query_finish_start.elapsed().as_micros();
  81. println!("Query client finish: {} µs", query_finish_us);
  82. println!("idx = {}; entry = {}", idx, entry);
  83. }
  84. }