Commit b8dc7ee3 authored by Yuanle Song's avatar Yuanle Song
Browse files

add go-pq code and result

parent 0ff34d2d
Loading
Loading
Loading
Loading
+16 −1
Original line number Diff line number Diff line
* COMMENT -*- mode: org -*-
#+Date: 2016-12-16
Time-stamp: <2016-12-23>
Time-stamp: <2017-01-18>

* database binding benchmark
I want to know the efficiency of the db binding for different language,
@@ -69,3 +69,18 @@ https://www.postgresql.org/docs/9.4/static/libpq.html
--
0.50
28.0
** postgres 9.4, go 1.7.3, pq
0.385
27.0
--
0.367
27.6
--
0.423
30.7
--
0.362
27.2
--
0.372
26.4

go-pq/README

0 → 100644
+50 −0
Original line number Diff line number Diff line
* COMMENT -*- mode: org -*-
#+Date: 2017-01-18
Time-stamp: <2017-01-18>

* go, postgres, using lib/pq which is a pure go client

pq - GoDoc
https://godoc.org/github.com/lib/pq

SQLInterface · golang/go Wiki
https://github.com/golang/go/wiki/SQLInterface

database/sql is in stdlib.

Get dependency:

    go get github.com/lib/pq

To run it:

    go run bench.go

* dev
- problems
  - SELECT command failed: pq: SSL is not enabled on the server

    sslmode - Whether or not to use SSL (default is require, this is not the default for libpq)

    // stupid default param.

    https://www.postgresql.org/docs/9.1/static/libpq-ssl.html
    Table 31-1. SSL Mode Descriptions

    The default value for sslmode is prefer. Which is not supported by go's
    driver.

    I will use sslmode=disable for this benchmark.

  - panic: SELECT command failed: pq: remaining connection slots are reserved for non-replication superuser connections

    Does go create a new connection every time I run a query?

    type DB
    https://golang.org/pkg/database/sql/#DB

    You need to run Close() on the Rows, or call .Next() repeatedly until it
    closes itself.

    Common Pitfalls When Using database/sql in Go
    https://www.vividcortex.com/blog/2015/09/22/common-pitfalls-go/

go-pq/bench.go

0 → 100644
+48 −0
Original line number Diff line number Diff line
package main

import (
	"fmt"
	"time"
	"log"
	"database/sql"
	_ "github.com/lib/pq"
)

const TIMES = 3000

func trackRunTime(start time.Time, name string) {
    elapsed := time.Since(start)
    log.Printf("%s took %s", name, elapsed)
}

func selectTest(db *sql.DB) {
	defer trackRunTime(time.Now(), "select")
	for i := 0; i < TIMES; i++ {
		rows, err := db.Query("SELECT id, bar FROM foo")
		if err != nil {
			panic(fmt.Sprintf("SELECT command failed: %v\n", err))
		}
		for rows.Next() {    // for used as while loop
			//fetch and ignore rows, this will also Close() the rows.
		}
	}
}

func updateTest(db *sql.DB) {
	defer trackRunTime(time.Now(), "update")
	for i := 0; i < TIMES; i++ {
		_, err := db.Exec("UPDATE foo SET bar=bar+1 WHERE id=1")
		if err != nil {
			panic(fmt.Sprintf("UPDATE command failed: %v\n", err))
		}
	}
}

func main() {
	db, err := sql.Open("postgres", "host=localhost dbname=t1 user=t1 password=fNfwREMqO69TB9YqE+/OzF5/k+s= sslmode=disable")
	if err != nil {
		log.Fatal(err)
	}
	selectTest(db)
	updateTest(db)
}