test_main.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*************************************************************************
  2. * Copyright (C) [2019] 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_b16c16_bgra_mlu270.cambricon", "/",
  35. "http://video.cambricon.com/models/MLU270/resnet50_b16c16_bgra_mlu270.cambricon"},
  36. {"feature_extract_for_tracker_b4c4_argb_mlu270.cambricon", "/",
  37. "http://video.cambricon.com/models/MLU270/feature_extract_for_tracker_b4c4_argb_mlu270.cambricon"},
  38. {"yolov3_b4c4_argb_mlu270.cambricon", "/",
  39. "http://video.cambricon.com/models/MLU270/yolov3_b4c4_argb_mlu270.cambricon"}};
  40. class TestEnvironment : public testing::Environment {
  41. public:
  42. virtual void SetUp() {
  43. edk::MluContext mlu_ctx;
  44. mlu_ctx.SetDeviceId(0);
  45. mlu_ctx.BindDevice();
  46. LOGI(MODULESUNITEST) << "Set Up global environment.";
  47. }
  48. };
  49. inline bool check_file_existence(const std::string &name) { return (access(name.c_str(), F_OK) == 0); }
  50. std::string GetExecPath() {
  51. char path[PATH_MAX_SIZE];
  52. int cnt = readlink("/proc/self/exe", path, PATH_MAX_SIZE);
  53. if (cnt < 0 || cnt >= PATH_MAX_SIZE) {
  54. return "";
  55. }
  56. if (path[cnt - 1] == '/') {
  57. path[cnt - 1] = '\0';
  58. } else {
  59. path[cnt] = '\0';
  60. }
  61. std::string result = std::string(path);
  62. return result.substr(0, result.rfind('/') + 1);
  63. }
  64. // split the path for make it
  65. std::vector<std::string> split_path(const std::string &s, char c) {
  66. std::stringstream ss(s);
  67. std::string piece;
  68. std::vector<std::string> chip_path;
  69. while (std::getline(ss, piece, c)) {
  70. chip_path.push_back(piece);
  71. }
  72. return chip_path;
  73. }
  74. void GetModuleExists(const std::vector<std::array<std::string, 3>> model_info) {
  75. std::string get_execute_path = GetExecPath();
  76. // std::cout << "program execute path is :" << get_execute_path << std::endl;
  77. std::string model_path;
  78. std::string model_file_path;
  79. std::string tmp = get_execute_path + "../../data/models";
  80. // if path not exists, return -1 and build it
  81. if (access(tmp.c_str(), F_OK) != 0) {
  82. mkdir(tmp.c_str(), 0777);
  83. }
  84. tmp += "/MLU270";
  85. if (access(tmp.c_str(), F_OK) != 0) {
  86. mkdir(tmp.c_str(), 0777);
  87. }
  88. for (unsigned i = 0; i < model_info.size(); i++) {
  89. std::string model_name = model_info[i][0];
  90. std::string tmp_path = get_execute_path + "../../data/models";
  91. model_path = tmp_path + model_info[i][1];
  92. model_file_path = model_path + model_name;
  93. // model file does not exists
  94. if (!check_file_existence(model_file_path)) {
  95. // std::vector<std::string> chip_path = split_path(model_pair.find(model_name)->second.first);
  96. std::vector<std::string> chip_path = split_path(model_info[i][1], '/');
  97. for (unsigned i = 0; i < chip_path.size(); i++) {
  98. tmp_path = tmp_path + "/" + chip_path[i];
  99. if (access(tmp_path.c_str(), F_OK) != 0) {
  100. mkdir(tmp_path.c_str(), 0777);
  101. }
  102. }
  103. // std::string cmd ="wget -P " + model_path + " " + model_pair.find(model_name)->second.second;
  104. std::string cmd = "wget -P " + model_path + " " + model_info[i][2];
  105. if (system(cmd.c_str()) != 0) {
  106. std::cerr << "shell execute failed" << std::endl;
  107. }
  108. }
  109. }
  110. }
  111. int main(int argc, char **argv) {
  112. // GetModuleExists(model_name, modulepath_pair);
  113. GetModuleExists(model_info);
  114. // fork process and write log to file in child proccess is not supported in log system
  115. // cnstream::InitCNStreamLogging(GetExecPath().c_str());
  116. testing::InitGoogleTest(&argc, argv);
  117. ::gflags::ParseCommandLineFlags(&argc, &argv, false);
  118. // FLAGS_alsologtostderr = true;
  119. testing::AddGlobalTestEnvironment(new TestEnvironment);
  120. int ret = RUN_ALL_TESTS();
  121. // cnstream::ShutdownCNStreamLogging();
  122. return ret;
  123. }