aboutsummaryrefslogtreecommitdiffstats
path: root/fnet/src/tests/locking/drainpackets.cpp
blob: 79c95eabf8db108fd40f7da3042956ae0e74a78a (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/testkit/test_kit.h>
#include <vespa/fnet/packetqueue.h>
#include <vespa/fnet/packet.h>
#include <mutex>
#include <chrono>

class MyPacket : public FNET_Packet
{
public:
  uint32_t  GetPCODE() override { return 0; }
  uint32_t GetLength() override { return 0; }
  void Encode(FNET_DataBuffer *) override {}
  bool Decode(FNET_DataBuffer *, uint32_t) override { return true; }
};


TEST("drain packets") {
  using clock = std::chrono::steady_clock;
  using ms_double = std::chrono::duration<double, std::milli>;

  clock::time_point start;
  ms_double         ms;

  std::mutex      lock;

  FNET_PacketQueue   q1(512);
  FNET_PacketQueue   q2(512);
  FNET_PacketQueue   q3(512);

  int        i;

  // create dummy packets

  for (i = 0; i < 500; i++) {
    q1.QueuePacket_NoLock(new MyPacket(), FNET_Context());
  }

  // drain packets directly with single lock interval

  start = clock::now();

  for (i = 0; i < 10000; i++) {

    FNET_Packet  *packet;
    FNET_Context  context;

    {
        std::lock_guard<std::mutex> guard(lock);
        while (!q1.IsEmpty_NoLock()) {
            packet = q1.DequeuePacket_NoLock(&context);
            q3.QueuePacket_NoLock(packet, context);
        }
    }

    //------------------------

    {
        std::lock_guard<std::mutex> guard(lock);
        while (!q3.IsEmpty_NoLock()) {
            packet = q3.DequeuePacket_NoLock(&context);
            q1.QueuePacket_NoLock(packet, context);
        }
    }
  }

  ms = (clock::now() - start);
  fprintf(stderr, "direct, single lock interval (10M packets): %1.2f ms\n",
          ms.count());

  // flush packets, then move without lock

  start = clock::now();

  for (i = 0; i < 10000; i++) {

    FNET_Packet  *packet;
    FNET_Context  context;

    {
        std::lock_guard<std::mutex> guard(lock);
        q1.FlushPackets_NoLock(&q2);
    }

    while (!q2.IsEmpty_NoLock()) {
      packet = q2.DequeuePacket_NoLock(&context);
      q3.QueuePacket_NoLock(packet, context);
    }

    //------------------------

    {
        std::lock_guard<std::mutex> guard(lock);
        q3.FlushPackets_NoLock(&q2);
    }

    while (!q2.IsEmpty_NoLock()) {
      packet = q2.DequeuePacket_NoLock(&context);
      q1.QueuePacket_NoLock(packet, context);
    }
  }

  ms = (clock::now() - start);
  fprintf(stderr, "indirect (10M packets): %1.2f ms\n", ms.count());

  // drain packets directly with multiple lock intervals

  start = clock::now();

  for (i = 0; i < 10000; i++) {

    FNET_Packet  *packet;
    FNET_Context  context;

    while ((packet = q1.DequeuePacket(0, &context)) != nullptr) {
      q3.QueuePacket_NoLock(packet, context);
    }

    //------------------------

    while ((packet = q3.DequeuePacket(0, &context)) != nullptr) {
      q1.QueuePacket_NoLock(packet, context);
    }
  }

  ms = (clock::now() - start);
  fprintf(stderr, "direct, multiple lock intervals (10M packets): %1.2f ms\n",
          ms.count());

  EXPECT_TRUE(q1.GetPacketCnt_NoLock() == 500 &&
         q2.GetPacketCnt_NoLock() == 0 &&
         q3.GetPacketCnt_NoLock() == 0);
}

TEST_MAIN() { TEST_RUN_ALL(); }