Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / exec / ExecTime.test.cc
1 /*
2  * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *    http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "ExecTime.h"
18
19 #include "backend/IConfig.h"
20 #include "backend/Backend.h"
21
22 #include <gtest/gtest.h>
23
24 #include <string>
25
26 namespace
27 {
28 using namespace onert;
29 using namespace exec;
30 using namespace backend;
31
32 struct MockConfig : public IConfig
33 {
34   std::string id() override { return "b1"; }
35   bool initialize() override { return true; };
36   bool supportPermutation() override { return false; }
37   ir::Layout supportLayout(const ir::IOperation &, ir::Layout) override
38   {
39     return ir::Layout::UNKNOWN;
40   }
41   bool supportDynamicTensor() override { return false; }
42   bool supportFP16() override { return false; }
43 };
44
45 struct MockBackend : public ::onert::backend::Backend
46 {
47   std::shared_ptr<onert::backend::IConfig> config() const override
48   {
49     return std::make_shared<MockConfig>();
50   }
51   std::unique_ptr<onert::backend::BackendContext> newContext(ContextData &&) const override
52   {
53     return nullptr;
54   }
55 };
56
57 TEST(ExecTime, roundtrip_ok)
58 {
59   const auto *b = new MockBackend();
60   std::vector<const Backend *> bs = {b};
61   {
62     ExecTime et(bs);
63     et.updateOperationExecTime(b, "op1", true, 100, 100);
64     et.updateOperationExecTime(b, "op1", true, 200, 200);
65     et.updateOperationExecTime(b, "op1", false, 100, 888);
66     et.storeOperationsExecTime();
67   }
68   {
69     ExecTime et(bs);
70     auto time = et.getOperationExecTime(b, "op1", true, 100);
71     ASSERT_EQ(time, 100);
72     // Check interpolation
73     time = et.getOperationExecTime(b, "op1", true, 150);
74     ASSERT_EQ(time, 150);
75     time = et.getOperationExecTime(b, "op1", false, 100);
76     ASSERT_EQ(time, 888);
77     et.storeOperationsExecTime();
78   }
79   // clean up
80   EXPECT_EQ(remove("exec_time.json"), 0);
81 }
82
83 TEST(ExecTime, structure)
84 {
85
86   const auto *b = new MockBackend();
87   std::vector<const Backend *> bs = {b};
88   {
89     ExecTime et(bs);
90     et.updateOperationExecTime(b, "op1", true, 100, 100);
91     et.updateOperationExecTime(b, "op1", true, 200, 200);
92     et.storeOperationsExecTime();
93   }
94   {
95     ExecTime et(bs);
96     auto time = et.getOperationExecTime(b, "op1", true, 100);
97     ASSERT_EQ(time, 100);
98     // Check interpolation
99     time = et.getOperationExecTime(b, "op1", true, 200);
100     ASSERT_EQ(time, 200);
101     et.storeOperationsExecTime();
102   }
103   // clean up
104   EXPECT_EQ(remove("exec_time.json"), 0);
105 }
106 } // unnamed namespace