package bakeoff import ( "net/url" "regexp" ) // Weight is what a first visit to a page downloads, apart from the shared // CSS every app serves identically. type Weight struct{ HTMLBytes, JSBytes int } var scriptSrc = regexp.MustCompile(`]*\bsrc="([^"]+)"`) // PageWeight sums the page's HTML and every same-origin script it loads. func PageWeight(s *Session, path string) (Weight, error) { html, err := s.get(path) if err != nil { return Weight{}, err } w := Weight{HTMLBytes: len(html)} for _, m := range scriptSrc.FindAllStringSubmatch(html, -1) { u, err := s.Base.Parse(m[1]) if err != nil || u.Host != s.Base.Host { continue } js, err := s.get((&url.URL{Path: u.Path, RawQuery: u.RawQuery}).String()) if err != nil { return Weight{}, err } w.JSBytes += len(js) } return w, nil }