TopoSortBench.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "bench/Benchmark.h"
  8. #include "include/core/SkString.h"
  9. #include "include/utils/SkRandom.h"
  10. #include "src/core/SkTTopoSort.h"
  11. #include "tools/ToolUtils.h"
  12. class TopoSortBench : public Benchmark {
  13. public:
  14. TopoSortBench() { }
  15. ~TopoSortBench() override {
  16. }
  17. bool isSuitableFor(Backend backend) override {
  18. return kNonRendering_Backend == backend;
  19. }
  20. protected:
  21. const char* onGetName() override {
  22. return "sort_topo_rand";
  23. }
  24. // Delayed initialization only done if onDraw will be called.
  25. void onDelayedSetup() override {
  26. ToolUtils::TopoTestNode::AllocNodes(&fGraph, kNumElements);
  27. for (int i = kNumElements-1; i > 0; --i) {
  28. int numEdges = fRand.nextU() % (kMaxEdges+1);
  29. for (int j = 0; j < numEdges; ++j) {
  30. int dep = fRand.nextU() % i;
  31. fGraph[i]->dependsOn(fGraph[dep].get());
  32. }
  33. }
  34. }
  35. void onDraw(int loops, SkCanvas*) override {
  36. for (int i = 0; i < loops; ++i) {
  37. for (int j = 0; j < fGraph.count(); ++j) {
  38. fGraph[j]->reset();
  39. }
  40. ToolUtils::TopoTestNode::Shuffle(&fGraph, &fRand);
  41. SkDEBUGCODE(bool actualResult =) SkTTopoSort<ToolUtils::TopoTestNode>(&fGraph);
  42. SkASSERT(actualResult);
  43. #ifdef SK_DEBUG
  44. for (int j = 0; j < fGraph.count(); ++j) {
  45. SkASSERT(fGraph[j]->check());
  46. }
  47. #endif
  48. }
  49. }
  50. private:
  51. static const int kNumElements = 1000;
  52. static const int kMaxEdges = 5;
  53. SkTArray<sk_sp<ToolUtils::TopoTestNode>> fGraph;
  54. SkRandom fRand;
  55. typedef Benchmark INHERITED;
  56. };
  57. ///////////////////////////////////////////////////////////////////////////////
  58. DEF_BENCH( return new TopoSortBench(); )