File: spsc_queue_stress_test.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (237 lines) | stat: -rw-r--r-- 5,567 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//  Copyright (C) 2011-2013 Tim Blechmann
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)

#include <boost/lockfree/spsc_queue.hpp>

#define BOOST_TEST_MAIN
#ifdef BOOST_LOCKFREE_INCLUDE_TESTS
#    include <boost/test/included/unit_test.hpp>
#else
#    include <boost/test/unit_test.hpp>
#endif

#include <array>
#include <cstdint>
#include <iostream>
#include <thread>

#include "test_common.hpp"
#include "test_helpers.hpp"

using namespace boost;
using namespace boost::lockfree;
using namespace std;

#ifndef BOOST_LOCKFREE_STRESS_TEST
static const long nodes_per_thread = 100000;
#else
static const long nodes_per_thread = 100000000;
#endif

struct spsc_queue_tester
{
    spsc_queue< int, capacity< 128 > > sf;

    std::atomic< long > spsc_queue_cnt, received_nodes;

// In VxWorks one RTP just supports 65535 objects
#ifndef __VXWORKS__
    static_hashed_set< int, 1 << 16 > working_set;
#else
    static_hashed_set< int, 1 << 15 > working_set;
#endif

    spsc_queue_tester( void ) :
        spsc_queue_cnt( 0 ),
        received_nodes( 0 )
    {}

    void add( void )
    {
        for ( size_t i = 0; i != nodes_per_thread; ++i ) {
            int id = generate_id< int >();
            working_set.insert( id );

            while ( sf.push( id ) == false ) {}

            ++spsc_queue_cnt;
        }
        running = false;
    }

    bool get_element( void )
    {
        int  data;
        bool success = sf.pop( data );

        if ( success ) {
            ++received_nodes;
            --spsc_queue_cnt;
            bool erased = working_set.erase( data );
            (void)erased;
            assert( erased );
            return true;
        } else
            return false;
    }

    std::atomic< bool > running;

    void get( void )
    {
        for ( ;; ) {
            bool success = get_element();
            if ( !running && !success )
                break;
        }

        while ( get_element() )
            ;
    }

    void run( void )
    {
        running = true;

        BOOST_TEST_REQUIRE( sf.empty() );

        std::thread reader( [ & ] {
            get();
        } );

        std::thread writer( [ & ] {
            add();
        } );
        cout << "reader and writer threads created" << endl;

        writer.join();
        cout << "writer threads joined. waiting for readers to finish" << endl;

        reader.join();

        BOOST_TEST_REQUIRE( received_nodes == nodes_per_thread );
        BOOST_TEST_REQUIRE( spsc_queue_cnt == 0 );
        BOOST_TEST_REQUIRE( sf.empty() );
        BOOST_TEST_REQUIRE( working_set.count_nodes() == 0 );
    }
};

BOOST_AUTO_TEST_CASE( spsc_queue_test_caching )
{
    std::shared_ptr< spsc_queue_tester > test1( new spsc_queue_tester );
    test1->run();
}

struct spsc_queue_tester_buffering
{
    spsc_queue< int, capacity< 128 > > sf;

    std::atomic< long > spsc_queue_cnt;

// In VxWorks one RTP just supports 65535 objects
#ifndef __VXWORKS__
    static_hashed_set< int, 1 << 16 > working_set;
#else
    static_hashed_set< int, 1 << 15 > working_set;
#endif

    std::atomic< size_t > received_nodes;

    spsc_queue_tester_buffering( void ) :
        spsc_queue_cnt( 0 ),
        received_nodes( 0 )
    {}

    static const size_t buf_size = 5;

    void add( void )
    {
        std::array< int, buf_size > input_buffer;
        for ( size_t i = 0; i != nodes_per_thread; i += buf_size ) {
            for ( size_t i = 0; i != buf_size; ++i ) {
                int id = generate_id< int >();
                working_set.insert( id );
                input_buffer[ i ] = id;
            }

            size_t pushed = 0;

            do {
                pushed += sf.push( input_buffer.data() + pushed, input_buffer.size() - pushed );
            } while ( pushed != buf_size );

            spsc_queue_cnt += buf_size;
        }
        running = false;
    }

    bool get_elements( void )
    {
        std::array< int, buf_size > output_buffer;

        size_t popd = sf.pop( output_buffer.data(), output_buffer.size() );

        if ( popd ) {
            received_nodes += size_t( popd );
            spsc_queue_cnt -= long( popd );

            for ( size_t i = 0; i != popd; ++i ) {
                bool erased = working_set.erase( output_buffer[ i ] );
                (void)erased;
                assert( erased );
            }

            return true;
        } else
            return false;
    }

    std::atomic< bool > running;

    void get( void )
    {
        for ( ;; ) {
            bool success = get_elements();
            if ( !running && !success )
                break;
        }

        while ( get_elements() )
            ;
    }

    void run( void )
    {
        running = true;

        std::thread reader( [ & ] {
            get();
        } );

        std::thread writer( [ & ] {
            add();
        } );

        cout << "reader and writer threads created" << endl;

        writer.join();
        cout << "writer threads joined. waiting for readers to finish" << endl;

        reader.join();

        BOOST_TEST_REQUIRE( received_nodes == nodes_per_thread );
        BOOST_TEST_REQUIRE( spsc_queue_cnt == 0 );
        BOOST_TEST_REQUIRE( sf.empty() );
        BOOST_TEST_REQUIRE( working_set.count_nodes() == 0 );
    }
};


BOOST_AUTO_TEST_CASE( spsc_queue_test_buffering )
{
    std::shared_ptr< spsc_queue_tester_buffering > test1( new spsc_queue_tester_buffering );
    test1->run();
}