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
use std::io::Write;
use std::io::Result as IoResult;
use super::{Span};

pub fn dump_html_custom<W: Write>(mut out: W, spans: &[Span]) -> IoResult<()> {
    fn dump_spans<W: Write>(out: &mut W, span: &Span) -> IoResult<()> {
        try!(writeln!(out, "{{"));
        try!(writeln!(out, r#"name: "{}","#, span.name));
        try!(writeln!(out, "value: {},", span.delta));
        try!(writeln!(out, "start: {},", span.start_ns));
        try!(writeln!(out, "end: {},", span.end_ns));
        try!(writeln!(out, "children: ["));
        for child in &span.children {
            try!(dump_spans(out, child));
            try!(writeln!(out, ","));
        }
        try!(writeln!(out, "],"));
        try!(writeln!(out, "}}"));
        Ok(())
    }

    try!(write!(out, r#"
<!doctype html>
<html>
    <head>
        <style>
            html, body {{
                width: 100%;
                height: 100%;
                margin: 0;
                padding: 0;
            }}
            {}
        </style>
        <script>
            {}
            {}
            {}
        </script>
    </head>
    <body>
        <script>
            var width = document.body.offsetWidth;
            var height = document.body.offsetHeight - 100;
            var flamegraph =
                d3.flameGraph()
                  .width(width)
                  .height(height)
                  .tooltip(false)
                  .sort(function(a, b){{
                    if (a.start < b.start) {{
                        return -1;
                    }} else if (a.start > b.start) {{
                        return 1;
                    }} else {{
                        return 0;
                    }}
                  }});
            d3.select("body").datum({{ children: [
"#, include_str!("../resources/flameGraph.css"), include_str!("../resources/d3.js"), include_str!("../resources/d3-tip.js"), include_str!("../resources/flameGraph.js")));

    for span in spans {
        try!(dump_spans(&mut out, &span));
        try!(writeln!(out, ","));
    }

    try!(write!(out, r#"]}}).call(flamegraph);
         </script>
    </body>
</html>"#));

    Ok(())
}

pub fn dump_html<W: Write>(out: W) -> IoResult<()> {
    dump_html_custom(out, &::spans())
}