【发布时间】:2019-11-14 14:20:20
【问题描述】:
到目前为止,我使用 cloud sql 作为后端数据库构建了一个项目。
由于数据是由非常小的记录构成的,我认为,文档存储会更适合它——尤其是在 postgres 不容易扩展的情况下。所以我想尝试 Datastore - 已弃用并由 firestore 取代 - 很好。
我将一个文档插入到一个集合中,用一张表在云 sql 上创建了一个测试 sql 数据库并插入了相同的记录,然后我在 Go 中运行了一个简单的基准测试。
Soooo:**cloud sql postgres 可以在 Firebase 管理大约 66 个(17.006.551 ns/op)的同时返回 6000 个(198.650 ns/op)响应。**
我一定是在这里做错了什么。即使 postgres 无法扩展,它也可能会减慢 100 倍,然后才能接近一个包含一个文档的集合上的一个索引的 firestore 的性能。
我使用以下标志从 4-core, 8GB ram compute instance 运行我的基准测试:
go test -bench=. -benchtime=1s -test.parallel=1 -cpu=1
这是我用于 Firestore 的基准:
func Benchmark_fetchSingle(b *testing.B) {
ctx := context.Background()
client, _ := firestore.NewClient(ctx, "project-123", option.WithCredentialsFile("key.json"))
defer client.Close()
for n := 0; n < b.N; n++ {
c,_ := client.Collection("documents").Doc("DOCUMENTID").Get(ctx)
c = c
}
}
这是用于云 sql (postgres) 的:
func Benchmark_sql(b *testing.B) {
psqlInfo := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s " +
"sslmode=require sslrootcert=server.chain.cert sslcert=client.cert sslkey=client.key",
"ip.address.0.0", "5432", "user", "password", "database")
sqldb, _ := sql.Open("postgres", psqlInfo)
stmt, _ := sqldb.Prepare(`select property1, property2 from documents where pk = $1;`)
defer sqldb.Close()
for n := 0; n < b.N; n++ {
rows, _ := stmt.Query("DOCUMENTPK")
rows.Next()
stuff := struct{
P1 string
P2 string}{}
rows.Scan(&(stuff.P1), &(stuff.P2))
stuff = stuff
rows.Close()
}
}
它是否错误配置了 Firestore? 我认为还有 spanner 和 bigtable 作为 no-sql 替代品 - 但它们的成本对于我的简单用例来说是巨大的(至少是估计,我觉得这很不透明)
【问题讨论】:
-
我在下面快速写出了 Firestore 性能的主要内在特征。但是,这并不能解释您所看到的。我不确定我是否正确解析了您的结果,但如果检索单个文档确实需要 17 秒,那么 Firestore 测试似乎在其网络流量中遇到了一些障碍,而您的其他测试没有。
标签: go google-cloud-firestore gcloud