xs/metrics.xs

filter: Statementstmt 98.7% 153 / 155 Branchbran 64.2% 72 / 112 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 84.2% 225 / 267 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/alert.xs ↑ Summary xs/traces.xs »
1 MODULE = Punk::Observe PACKAGE = Punk::Observe::Metric PREFIX = pom_
2  
3 # --- the bit stream ---------------------------------------------------------
4  
5 # Round-trip a list of (width, value) pairs. The most testable code in the
6 # project, so it is exposed and tested exhaustively rather than implicitly.
7 void
8 pom_bits_roundtrip(SV *pairs)
9 PPCODE:
10 {
11 po_bw w;
12 AV *av;
13 SSize_t i, n;
14 66 AV *out = newAV();
15  
16 66 ▶ if (!SvROK(pairs) || SvTYPE(SvRV(pairs)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(pairs) || SvTYPE(SvRV(pairs)) != SVt_PVAV) 66 0 66
if (!SvROK(pairs) || SvTYPE(SvRV(pairs)) != SVt_PVAV) 0 66 66
17 0 croak("pairs must be an arrayref");
18 66 av = (AV *)SvRV(pairs);
19 66 n = av_len(av) + 1;
20 66 ▶ if (!po_bw_init(&w, 32)) croak("oom");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_bw_init(&w, 32)) croak("oom"); 0 66 66
21  
22 137 ▶ for (i = 0; i < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i += 2) { 71 66 137
23 71 SV **wid = av_fetch(av, i, 0);
24 71 SV **val = av_fetch(av, i + 1, 0);
25 71 po_u64 v = 0;
26 71 ▶ if (!wid || !val) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!wid || !val) break; 71 0 71
if (!wid || !val) break; 71 0 71
27 71 (void)po_sv_to_u64(aTHX_ *val, &v);
28 71 po_bw_put(&w, v, (int)SvIV(*wid));
29 }
30 {
31 po_br r;
32 66 po_br_init(&r, w.buf, w.nbits);
33 137 ▶ for (i = 0; i < n; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i += 2) { 71 66 137
34 71 SV **wid = av_fetch(av, i, 0);
35 71 ▶ if (!wid) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!wid) break; 0 71 71
36 71 av_push(out, po_u64_to_sv(po_br_get(&r, (int)SvIV(*wid))));
37 }
38 {
39 66 HV *res = newHV();
40 66 hv_stores(res, "values", newRV_noinc((SV *)out));
41 66 hv_stores(res, "bits", newSVuv((UV)w.nbits));
42 66 hv_stores(res, "err", newSViv(r.err));
43 66 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 66 66
44 }
45 }
46 66 po_bw_free(&w);
47 }
48  
49 # Sign extension, isolated. A 12-bit -2047 read back as 2049 is one missing
50 # sign extension, and the symptom is a point in the wrong place on a chart.
51 SV *
52 pom_sext(SV *v, SV *bits)
53 CODE:
54 {
55 11 po_u64 u = 0;
56 po_i64 s;
57 11 (void)po_sv_to_u64(aTHX_ v, &u);
58 11 s = po_sext(u, (int)SvIV(bits));
59 11 RETVAL = newSViv((IV)s);
60 }
61 OUTPUT:
62 RETVAL
63  
64 # --- gorilla ----------------------------------------------------------------
65  
66 # Encode a series of (timestamp, value-bit-pattern) and decode it back.
67 # Values cross as BIT PATTERNS so NaN payloads, the infinities and negative
68 # zero survive the Perl boundary as exactly what they are.
69 void
70 pom_gorilla_roundtrip(SV *ts, SV *vals)
71 PPCODE:
72 {
73 po_gor_w w;
74 AV *at, *av;
75 SSize_t i, n;
76 38 AV *ot = newAV(), *ov = newAV();
77  
78 38 ▶ if (!SvROK(ts) || !SvROK(vals)) croak("arrayrefs required");
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ts) || !SvROK(vals)) croak("arrayrefs required"); 38 0 38
if (!SvROK(ts) || !SvROK(vals)) croak("arrayrefs required"); 0 38 38
79 38 at = (AV *)SvRV(ts); av = (AV *)SvRV(vals);
80 38 n = av_len(at) + 1;
81 38 ▶ if (!po_gor_w_init(&w)) croak("oom");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_gor_w_init(&w)) croak("oom"); 0 38 38
82  
83 104855 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 104817 38 104855
84 104817 SV **t = av_fetch(at, i, 0);
85 104817 SV **v = av_fetch(av, i, 0);
86 104817 po_u64 tt = 0, vv = 0;
87 104817 ▶ if (!t || !v) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!t || !v) break; 104817 0 104817
if (!t || !v) break; 104817 0 104817
88 104817 (void)po_sv_to_u64(aTHX_ *t, &tt);
89 104817 (void)po_sv_to_u64(aTHX_ *v, &vv);
90 104817 po_gor_put(&w, tt, vv);
91 }
92  
93 {
94 po_gor_r r;
95 po_u64 t2, v2;
96 38 po_gor_r_init(&r, w.bw.buf, w.bw.nbits, w.n);
97 104855 ▶ while (po_gor_next(&r, &t2, &v2)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_gor_next(&r, &t2, &v2)) { 104817 38 104855
98 104817 av_push(ot, po_u64_to_sv(t2));
99 104817 av_push(ov, po_u64_to_sv(v2));
100 }
101 {
102 38 HV *res = newHV();
103 38 hv_stores(res, "t", newRV_noinc((SV *)ot));
104 38 hv_stores(res, "v", newRV_noinc((SV *)ov));
105 38 hv_stores(res, "bits", newSVuv((UV)w.bw.nbits));
106 38 hv_stores(res, "bytes", newSVuv((UV)po_bw_bytes(&w.bw)));
107 38 hv_stores(res, "points", newSVuv((UV)w.n));
108 38 hv_stores(res, "err", newSViv(r.br.err));
109 38 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 38 38
110 }
111 }
112 38 po_gor_w_free(&w);
113 }
114  
115 # A double's bit pattern and back, so a test can speak in doubles where that
116 # is clearer and in bits where it must be exact.
117 SV *
118 pom_d2b(double d)
119 CODE:
120 105435 RETVAL = po_u64_to_sv(po_d2b(d));
121 OUTPUT:
122 RETVAL
123  
124 double
125 pom_b2d(SV *b)
126 CODE:
127 {
128 1 po_u64 v = 0;
129 1 (void)po_sv_to_u64(aTHX_ b, &v);
130 1 RETVAL = po_b2d(v);
131 }
132 OUTPUT:
133 RETVAL
134  
135 # --- chunks -----------------------------------------------------------------
136  
137 void
138 pom_chunk(SV *ts, SV *vals, SV *is_int, SV *flags)
139 PPCODE:
140 {
141 po_chunk_w c;
142 AV *at, *av;
143 SSize_t i, n;
144 7 AV *ot = newAV(), *ov = newAV();
145  
146 7 ▶ if (!SvROK(ts) || !SvROK(vals)) croak("arrayrefs required");
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ts) || !SvROK(vals)) croak("arrayrefs required"); 7 0 7
if (!SvROK(ts) || !SvROK(vals)) croak("arrayrefs required"); 0 7 7
147 7 at = (AV *)SvRV(ts); av = (AV *)SvRV(vals);
148 7 n = av_len(at) + 1;
149 7 ▶ if (!po_chunk_w_init(&c, (int)SvIV(is_int), (uint16_t)SvUV(flags)))
Branch: 1 branch
BranchTrueFalseTotal
if (!po_chunk_w_init(&c, (int)SvIV(is_int), (uint16_t)SvUV(flags))) 0 7 7
150 0 croak("oom");
151  
152 167 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 162 5 167
153 162 SV **t = av_fetch(at, i, 0);
154 162 SV **v = av_fetch(av, i, 0);
155 162 po_u64 tt = 0, vv = 0;
156 162 ▶ if (!t || !v) break;
Branch: 2 branches
BranchTrueFalseTotal
if (!t || !v) break; 162 0 162
if (!t || !v) break; 162 0 162
157 162 (void)po_sv_to_u64(aTHX_ *t, &tt);
158 162 (void)po_sv_to_u64(aTHX_ *v, &vv);
159 162 ▶ if (po_chunk_full(&c, tt)) break;
Branch: 1 branch
BranchTrueFalseTotal
if (po_chunk_full(&c, tt)) break; 2 160 162
160 160 po_chunk_add(&c, tt, vv);
161 }
162  
163 {
164 po_chunk_r rd;
165 po_u64 t2, v2;
166 7 po_chunk_r_init(&rd, &c.h, c.g.bw.buf);
167 167 ▶ while (po_chunk_next(&rd, &t2, &v2)) {
Branch: 1 branch
BranchTrueFalseTotal
while (po_chunk_next(&rd, &t2, &v2)) { 160 7 167
168 160 av_push(ot, po_u64_to_sv(t2));
169 160 av_push(ov, po_u64_to_sv(v2));
170 }
171 {
172 7 HV *res = newHV();
173 7 hv_stores(res, "t", newRV_noinc((SV *)ot));
174 7 hv_stores(res, "v", newRV_noinc((SV *)ov));
175 7 hv_stores(res, "count", newSVuv((UV)c.h.count));
176 7 hv_stores(res, "bytes", newSVuv((UV)po_bw_bytes(&c.g.bw)));
177 7 hv_stores(res, "flags", newSVuv((UV)c.h.flags));
178 7 hv_stores(res, "resets", newSViv(c.resets));
179 7 hv_stores(res, "t_first", po_u64_to_sv(c.h.t_first));
180 7 hv_stores(res, "t_last", po_u64_to_sv(c.h.t_last));
181 7 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 7 7
182 }
183 }
184 7 po_chunk_w_free(&c);
185 }
186  
187 # --- exemplars --------------------------------------------------------------
188  
189 void
190 pom_exemplars(SV *specs)
191 PPCODE:
192 {
193 po_exemplars x;
194 AV *av;
195 SSize_t i, n;
196 2 AV *out = newAV();
197 2 int refused = 0;
198  
199 2 ▶ if (!SvROK(specs)) croak("arrayref required");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(specs)) croak("arrayref required"); 0 2 2
200 2 av = (AV *)SvRV(specs);
201 2 n = av_len(av) + 1;
202 2 ▶ if (!po_exemplars_init(&x)) croak("oom");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_exemplars_init(&x)) croak("oom"); 0 2 2
203  
204 505 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 503 2 505
205 503 SV **e = av_fetch(av, i, 0);
206 HV *h; SV **f;
207 po_exemplar ex;
208 503 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 503 0 503
if (!e || !SvROK(*e)) continue; 0 503 503
209 503 h = (HV *)SvRV(*e);
210 503 memset(&ex, 0, sizeof(ex));
211 503 ▶ if ((f = hv_fetchs(h, "t", 0))) (void)po_sv_to_u64(aTHX_ *f, &ex.t);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "t", 0))) (void)po_sv_to_u64(aTHX_ *f, &ex.t); 503 0 503
212 503 ▶ if ((f = hv_fetchs(h, "value", 0))) (void)po_sv_to_u64(aTHX_ *f, &ex.value_bits);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "value", 0))) (void)po_sv_to_u64(aTHX_ *f, &ex.value_bits); 503 0 503
213 503 ▶ if ((f = hv_fetchs(h, "trace_hi", 0))) (void)po_sv_to_u64(aTHX_ *f, &ex.trace_hi);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "trace_hi", 0))) (void)po_sv_to_u64(aTHX_ *f, &ex.trace_hi); 503 0 503
214 503 ▶ if ((f = hv_fetchs(h, "trace_lo", 0))) (void)po_sv_to_u64(aTHX_ *f, &ex.trace_lo);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "trace_lo", 0))) (void)po_sv_to_u64(aTHX_ *f, &ex.trace_lo); 503 0 503
215 503 ▶ if ((f = hv_fetchs(h, "span_id", 0))) (void)po_sv_to_u64(aTHX_ *f, &ex.span_id);
Branch: 1 branch
BranchTrueFalseTotal
if ((f = hv_fetchs(h, "span_id", 0))) (void)po_sv_to_u64(aTHX_ *f, &ex.span_id); 503 0 503
216 503 ▶ if (!po_exemplar_add(&x, &ex)) refused++;
Branch: 1 branch
BranchTrueFalseTotal
if (!po_exemplar_add(&x, &ex)) refused++; 1 502 503
217 }
218 {
219 uint32_t k;
220 504 ▶ for (k = 0; k < x.n; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < x.n; k++) { 502 2 504
221 502 HV *h = newHV();
222 502 hv_stores(h, "t", po_u64_to_sv(x.e[k].t));
223 502 hv_stores(h, "trace_hi", po_u64_to_sv(x.e[k].trace_hi));
224 502 hv_stores(h, "trace_lo", po_u64_to_sv(x.e[k].trace_lo));
225 502 hv_stores(h, "span_id", po_u64_to_sv(x.e[k].span_id));
226 502 av_push(out, newRV_noinc((SV *)h));
227 }
228 }
229 {
230 2 HV *res = newHV();
231 2 hv_stores(res, "kept", newRV_noinc((SV *)out));
232 2 hv_stores(res, "refused", newSViv(refused));
233 2 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 2 2
234 }
235 2 po_exemplars_free(&x);
236 }
237  
238 # --- postings ---------------------------------------------------------------
239  
240 void
241 pom_postings(SV *lists)
242 PPCODE:
243 {
244 AV *av;
245 SSize_t nl, li;
246 char **enc;
247 size_t *elen;
248 56 AV *sizes = newAV();
249  
250 56 ▶ if (!SvROK(lists)) croak("arrayref required");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(lists)) croak("arrayref required"); 0 56 56
251 56 av = (AV *)SvRV(lists);
252 56 nl = av_len(av) + 1;
253 56 ▶ if (nl < 1) croak("need at least one list");
Branch: 1 branch
BranchTrueFalseTotal
if (nl < 1) croak("need at least one list"); 0 56 56
254  
255 56 ▶ Newxz(enc, nl, char *);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(enc, nl, char *); 0 56 56
256 56 ▶ Newxz(elen, nl, size_t);
Branch: 1 branch
BranchTrueFalseTotal
Newxz(elen, nl, size_t); 0 56 56
257  
258 162 ▶ for (li = 0; li < nl; li++) {
Branch: 1 branch
BranchTrueFalseTotal
for (li = 0; li < nl; li++) { 106 56 162
259 106 SV **e = av_fetch(av, li, 0);
260 AV *inner;
261 SSize_t i, n;
262 po_postings p;
263 106 ▶ if (!e || !SvROK(*e)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!e || !SvROK(*e)) continue; 106 0 106
if (!e || !SvROK(*e)) continue; 0 106 106
264 106 inner = (AV *)SvRV(*e);
265 106 n = av_len(inner) + 1;
266 106 ▶ if (!po_postings_init(&p)) croak("oom");
Branch: 1 branch
BranchTrueFalseTotal
if (!po_postings_init(&p)) croak("oom"); 0 106 106
267 78931 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 78825 106 78931
268 78825 SV **v = av_fetch(inner, i, 0);
269 78825 ▶ if (v) po_postings_add(&p, (uint32_t)SvUV(*v));
Branch: 1 branch
BranchTrueFalseTotal
if (v) po_postings_add(&p, (uint32_t)SvUV(*v)); 78825 0 78825
270 }
271 106 po_postings_finish(&p);
272 106 elen[li] = po_postings_encode(&p, &enc[li]);
273 106 av_push(sizes, newSVuv((UV)elen[li]));
274 106 po_postings_free(&p);
275 }
276  
277 {
278 56 HV *res = newHV();
279 56 AV *members = newAV();
280 po_post_r r;
281 uint32_t id;
282 56 ▶ if (po_post_open(&r, enc[0], elen[0]))
Branch: 1 branch
BranchTrueFalseTotal
if (po_post_open(&r, enc[0], elen[0])) 56 0 56
283 44811 ▶ while (po_post_next(&r, &id)) av_push(members, newSVuv((UV)id));
Branch: 1 branch
BranchTrueFalseTotal
while (po_post_next(&r, &id)) av_push(members, newSVuv((UV)id)); 44755 56 44811
284 56 hv_stores(res, "first", newRV_noinc((SV *)members));
285 56 hv_stores(res, "sizes", newRV_noinc((SV *)sizes));
286  
287 56 ▶ if (nl >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (nl >= 2) { 50 6 56
288 uint32_t *out;
289 uint32_t got;
290 50 AV *inter = newAV();
291 uint32_t k;
292 50 Newx(out, 100000, uint32_t);
293 50 got = po_post_intersect(enc[0], elen[0], enc[1], elen[1],
294 out, 100000);
295 1668 ▶ for (k = 0; k < got && k < 100000; k++)
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < got && k < 100000; k++) 1618 50 1668
for (k = 0; k < got && k < 100000; k++) 1618 0 1618
296 1618 av_push(inter, newSVuv((UV)out[k]));
297 50 hv_stores(res, "intersection", newRV_noinc((SV *)inter));
298 50 Safefree(out);
299 }
300 56 ▶ mXPUSHs(newRV_noinc((SV *)res));
Branch: 1 branch
BranchTrueFalseTotal
mXPUSHs(newRV_noinc((SV *)res)); 0 56 56
301 }
302 162 ▶ for (li = 0; li < nl; li++) free(enc[li]);
Branch: 1 branch
BranchTrueFalseTotal
for (li = 0; li < nl; li++) free(enc[li]); 106 56 162
303 56 Safefree(enc);
304 56 Safefree(elen);
305 }
« xs/alert.xs ↑ Summary xs/traces.xs »