[Refactoring] remove shotgun surgery code smell
authorYelin Jeong <yelini.jeong@samsung.com>
Wed, 6 Mar 2024 12:33:28 +0000 (21:33 +0900)
committerWook Song <wook16.song@samsung.com>
Tue, 18 Mar 2025 07:28:09 +0000 (16:28 +0900)
Each apptest has its own Tester class so a single change is made to multiple classes simultaneously.
This patch moves methods to remove such code smell.

Signed-off-by: Yelin Jeong <yelini.jeong@samsung.com>
tests/apptests/tvn_triv2.cc
tests/apptests/tvn_triv2_aging.cc
tests/apptests/tvn_triv2_bulk.cc
tests/apptests/tvn_triv2_preempt.cc
tests/apptests/tvn_triv2_profile.cc
tests/unittests/ne_libnpuhost_test.cc
tests/utils/ne_test_utils.cc
tests/utils/ne_test_utils.h

index 7d55c5f8fe10a6ddfcb04c7fd07f96f98ae1f902..6ee8c5497f838b395233283a597dfd9ff102c2f7 100644 (file)
 
 using namespace std;
 
-/** @brief C++ class to describe how to use npu-engine library */
-class Tester : public UtilTRIV2 {
- public:
-  Tester () : model_id_ (0) {}
-
-  /** @brief initilize the device handle */
-  int init (std::string model_dir) {
-    if (model_dir == "")
-      return -EINVAL;
-
-    std::string model_path = model_dir + "/model.tvn";
-    npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
-    if (meta == nullptr)
-      return -EINVAL;
-
-    uint32_t tops = NPU_VERSION_TOPS (meta->npu_version);
-    free (meta);
-
-    int status = UtilTRIV2::init (tops);
-    if (status != test_ret_success) {
-      cerr << "Failed to initialize\n";
-      return status;
-    }
-
-    return UtilTRIV2::loadModel (model_dir, &model_id_, NPU_PRIORITY_MID, NPU_TIMEOUT_MS);
-  }
-
-  /** @brief run the inference */
-  int run () {
-    if (model_id_ == 0)
-      return test_ret_failure;
-
-    int task_id = UtilTRIV2::run (model_id_);
-    if (task_id < 0)
-      return task_id;
-
-    return wait () == 1 ? test_ret_success : test_ret_failure;
-  }
-
- private:
-  uint32_t model_id_;
-};
-
 /** @brief apptest main  */
 int
 main (int argc, char **argv) {
-  Tester tester;
+  UtilTRIV2 tester;
   char *dir;
-  int status;
-  int index;
+  int task_id, status, index;
+  uint32_t model_id;
 
   status = tester.parseArgs (argc, argv, "model_path", &index);
   if (status == test_ret_skipped || index < 0)
@@ -74,12 +31,12 @@ main (int argc, char **argv) {
 
   dir = argv[index];
   /** initialize triv2 device */
-  status = tester.init (dir);
+  status = tester.prepare (dir, &model_id);
   if (status < test_ret_success)
     goto err;
 
   /** run the inference with the device */
-  status = tester.run ();
+  status = tester.run (model_id, &task_id);
   if (status < test_ret_success)
     goto err;
 
index 50ca497ed98b14fa4af6e68d677aff56a98939ca..3089c6843acc8b22e8709f7f3bc02c8f2c984f5a 100644 (file)
@@ -38,23 +38,6 @@ class Tester : public UtilTRIV2 {
  public:
   Tester () : tops_ (0), num_iter_ (0), mode_ (AGING_TEST_NONE) {}
 
-  /** @brief initilize the device handle */
-  int init (string model_dir) {
-    if (model_dir == "")
-      return -EINVAL;
-
-    model_dir_ = model_dir;
-    string model_path = model_dir + "/model.tvn";
-    npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
-    if (meta == nullptr)
-      return -EINVAL;
-
-    tops_ = NPU_VERSION_TOPS (meta->npu_version);
-    free (meta);
-
-    return 0;
-  }
-
   void check_memleak () {
     size_t alloc_total = 0;
     size_t free_total = 0;
@@ -91,12 +74,6 @@ class Tester : public UtilTRIV2 {
     uint32_t model_id;
     int req_id, status;
 
-    status = UtilTRIV2::init (tops_);
-    if (status != test_ret_success) {
-      cerr << "Failed to initialize\n";
-      return status;
-    }
-
     if (mode_ == AGING_TEST_MODEL) {
       /* repeat model_register/unregister */
       for (uint32_t i = 0; i < num_iter_; i++) {
@@ -225,6 +202,8 @@ class Tester : public UtilTRIV2 {
     return status;
   }
 
+  void set_model (string model_dir) { model_dir_ = model_dir; }
+
  private:
   string model_dir_;
   uint32_t tops_;
@@ -237,8 +216,9 @@ int
 main (int argc, char **argv) {
   const char *help = "model_dir test_mode num_iterations";
   Tester tester;
-  int status;
-  int index;
+  int status, index;
+  uint32_t model_id;
+  char *dir;
 
   status = tester.parseArgs (argc, argv, help, &index);
   if (status == test_ret_skipped || index < 0)
@@ -250,11 +230,14 @@ main (int argc, char **argv) {
     goto skip;
   }
 
+  dir = argv[index];
   /** initialize triv2 device */
-  status = tester.init (argv[index]);
+  status = tester.prepare (dir, &model_id);
   if (status < test_ret_success)
     goto err;
 
+  tester.set_model (dir);
+
   /** configure testing */
   status = tester.config (argv[index + 1], argv[index + 2]);
   if (status < test_ret_success)
index 9926fbe44b670cb285b6232230831e693901a687..d36d12e87d8e7b48d1167092339831cda02644a4 100644 (file)
 
 using namespace std;
 
-/** @brief C++ class to describe how to use npu-engine library */
-class Tester : public UtilTRIV2 {
- public:
-  Tester () : model_id_ (0) {}
-
-  /** @brief initilize the device handle */
-  int init (std::string model_dir) {
-    std::string model_path = model_dir + "/model.tvn";
-    npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
-    if (meta == nullptr) {
-      cerr << "Failed to get the model metadata\n";
-      return -EINVAL;
-    }
-
-    uint32_t tops = NPU_VERSION_TOPS (meta->npu_version);
-    free (meta);
-
-    int status = UtilTRIV2::init (tops);
-    if (status != test_ret_success) {
-      cerr << "Failed to initialize the device\n";
-      return status;
-    }
-
-    return UtilTRIV2::loadModel (model_dir, &model_id_, NPU_PRIORITY_MID, NPU_TIMEOUT_MS);
-  }
-
-  /** @brief run the inference */
-  int run () {
-    if (model_id_ == 0)
-      return test_ret_failure;
-
-    int task_id = UtilTRIV2::run (model_id_);
-    if (task_id < 0)
-      return task_id;
-
-    return wait () == 1 ? test_ret_success : test_ret_failure;
-  }
-
- private:
-  uint32_t model_id_;
-};
-
 /** @brief apptest main  */
 int
 main (int argc, char **argv) {
-  Tester tester;
+  UtilTRIV2 tester;
   char *dir_str;
-  int status;
-  int index;
+  int task_id, status, index;
 
   std::ios_base::fmtflags f (cerr.flags ());
 
@@ -87,7 +44,7 @@ main (int argc, char **argv) {
     return -errno;
   }
 
-  uint32_t total_succs, total_fails;
+  uint32_t total_succs, total_fails, model_id;
   struct dirent *dir;
 
   total_succs = total_fails = 0;
@@ -103,10 +60,10 @@ main (int argc, char **argv) {
 
     /** initialize triv2 device */
     string path (dir_str);
-    status = tester.init (path + "/" + dir->d_name);
+    status = tester.prepare (path + "/" + dir->d_name, &model_id);
     if (status == test_ret_success) {
       /** run the inference with the device */
-      status = tester.run ();
+      status = tester.run (model_id, &task_id);
       if (status == test_ret_success) {
         cerr << "PASSED\n";
         total_succs++;
index f09b89c32145953fdacafb6ced7facc23529eb77..7166563c62517f95152e2d441a8f2accbc654707 100644 (file)
@@ -65,11 +65,11 @@ class Tester : public UtilTRIV2 {
     if (mid_model_id_ == 0 || high_model_id_ == 0)
       return test_ret_failure;
 
-    int mid_task_id = UtilTRIV2::run (mid_model_id_);
+    int mid_task_id = UtilTrinity::run (mid_model_id_);
     if (mid_task_id < 0)
       return mid_task_id;
 
-    int high_task_id = UtilTRIV2::run (high_model_id_);
+    int high_task_id = UtilTrinity::run (high_model_id_);
     if (high_task_id < 0)
       return high_task_id;
 
index 8d2b893a0640141c34db1ee6508333a2e2e12fbb..cc28bf0e81634db5b34c948296868f4883e14739 100644 (file)
@@ -20,35 +20,6 @@ using namespace std;
 /** @brief C++ class to describe how to use npu-engine library */
 class Tester : public UtilTRIV2 {
  public:
-  Tester () : model_id_ (0) {}
-
-  /** @brief initilize the device handle */
-  int init (std::string model_dir) {
-    int status = UtilTRIV2::init ();
-    if (status != test_ret_success) {
-      cerr << "Failed to initialize\n";
-      return status;
-    }
-
-    return UtilTRIV2::loadModel (model_dir, &model_id_, NPU_PRIORITY_MID, NPU_TIMEOUT_MS);
-  }
-
-  /** @brief run the inference */
-  int run () {
-    if (model_id_ == 0)
-      return test_ret_failure;
-
-    int task_id = UtilTRIV2::run (model_id_);
-    if (task_id < 0)
-      return task_id;
-
-    int result = wait () == 1 ? test_ret_success : test_ret_failure;
-
-    print_profile (task_id);
-
-    return result;
-  }
-
   /** @brief print profile */
   void print_profile (int task_id) {
     npu_profile profile;
@@ -90,9 +61,6 @@ class Tester : public UtilTRIV2 {
       cerr << "Failed to get profile: " << status << "\n";
     }
   }
-
- private:
-  uint32_t model_id_;
 };
 
 /** @brief apptest main  */
@@ -100,8 +68,8 @@ int
 main (int argc, char **argv) {
   Tester tester;
   char *dir;
-  int status;
-  int index;
+  int task_id, status, index;
+  uint32_t model_id;
 
   status = tester.parseArgs (argc, argv, "model_path", &index);
   if (status == test_ret_skipped || index < 0)
@@ -110,16 +78,19 @@ main (int argc, char **argv) {
     goto err;
 
   dir = argv[index];
+
   /** initialize triv2 device */
-  status = tester.init (dir);
+  status = tester.prepare (dir, &model_id);
   if (status < test_ret_success)
     goto err;
 
   /** run the inference with the device */
-  status = tester.run ();
+  status = tester.run (model_id, &task_id);
   if (status < test_ret_success)
     goto err;
 
+  tester.print_profile (task_id);
+
   cerr << "[APPTEST] " << argv[0] << ": PASSED\n";
   return 0;
 
index 1460a4844d0a05b9686cab90ac145bc54d9b6dbd..61366ea6f85903f6ada9ff03707fa59f95742f2a 100644 (file)
@@ -116,7 +116,7 @@ TEST (ne_libnpuhost_test, get_device_instance_n) {
  * @brief test APIs to run model with input data (TRIV2)
  */
 TEST (ne_libnpuhost_test, run_input_triv2_binfmt_v3) {
-  UtilTRIV2 *tester = new UtilTRIV2 (testenv.get_devices ()[1]);
+  UtilTrinity *tester = new UtilTrinity (testenv.get_devices ()[1]);
 
   ASSERT_EQ (tester->init (testenv.get_tops ()), 0);
 
@@ -140,7 +140,7 @@ TEST (ne_libnpuhost_test, run_input_triv2_binfmt_v3) {
  * @brief test APIs to run model with input data (TRIV2) (negative cases)
  */
 TEST (ne_libnpuhost_test, run_input_triv2_binfmt_v3_n) {
-  UtilTRIV2 *tester = new UtilTRIV2 (testenv.get_devices ()[1]);
+  UtilTrinity *tester = new UtilTrinity (testenv.get_devices ()[1]);
 
   /* without init */
   EXPECT_NE (tester->run (0, true), 0);
@@ -1073,7 +1073,7 @@ TEST (ne_libnpuhost_test, write_logs_n) {
  * @brief test profile APIs (legacy)
  */
 TEST (ne_libnpuhost_test, profile_apis_legacy) {
-  UtilTRIV2 tester;
+  UtilTrinity tester;
 
   ASSERT_EQ (tester.init (testenv.get_tops ()), 0);
 
index 8a88f7d4e7543344d97920eeef5e40bb246d688a..974cc00699237f4ae69bb259bb44028ea00f1974 100644 (file)
@@ -778,6 +778,59 @@ UtilTrinity::set_constraint (uint32_t model_id, uint32_t timeout, npu_priority p
   return setNPU_constraint (dev_, model_id, constraint);
 }
 
+/**
+ * @brief Get model's tops
+ * @param[in] model_dir The directory of tvn model to get tops
+ */
+int
+UtilTRIV2::get_tops (std::string model_dir) {
+  if (model_dir == "")
+    return -EINVAL;
+
+  std::string model_path = model_dir + "/model.tvn";
+  npubin_meta *meta = getNPUmodel_metadata (model_path.c_str (), false);
+  if (meta == nullptr)
+    return -EINVAL;
+
+  uint32_t tops = NPU_VERSION_TOPS (meta->npu_version);
+  free (meta);
+
+  return tops;
+}
+
+/**
+ * @brief Init and load model
+ * @param[in] model_dir The directory of tvn model to test
+ * @param[out] model_id The model id of given model
+ */
+int
+UtilTRIV2::prepare (std::string model_dir, uint32_t *model_id) {
+  int status, tops = get_tops (model_dir);
+  status = UtilTrinity::init (tops);
+
+  if (status != test_ret_success)
+    return status;
+
+  return UtilTRIV2::loadModel (model_dir, model_id, NPU_PRIORITY_MID, 10);
+}
+
+/**
+ * @brief Run initialized model
+ * @param[in] model_id The model id to run
+ * @param[out] task_id The task id of model
+ */
+int
+UtilTRIV2::run (uint32_t model_id, int *task_id) {
+  if (model_id == 0)
+    return test_ret_failure;
+
+  *task_id = UtilTrinity::run (model_id);
+  if (*task_id < 0)
+    return *task_id;
+
+  return wait () == 1 ? test_ret_success : test_ret_failure;
+}
+
 /** @brief implementation of extract_node_id for TRIV2 */
 bool
 UtilTRIV2::extract_node_id (uint32_t *id) {
index c516b5656c3d1e54ec1acc65e6e93834617a62e7..00ba669220e050f0f1ec38c20112fdff8340f06b 100644 (file)
@@ -96,6 +96,8 @@ class UtilModel {
 /** @brief utility to access trinity device */
 class UtilTrinity {
  public:
+  UtilTrinity () : UtilTrinity (NPUCOND_TRIV24_CONN_SOCIP, true, true) {}
+  UtilTrinity (dev_type type) : UtilTrinity (type, true, true) {}
   UtilTrinity (dev_type type, bool need_model, bool verify);
   virtual ~UtilTrinity ();
 
@@ -182,6 +184,10 @@ class UtilTRIV2 : public UtilTrinity {
   UtilTRIV2 () : UtilTrinity (NPUCOND_TRIV24_CONN_SOCIP, true, true) {}
   UtilTRIV2 (dev_type type) : UtilTrinity (type, true, true) {}
 
+  int get_tops (std::string model_dir);
+  int prepare (std::string model_dir, uint32_t *model_id);
+  int run (uint32_t model_id, int *task_id);
+
  private:
   bool check_version (npubin_meta *meta);
   int set_data_info (npubin_meta *meta, uint32_t model_id);