Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean HTML stuff, add Timeline.WriteHTMLTo #42

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

arl
Copy link

@arl arl commented Sep 8, 2018

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
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