Skip to content

Instantly share code, notes, and snippets.

@shouichi
Created Jun 29, 2014
Embed
What would you like to do?
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() {
}
}
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