[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] (no subject)
From: |
Greg Chicares |
Subject: |
[lmi-commits] (no subject) |
Date: |
Sat, 30 Jul 2016 23:06:08 +0000 (UTC) |
branch: master
commit c363b00e4cf4a7536e65ff0851656a2e3068bd77
Author: Gregory W. Chicares <address@hidden>
Date: Sat Jul 30 23:05:04 2016 +0000
Add unit test for class-instance caching
The speed test shows little benefit from caching. That is not
astonishing because the class it tests merely reads a file, without
the costly processing that motivated caching in the first place.
However, if this line:
std::time_t const write_time = fs::last_write_time(filename);
in the caching implementation is experimentally replaced with
std::time_t const write_time = 0;
then caching improves the speed by several orders of magnitude.
---
Makefile.am | 9 ++++
cache_file_reads_test.cpp | 121 +++++++++++++++++++++++++++++++++++++++++++++
objects.make | 7 +++
3 files changed, 137 insertions(+)
diff --git a/Makefile.am b/Makefile.am
index 42d9b1c..2c35293 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -89,6 +89,7 @@ TESTS = \
test_any_member \
test_assert_lmi \
test_authenticity \
+ test_cache_file_reads \
test_calendar_date \
test_callback \
test_comma_punct \
@@ -576,6 +577,14 @@ test_authenticity_CXXFLAGS = $(AM_CXXFLAGS)
test_authenticity_LDADD = \
$(BOOST_LIBS)
+test_cache_file_reads_SOURCES = \
+ $(common_test_objects) \
+ cache_file_reads_test.cpp \
+ timer.cpp
+test_cache_file_reads_CXXFLAGS = $(AM_CXXFLAGS)
+test_cache_file_reads_LDADD = \
+ $(BOOST_LIBS)
+
test_calendar_date_SOURCES = \
$(common_test_objects) \
calendar_date.cpp \
diff --git a/cache_file_reads_test.cpp b/cache_file_reads_test.cpp
new file mode 100755
index 0000000..33817ce
--- /dev/null
+++ b/cache_file_reads_test.cpp
@@ -0,0 +1,121 @@
+// Cache class instances constructed from files--unit test.
+//
+// Copyright (C) 2016 Gregory W. Chicares.
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 2 as
+// published by the Free Software Foundation.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software Foundation,
+// Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+//
+// http://savannah.nongnu.org/projects/lmi
+// email: <address@hidden>
+// snail: Chicares, 186 Belle Woods Drive, Glastonbury CT 06033, USA
+
+#include "pchfile.hpp"
+
+#include "cache_file_reads.hpp"
+
+#include "istream_to_string.hpp"
+#include "miscellany.hpp" // ios_in_binary()
+#include "test_tools.hpp"
+#include "timer.hpp"
+
+#include <boost/filesystem/exception.hpp>
+
+#include <fstream>
+
+class X
+ :public cache_file_reads<X>
+{
+ public:
+ X()
+ {}
+ X(std::string const& filename)
+ {
+ std::ifstream ifs(filename, ios_in_binary());
+ istream_to_string(ifs, s_);
+ }
+
+ std::string const& s() const
+ {
+ return s_;
+ }
+
+ private:
+ std::string s_;
+};
+
+class cache_file_reads_test
+{
+ public:
+ static void test()
+ {
+ test_preconditions();
+ assay_speed();
+ }
+
+ private:
+ static void test_preconditions();
+ static void assay_speed();
+
+ static void mete_uncached();
+ static void mete_cached ();
+};
+
+void cache_file_reads_test::test_preconditions()
+{
+ // X() and X(filename) are required.
+ X x0;
+ X x1("sample.ill");
+
+ // The cache is accessible with or without an object.
+ BOOST_TEST_EQUAL
+ (x0.read_from_cache("sample.ill")->s()
+ ,X::read_from_cache("sample.ill")->s()
+ );
+
+ // The file must exist.
+ BOOST_TEST_THROW
+ (X::read_from_cache("no_such_file")
+ ,boost::filesystem::filesystem_error
+ ,lmi_test::what_regex("no_such_file.*cannot find the file specified")
+ );
+}
+
+void cache_file_reads_test::assay_speed()
+{
+ std::cout
+ << "\n Speed tests..."
+ << "\n Uncached: " << TimeAnAliquot(mete_uncached)
+ << "\n Cached : " << TimeAnAliquot(mete_cached )
+ << std::endl
+ ;
+}
+
+void cache_file_reads_test::mete_uncached()
+{
+ X const x("sample.ill");
+ volatile std::string::size_type z = x.s().size();
+}
+
+void cache_file_reads_test::mete_cached()
+{
+ X const& x(*X::read_from_cache("sample.ill"));
+ volatile std::string::size_type z = x.s().size();
+}
+
+int test_main(int, char*[])
+{
+ cache_file_reads_test::test();
+
+ return EXIT_SUCCESS;
+}
+
diff --git a/objects.make b/objects.make
index b806edc..54f785b 100644
--- a/objects.make
+++ b/objects.make
@@ -398,6 +398,7 @@ unit_test_targets := \
any_member_test \
assert_lmi_test \
authenticity_test \
+ cache_file_reads_test \
calendar_date_test \
callback_test \
comma_punct_test \
@@ -515,6 +516,12 @@ authenticity_test$(EXEEXT): \
system_command.o \
system_command_non_wx.o \
+cache_file_reads_test$(EXEEXT): \
+ $(boost_filesystem_objects) \
+ $(common_test_objects) \
+ cache_file_reads_test.o \
+ timer.o \
+
calendar_date_test$(EXEEXT): \
$(common_test_objects) \
calendar_date.o \