Implementation:Tensorflow Serving Resource Tracker Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Resource Management |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
Test suite validating the ResourceTracker component, which tracks resource allocation and availability across devices to determine whether new servables can be loaded.
Description
This test file exercises the ResourceTracker class, which manages a pool of total resources (main RAM, GPU RAM across multiple instances) and tracks used resources as servables are reserved and released. The fixture ResourceTrackerTest creates a tracker with 16 units each of main/RAM, GPU 0/RAM, and GPU 1/RAM, along with four mock loaders (loader_0_ through loader_3_) with varying resource estimates and an invalid_resources_loader_ for error testing. Tests cover unbound and unnormalized total resources, resource recomputation, reservation success/failure for both bound and unbound resources, and invalid resource estimates.
Usage
Run these tests to validate that resource tracking correctly accounts for device-bound and unbound resource allocations, properly rejects loads that would exceed capacity, and handles invalid resource estimates gracefully.
Code Reference
Source Location
- Repository: Tensorflow_Serving
- File:
tensorflow_serving/resources/resource_tracker_test.cc - Lines: 1-457
Test Fixture
class ResourceTrackerTest : public ::testing::Test {
protected:
ResourceTrackerTest()
: total_resources_(
CreateProto<ResourceAllocation>(
"resource_quantities { "
" resource { device: 'main' device_instance { value: 0 } kind: 'ram' } "
" quantity: 16 "
"} "
"resource_quantities { "
" resource { device: 'gpu' device_instance { value: 0 } kind: 'ram' } "
" quantity: 16 "
"} "
"resource_quantities { "
" resource { device: 'gpu' device_instance { value: 1 } kind: 'ram' } "
" quantity: 16 "
"} ")) {
std::unique_ptr<ResourceUtil> util(
new ResourceUtil({{{"main", 1}, {"gpu", 2}}}));
TF_CHECK_OK(ResourceTracker::Create(
total_resources_, std::move(util), &tracker_));
// ... mock loader setup ...
}
ResourceAllocation total_resources_;
std::unique_ptr<ResourceTracker> tracker_;
std::unique_ptr<NiceMock<test_util::MockLoader>> loader_0_;
std::unique_ptr<NiceMock<test_util::MockLoader>> loader_1_;
std::unique_ptr<NiceMock<test_util::MockLoader>> loader_2_;
std::unique_ptr<NiceMock<test_util::MockLoader>> loader_3_;
std::unique_ptr<NiceMock<test_util::MockLoader>> invalid_resources_loader_;
};
Build Target
bazel test //tensorflow_serving/resources:resource_tracker_test
Test Coverage
Key Test Cases
| Test Name | Category | Description |
|---|---|---|
| UnboundTotalResources | Validation | Rejects total resources that contain unbound device entries |
| UnnormalizedTotalResources | Validation | Rejects total resources that are not normalized |
| RecomputeUsedResources | Tracking | Validates recomputation of used resources from a set of loaders |
| ReserveResourcesSuccessWithUsedResourcesBound | Reservation | Tests successful reservation when used resources are device-bound |
| ReserveResourcesFailureWithUsedResourcesBound | Reservation | Tests reservation rejection when bound resources exceed capacity |
| ReserveResourcesSuccessWithUsedResourcesUnbound | Reservation | Tests successful reservation with unbound (device-agnostic) resources |
| ReserveResourcesFailureWithUsedResourcesUnbound | Reservation | Tests reservation rejection when unbound resources exceed capacity |
| InvalidResourceEstimate | Error Handling | Validates error handling for loaders with invalid resource estimates |
Usage Examples
Test Pattern
TEST_F(ResourceTrackerTest, RecomputeUsedResources) {
TF_ASSERT_OK(tracker_->RecomputeUsedResources(
{loader_0_.get(), loader_1_.get()}));
// Verify used resources reflect the sum of loader_0_ and loader_1_ estimates
bool success;
TF_ASSERT_OK(tracker_->ReserveResources(*loader_2_, &success));
// loader_2_ needs 15 main/ram; with 6 already used, 10 remain -> fails
EXPECT_FALSE(success);
TF_ASSERT_OK(tracker_->ReserveResources(*loader_3_, &success));
// loader_3_ needs 12 gpu/ram (unbound); distributes across instances -> ok
EXPECT_TRUE(success);
}