lichao
2021-03-30 8cbb55f3066f71f5a4328193414d4555c87e96be
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
#include <stdio.h>
#include <string>
#include <vector>
#include <thread>
#include <atomic>
#include <boost/uuid/uuid_generators.hpp>
#include <boost/uuid/uuid_io.hpp>
#include "pubsub.h"
#include "defs.h"
#include "util.h"
 
template <class A, class B> struct IsSameType { static const bool value = false; };
template <class A> struct IsSameType<A,A> { static const bool value = true; };
 
 
BOOST_AUTO_TEST_CASE(ShmTest)
{
    const int size = 1024*1024;
    const std::string shm_name("ShmRemoveTest");
    SharedMemory m0(shm_name, size);
    int *p = m0.find_or_construct<int>("abc")(100);
    if (p) {
        printf("*p = %d\n", *p);
        ++*p;
    }
    {
        SharedMemory m1(shm_name, size);
        m1.Remove();
        // m1.Remove();
    }
    if (p) {
        printf("again *p = %d\n", *p);
        // ++*p;
    }
}
 
BOOST_AUTO_TEST_CASE(PubSubTest)
{
    const std::string shm_name("ShmPubSub");
    ShmRemover auto_remove(shm_name); //remove twice? in case of killed?
    SharedMemory shm(shm_name, 1024*1024*50);
    auto Avail = [&]() { return shm.get_free_memory(); };
    auto init_avail = Avail();
 
    BusManager bus(shm);
    bus.Start(1);
    std::this_thread::sleep_for(100ms);
 
    std::atomic<uint64_t> count(0);
    std::atomic<ptime> last_time(Now() - seconds(1));
    std::atomic<uint64_t> last_count(0);
 
    const uint64_t nmsg = 1000 * 100;
 
    const int timeout = 1000;
    auto Sub = [&](int id, const std::vector<std::string> &topics) {
        ShmMsgQueue client(shm, 8);
        client.Send(kBHBusQueueId, MakeSub(client.Id(), topics), timeout);
        for (int i = 0; i < nmsg * topics.size(); ++i) {
            BHMsg msg;
            if (client.Recv(msg, 1000)) {
                if (msg.type() != kMsgTypePublish) {
                    BOOST_CHECK(false);
                }
                DataPub pub;
                if (!pub.ParseFromString(msg.body())) {
                    BOOST_CHECK(false);
                }
                ++count;
                auto cur = Now();
                if (last_time.exchange(cur) < cur) {
                    std::cout << "time: " << cur;
                    printf("sub recv, total msg:%10ld, speed:[%8ld/s], used mem:%8ld \n",
                           count.load(), count - last_count.exchange(count), init_avail - Avail());
                }
                // printf("sub %2d recv: %s/%s\n", id, pub.topic().c_str(), pub.data().c_str());
 
            } else {
                printf("sub %2d recv timeout\n", id);
            }
 
        }
    };
    auto Pub = [&](const std::string &topic) {
        ShmMsgQueue provider(shm, 0);
        for (int i = 0; i < nmsg; ++i) {
            std::string data = topic + std::to_string(i);
            bool r = provider.Send(kBHBusQueueId, MakePub(topic, data.data(), data.size()), timeout);
            if (!r) {
                printf("pub ret: %s\n", r ? "ok" : "fail");
            }
        }
    };
    ThreadManager threads;
    typedef std::vector<std::string> Topics;
    Topics topics;
    topics.push_back("000-xxx");
    topics.push_back("111-football");
    topics.push_back("222-sport");
    // topics.push_back("333-sport");
    // topics.push_back("444-sport");
    Topics part;
    for (int i = 0; i < topics.size(); ++i) {
        part.push_back(topics[i]);
        threads.Launch(Sub, i, part);
    }
    std::this_thread::sleep_for(100ms);
    for (auto &topic: topics) {
        threads.Launch(Pub, topic);
    }
    threads.Launch(Pub, "some_else");
 
    threads.WaitAll();
 
    bus.Stop();
}
 
inline int MyMin(int a, int b) {
    printf("MyMin\n");
    return a < b ? a : b;
}
int test_main(int argc, char *argv[])
{
    printf("test main\n");
    int a = 0;
    int b = 0;
    BOOST_CHECK_EQUAL(a, b);
    int n = MyMin(4,6);
    for (int i = 0; i < n; ++i) {
        printf("i = %d\n", i);
    }
 
    return 0;
}