File: stress_test.h

package info (click to toggle)
libcds 2.3.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 15,564 kB
  • sloc: cpp: 135,002; ansic: 7,218; perl: 243; sh: 237; makefile: 6
file content (190 lines) | stat: -rw-r--r-- 6,033 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (c) 2006-2018 Maxim Khizhinsky
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)

#ifndef CDSTEST_STRESS_TEST_H
#define CDSTEST_STRESS_TEST_H

#include <map>
#include <cds_test/fixture.h>
#include <cds_test/thread.h>

namespace cds_test {

    // Test configuration
    class config
    {
    public:
        std::string get( const char * pszParamName, const char * pszDefVal = NULL ) const
        {
            std::string strParamName( pszParamName );
            auto it = m_Cfg.find( strParamName );
            if ( it == m_Cfg.end())
                return std::string( pszDefVal ); // param not found -> returns default value
            return it->second;
        }

        int get_int( const char * pszParamName, int defVal = 0 ) const
        {
            std::string strParamName( pszParamName );
            cfg_map::const_iterator it = m_Cfg.find( strParamName );
            if ( it == m_Cfg.end())
                return defVal; // param not found -> returns default value
            return atoi( it->second.c_str());
        }

        unsigned int get_uint( const char * pszParamName, unsigned int defVal = 0 ) const
        {
            std::string strParamName( pszParamName );
            cfg_map::const_iterator it = m_Cfg.find( strParamName );
            if ( it == m_Cfg.end())
                return defVal; // param not found -> returns default value
            return static_cast<unsigned int>( strtoul( it->second.c_str(), NULL, 10 ));
        }

        long get_long( const char * pszParamName, long defVal = 0 ) const
        {
            std::string strParamName( pszParamName );
            cfg_map::const_iterator it = m_Cfg.find( strParamName );
            if ( it == m_Cfg.end())
                return defVal; // param not found -> returns default value
            return strtol( it->second.c_str(), NULL, 10 );
        }

        unsigned long get_ulong( const char * pszParamName, unsigned long defVal = 0 ) const
        {
            std::string strParamName( pszParamName );
            cfg_map::const_iterator it = m_Cfg.find( strParamName );
            if ( it == m_Cfg.end())
                return defVal; // param not found -> returns default value
            return strtoul( it->second.c_str(), NULL, 10 );
        }

        size_t get_size_t( const char * pszParamName, size_t defVal = 0 ) const
        {
            std::string strParamName( pszParamName );
            cfg_map::const_iterator it = m_Cfg.find( strParamName );
            if ( it == m_Cfg.end())
                return defVal; // param not found -> returns default value
            return static_cast<size_t>( strtoul( it->second.c_str(), NULL, 10 ));
        }

        bool get_bool( const char * pszParamName, bool defVal = false ) const
        {
            std::string strParamName( pszParamName );
            cfg_map::const_iterator it = m_Cfg.find( strParamName );
            if ( it == m_Cfg.end())
                return defVal; // param not found -> returns default value
            return !( it->second.empty()
                   || it->second == "0"
                   || it->second == "false"
                   || it->second == "no"
                   );
        }

    private:
        typedef std::map< std::string, std::string >  cfg_map;
        cfg_map m_Cfg; // map param_name => value

        friend class config_file;
    };

    class property_stream
    {
    public:
        static std::string const& stat_prefix();
        static void set_stat_prefix( char const* prefix );
    };

    struct stat_prefix
    {
        char const* prefix_;

        stat_prefix()
            : prefix_( nullptr )
        {}

        stat_prefix( char const* prefix )
            : prefix_( prefix )
        {}
    };

    static inline property_stream& operator<<( property_stream& s, stat_prefix&& prefix )
    {
        s.set_stat_prefix( prefix.prefix_ );
        return s;
    }

    template <typename T>
    static inline property_stream& operator <<( property_stream& s, std::pair<char const*, T > prop )
    {
        std::stringstream ss;
        ss << prop.second;
        ::testing::Test::RecordProperty( prop.first, ss.str().c_str());
        return s;
    }

    template <typename T>
    static inline property_stream& operator <<( property_stream& s, std::pair<std::string, T > prop )
    {
        std::stringstream ss;
        ss << prop.second;
        ::testing::Test::RecordProperty( prop.first.c_str(), ss.str().c_str());
        return s;
    }

    static inline property_stream& operator <<( property_stream& s, std::pair<char const*, std::chrono::milliseconds > prop )
    {
        std::stringstream ss;
        ss << prop.second.count();
        ::testing::Test::RecordProperty( prop.first, ss.str().c_str());
        return s;
    }

#define CDSSTRESS_STAT_OUT_( name, val ) std::make_pair( name, val )
#define CDSSTRESS_STAT_OUT( s, field ) CDSSTRESS_STAT_OUT_( property_stream::stat_prefix() + "." #field, s.field.get())

    class stress_fixture : public fixture
    {
        typedef fixture base_class;

    protected:
        stress_fixture()
            : m_thread_pool( *this )
        {}

        //static void SetUpTestCase();
        //static void TearDownTestCase();
        void TearDown()
        {
            print_hp_stat();
            base_class::TearDown();
        }

        thread_pool& get_pool()
        {
            return m_thread_pool;
        }

        static void print_hp_stat();

    public:
        static config const& get_config( char const * slot );
        static config const& get_config( std::string const& slot );

        static std::vector<std::string> load_dictionary();

        static property_stream& propout();

    private:
        thread_pool     m_thread_pool;
    };


    // Internal functions
    void init_config( int argc, char **argv );

} // namespace cds_test

#endif // CDSTEST_FIXTURE_H