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
238
|
/// Collects statistics about the SKS packet dump using the openpgp
/// crate, Sequoia's low-level API.
///
/// Note that to achieve reasonable performance, you need to compile
/// Sequoia and this program with optimizations:
///
/// % cargo run -p sequoia-openpgp --example statistics --release \
/// -- <packet-dump>
use std::env;
extern crate sequoia_openpgp as openpgp;
use openpgp::Packet;
use openpgp::constants::SignatureType;
use openpgp::packet::{BodyLength, Tag};
use openpgp::parse::{Parse, PacketParserResult, PacketParser};
fn main() {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
panic!("Collects statistics about OpenPGP packet dumps.\n\n\
Usage: {} <packet-dump>\n", args[0]);
}
// Global stats.
let mut packet_count = 0;
let mut packet_size = 0 as usize;
// Per-tag statistics.
let mut tags_count = vec![0; 64];
let mut tags_unknown = vec![0; 64];
let mut tags_size_bytes = vec![0 as usize; 64];
let mut tags_size_count = vec![0; 64];
let mut tags_size_min = vec![::std::u32::MAX; 64];
let mut tags_size_max = vec![0; 64];
let mut sigs_count = vec![0; 256];
// Per-TPK statistics.
let mut tpk_count = 0;
let mut tpk = PerTPK::min();
let mut tpk_min = PerTPK::max();
let mut tpk_max = PerTPK::min();
// Create a parser.
let mut ppr = PacketParser::from_file(&args[1])
.expect("Failed to create reader");
// Iterate over all packets.
while let PacketParserResult::Some(pp) = ppr {
// While the packet is in the parser, get some data for later.
let size = match pp.header().length {
BodyLength::Full(n) => Some(n),
_ => None,
};
// Get the packet and advance the parser.
let (packet, tmp) = pp.next().expect("Failed to get next packet");
ppr = tmp;
packet_count += 1;
if let Some(n) = size {
packet_size += n as usize;
}
let i = u8::from(packet.tag()) as usize;
tags_count[i] += 1;
match packet {
// If a new TPK starts, update TPK statistics.
Packet::PublicKey(_) | Packet::SecretKey(_) => {
if tpk_count > 0 {
tpk.update_min_max(&mut tpk_min, &mut tpk_max);
}
tpk_count += 1;
tpk = PerTPK::min();
},
Packet::Signature(ref sig) => {
sigs_count[u8::from(sig.sigtype()) as usize] += 1;
tpk.sigs[u8::from(sig.sigtype()) as usize] += 1;
},
_ => (),
}
if let Packet::Unknown(_) = packet {
tags_unknown[i] += 1;
} else {
// Only record size statistics of packets we successfully
// parsed.
if let Some(n) = size {
tags_size_bytes[i] += n as usize;
tags_size_count[i] += 1;
if n < tags_size_min[i] {
tags_size_min[i] = n;
}
if n > tags_size_max[i] {
tags_size_max[i] = n;
}
tpk.bytes += n as usize;
}
tpk.packets += 1;
tpk.tags[i] += 1;
}
}
tpk.update_min_max(&mut tpk_min, &mut tpk_max);
// Print statistics.
println!("# Packet statistics\n\n\
{:>22} {:>9} {:>9} {:>9} {:>9} {:>9}",
"", "count", "unknown",
"min size", "mean size", "max size");
println!("-------------------------------------------------------\
-----------------");
println!("{:>22}", "- Packets -");
for t in 0..64 {
let count = tags_count[t];
if count > 0 {
println!("{:>22} {:>9} {:>9} {:>9} {:>9} {:>9}",
format!("{:?}", Tag::from(t as u8)),
count,
tags_unknown[t],
tags_size_min[t],
tags_size_bytes[t] / tags_size_count[t],
tags_size_max[t]);
}
}
println!("\n{:>22}", "- Signatures -");
for t in 0..256 {
let max = tpk_max.sigs[t];
if max > 0 {
println!("{:>22} {:>9}",
format!("{:?}", SignatureType::from(t as u8)),
sigs_count[t]);
}
}
if tpk_count == 0 {
return;
}
println!();
println!("# TPK statistics\n\n\
{:>22} {:>9} {:>9} {:>9}",
"", "min", "mean", "max");
println!("----------------------------------------------------");
println!("{:>22} {:>9} {:>9} {:>9}",
"Size (packets)",
tpk_min.packets, packet_count / tpk_count, tpk_max.packets);
println!("{:>22} {:>9} {:>9} {:>9}",
"Size (bytes)",
tpk_min.bytes, packet_size / tpk_count, tpk_max.bytes);
println!("\n{:>22}", "- Packets -");
for t in 0..64 {
let max = tpk_max.tags[t];
if t as u8 != Tag::PublicKey.into() && max > 0 {
println!("{:>22} {:>9} {:>9} {:>9}",
format!("{:?}", Tag::from(t as u8)),
tpk_min.tags[t],
tags_count[t] / tpk_count,
max);
}
}
println!("\n{:>22}", "- Signatures -");
for t in 0..256 {
let max = tpk_max.sigs[t];
if max > 0 {
println!("{:>22} {:>9} {:>9} {:>9}",
format!("{:?}",
SignatureType::from(t as u8)),
tpk_min.sigs[t],
sigs_count[t] / tpk_count,
max);
}
|