1497cc7d9cabab249e540566ce1a642a3d351dc6
[libcds.git] / test / stress / framework / stress_test.cpp
1 /*\r
2     This file is a part of libcds - Concurrent Data Structures library\r
3 \r
4     (C) Copyright Maxim Khizhinsky (libcds.dev@gmail.com) 2006-2017\r
5 \r
6     Source code repo: http://github.com/khizmax/libcds/\r
7     Download: http://sourceforge.net/projects/libcds/files/\r
8 \r
9     Redistribution and use in source and binary forms, with or without\r
10     modification, are permitted provided that the following conditions are met:\r
11 \r
12     * Redistributions of source code must retain the above copyright notice, this\r
13     list of conditions and the following disclaimer.\r
14 \r
15     * Redistributions in binary form must reproduce the above copyright notice,\r
16     this list of conditions and the following disclaimer in the documentation\r
17     and/or other materials provided with the distribution.\r
18 \r
19     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
20     AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
21     IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
22     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
23     FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
24     DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
25     SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
26     CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
27     OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
28     OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
29 */\r
30 \r
31 #include <fstream>\r
32 #include <iostream>\r
33 #include <cds_test/stress_test.h>\r
34 \r
35 namespace cds_test {\r
36 \r
37     static std::string s_stat_prefix( "stat" );\r
38 \r
39     /*static*/ std::string const& property_stream::stat_prefix()\r
40     {\r
41         return s_stat_prefix;\r
42     }\r
43 \r
44     /*static*/ void property_stream::set_stat_prefix( char const* prefix )\r
45     {\r
46         if ( prefix && prefix[0] )\r
47             s_stat_prefix = prefix;\r
48         else\r
49             s_stat_prefix = "stat";\r
50     }\r
51 \r
52     /*static*/ property_stream& stress_fixture::propout()\r
53     {\r
54         static property_stream s_prop_stream;\r
55         return s_prop_stream;\r
56     }\r
57 \r
58     /*static*/ std::vector<std::string> stress_fixture::load_dictionary()\r
59     {\r
60         std::vector<std::string> arrString;\r
61 \r
62         std::ifstream s;\r
63         char const* filename = "./dictionary.txt";\r
64         s.open( filename );\r
65         if ( !s.is_open()) {\r
66             std::cerr << "WARNING: Cannot open test file " << filename << std::endl;\r
67             return arrString;\r
68         }\r
69 \r
70         std::string line;\r
71         std::getline( s, line );\r
72 \r
73         arrString.reserve( std::stoul( line ));\r
74 \r
75         while ( !s.eof()) {\r
76             std::getline( s, line );\r
77             if ( !line.empty())\r
78                 arrString.push_back( std::move( line ));\r
79         }\r
80 \r
81         s.close();\r
82 \r
83         return arrString;\r
84     }\r
85 \r
86 \r
87     static int s_nDetailLevel = 0;\r
88 \r
89     /*static*/ void stress_fixture::init_detail_level( int argc, char **argv )\r
90     {\r
91         bool found = false;\r
92         for ( int i = 0; i < argc; ++i ) {\r
93             char * arg = argv[i];\r
94             char * eq = strchr( arg, '=' );\r
95             if ( eq ) {\r
96                 if ( strncmp( arg, "--detail_level", eq - arg ) == 0 || strncmp( arg, "--detail-level", eq - arg ) == 0 ) {\r
97                     s_nDetailLevel = atoi( eq + 1 );\r
98                     found = true;\r
99                 }\r
100             }\r
101         }\r
102 \r
103         if ( !found ) {\r
104             // Get detail level from environment variable\r
105             char const * env = getenv( "CDSTEST_DETAIL_LEVEL" );\r
106             if ( env && env[0] )\r
107                 s_nDetailLevel = atoi( env );\r
108         }\r
109 \r
110         std::cout << "Stress test detail level=" << s_nDetailLevel << std::endl;\r
111     }\r
112 \r
113     /*static*/ bool stress_fixture::check_detail_level( int nLevel )\r
114     {\r
115         if ( nLevel <= s_nDetailLevel )\r
116             return true;\r
117 \r
118         std::cout << "Skipped (detail level=" << nLevel << ")" << std::endl;\r
119         propout() << std::make_pair( "skipped", 1 );\r
120         return false;\r
121     }\r
122 \r
123 } // namespace\r