Created
July 20, 2012 03:02
-
-
Save mgutz/3148405 to your computer and use it in GitHub Desktop.
go + postgres + pq library
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 mine | |
import ( | |
"fmt" | |
_ "github.com/bmizerany/pq" | |
"database/sql" | |
"testing" | |
) | |
func BenchmarkDriver(t *testing.B) { | |
t.StopTimer() | |
db, err := sql.Open("postgres", "user=pgdev dbname=db_dev password=password sslmode=disable") | |
t.StartTimer() | |
if err != nil { | |
t.Fatal(err) | |
} | |
_, err = db.Exec("BEGIN") | |
if err != nil { | |
t.Fatal(err) | |
} | |
for i := 0; i < 100000; i++ { | |
if i % 2 == 0 { | |
_, err := db.Exec("INSERT INTO users (user_name, first_name, last_name) VALUES ('go', 'is', 'it')") | |
if err != nil { | |
t.Fatal(err) | |
} | |
} else { | |
rows, err := db.Query("SELECT user_name, first_name, last_name from users LIMIT 50") | |
if err != nil { | |
t.Fatal(err) | |
} | |
for rows.Next() { | |
if i == 3 { | |
var user_name string | |
var first_name string | |
var last_name string | |
err = rows.Scan(&user_name, &first_name, &last_name) | |
fmt.Println(user_name, first_name, last_name) | |
} | |
} | |
} | |
} | |
db.Exec("COMMIT") | |
db.Close() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment