bench_random.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. #include "include/header.h"
  19. #include "bench_random.h"
  20. #include "random_creater.h"
  21. #include "staticstic.h"
  22. namespace wwsearch {
  23. // Test random ?
  24. const char* BenchRandom::Description =
  25. "-n [create id num] -m [0:32 1:64 2:string] -d [debug]";
  26. const char* BenchRandom::Usage = "Benchmark for random";
  27. void BenchRandom::Run(wwsearch::ArgsHelper& args) {
  28. std::vector<std::set<uint64_t> > containers;
  29. containers.resize(1000);
  30. std::set<std::string> containers_str;
  31. uint64_t conflict_count = 0;
  32. uint64_t num = args.UInt('n');
  33. uint64_t left_num = num;
  34. uint32_t mode = args.UInt('m');
  35. uint32_t debug = args.UInt('d');
  36. RandomCreater randomer;
  37. randomer.Init(time(NULL));
  38. while (left_num-- > 0) {
  39. uint32_t temp;
  40. uint64_t v;
  41. std::string str;
  42. if (mode != 2) {
  43. if (mode == 0) {
  44. v = randomer.GetUInt32();
  45. } else if (mode == 1) {
  46. v = randomer.GetUInt64();
  47. }
  48. uint32_t idx = v % 1000;
  49. auto ret = containers[idx].insert(v);
  50. if (!ret.second) {
  51. conflict_count++;
  52. }
  53. } else {
  54. str = randomer.GetString(10);
  55. auto ret = containers_str.insert(str);
  56. if (!ret.second) {
  57. conflict_count++;
  58. }
  59. }
  60. if (debug) {
  61. if (mode != 2)
  62. printf("v=%llu\n", v);
  63. else
  64. printf("str=%s\n", str.c_str());
  65. }
  66. }
  67. printf("Insert num:%llu,conflict:%llu\n", num, conflict_count);
  68. return;
  69. }
  70. } // namespace wwsearch