Skip to content

Commit c3e758d

Browse files
committed
Test performance of vsg::cast<> implementations.
1 parent ffef044 commit c3e758d

File tree

4 files changed

+121
-1
lines changed

4 files changed

+121
-1
lines changed

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ add_subdirectory(vsgreallocations)
33
add_subdirectory(vsgtriangles)
44
add_subdirectory(vsgunicode)
55
add_subdirectory(vsgperformance)
6+
add_subdirectory(vsgcast)

tests/vsgcast/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
set(SOURCES
2+
vsgcast.cpp
3+
)
4+
5+
add_executable(vsgcast ${SOURCES})
6+
7+
target_link_libraries(vsgcast vsg::vsg)
8+
9+
if (vsgXchange_FOUND)
10+
target_compile_definitions(vsgcast PRIVATE vsgXchange_FOUND)
11+
target_link_libraries(vsgcast vsgXchange::vsgXchange)
12+
endif()
13+
14+
install(TARGETS vsgcast RUNTIME DESTINATION bin)

tests/vsgcast/vsgcast.cpp

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include <vsg/all.h>
2+
3+
#ifdef vsgXchange_FOUND
4+
# include <vsgXchange/all.h>
5+
#endif
6+
7+
#include <algorithm>
8+
#include <chrono>
9+
#include <iostream>
10+
#include <thread>
11+
12+
size_t traverseChildren(const vsg::Group* group)
13+
{
14+
size_t count = group->children.size();
15+
for(auto& child : group->children)
16+
{
17+
if (auto child_group = child.cast<vsg::Group>()) count += traverseChildren(child_group);
18+
}
19+
return count;
20+
}
21+
22+
int main(int argc, char** argv)
23+
{
24+
// set up defaults and read command line arguments to override them
25+
vsg::CommandLine arguments(&argc, argv);
26+
27+
if (arguments.read("--args")) std::cout << arguments << std::endl;
28+
29+
// if we want to redirect std::cout and std::cerr to the vsg::Logger call vsg::Logger::redirect_stdout()
30+
if (arguments.read({"--redirect-std", "-r"})) vsg::Logger::instance()->redirect_std();
31+
32+
// set up vsg::Options to pass in filepaths, ReaderWriters and other IO related options to use when reading and writing files.
33+
auto options = vsg::Options::create();
34+
options->sharedObjects = vsg::SharedObjects::create();
35+
options->fileCache = vsg::getEnv("VSG_FILE_CACHE");
36+
options->paths = vsg::getEnvPaths("VSG_FILE_PATH");
37+
38+
#ifdef vsgXchange_all
39+
// add vsgXchange's support for reading and writing 3rd party file formats
40+
options->add(vsgXchange::all::create());
41+
#endif
42+
43+
options->readOptions(arguments);
44+
45+
size_t iterationCount = arguments.value<size_t>(100000, "-i");
46+
47+
auto root = vsg::Group::create();
48+
49+
vsg::Path path;
50+
51+
// read any vsg files
52+
for (int i = 1; i < argc; ++i)
53+
{
54+
vsg::Path filename = arguments[i];
55+
path = vsg::filePath(filename);
56+
57+
auto object = vsg::read(filename, options);
58+
if (auto node = object.cast<vsg::Node>())
59+
{
60+
root->addChild(node);
61+
}
62+
else if (object)
63+
{
64+
std::cout << "Unable to view object of type " << object->className() << std::endl;
65+
}
66+
else
67+
{
68+
std::cout << "Unable to load file " << filename << std::endl;
69+
}
70+
}
71+
72+
if (root->children.empty())
73+
{
74+
for(size_t i = 0; i<1000; ++i)
75+
{
76+
if (i%3==0) root->addChild(vsg::Node::create());
77+
else if (i%3==1) root->addChild(vsg::VertexDraw::create());
78+
else if (i%3==2) root->addChild(vsg::Geometry::create());
79+
}
80+
}
81+
82+
if (VSG_USE_dynamic_cast)
83+
{
84+
std::cout<<"Using dynamic_cast<> based RTTI vsg::Object::cast<> implementation."<<std::endl;
85+
}
86+
else
87+
{
88+
std::cout<<"Using typeid() based RTTI vsg::Object::cast<> implementation - faster than using dynamic_cast<>."<<std::endl;
89+
}
90+
91+
auto startTime = vsg::clock::now();
92+
93+
size_t count = 0;
94+
95+
for(size_t i=0; i<iterationCount; ++i)
96+
{
97+
count += (1 + traverseChildren(root));
98+
}
99+
100+
auto time = std::chrono::duration<float, std::chrono::seconds::period>(vsg::clock::now() - startTime).count();
101+
std::cout << "Time " << time*1000.0 << "ms" << " count = " << count << std::endl;
102+
std::cout << "Cast and traverse per second " << static_cast<size_t>((static_cast<double>(count)/time))<< std::endl;
103+
104+
// clean up done automatically thanks to ref_ptr<>
105+
return 0;
106+
}

tests/vsgperformance/vsgperformance.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ int main(int argc, char** argv)
189189
auto maxTime = arguments.value(std::numeric_limits<double>::max(), "--max-time");
190190

191191
if (arguments.read("--ThreadLogger")) vsg::Logger::instance() = vsg::ThreadLogger::create();
192-
if (int log_level = 0; arguments.read("--log-level", log_level)) vsg::Logger::instance()->level = vsg::Logger::Level(log_level);
193192
auto numFrames = arguments.value(-1, "-f");
194193
auto pathFilename = arguments.value<vsg::Path>("", "-p");
195194
auto loadLevels = arguments.value(0, "--load-levels");

0 commit comments

Comments
 (0)