Created
June 29, 2014 03:23
-
-
Save shouichi/07ab210d73bba8b0ad16 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment