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
|
// Copyright 2017 Yahoo Holdings. 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/fnet.h>
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") {
FastOS_Time start;
FastOS_Time stop;
FastOS_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.SetNow();
for (i = 0; i < 10000; i++) {
FNET_Packet *packet;
FNET_Context context;
lock.Lock();
while (!q1.IsEmpty_NoLock()) {
packet = q1.DequeuePacket_NoLock(&context);
q3.QueuePacket_NoLock(packet, context);
}
lock.Unlock();
//------------------------
lock.Lock();
while (!q3.IsEmpty_NoLock()) {
packet = q3.DequeuePacket_NoLock(&context);
q1.QueuePacket_NoLock(packet, context);
}
lock.Unlock();
}
stop.SetNow();
stop -= start;
fprintf(stderr, "direct, single lock interval (10M packets): %1.2f ms\n",
stop.MilliSecs());
// flush packets, then move without lock
start.SetNow();
for (i = 0; i < 10000; i++) {
FNET_Packet *packet;
FNET_Context context;
lock.Lock();
q1.FlushPackets_NoLock(&q2);
lock.Unlock();
while (!q2.IsEmpty_NoLock()) {
packet = q2.DequeuePacket_NoLock(&context);
q3.QueuePacket_NoLock(packet, context);
}
//------------------------
lock.Lock();
q3.FlushPackets_NoLock(&q2);
lock.Unlock();
while (!q2.IsEmpty_NoLock()) {
packet = q2.DequeuePacket_NoLock(&context);
q1.QueuePacket_NoLock(packet, context);
}
}
stop.SetNow();
stop -= start;
fprintf(stderr, "indirect (10M packets): %1.2f ms\n", stop.MilliSecs());
// drain packets directly with multiple lock intervals
start.SetNow();
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);
}
}
stop.SetNow();
stop -= start;
fprintf(stderr, "direct, multiple lock intervals (10M packets): %1.2f ms\n",
stop.MilliSecs());
EXPECT_TRUE(q1.GetPacketCnt_NoLock() == 500 &&
q2.GetPacketCnt_NoLock() == 0 &&
q3.GetPacketCnt_NoLock() == 0);
}
TEST_MAIN() { TEST_RUN_ALL(); }
|