test_main.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*************************************************************************
  2. * Copyright (C) [2021] by Cambricon, Inc. 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. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gflags/gflags.h>
  21. #include <array>
  22. #include <iostream>
  23. #include <sstream>
  24. #include <string>
  25. #include <utility>
  26. #include <vector>
  27. #include "cnstream_logging.hpp"
  28. #include "gtest/gtest.h"
  29. #include "sys/stat.h"
  30. #include "sys/types.h"
  31. #include "device/mlu_context.h"
  32. #define PATH_MAX_SIZE 1024
  33. const std::vector<std::array<std::string, 3>> model_info = {
  34. {"resnet50_offline.cambricon", "/Classification/resnet50/",
  35. "http://video.cambricon.com/models/MLU270/Classification/resnet50/resnet50_offline.cambricon"},
  36. {"resnet50_offline_v1.3.0.cambricon", "/Classification/resnet50/",
  37. "http://video.cambricon.com/models/MLU270/Classification/resnet50/resnet50_offline_v1.3.0.cambricon"},
  38. {"yuv2gray.cambricon", "/KCF/", "http://video.cambricon.com/models/MLU270/KCF/yuv2gray.cambricon"},
  39. {"feature_extract_4c4b_argb_270_v1.5.0.cambricon", "/feature_extract/",
  40. "http://video.cambricon.com/models/MLU270/feature_extract/feature_extract_4c4b_argb_270_v1.5.0.cambricon"},
  41. };
  42. class TestEnvironment : public testing::Environment {
  43. public:
  44. virtual void SetUp() {
  45. edk::MluContext mlu_ctx;
  46. mlu_ctx.SetDeviceId(0);
  47. // mlu_ctx.SetChannelId(0);
  48. mlu_ctx.BindDevice();
  49. LOGI(MODULESUNITEST) << "Set Up global environment.";
  50. }
  51. };
  52. inline bool check_file_existence(const std::string &name) { return (access(name.c_str(), F_OK) == 0); }
  53. std::string GetExecPath() {
  54. char path[PATH_MAX_SIZE];
  55. int cnt = readlink("/proc/self/exe", path, PATH_MAX_SIZE);
  56. if (cnt < 0 || cnt >= PATH_MAX_SIZE) {
  57. return "";
  58. }
  59. if (path[cnt - 1] == '/') {
  60. path[cnt - 1] = '\0';
  61. } else {
  62. path[cnt] = '\0';
  63. }
  64. std::string result = std::string(path);
  65. return result.substr(0, result.rfind('/') + 1);
  66. }
  67. // split the path for make it
  68. std::vector<std::string> split_path(const std::string &s, char c) {
  69. std::stringstream ss(s);
  70. std::string piece;
  71. std::vector<std::string> chip_path;
  72. while (std::getline(ss, piece, c)) {
  73. chip_path.push_back(piece);
  74. }
  75. return chip_path;
  76. }
  77. void GetModuleExists(const std::vector<std::array<std::string, 3>> model_info) {
  78. std::string get_execute_path = GetExecPath();
  79. // std::cout << "program execute path is :" << get_execute_path << std::endl;
  80. std::string model_path;
  81. std::string model_file_path;
  82. std::string tmp = get_execute_path + "../../data/models";
  83. // if path not exists, return -1 and build it
  84. if (access(tmp.c_str(), F_OK) != 0) {
  85. mkdir(tmp.c_str(), 0777);
  86. }
  87. tmp += "/MLU270";
  88. if (access(tmp.c_str(), F_OK) != 0) {
  89. mkdir(tmp.c_str(), 0777);
  90. }
  91. for (unsigned i = 0; i < model_info.size(); i++) {
  92. std::string model_name = model_info[i][0];
  93. std::string tmp_path = get_execute_path + "../../data/models/MLU270";
  94. model_path = tmp_path + model_info[i][1];
  95. model_file_path = model_path + model_name;
  96. // model file does not exists
  97. if (!check_file_existence(model_file_path)) {
  98. // std::vector<std::string> chip_path = split_path(model_pair.find(model_name)->second.first);
  99. std::vector<std::string> chip_path = split_path(model_info[i][1], '/');
  100. for (unsigned i = 0; i < chip_path.size(); i++) {
  101. tmp_path = tmp_path + "/" + chip_path[i];
  102. if (access(tmp_path.c_str(), F_OK) != 0) {
  103. mkdir(tmp_path.c_str(), 0777);
  104. }
  105. }
  106. // std::string cmd ="wget -P " + model_path + " " + model_pair.find(model_name)->second.second;
  107. std::string cmd = "wget -P " + model_path + " " + model_info[i][2];
  108. if (system(cmd.c_str()) != 0) {
  109. std::cerr << "shell execute failed" << std::endl;
  110. }
  111. }
  112. }
  113. }
  114. int main(int argc, char **argv) {
  115. // GetModuleExists(model_name, modulepath_pair);
  116. GetModuleExists(model_info);
  117. // fork process and write log to file in child proccess is not supported in log system
  118. // cnstream::InitCNStreamLogging(GetExecPath().c_str());
  119. testing::InitGoogleTest(&argc, argv);
  120. ::gflags::ParseCommandLineFlags(&argc, &argv, false);
  121. // FLAGS_alsologtostderr = true;
  122. testing::AddGlobalTestEnvironment(new TestEnvironment);
  123. int ret = RUN_ALL_TESTS();
  124. // cnstream::ShutdownCNStreamLogging();
  125. return ret;
  126. }