与nodejs和postgresql基准

时间:2017-03-18 09:12:18

标签: node.js postgresql

我正在使用nodejs创建一个应用程序,它使用Hapi用于Web框架,knex用于sql builder,主要代码如下:

server.route({
  method: 'POST',
  path: '/location',
  config: {
    tags: ['api'],
    validate: {
      payload: {
        longitude: Joi.string().regex(/^\d+\.\d+$/).required(),
        latitude: Joi.string().regex(/^\d+\.\d+$/).required(),
        date: Joi.string().regex(/^\d{4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}.+$/).required(),
        phone: Joi.string().required(/^1\d{10}$/)
      }
    }
  },
  handler: createLocation
})


async function createLocation(request, reply){
  try{
    const data = await knex('locations').insert(request.payload)
    reply(data)
  }catch(error){
    reply(error)
  }
}

它只是向postgresql插入一些日期。我正在使用Wrk对Google Compute Engine(最便宜的机器)中的并发吞吐量进行基准测试,结果:

$ wrk -c 100 -t 12 http://localhost/api/location -s wrk.lua

Running 10s test @ http://panpan.tuols.com/api/location
  12 threads and 100 connections
  Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency   542.22ms  102.93ms   1.02s    88.32%
    Req/Sec    21.95     18.74    70.00     78.71%
  1730 requests in 10.02s, 0.94MB read
Requests/sec:    172.65
Transfer/sec:     96.44KB

然后我使用pgbench测试postgresql插入性能:

$ pgbench  location -U postgres -h localhost -r -t 1000 -f index.sql -c 10

transaction type: index.sql
scaling factor: 1
query mode: simple
number of clients: 10
number of threads: 1
number of transactions per client: 1000
number of transactions actually processed: 10000/10000
latency average = 1.663 ms
tps = 6014.610692 (including connections establishing)
tps = 6029.973067 (excluding connections establishing)
script statistics:
 - statement latencies in milliseconds:
         1.595  INSERT INTO "public"."locations"("phone", "longitude", "latitude", "date", "createdAt", "updatedAt") VALUES('18382383428', '123,33', '123.33', 'now()', 'now()', 'now()') RETURNING "id", "phone", "longitude", "latitude", "date", "createdAt", "updatedAt";

nodejs是172.65 req / s,而postgresql内部本机是6000 req / s,实际上是做了一些事情,如果忽略了http开销,差别不应该那么大,为什么性能如此之多拥抱不同?是nodejs还是node-pg包问题?

0 个答案:

没有答案