运行此代码时,我必须等待 10 秒才能打印 s.Locations 并等待 60 秒以上才能打印 n.Titles。这是什么原因造成的?
有关如何解决此问题的提示会很有帮助,即查看完成某些代码行需要多长时间。 Go 的新手,所以不确定如何准确地执行此操作。
我已确保关闭连接。由于我计算机上的其他所有内容加载速度都非常快,我不认为通过 http.Get 访问互联网应该很慢。
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// SitemapIndex is the root xml
type SitemapIndex struct {
Locations []string `xml:"sitemap>loc"`
}
// News is the individual categories
type News struct {
Titles []string `xml:"url>news>title"`
Keywords []string `xml:"url>news>keywords"`
Locations []string `xml:"url>loc"`
}
// NewsMap is the
type NewsMap struct {
Keywords string
Location string
}
func main() {
var s SitemapIndex
var n News
// np := make(map[string]NewsMap)
resp, _ := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
bytes, _ := ioutil.ReadAll(resp.Body)
xml.Unmarshal(bytes, &s)
resp.Body.Close()
for i := range s.Locations {
s.Locations[i] = strings.TrimSpace(s.Locations[i])
}
fmt.Println(s.Locations) // slice of data
for _, Location := range s.Locations {
resp, _ := http.Get(Location)
bytes, _ := ioutil.ReadAll(resp.Body)
xml.Unmarshal(bytes, &n)
resp.Body.Close()
}
fmt.Println(n.Titles)
}
我得到了输出,但我必须为 s.Locations 等待 10 秒,为 n.Titles 等待 60 秒以上
最佳答案
Tips on how to troubleshoot this would be helpful.
从简单的事情开始,一次测量一件事情,一个科学实验。
使用 curl 测量基本响应时间。
$ curl https://www.google.com/robots.txt -o /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 7246 0 7246 0 0 94103 0 --:--:-- --:--:-- --:--:-- 94103
$ curl https://www.nytimes.com/robots.txt -o /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 934 100 934 0 0 61 0 0:00:15 0:00:15 --:--:-- 230
$ curl https://www.washingtonpost.com/robots.txt -o /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3360 100 3360 0 0 133 0 0:00:25 0:00:25 --:--:-- 869
$
谷歌没有延迟。纽约时报有 15 秒的延迟。华盛顿邮报有 25 秒的延迟。
在 Go 中,确认华盛顿邮报有 25 秒的延迟。
$ go run wapo.go
25.174366651s
$ cat wapo.go
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
func main() {
start := time.Now()
resp, err := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
resp.Body.Close()
fmt.Fprintln(os.Stderr, time.Since(start))
}
$
接下来,从另一台计算机尝试不同的 ISP。
$ curl https://www.google.com/robots.txt -o /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 7246 0 7246 0 0 27343 0 --:--:-- --:--:-- --:--:-- 27343
$ curl https://www.nytimes.com/robots.txt -o /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 934 100 934 0 0 2017 0 --:--:-- --:--:-- --:--:-- 2017
$ curl https://www.washingtonpost.com/robots.txt -o /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3360 100 3360 0 0 356 0 0:00:09 0:00:09 --:--:-- 840
$ curl https://www.washingtonpost.com/news-sitemaps/index.xml -o /dev/null
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1101 100 1101 0 0 104 0 0:00:10 0:00:10 --:--:-- 266
$
$ go run wapo.go
8.378458882s
$
谷歌没有延迟。 《纽约时报》稍有延迟。华盛顿邮报有 9 秒的延迟。
Go 代码和编译器是一样的:
$ go version
go version devel +a25c2878c7 Sat Jul 27 23:29:18 2019 +0000 linux/amd64
$ cat wapo.go
package main
import (
"fmt"
"net/http"
"os"
"time"
)
func main() {
start := time.Now()
resp, err := http.Get("https://www.washingtonpost.com/news-sitemaps/index.xml")
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
resp.Body.Close()
fmt.Fprintln(os.Stderr, time.Since(start))
}
$
因此,关注网络和站点因素。
关于http - 为什么我的代码需要这么长时间才能返回结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57246203/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/
我有一个模型:classItem项目有一个属性“商店”基于存储的值,我希望Item对象对特定方法具有不同的行为。Rails中是否有针对此的通用设计模式?如果方法中没有大的if-else语句,这是如何干净利落地完成的? 最佳答案 通常通过Single-TableInheritance. 关于ruby-on-rails-Rails-子类化模型的设计模式是什么?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.co
如何在buildr项目中使用Ruby?我在很多不同的项目中使用过Ruby、JRuby、Java和Clojure。我目前正在使用我的标准Ruby开发一个模拟应用程序,我想尝试使用Clojure后端(我确实喜欢功能代码)以及JRubygui和测试套件。我还可以看到在未来的不同项目中使用Scala作为后端。我想我要为我的项目尝试一下buildr(http://buildr.apache.org/),但我注意到buildr似乎没有设置为在项目中使用JRuby代码本身!这看起来有点傻,因为该工具旨在统一通用的JVM语言并且是在ruby中构建的。除了将输出的jar包含在一个独特的、仅限ruby
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has
为什么4.1%2返回0.0999999999999996?但是4.2%2==0.2。 最佳答案 参见此处:WhatEveryProgrammerShouldKnowAboutFloating-PointArithmetic实数是无限的。计算机使用的位数有限(今天是32位、64位)。因此计算机进行的浮点运算不能代表所有的实数。0.1是这些数字之一。请注意,这不是与Ruby相关的问题,而是与所有编程语言相关的问题,因为它来自计算机表示实数的方式。 关于ruby-为什么4.1%2使用Ruby返
是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou
我注意到像bundler这样的项目在每个specfile中执行requirespec_helper我还注意到rspec使用选项--require,它允许您在引导rspec时要求一个文件。您还可以将其添加到.rspec文件中,因此只要您运行不带参数的rspec就会添加它。使用上述方法有什么缺点可以解释为什么像bundler这样的项目选择在每个规范文件中都需要spec_helper吗? 最佳答案 我不在Bundler上工作,所以我不能直接谈论他们的做法。并非所有项目都checkin.rspec文件。原因是这个文件,通常按照当前的惯例,只