TensorFlow Serving C++ API Documentation
resource_estimator_test.cc
1 /* Copyright 2020 Google Inc. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7  http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include "tensorflow_serving/servables/tensorflow/oss/resource_estimator.h"
16 
17 #include <cstddef>
18 #include <vector>
19 
20 #include <gmock/gmock.h>
21 #include <gtest/gtest.h>
22 #include "tensorflow/core/lib/core/status_test_util.h"
23 #include "tensorflow/core/platform/path.h"
24 #include "tensorflow_serving/servables/tensorflow/bundle_factory_test_util.h"
25 #include "tensorflow_serving/test_util/test_util.h"
26 #include "tensorflow_serving/util/test_util/mock_file_probing_env.h"
27 
28 namespace tensorflow {
29 namespace serving {
30 namespace {
31 
32 using test_util::EqualsProto;
33 using ::testing::_;
34 using ::testing::DoAll;
35 using ::testing::Return;
36 using ::testing::SetArgPointee;
37 
38 class ResourceEstimatorTest : public ::testing::Test {
39  protected:
40  void SetUp() {
41  export_dir_ = "/foo/bar";
42  const string child = "child";
43  const string child_path = io::JoinPath(export_dir_, child);
44  file_size_ = 100;
45 
46  // Set up the expectation that the directory contains exactly one child with
47  // the given file size.
48  EXPECT_CALL(env_, FileExists(export_dir_))
49  .WillRepeatedly(Return(Status()));
50  EXPECT_CALL(env_, GetChildren(export_dir_, _))
51  .WillRepeatedly(DoAll(SetArgPointee<1>(std::vector<string>({child})),
52  Return(Status())));
53  EXPECT_CALL(env_, IsDirectory(child_path))
54  .WillRepeatedly(Return(errors::FailedPrecondition("")));
55  EXPECT_CALL(env_, GetFileSize(child_path, _))
56  .WillRepeatedly(
57  DoAll(SetArgPointee<1>(file_size_), Return(Status())));
58  }
59 
60  string export_dir_;
61  double file_size_;
62  test_util::MockFileProbingEnv env_;
63 };
64 
65 TEST_F(ResourceEstimatorTest, EstimateResourceFromPathWithFileProbingEnv) {
66  ResourceAllocation actual;
67  TF_ASSERT_OK(EstimateMainRamBytesFromPath(
68  export_dir_, /*use_validation_result=*/false, &env_, &actual));
69  ResourceAllocation expected =
70  test_util::GetExpectedResourceEstimate(file_size_);
71  EXPECT_THAT(actual, EqualsProto(expected));
72 }
73 
74 TEST_F(ResourceEstimatorTest, EstimateResourceFromValidationResult) {
75  // Currently, using validation result is not supported yet.
76  // Uses disk state to estimate the resource usage.
77  ResourceAllocation actual;
78  TF_ASSERT_OK(EstimateMainRamBytesFromPath(
79  export_dir_, /*use_validation_result=*/true, &env_, &actual));
80  ResourceAllocation expected =
81  test_util::GetExpectedResourceEstimate(file_size_);
82  EXPECT_THAT(actual, EqualsProto(expected));
83 }
84 
85 } // namespace
86 } // namespace serving
87 } // namespace tensorflow