1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 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 16 #ifndef ECMASCRIPT_MEM_CLOCK_SCOPE_H 17 #define ECMASCRIPT_MEM_CLOCK_SCOPE_H 18 19 #include "time.h" 20 #include "chrono" 21 #include "libpandabase/utils/logger.h" 22 23 namespace panda::ecmascript { 24 class ClockScope { 25 using Clock = std::chrono::high_resolution_clock; 26 using Duration = std::chrono::duration<uint64_t, std::nano>; 27 28 public: ClockScope()29 explicit ClockScope() 30 { 31 start_ = Clock::now(); 32 } 33 GetPauseTime()34 Duration GetPauseTime() const 35 { 36 return Clock::now() - start_; 37 } 38 TotalSpentTime()39 float TotalSpentTime() const 40 { 41 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(Clock::now() - start_); 42 return (float) duration.count() / MILLION_TIME; 43 } 44 45 private: 46 NO_COPY_SEMANTIC(ClockScope); 47 NO_MOVE_SEMANTIC(ClockScope); 48 49 Clock::time_point start_; 50 static constexpr uint32_t MILLION_TIME = 1000; 51 }; 52 } // namespace panda::ecmascript 53 54 #endif // ECMASCRIPT_MEM_CLOCK_SCOPE_H 55