File: 620-stats.py

package info (click to toggle)
libnbd 1.22.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,636 kB
  • sloc: ansic: 53,855; ml: 12,311; sh: 8,499; python: 4,595; makefile: 2,902; perl: 165; cpp: 24
file content (80 lines) | stat: -rw-r--r-- 2,279 bytes parent folder | download | duplicates (2)
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
# libnbd Python bindings
# Copyright Red Hat
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import nbd
import os

nbdkit = os.getenv("NBDKIT", "nbdkit")

h = nbd.NBD()

# Pre-connection, stats start out at 0
bs0 = h.stats_bytes_sent()
cs0 = h.stats_chunks_sent()
br0 = h.stats_bytes_received()
cr0 = h.stats_chunks_received()

assert bs0 == 0
assert cs0 == 0
assert br0 == 0
assert cr0 == 0

# Connection performs handshaking, which increments stats.
# The number of bytes/chunks here may grow over time as more features get
# automatically negotiated, so merely check that they are non-zero.
h.connect_command([nbdkit, "-s", "--exit-with-parent", "null"])

bs1 = h.stats_bytes_sent()
cs1 = h.stats_chunks_sent()
br1 = h.stats_bytes_received()
cr1 = h.stats_chunks_received()

assert cs1 > 0
assert bs1 > cs1
assert cr1 > 0
assert br1 > cr1

# A flush command should be one chunk out, one chunk back (even if
# structured replies are in use)
h.flush()

bs2 = h.stats_bytes_sent()
cs2 = h.stats_chunks_sent()
br2 = h.stats_bytes_received()
cr2 = h.stats_chunks_received()

assert bs2 == bs1 + 28
assert cs2 == cs1 + 1
assert br2 == br1 + 16   # assumes nbdkit uses simple reply
assert cr2 == cr1 + 1

# Stats are still readable after the connection closes; we don't know if
# the server sent reply bytes to our NBD_CMD_DISC, so don't insist on it.
h.shutdown()

bs3 = h.stats_bytes_sent()
cs3 = h.stats_chunks_sent()
br3 = h.stats_bytes_received()
cr3 = h.stats_chunks_received()

assert bs3 > bs2
assert cs3 == cs2 + 1
assert br3 >= br2
assert cr3 == cr2 + (br3 > br2)

# Try to trigger garbage collection of h
h = None