Skip to content

Clean HTML stuff, add Timeline.WriteHTMLTo - #42

Open
arl wants to merge 3 commits into
jamiealquiza:masterfrom
develersrl:add-writehtmlto-api
Open

Clean HTML stuff, add Timeline.WriteHTMLTo#42
arl wants to merge 3 commits into
jamiealquiza:masterfrom
develersrl:add-writehtmlto-api

Conversation

@arl

@arl arl commented Sep 8, 2018

Copy link
Copy Markdown

Fixes #32

Timeline.WriteHTMLTo takes a io.Writer so that it can directly
be used in various cases, for example directly passing an
http.ResponseWriter. Using a writer, many allocations are removed
by directly formatting the output with fmt.Fprint, remove the
`bytes.Buffer. as well.

Use a text/template for HTML generation. An html/template are
very similar but escapes the strings, and it was not playing
well the JSON marshaling of graph labels, also it is not based on
user input so no risk of code injection.

genGraphHTML also takes a writer to eliminate more copies.

Modify Timeline.WriteHTML to keep backward compatibility but
make it use WriteHTMLTo under the hood.

Add BenchmarkWriteHTMLTo. In the following benchmarks, old is
a version of WriteHTMLTo equivalent to original WriteHTML
code, present in current master, whereas new is this commit.

name           old time/op    new time/op    delta
WriteHTMLTo-8    25.4µs ±12%     7.0µs ± 3%  -72.49%  (p=0.008 n=5+5)

name           old alloc/op   new alloc/op   delta
WriteHTMLTo-8     161kB ± 0%       1kB ± 0%  -99.37%  (p=0.008 n=5+5)

name           old allocs/op  new allocs/op  delta
WriteHTMLTo-8      56.0 ± 0%      50.0 ± 0%  -10.71%  (p=0.008 n=5+5)

arl added 3 commits September 8, 2018 13:43
Fixes jamiealquiza#32

`Timeline.WriteHTMLTo` takes a `io.Writer` so that it can directly
be used in various cases, for example directly passing an
http.ResponseWriter. Using a writer, many allocations are removed
by directly formatting the output with `fmt.Fprint`, remove the
`bytes.Buffer. as well.

Use a `text/template` for HTML generation. An `html/template` are
very similar but escapes the strings, and it was not playing
well the JSON marshaling of graph labels, also it is not based on
user input so no risk of code injection.

`genGraphHTML` also takes a writer to eliminate more copies.

Modify `Timeline.WriteHTML` to keep backward compatibility but
make it use WriteHTMLTo under the hood.

Add `BenchmarkWriteHTMLTo`. In the following benchmarks, `old` is
a version of `WriteHTMLTo` equivalent to original `WriteHTML`
code, present in current master, whereas `new` is this commit.

    name           old time/op    new time/op    delta
    WriteHTMLTo-8    25.4µs ±12%     7.0µs ± 3%  -72.49%  (p=0.008 n=5+5)

    name           old alloc/op   new alloc/op   delta
    WriteHTMLTo-8     161kB ± 0%       1kB ± 0%  -99.37%  (p=0.008 n=5+5)

    name           old allocs/op  new allocs/op  delta
    WriteHTMLTo-8      56.0 ± 0%      50.0 ± 0%  -10.71%  (p=0.008 n=5+5)
`Timeline.WriteHTML` panicked if no metrics have been added via
`AddTime`. Fix this and add a test to verify new behaviour.
@jamiealquiza

Copy link
Copy Markdown
Owner

Hi, apologies, I just now noticed these pull requests today (github notifications aren't the best). I'll review these shortly!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants