1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
7 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
9 \*---------------------------------------------------------------------------*/
10 /*---------------------------------------------------------------------------*\
13 * This library is free software; you can redistribute it and/or modify it *
14 * under the terms of the GNU Library General Public License as published *
15 * by the Free Software Foundation, version 2. *
17 * This library is distributed in the hope that it will be useful, but *
18 * WITHOUT ANY WARRANTY; without even the implied warranty of *
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
20 * Library General Public License for more details. *
22 * You should have received a copy of the GNU Library General Public *
23 * License along with this library; if not, write to the Free Software *
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
26 \*---------------------------------------------------------------------------*/
27 /*---------------------------------------------------------------------------*\
35 \*---------------------------------------------------------------------------*/
37 #include <UnitTest++.h>
39 // Unit tests for Performance monitoring code
41 #include "OSGConfig.h"
42 #include "OSGPerfMonitor.h"
43 #include "OSGPerfMonitorGuard.h"
45 #include <boost/filesystem/operations.hpp>
47 namespace bf
= boost::filesystem
;
49 SUITE(PerfMonitorTests
)
52 struct PerfFileFixture
56 OSG::PerfMonitor::the()->reset();
57 OSG::PerfMonitor::the()->enable(false);
59 test_file
= bf::path("test_perf.data");
60 bf::remove(test_file
);
65 bf::remove(test_file
);
72 TEST_FIXTURE(PerfFileFixture
, TestInitialization
)
74 OSG::PerfMonitor::the()->reset();
75 CHECK_EQUAL(OSG::PerfMonitor::the()->mSamples
.size(), 0);
76 CHECK_EQUAL(OSG::PerfMonitor::the()->getEnabled(), false);
77 CHECK_EQUAL((void*)OSG::PerfMonitor::the()->mOutFile
, (void*)NULL
);
81 TEST_FIXTURE(PerfFileFixture
, TestSample
)
83 OSG::PerfMonitor::the()->reset();
84 CHECK_EQUAL(OSG::PerfMonitor::the()->mSamples
.size(), 0);
85 OSG::PerfMonitor::the()->sample(OSG::PerfMonitorBase::MARK
, "test");
86 CHECK_EQUAL(OSG::PerfMonitor::the()->mSamples
.size(), 0);
88 OSG::PerfMonitor::the()->enable(true);
89 OSG::PerfMonitor::the()->sample(OSG::PerfMonitorBase::MARK
, "test");
90 CHECK_EQUAL(OSG::PerfMonitor::the()->mSamples
.size(), 1);
93 TEST_FIXTURE(PerfFileFixture
, TestPerfGuards
)
95 OSG::PerfMonitor::the()->reset();
96 OSG::PerfMonitor::the()->enable(true);
98 CHECK(OSG::PerfMonitor::the()->mSamples
.size() == 0);
102 OSG::PerfMonitorGuard
g("test_scope");
103 CHECK_EQUAL(1, OSG::PerfMonitor::the()->mSamples
.size());
105 CHECK_EQUAL(2, OSG::PerfMonitor::the()->mSamples
.size());
107 // Test nested scopes
109 OSG::PerfMonitorGuard
g("test_outer_scope");
110 CHECK_EQUAL(3, OSG::PerfMonitor::the()->mSamples
.size());
112 OSG::PerfMonitorGuard
g("test_inner_scope");
113 CHECK_EQUAL(4, OSG::PerfMonitor::the()->mSamples
.size());
115 CHECK_EQUAL(5, OSG::PerfMonitor::the()->mSamples
.size());
117 CHECK_EQUAL(6, OSG::PerfMonitor::the()->mSamples
.size());
120 TEST_FIXTURE(PerfFileFixture
, TestPerfOutput
)
122 OSG::PerfMonitor::the()->reset();
123 OSG::PerfMonitor::the()->enable(true);
125 CHECK(!bf::exists(test_file
));
127 OSG::PerfMonitor::the()->setOutputFile(test_file
.native_file_string());
128 CHECK(bf::exists(test_file
));
130 OSG::PerfMonitor::the()->sample(OSG::PerfMonitorBase::ENTER
, "test", 21.0);
131 OSG::PerfMonitor::the()->flushOutput();
132 OSG::PerfMonitor::the()->setOutputFile("");
133 CHECK(bf::exists(test_file
));
135 // TODO: Add test to look at the file output
138 TEST_FIXTURE(PerfFileFixture
, TestFlushing
)
140 OSG::PerfMonitor::the()->reset();
141 OSG::PerfMonitor::the()->enable(true);
142 OSG::PerfMonitor::the()->setOutputFlushRate(10);
143 CHECK_EQUAL(0, OSG::PerfMonitor::the()->mSamples
.size());
145 // Check that size grows
146 for(unsigned i
=1; i
<5; i
++)
148 OSG::PerfMonitor::the()->sample(OSG::PerfMonitorBase::MARK
, "test");
149 OSG::PerfMonitor::the()->updateFrame(); // Adds frame sample
150 CHECK_EQUAL(i
*2, OSG::PerfMonitor::the()->mSamples
.size());
153 // Check that this one causes a flush
154 OSG::PerfMonitor::the()->sample(OSG::PerfMonitorBase::MARK
, "test");
155 CHECK_EQUAL(9, OSG::PerfMonitor::the()->mSamples
.size());
156 OSG::PerfMonitor::the()->updateFrame();
157 CHECK_EQUAL(0, OSG::PerfMonitor::the()->mSamples
.size());
162 TEST_FIXTURE(FileFixture, CreateOSBFile)
164 OSG::NodePtr n = OSG::Node::create();
166 CHECK(!bf::exists(test_file));
167 OSG::SceneFileHandler::the()->write(n, test_file.native_file_string().c_str());
168 CHECK(bf::exists(test_file));
171 OSG::SceneFileHandler::the()->read(test_file.native_file_string().c_str());
172 CHECK(new_n != OSGNullFC);
175 TEST_FIXTURE(FileFixture, CreateOSBTree)
178 OSG::NodePtr base_node = OSG::Node::create();
179 base_node->setCore(OSG::Group::create());
180 OSG::NodePtr child_node = OSG::Node::create();
181 child_node->setCore(OSG::Group::create());
183 base_node->addChild(child_node);
185 CHECK(!bf::exists(test_file));
186 OSG::SceneFileHandler::the()->write(base_node, test_file.native_file_string().c_str());
187 CHECK(bf::exists(test_file));
190 OSG::SceneFileHandler::the()->read(test_file.native_file_string().c_str());
191 CHECK(new_n != OSGNullFC);
192 CHECK(new_n->getCore() != OSGNullFC);
193 CHECK(new_n->getNChildren() == 1);
194 CHECK(new_n->getChild(0) != OSGNullFC);
195 CHECK(new_n->getChild(0)->getCore() != OSGNullFC);