Skip to content

Commit 8a7dd7d

Browse files
committed
Setup initial cpp unittests
1 parent 5e302bb commit 8a7dd7d

File tree

11 files changed

+152
-26
lines changed

11 files changed

+152
-26
lines changed

benchmarks/config.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Apache License, Version 2.0
3-
* Copyright 2020 NVIDIA Corporation
3+
* Copyright 2020-2021 NVIDIA Corporation
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -21,7 +21,8 @@
2121

2222
struct AppConfig
2323
{
24-
std::string input_file = "test_data/private/generic_tiff_000.tif";
24+
std::string test_folder;
25+
std::string test_file;
2526
bool discard_cache = false;
2627
int random_seed = 0;
2728
bool random_start_location = false;
@@ -42,6 +43,38 @@ struct AppConfig
4243
std::string benchmark_color; // {auto|true|false}
4344
std::string benchmark_counters_tabular;
4445
std::string v; // <verbosity>
46+
47+
std::string get_input_path(const std::string default_value = "generated/tiff_stripe_4096x4096_256.tif") const
48+
{
49+
// If `test_file` is absolute path
50+
if (!test_folder.empty() && test_file.substr(0, 1) == "/")
51+
{
52+
return test_file;
53+
}
54+
else
55+
{
56+
std::string test_data_folder = test_folder;
57+
if (test_data_folder.empty())
58+
{
59+
if (const char* env_p = std::getenv("CUCIM_TESTDATA_FOLDER"))
60+
{
61+
test_data_folder = env_p;
62+
}
63+
else
64+
{
65+
test_data_folder = "test_data";
66+
}
67+
}
68+
if (test_file.empty())
69+
{
70+
return test_data_folder + "/" + default_value;
71+
}
72+
else
73+
{
74+
return test_data_folder + "/" + test_file;
75+
}
76+
}
77+
}
4578
};
4679

4780
#endif // CUCIM_CONFIG_H

benchmarks/main.cpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, NVIDIA CORPORATION.
2+
* Copyright (c) 2020-2021, NVIDIA CORPORATION.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,8 @@ static AppConfig g_config;
3232

