2 * Copyright 2015 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkTTopoSort.h"
13 #include "sk_tool_utils.h"
15 class TopoSortBench : public Benchmark {
19 ~TopoSortBench() override {
20 sk_tool_utils::TopoTestNode::DeallocNodes(&fGraph);
23 bool isSuitableFor(Backend backend) override {
24 return kNonRendering_Backend == backend;
28 const char* onGetName() override {
29 return "sort_topo_rand";
32 // Delayed initialization only done if onDraw will be called.
33 void onDelayedSetup() override {
34 sk_tool_utils::TopoTestNode::AllocNodes(&fGraph, kNumElements);
36 for (int i = kNumElements-1; i > 0; --i) {
37 int numEdges = fRand.nextU() % (kMaxEdges+1);
39 for (int j = 0; j < numEdges; ++j) {
40 int dep = fRand.nextU() % i;
42 fGraph[i]->dependsOn(fGraph[dep]);
47 void onDraw(int loops, SkCanvas*) override {
48 for (int i = 0; i < loops; ++i) {
49 for (int j = 0; j < fGraph.count(); ++j) {
53 sk_tool_utils::TopoTestNode::Shuffle(&fGraph, &fRand);
55 SkDEBUGCODE(bool actualResult =) SkTTopoSort<sk_tool_utils::TopoTestNode>(&fGraph);
56 SkASSERT(actualResult);
59 for (int j = 0; j < fGraph.count(); ++j) {
60 SkASSERT(fGraph[j]->check());
67 static const int kNumElements = 1000;
68 static const int kMaxEdges = 5;
70 SkTDArray<sk_tool_utils::TopoTestNode*> fGraph;
73 typedef Benchmark INHERITED;
76 ///////////////////////////////////////////////////////////////////////////////
78 DEF_BENCH( return new TopoSortBench(); )