b4a7e5ff36000241901774d3b35283c84bd13845
[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-2016\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     struct property_stream\r
38     {};\r
39 \r
40     /*static*/ property_stream& stress_fixture::propout()\r
41     {\r
42         static property_stream s_prop_stream;\r
43         return s_prop_stream;\r
44     }\r
45 \r
46     /*static*/ std::vector<std::string> stress_fixture::load_dictionary()\r
47     {\r
48         std::vector<std::string> arrString;\r
49 \r
50         std::ifstream s;\r
51         char const* filename = "./dictionary.txt";\r
52         s.open( filename );\r
53         if ( !s.is_open()) {\r
54             std::cerr << "WARNING: Cannot open test file " << filename << std::endl;\r
55             return arrString;\r
56         }\r
57 \r
58         std::string line;\r
59         std::getline( s, line );\r
60 \r
61         arrString.reserve( std::stoul( line ));\r
62 \r
63         while ( !s.eof()) {\r
64             std::getline( s, line );\r
65             if ( !line.empty())\r
66                 arrString.push_back( std::move( line ));\r
67         }\r
68 \r
69         s.close();\r
70 \r
71         return arrString;\r
72     }\r
73 \r
74 \r
75     static int s_nDetailLevel = 0;\r
76 \r
77     /*static*/ void stress_fixture::init_detail_level( int argc, char **argv )\r
78     {\r
79         bool found = false;\r
80         for ( int i = 0; i < argc; ++i ) {\r
81             char * arg = argv[i];\r
82             char * eq = strchr( arg, '=' );\r
83             if ( eq ) {\r
84                 if ( strncmp( arg, "--detail_level", eq - arg ) == 0 || strncmp( arg, "--detail-level", eq - arg ) == 0 ) {\r
85                     s_nDetailLevel = atoi( eq + 1 );\r
86                     found = true;\r
87                 }\r
88             }\r
89         }\r
90 \r
91         if ( !found ) {\r
92             // Get detail level from environment variable\r
93             char const * env = getenv( "CDSTEST_DETAIL_LEVEL" );\r
94             if ( env && env[0] )\r
95                 s_nDetailLevel = atoi( env );\r
96         }\r
97 \r
98         std::cout << "Stress test detail level=" << s_nDetailLevel << std::endl;\r
99     }\r
100 \r
101     /*static*/ bool stress_fixture::check_detail_level( int nLevel )\r
102     {\r
103         if ( nLevel <= s_nDetailLevel )\r
104             return true;\r
105 \r
106         std::cout << "Skipped (detail level=" << nLevel << ")" << std::endl;\r
107         propout() << std::make_pair( "skipped", 1 );\r
108         return false;\r
109     }\r
110 \r
111 } // namespace\r