3333
static void test_cucim(benchmark::State& state)
3434
{
35+
std::string input_path = g_config.get_input_path();
36+
3537
int arg = -1;
3638
for (auto state_item : state)
3739
{
@@ -46,7 +48,7 @@ static void test_cucim(benchmark::State& state)
4648

4749
if (g_config.discard_cache)
4850
{
49-
int fd = open(g_config.input_file.c_str(), O_RDONLY);
51+
int fd = open(input_path.c_str(), O_RDONLY);
5052
fdatasync(fd);
5153
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
5254
close(fd);
@@ -61,7 +63,7 @@ static void test_cucim(benchmark::State& state)
6163
request_location[1] = rand() % (g_config.image_height - state.range(0));
6264
}
6365

64-
cucim::CuImage image = cucim::CuImage(g_config.input_file.c_str());
66+
cucim::CuImage image = cucim::CuImage(input_path.c_str());
6567
cucim::CuImage region =
6668
image.read_region({ request_location[0], request_location[1] }, { state.range(0), state.range(0) }, 0,
6769
cucim::DimIndices{}, "cpu", nullptr, "");
@@ -70,6 +72,8 @@ static void test_cucim(benchmark::State& state)
7072

7173
static void test_openslide(benchmark::State& state)
7274
{
75+
std::string input_path = g_config.get_input_path();
76+
7377
int arg = -1;
7478
for (auto _ : state)
7579
{
@@ -84,15 +88,15 @@ static void test_openslide(benchmark::State& state)
8488

8589
if (g_config.discard_cache)
8690
{
87-
int fd = open(g_config.input_file.c_str(), O_RDONLY);
91+
int fd = open(input_path.c_str(), O_RDONLY);
8892
fdatasync(fd);
8993
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
9094
close(fd);
9195
}
9296
}
9397
state.ResumeTiming();
9498

95-
openslide_t* slide = openslide_open(g_config.input_file.c_str());
99+
openslide_t* slide = openslide_open(input_path.c_str());
96100
uint32_t* buf = static_cast<uint32_t*>(cucim_malloc(state.range(0) * state.range(0) * 4));
97101
int64_t request_location[2] = { 0, 0 };
98102
if (g_config.random_start_location)
@@ -131,10 +135,12 @@ static bool remove_help_option(int* argc, char** argv)
131135

132136
static bool setup_configuration()
133137
{
134-
openslide_t* slide = openslide_open(g_config.input_file.c_str());
138+
std::string input_path = g_config.get_input_path();
139+
140+
openslide_t* slide = openslide_open(input_path.c_str());
135141
if (slide == nullptr)
136142
{
137-
fmt::print("[Error] Cannot load {}!\n", g_config.input_file);
143+
fmt::print("[Error] Cannot load {}!\n", input_path);
138144
return false;
139145
}
140146
int64_t w, h;
@@ -159,7 +165,8 @@ int main(int argc, char** argv)
159165
// return 1;
160166

161167
CLI::App app{ "cuCIM Benchmark" };
162-
app.add_option("--test_file", g_config.input_file, "An input .tif/.svs file path");
168+
app.add_option("--test_folder", g_config.test_folder, "An input test folder path");
169+
app.add_option("--test_file", g_config.test_file, "An input test image file path");
163170
app.add_option("--discard_cache", g_config.discard_cache, "Discard page cache for the input file for each iteration");
164171
app.add_option("--random_seed", g_config.random_seed, "A random seed number");
165172
app.add_option(

cpp/plugins/cucim.kit.cuslide/benchmarks/config.h

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Apache License, Version 2.0
3-
* Copyright 2020 NVIDIA Corporation
3+
* Copyright 2020-2021 NVIDIA Corporation
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -21,7 +21,8 @@
2121

2222
struct AppConfig
2323
{
24-
std::string input_file = "test_data/private/generic_tiff_000.tif";
24+
std::string test_folder;
25+
std::string test_file;
2526
bool discard_cache = false;
2627
int random_seed = 0;
2728
bool random_start_location = false;
@@ -42,6 +43,38 @@ struct AppConfig
4243
std::string benchmark_color; // {auto|true|false}
4344
std::string benchmark_counters_tabular;
4445
std::string v; // <verbosity>
46+
47+
std::string get_input_path(const std::string default_value = "generated/tiff_stripe_4096x4096_256.tif") const
48+
{
49+
// If `test_file` is absolute path
50+
if (!test_folder.empty() && test_file.substr(0, 1) == "/")
51+
{
52+
return test_file;
53+
}
54+
else
55+
{
56+
std::string test_data_folder = test_folder;
57+
if (test_data_folder.empty())
58+
{
59+
if (const char* env_p = std::getenv("CUCIM_TESTDATA_FOLDER"))
60+
{
61+
test_data_folder = env_p;
62+
}
63+
else
64+
{
65+
test_data_folder = "test_data";
66+
}
67+
}
68+
if (test_file.empty())
69+
{
70+
return test_data_folder + "/" + default_value;
71+
}
72+
else
73+
{
74+
return test_data_folder + "/" + test_file;
75+
}
76+
}
77+
}
4578
};
4679

4780
#endif // CUSLIDE_CONFIG_H

cpp/plugins/cucim.kit.cuslide/benchmarks/main.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ static AppConfig g_config;
4343

4444
static void test_basic(benchmark::State& state)
4545
{
46+
std::string input_path = g_config.get_input_path();
47+
4648
int arg = -1;
4749
for (auto state_item : state)
4850
{
@@ -57,7 +59,7 @@ static void test_basic(benchmark::State& state)
5759

5860
if (g_config.discard_cache)
5961
{
60-
int fd = open(g_config.input_file.c_str(), O_RDONLY);
62+
int fd = open(input_path.c_str(), O_RDONLY);
6163
fdatasync(fd);
6264
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
6365
close(fd);
@@ -83,7 +85,7 @@ static void test_basic(benchmark::State& state)
8385
return;
8486
}
8587

86-
auto handle = image_format->formats[0].image_parser.open(g_config.input_file.c_str());
88+
auto handle = image_format->formats[0].image_parser.open(input_path.c_str());
8789

8890
cucim::io::format::ImageMetadata metadata{};
8991
image_format->formats[0].image_parser.parse(&handle, &metadata.desc());
@@ -118,6 +120,8 @@ static void test_basic(benchmark::State& state)
118120

119121
static void test_openslide(benchmark::State& state)
120122
{
123+
std::string input_path = g_config.get_input_path();
124+
121125
int arg = -1;
122126
for (auto _ : state)
123127
{
@@ -132,15 +136,15 @@ static void test_openslide(benchmark::State& state)
132136

133137
if (g_config.discard_cache)
134138
{
135-
int fd = open(g_config.input_file.c_str(), O_RDONLY);
139+
int fd = open(input_path.c_str(), O_RDONLY);
136140
fdatasync(fd);
137141
posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED);
138142
close(fd);
139143
}
140144
}
141145
state.ResumeTiming();
142146

143-
openslide_t* slide = openslide_open(g_config.input_file.c_str());
147+
openslide_t* slide = openslide_open(input_path.c_str());
144148
uint32_t* buf = static_cast<uint32_t*>(cucim_malloc(state.range(0) * state.range(0) * 4));
145149
int64_t request_location[2] = { 0, 0 };
146150
if (g_config.random_start_location)
@@ -177,10 +181,11 @@ static bool remove_help_option(int* argc, char** argv)
177181

178182
static bool setup_configuration()
179183
{
180-
openslide_t* slide = openslide_open(g_config.input_file.c_str());
184+
std::string input_path = g_config.get_input_path();
185+
openslide_t* slide = openslide_open(input_path.c_str());
181186
if (slide == nullptr)
182187
{
183-
fmt::print("[Error] Cannot load {}!\n", g_config.input_file);
188+
fmt::print("[Error] Cannot load {}!\n", input_path);
184189
return false;
185190
}
186191

@@ -205,7 +210,8 @@ int main(int argc, char** argv)
205210
// if (::benchmark::ReportUnrecognizedArguments(argc, argv))
206211
// return 1;
207212
CLI::App app{ "benchmark: cuSlide" };
208-
app.add_option("--test_file", g_config.input_file, "An input .tif/.svs file path");
213+
app.add_option("--test_folder", g_config.test_folder, "An input test folder path");
214+
app.add_option("--test_file", g_config.test_file, "An input test image file path");
209215
app.add_option("--discard_cache", g_config.discard_cache, "Discard page cache for the input file for each iteration");
210216
app.add_option("--random_seed", g_config.random_seed, "A random seed number");
211217
app.add_option(

cpp/plugins/cucim.kit.cuslide/tests/config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* Apache License, Version 2.0
3-
* Copyright 2020 NVIDIA Corporation
3+
* Copyright 2020-2021 NVIDIA Corporation
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -25,7 +25,7 @@ struct AppConfig
2525
std::string test_folder;
2626
std::string test_file;
2727
std::string temp_folder = "/tmp";
28-
std::string get_input_path(const char* default_value = "private/generic_tiff_000.tif") const
28+
std::string get_input_path(const std::string default_value = "generated/tiff_stripe_4096x4096_256.tif") const
2929
{
3030
// If `test_file` is absolute path
3131
if (!test_folder.empty() && test_file.substr(0, 1) == "/")

cpp/tests/config.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,15 @@
2020
#include <string>
2121
#include <cstdlib>
2222

23+
#define XSTR(x) STR(x)
24+
#define STR(x) #x
25+
2326
struct AppConfig
2427
{
2528
std::string test_folder;
2629
std::string test_file;
2730
std::string temp_folder = "/tmp";
28-
std::string get_input_path(const char* default_value = "private/generic_tiff_000.tif") const
31+
std::string get_input_path(const std::string default_value = "generated/tiff_stripe_4096x4096_256.tif") const
2932
{
3033
// If `test_file` is absolute path
3134
if (!test_folder.empty() && test_file.substr(0, 1) == "/")
@@ -56,7 +59,7 @@ struct AppConfig
5659
}
5760
}
5861
}
59-
std::string get_plugin_path(const char* default_value = "cucim.kit.cuslide@0.0.0.so")
62+
std::string get_plugin_path(const char* default_value = "cucim.kit.cuslide@" XSTR(CUCIM_VERSION) ".so")
6063
{
6164
std::string plugin_path = default_value;
6265
if (const char* env_p = std::getenv("CUCIM_TEST_PLUGIN_PATH"))

cpp/tests/test_read_region.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,21 @@ SCENARIO("Verify read_region()", "[test_read_region.cpp]")
110110
// }
111111
printf("\ncucim count: %d\n", hash);
112112
cucim_free(image_data.container.data);
113+
if (image_data.container.shape)
114+
{
115+
cucim_free(image_data.container.shape);
116+
image_data.container.shape = nullptr;
117+
}
118+
if (image_data.container.strides)
119+
{
120+
cucim_free(image_data.container.strides);
121+
image_data.container.strides = nullptr;
122+
}
123+
if (image_data.shm_name)
124+
{
125+
cucim_free(image_data.shm_name);
126+
image_data.shm_name = nullptr;
127+
}
113128
image_format->formats[0].image_parser.close(&handle);
114129

115130
auto end = std::chrono::high_resolution_clock::now();

cucim.code-workspace

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
"cwd": "${workspaceFolder}",
162162
"env": {
163163
// Workaround the environment variable issue: https://github.com/microsoft/vscode/issues/121470
164-
"PATH": "${env:PATH}"
164+
"PATH": "${env:HOME}/.local/bin:${env:PATH}"
165165
}
166166
},
167167
"presentation": {

python/cucim/tests/util/gen_image.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ def gen(self):
8282
image_size=image_size,
8383
tile_size=tile_size,
8484
compression=compression)
85+
self.logger.info(' Generated %s...', image_path)
8586
results.append(image_path)
8687

8788
self.logger.info('[Finished] Dataset generation')

test_data/README.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@
22

33
Symbolic links for for this folder is available under the following subfolders:
44

5-
- `cpp/plugins/cucim.kit.cuslide
5+
- `cpp/plugins/cucim.kit.cuslide`
66
- `python/cucim`
77

8+
## Generated Test Data (`test_data/generated` folder)
9+
10+
Generated by executing `gen_images.sh`.
11+
12+
- tiff_stripe_32x32_16.tif
13+
- tiff_stripe_4096x4096_256.tif
14+
815
## Private Test Data (`test_data/private` folder)
916

1017
- `generic_tiff_000.tif` : 256x256 multi-resolution/tiled TIF conversion of `TUPAC-TR-467.svs` from the dataset
1118
of [Tumor Proliferation Assessment Challenge 2016](http://tupac.tue-image.nl/node/3) (TUPAC16 | MICCAI Grand Challenge) which are publicly
1219
available through [The Cancer Genome Atlas (TCGA)](https://www.cancer.gov/about-nci/organization/ccg/research/structural-genomics/tcga) under [CC BY 3.0 License](https://creativecommons.org/licenses/by/3.0/)
13-
- `philips_tiff_000.tif` : `patient_100_node_0.tif` from the [Camelyon16 challenge data](https://drive.google.com/drive/folders/0BzsdkU4jWx9BUzVXeUg0dUNOR1U) (another [link](https://camelyon17.grand-challenge.org/Data/)) which is under [CC0 License](https://choosealicense.com/licenses/cc0-1.0/)
20+
- `philips_tiff_000.tif` : `normal_042.tif` from the [Camelyon16 challenge data](ftp://parrot.genomics.cn/gigadb/pub/10.5524/100001_101000/100439/CAMELYON16/training/normal/normal_042.tif) (another [link](https://camelyon17.grand-challenge.org/Data/)) which is under [CC0 License](https://choosealicense.com/licenses/cc0-1.0/)

0 commit comments

Comments
 (0)