boost 单元测试模板会产生臃肿的代码。如何避免这种情况?
我使用 boost 单元测试库完成了大约一百个简单测试。我不仅得到了很长的编译时间(大约半分钟),而且生成的可执行文件的大小也变得非常大 - 仅仅一百个简单测试就达到 4MB。如果不使用 boost 测试来完成测试,则可执行文件大小仅为 120kB。
我怎样才能减轻肿胀?这个问题只是因为兴趣,而不是我需要测试代码才能具有闪亮的性能:)
调试信息已经被剥离。我已经尝试了所有优化选项但没有成功。
编辑:
每个测试基本上如下:
PlainOldDataObject a, b;
a = { ... initial_data ... };
a = some_simple_operation(a);
b = { ... expected_result ... };
BOOST_CHECK(std::memcmp(&a, &b, sizeof(PlainOldDataObject)) == 0);
I have about a hundred of simple tests done with boost unit test library. Not only I get very long compile times (in order of half a minute), but the size of the resulting executable gets really big - 4MB for just a hundred simple tests. If the tests are done without using boost test, the executable size is a mere 120kB.
How can I lessen the bloat? This question is just because of interest, not that I need test code to have shiny performance :)
The debugging info is already stripped. I've tried all optimization options with no success.
EDIT:
Each test is basically as follows:
PlainOldDataObject a, b;
a = { ... initial_data ... };
a = some_simple_operation(a);
b = { ... expected_result ... };
BOOST_CHECK(std::memcmp(&a, &b, sizeof(PlainOldDataObject)) == 0);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
I. 您采用哪种使用方式?如果您使用单元测试框架的单头变体,您应该切换到离线变体(静态或动态)
II。如果您怀疑 BOOST_AUTO_TEST_CASE 宏有问题,您有多种选择:
放弃每个测试用例策略的单个断言并使用多个“主题”测试用例。我个人认为这是可以接受的。
使用手动测试用例注册。您可以使用自己的宏将其自动化,以避免繁琐的重复。
分割成多个测试文件。您可能会看到至少一些编译时间的改进(也可能不会)。
三.如果您怀疑 BOOST_CHECK 语句,您无能为力,但我会很惊讶地看到它们产生如此多的开销。也许你应该进一步调查。
I. Which usage variant do you employ? If you employ single header variant of unit test framework, you should switch to offline variant (either static or dynamic)
II. If you suspect that BOOST_AUTO_TEST_CASE macro is at fault, you have several options:
Give up single assertion per test case policy and use number of "themed" test cases. I personally find this acceptable.
Use manual test case registration. You can probably automate it with your own macro to avoid tedious repetition.
Split into multiple test files. You might see at least some compilation time improvement (or might not).
III. If you suspect BOOST_CHECK statements, there is not much you can do, but I'd be rather surprised to see this much overhead from them. Maybe you should investigate further.
尝试使用 Loki 库 代替:它还有许多常用的通用组件(包括静态断言宏、类似于 BOOST_CHECK)。
Loki 被认为是轻量级的,但比 boost 更强大,因为它使用基于策略的方法来进行类设计。然而,它没有那么多种类的工具,只有最常见的工具:智能指针、小对象分配器、元编程助手、工厂和其他一些工具。但是,如果您不需要任何像 ex 的序列化这样的可怕的 boost 库,您可能会发现它可以满足您的需求。
Try using Loki library instead: it also has many common-used generic components (including a static assertion macro, similar to BOOST_CHECK).
Loki is known to be lightweight, but even more powerful than boost is, because it uses a policy-based approach to class design. However, it doesn't have all that variety of tools, only most common ones: smart pointers, small object allocator, meta-programming helpers, factories and a few others. But if you don't need any of those monstrous boost libs like serialization for ex, you may find it satisfying for your needs.