Skip to content

Instantly share code, notes, and snippets.

@shouichi
Created June 29, 2014 03:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shouichi/07ab210d73bba8b0ad16 to your computer and use it in GitHub Desktop.
Save shouichi/07ab210d73bba8b0ad16 to your computer and use it in GitHub Desktop.
package main
import (
"database/sql"
"flag"
"log"
"os"
"runtime/pprof"
"time"
_ "github.com/lib/pq"
)
func main() {
nPtr := flag.Int("n", 10, "number of goroutines")
flag.Parse()
nGoroutines := *nPtr
f, err := os.Create("cpuprofile")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
db, err := sql.Open("postgres", "user=postgres host=127.0.0.1 dbname=benchmark sslmode=disable")
if err != nil {
log.Fatal(err)
}
err = db.Ping()
if err != nil {
log.Fatal(err)
}
defer db.Close()
done := make(chan bool)
for i := 0; i < nGoroutines; i++ {
go func() {
start := time.Now()
read(db)
log.Println(time.Since(start))
done <- true
}()
}
for i := 0; i < nGoroutines; i++ {
<-done
}
}
func read(db *sql.DB) {
rows, err := db.Query("SELECT email FROM users")
if err != nil {
log.Fatal(err)
}
defer rows.Close()
// Do nothing but iteration
for rows.Next() {
}
}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment