1 // Copyright (C) 2018 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
8 #include <gtest/gtest.h>
9 #include <gmock/gmock.h>
11 #include "inference_engine.hpp"
12 #include "test_model_path.hpp"
13 #include <tests_file_utils.hpp>
20 #define UNUSED __attribute__((unused))
30 class TestsCommon : public ::testing::Test {
32 static size_t parseLine(char* line) {
33 // This assumes that a digit will be found and the line ends in " Kb".
34 size_t i = strlen(line);
36 while (*p <'0' || *p > '9') p++;
42 static size_t getVmSizeInKB(){
43 FILE* file = fopen("/proc/self/status", "r");
45 if (file != nullptr) {
48 while (fgets(line, 128, file) != NULL) {
49 if (strncmp(line, "VmSize:", 7) == 0) {
50 result = parseLine(line);
59 static size_t getVmSizeInKBWin() {
60 PROCESS_MEMORY_COUNTERS pmc;
61 pmc.cb = sizeof(PROCESS_MEMORY_COUNTERS);
62 GetProcessMemoryInfo(GetCurrentProcess(),&pmc, pmc.cb);
63 return pmc.WorkingSetSize;
69 static std::string library_path() {return ".";};
71 static std::string library_path() { return "./lib";};
74 static std::string archPath() {
75 if (sizeof(void*) == 8) {
76 return "../../lib/intel64";
78 return "../../lib/ia32";
83 void TearDown() override {}
85 void SetUp() override {
86 auto memsize = getVmSizeInKB();
88 std::cout << "\nMEM_USAGE=" << getVmSizeInKB() << "KB\n";
94 inline std::string get_mock_engine_name() {
95 return make_plugin_name("mock_engine");
98 inline std::string get_mock_extension_name() {
99 return make_plugin_name("mock_extensions");
101 static std::string get_data_path(){
102 const char* data_path = std::getenv("DATA_PATH");
104 if (data_path == NULL){
105 if(DATA_PATH != NULL){
106 data_path = DATA_PATH;
108 ::testing::AssertionFailure()<<"DATA_PATH not defined";
111 return std::string(data_path);
114 static std::string make_so_name(const std::string & input) {
117 return "lib" + input + ".dll";
119 return input + ".dll";
122 return "lib" + input + ".dylib";
124 return "lib" + input + ".so";
128 static std::string make_plugin_name(const std::string & input) {
129 return make_so_name(input);
132 static void fill_data(InferenceEngine::Blob::Ptr blob) {
133 fill_data(blob->buffer().as<float*>(), blob->size());
136 static void fill_data(float *data, size_t size, size_t duty_ratio = 10) {
137 for (size_t i = 0; i < size; i++) {
138 if ( ( i / duty_ratio)%2 == 1) {
141 data[i] = sin((float)i);
146 void compare(InferenceEngine::Blob &res, InferenceEngine::Blob &ref, float max_diff = 0.01f) {
148 float *res_ptr = res.buffer().as<float*>();
149 size_t res_size = res.size();
151 float *ref_ptr = ref.buffer().as<float*>();
152 size_t ref_size = ref.size();
154 ASSERT_EQ(res_size, ref_size);
156 for (size_t i = 0; i < ref_size; i++) {
157 ASSERT_NEAR(res_ptr[i], ref_ptr[i], max_diff);
161 void compare(float* res, float* ref, size_t size, float max_diff = 0.01f) {
162 for (size_t i = 0; i < size; i++) {
163 ASSERT_NEAR(res[i], ref[i], max_diff);
167 void replace(std::string& str, const std::string& from, const std::string& to)
169 std::string::size_type pos = 0;
171 while((pos = str.find(from, pos)) != std::string::npos) {
172 str.replace(pos, from.length(), to);
177 std::string replace(std::string& str, const std::string& from, const int& to) {
178 replace(str, from, std::to_string(to));
182 std::string replace(std::string& str, const std::string& from, const size_t& to) {
183 replace(str, from, std::to_string(to));
187 std::string replace(std::string& str, const std::string& from, const float& to) {
188 replace(str, from, std::to_string(to));
191 // trim from both ends (in place)
192 static inline std::string &trim(std::string &s) {
193 s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
194 s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
200 static InferenceEngine::StatusCode measurePerformance(const T & callInfer) {
201 bool isPerformance = nullptr != getenv("DLSDK_performance_test");
202 if (!isPerformance) {
206 typedef std::chrono::high_resolution_clock Time;
207 typedef std::chrono::nanoseconds ns;
208 typedef std::chrono::duration<float> fsec;
210 size_t niter = atoi(getenv("DLSDK_ITER_NUM"));
211 std::vector<double> times(niter);
212 InferenceEngine::StatusCode sts = InferenceEngine::OK;
214 for (size_t i = 0; i < niter; ++i)
216 auto t0 = Time::now();
218 auto t1 = Time::now();
220 ns d = std::chrono::duration_cast<ns>(fs);
221 double total = static_cast<double>(d.count());
223 times[i] = total*0.000001;
226 for (size_t i = 0; i < times.size(); i++)
227 std::cout << "Iteration: " << i << " | infer time: " << times[i] << " ms" << std::endl;
229 std::sort(times.begin(), times.end());
231 size_t first_index = (size_t)floor(times.size() * 0.25);
232 size_t last_index = (size_t)floor(times.size() * 0.75);
233 size_t num = last_index - first_index;
235 std::cout << "Q25: " << times[first_index] << std::endl;
236 std::cout << "Q75: " << times[last_index] << std::endl;
241 last_index = times.size();
245 std::vector<double> clipped_times;
247 for (auto i = first_index; i < last_index; i++)
249 clipped_times.push_back(times[i]);
253 mean = mean/clipped_times.size();
256 if (clipped_times.size()%2 != 0)
257 median = clipped_times[int(clipped_times.size()/2)];
258 else median = (clipped_times[int(clipped_times.size()/2)] + clipped_times[int(clipped_times.size()/2)-1])/2;
260 std::cout << "mean: " << mean << std::endl;
261 std::cout << "median: " << median << std::endl;
264 clipped_times.clear();
274 #if UINTPTR_MAX == 0xffffffff
276 #define ENVIRONMENT32
277 #elif UINTPTR_MAX == 0xffffffffffffffff
279 #define ENVIRONMENT64
281 # error Unsupported architecture
285 template <typename T,typename S>
286 std::shared_ptr<InferenceEngine::TBlob<T>> to_tblob(const std::shared_ptr<S> &obj)
288 return std::dynamic_pointer_cast<InferenceEngine::TBlob<T>>(obj);
291 inline InferenceEngine::InputInfo::Ptr getFirstInput(InferenceEngine::ICNNNetwork *pNet)
293 InferenceEngine::InputsDataMap inputs;
294 pNet->getInputsInfo(inputs);
295 //ASSERT_GT(inputs.size(), 0);
296 return inputs.begin()->second;
299 using OptionsMap = std::map<std::string, std::string>;