graphcycles_benchmark.cc 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2018 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "absl/synchronization/internal/graphcycles.h"
  15. #include <algorithm>
  16. #include <cstdint>
  17. #include <vector>
  18. #include "benchmark/benchmark.h"
  19. #include "absl/base/internal/raw_logging.h"
  20. namespace {
  21. void BM_StressTest(benchmark::State& state) {
  22. const int num_nodes = state.range(0);
  23. while (state.KeepRunningBatch(num_nodes)) {
  24. absl::synchronization_internal::GraphCycles g;
  25. std::vector<absl::synchronization_internal::GraphId> nodes(num_nodes);
  26. for (int i = 0; i < num_nodes; i++) {
  27. nodes[i] = g.GetId(reinterpret_cast<void*>(static_cast<uintptr_t>(i)));
  28. }
  29. for (int i = 0; i < num_nodes; i++) {
  30. int end = std::min(num_nodes, i + 5);
  31. for (int j = i + 1; j < end; j++) {
  32. ABSL_RAW_CHECK(g.InsertEdge(nodes[i], nodes[j]), "");
  33. }
  34. }
  35. }
  36. }
  37. BENCHMARK(BM_StressTest)->Range(2048, 1048576);
  38. } // namespace