1 // Copyright 2019 The Dawn 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 // http://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 15 #ifndef TESTS_PERFTESTS_DAWNPERFTEST_H_ 16 #define TESTS_PERFTESTS_DAWNPERFTEST_H_ 17 18 #include "tests/DawnTest.h" 19 20 namespace utils { 21 class Timer; 22 } 23 24 class DawnPerfTestPlatform; 25 26 void InitDawnPerfTestEnvironment(int argc, char** argv); 27 28 class DawnPerfTestEnvironment : public DawnTestEnvironment { 29 public: 30 DawnPerfTestEnvironment(int argc, char** argv); 31 ~DawnPerfTestEnvironment() override; 32 33 void SetUp() override; 34 void TearDown() override; 35 36 bool IsCalibrating() const; 37 unsigned int OverrideStepsToRun() const; 38 39 // Returns the path to the trace file, or nullptr if traces should 40 // not be written to a json file. 41 const char* GetTraceFile() const; 42 43 DawnPerfTestPlatform* GetPlatform() const; 44 45 private: 46 // Only run calibration which allows the perf test runner to save time. 47 bool mIsCalibrating = false; 48 49 // If non-zero, overrides the number of steps. 50 unsigned int mOverrideStepsToRun = 0; 51 52 const char* mTraceFile = nullptr; 53 54 std::unique_ptr<DawnPerfTestPlatform> mPlatform; 55 }; 56 57 class DawnPerfTestBase { 58 static constexpr double kCalibrationRunTimeSeconds = 1.0; 59 static constexpr double kMaximumRunTimeSeconds = 10.0; 60 static constexpr unsigned int kNumTrials = 3; 61 62 public: 63 // Perf test results are reported as the amortized time of |mStepsToRun| * |mIterationsPerStep|. 64 // A test deriving from |DawnPerfTestBase| must call the base contructor with 65 // |iterationsPerStep| appropriately to reflect the amount of work performed. 66 // |maxStepsInFlight| may be used to mimic having multiple frames or workloads in flight which 67 // is common with double or triple buffered applications. 68 DawnPerfTestBase(DawnTestBase* test, 69 unsigned int iterationsPerStep, 70 unsigned int maxStepsInFlight); 71 virtual ~DawnPerfTestBase(); 72 73 protected: 74 // Call if the test step was aborted and the test should stop running. 75 void AbortTest(); 76 77 void RunTest(); 78 void PrintPerIterationResultFromSeconds(const std::string& trace, 79 double valueInSeconds, 80 bool important) const; 81 void PrintResult(const std::string& trace, 82 double value, 83 const std::string& units, 84 bool important) const; 85 void PrintResult(const std::string& trace, 86 unsigned int value, 87 const std::string& units, 88 bool important) const; 89 90 private: 91 void DoRunLoop(double maxRunTime); 92 void OutputResults(); 93 94 void PrintResultImpl(const std::string& trace, 95 const std::string& value, 96 const std::string& units, 97 bool important) const; 98 99 virtual void Step() = 0; 100 101 DawnTestBase* mTest; 102 bool mRunning = false; 103 const unsigned int mIterationsPerStep; 104 const unsigned int mMaxStepsInFlight; 105 unsigned int mStepsToRun = 0; 106 unsigned int mNumStepsPerformed = 0; 107 double mCpuTime; 108 std::unique_ptr<utils::Timer> mTimer; 109 }; 110 111 template <typename Params = AdapterTestParam> 112 class DawnPerfTestWithParams : public DawnTestWithParams<Params>, public DawnPerfTestBase { 113 protected: DawnPerfTestWithParams(unsigned int iterationsPerStep,unsigned int maxStepsInFlight)114 DawnPerfTestWithParams(unsigned int iterationsPerStep, unsigned int maxStepsInFlight) 115 : DawnTestWithParams<Params>(), 116 DawnPerfTestBase(this, iterationsPerStep, maxStepsInFlight) { 117 } 118 ~DawnPerfTestWithParams() override = default; 119 }; 120 121 using DawnPerfTest = DawnPerfTestWithParams<>; 122 123 #endif // TESTS_PERFTESTS_DAWNPERFTEST_H_ 124