1
0
Fork 0

Benchmark ops

This commit is contained in:
Gregory Eremin 2015-03-06 20:35:13 +07:00
parent 9251ad8c6d
commit 7d38440126
10 changed files with 40 additions and 6 deletions

View File

@ -1,5 +1,9 @@
package db package db
import (
"time"
)
type Contrib struct { type Contrib struct {
Week uint64 `json:"week"` Week uint64 `json:"week"`
Author string `json:"author"` Author string `json:"author"`
@ -17,5 +21,6 @@ on duplicate key update
commits=values(commits), additions=values(additions), deletions=values(deletions)` commits=values(commits), additions=values(additions), deletions=values(deletions)`
func (c *Contrib) Save() { func (c *Contrib) Save() {
defer measure("SaveContrib", time.Now())
mustExecN(saveContribQuery, c) mustExecN(saveContribQuery, c)
} }

View File

@ -1,6 +1,9 @@
package db package db
import ( import (
"log"
"time"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx/reflectx" "github.com/jmoiron/sqlx/reflectx"
@ -27,3 +30,13 @@ func mustSelect(dest interface{}, query string, args ...interface{}) {
panic(err) panic(err)
} }
} }
func measure(op string, start time.Time) {
duration := time.Since(start).Nanoseconds()
outcome := "succeeded"
if err := recover(); err != nil {
outcome = "failed"
}
log.Printf("Operation %s %s; time: %d (%dms)\n", op, outcome, duration, duration/1000000)
}

View File

@ -1,5 +1,9 @@
package db package db
import (
"time"
)
type Org struct { type Org struct {
Login string `json:"login"` Login string `json:"login"`
Descr string `json:"descr"` Descr string `json:"descr"`
@ -10,6 +14,7 @@ type Org struct {
const userOrgsQuery = `select o.* from members m join orgs o on o.login = m.org where user = ?` const userOrgsQuery = `select o.* from members m join orgs o on o.login = m.org where user = ?`
func UserOrgs(login string) (orgs []*Org) { func UserOrgs(login string) (orgs []*Org) {
defer measure("UserOrgs", time.Now())
mustSelect(&orgs, userOrgsQuery, login) mustSelect(&orgs, userOrgsQuery, login)
return return
} }

View File

@ -21,10 +21,12 @@ on duplicate key update
updated_at=now()` updated_at=now()`
func (r *Repo) Save() { func (r *Repo) Save() {
defer measure("SaveRepo", time.Now())
mustExecN(saveRepoQuery, r) mustExecN(saveRepoQuery, r)
} }
func OrgRepos(login string) (repos []*Repo) { func OrgRepos(login string) (repos []*Repo) {
defer measure("OrgRepos", time.Now())
mustSelect(&repos, orgReposQuery, login) mustSelect(&repos, orgReposQuery, login)
return return
} }

View File

@ -1,5 +1,9 @@
package db package db
import (
"time"
)
type Team struct { type Team struct {
ID uint64 `json:"id"` ID uint64 `json:"id"`
Owner string `json:"owner"` Owner string `json:"owner"`
@ -9,6 +13,7 @@ type Team struct {
const orgTeamsQuery = `select * from teams where owner = ?` const orgTeamsQuery = `select * from teams where owner = ?`
func OrgTeams(login string) (teams []*Team) { func OrgTeams(login string) (teams []*Team) {
defer measure("OrgTeams", time.Now())
mustSelect(&teams, orgTeamsQuery, login) mustSelect(&teams, orgTeamsQuery, login)
return return
} }

View File

@ -21,5 +21,6 @@ on duplicate key update
quota = values(quota), remaining = values(remaining), reset_at = values(reset_at)` quota = values(quota), remaining = values(remaining), reset_at = values(reset_at)`
func (t *Token) Save() { func (t *Token) Save() {
defer measure("SaveToken", time.Now())
mustExecN(saveTokenQuery, t) mustExecN(saveTokenQuery, t)
} }

View File

@ -1,5 +1,7 @@
package db package db
import "time"
type User struct { type User struct {
Login string `json:"login"` Login string `json:"login"`
Name string `json:"name"` Name string `json:"name"`
@ -14,5 +16,6 @@ on duplicate key update
login=values(login), name=values(name), avatar_url=values(avatar_url)` login=values(login), name=values(name), avatar_url=values(avatar_url)`
func (u *User) Save() { func (u *User) Save() {
defer measure("SaveUser", time.Now())
mustExecN(saveUserQuery, u) mustExecN(saveUserQuery, u)
} }

View File

@ -15,7 +15,7 @@ import (
) )
func Authenticate(code string) (token, login string, err error) { func Authenticate(code string) (token, login string, err error) {
report("Authenticate", time.Now()) defer report("Authenticate", time.Now())
if token, err = FetchAccessToken(code); err != nil { if token, err = FetchAccessToken(code); err != nil {
return return
} }
@ -40,7 +40,7 @@ func Authenticate(code string) (token, login string, err error) {
} }
func FetchAccessToken(code string) (token string, err error) { func FetchAccessToken(code string) (token string, err error) {
report("FetchAccessToken", time.Now()) defer report("FetchAccessToken", time.Now())
payload := url.Values{} payload := url.Values{}
payload.Set("client_id", config.C().ClientID) payload.Set("client_id", config.C().ClientID)
payload.Set("client_secret", config.C().ClientSecret) payload.Set("client_secret", config.C().ClientSecret)
@ -68,7 +68,7 @@ func FetchAccessToken(code string) (token string, err error) {
} }
func FetchUserInfoWithToken(token string) (u *db.User, err error) { func FetchUserInfoWithToken(token string) (u *db.User, err error) {
report("FetchUserInfoWithToken", time.Now()) defer report("FetchUserInfoWithToken", time.Now())
var resp *http.Response var resp *http.Response
if resp, err = http.Get("https://api.github.com/user?access_token=" + token); err != nil { if resp, err = http.Get("https://api.github.com/user?access_token=" + token); err != nil {
return return

View File

@ -8,7 +8,7 @@ import (
) )
func SyncRepos(token, owner string) { func SyncRepos(token, owner string) {
report("SyncRepos", time.Now()) defer report("SyncRepos", time.Now())
client := newGithubClient(token) client := newGithubClient(token)
opt := &github.RepositoryListByOrgOptions{ opt := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{}, ListOptions: github.ListOptions{},
@ -35,7 +35,7 @@ func SyncRepos(token, owner string) {
} }
func SyncContrib(token, owner, repo string) { func SyncContrib(token, owner, repo string) {
report("SyncContrib", time.Now()) defer report("SyncContrib", time.Now())
client := newGithubClient(token) client := newGithubClient(token)
contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo) contribs, resp, err := client.Repositories.ListContributorsStats(owner, repo)
saveResponseMeta(token, resp) saveResponseMeta(token, resp)

View File

@ -31,7 +31,7 @@ func saveResponseMeta(token string, res *github.Response) {
func report(task string, start time.Time) { func report(task string, start time.Time) {
duration := time.Since(start).Nanoseconds() duration := time.Since(start).Nanoseconds()
outcome := "done" outcome := "succeeded"
if err := recover(); err != nil { if err := recover(); err != nil {
outcome = "failed" outcome = "failed"
} }