1 // Larbin
2 // Sebastien Ailleret
3 // 03-01-02 -> 04-01-02
4 
5 #include <iostream>
6 #include <cstring>
7 #include <unistd.h>
8 
9 #include "options.h"
10 
11 #include "types.h"
12 #include "global.h"
13 #include "fetch/file.h"
14 #include "utils/text.h"
15 #include "utils/debug.h"
16 #include "interf/output.h"
17 
18 #define nb 25
19 #define taille 4096
20 #define larg 40
21 
22 static double maxs = 0;
23 static double tabs[nb];
24 
25 static double maxb = 0;
26 static double tabb[nb];
27 
28 static uint64_t totalpages = 1;
29 static double totalbytes = 0;
30 
31 /** A page has been loaded successfully
32  * @param page the page that has been fetched
33  */
loaded(html * page)34 void loaded (html *page) {
35   uint32_t l = page->getLength();
36   int t = l / taille;
37   if (t >= nb) {
38     t = nb-1;
39   }
40   tabs[t]++;
41   if (tabs[t] > maxs) maxs = tabs[t];
42   tabb[t] += (double) l;
43   if (tabb[t] > maxb) maxb = tabb[t];
44   totalpages++;
45   totalbytes += (double) l;
46 }
47 
48 /** The fetch failed
49  * @param u the URL of the doc
50  * @param reason reason of the fail
51  */
failure(url * u,FetchError reason)52 void failure (url *u, FetchError reason) {
53 }
54 
55 /** initialisation function
56  */
initUserOutput()57 void initUserOutput () {
58   for (int i=0; i<nb; i++) {
59     tabs[i] = 0;
60     tabb[i] = 0;
61   }
62 }
63 
64 /** stats, called in particular by the webserver
65  * the webserver is in another thread, so be careful
66  * However, if it only reads things, it is probably not useful
67  * to use mutex, because incoherence in the webserver is not as critical
68  * as efficiency
69  */
dessine(int fds,double * tab,double * maxi)70 static void dessine(int fds, double *tab, double *maxi) {
71   for (int i=0; i<nb; i++) {
72     ecrire(fds, "|");
73     int n = (int) ((tab[i] * larg) / (*maxi+1));
74     for (int j=0; j<n; j++) ecrire(fds, "*");
75     ecrire(fds, "\n");
76   }
77 }
78 
outputStats(int fds)79 void outputStats(int fds) {
80   ecrire(fds, "Stats for ");
81   ecrireInt(fds, totalpages);
82   ecrire(fds, " pages.\nMean size of a page : ");
83   ecrireInt(fds, ((int) totalbytes) / totalpages);
84   ecrire(fds, "\n\nProportion of pages per size (one row is ");
85   ecrireInt(fds, taille);
86   ecrire(fds, " bytes, max size is ");
87   ecrireInt(fds, taille*nb);
88   ecrire(fds, " bytes) :\n\n");
89   dessine(fds, tabs, &maxs);
90   ecrire(fds, "\n\nbytes transfered by size :\n\n");
91   dessine(fds, tabb, &maxb);
92 }
93