Skip to content

Commit

Permalink
Merge branch 'master' of github.com:catatsuy/isucon9-qualify into opt…
Browse files Browse the repository at this point in the history
…imized-app
  • Loading branch information
kazeburo committed Aug 28, 2019
2 parents 424c99c + 2a76e34 commit 725b048
Show file tree
Hide file tree
Showing 21 changed files with 594 additions and 195 deletions.
22 changes: 21 additions & 1 deletion bench/scenario/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,27 @@ const (
FailedCardNumber = "FA10AAAA"
)

func LoginedSession(ctx context.Context, user1 asset.AppUser) (*session.Session, error) {
func activeSellerSession(ctx context.Context) (*session.Session, error) {
s := ActiveSellerPool.Dequeue()
if s != nil {
return s, nil
}

user1 := asset.GetRandomActiveSeller()
return loginedSession(ctx, user1)
}

func buyerSession(ctx context.Context) (*session.Session, error) {
s := BuyerPool.Dequeue()
if s != nil {
return s, nil
}

user1 := asset.GetRandomBuyer()
return loginedSession(ctx, user1)
}

func loginedSession(ctx context.Context, user1 asset.AppUser) (*session.Session, error) {
s1, err := session.NewSession()
if err != nil {
return nil, err
Expand Down
95 changes: 65 additions & 30 deletions bench/scenario/normal.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,21 @@ import (
)

func initialize(ctx context.Context, paymentServiceURL, shipmentServiceURL string) (bool, error) {
s1, err := session.NewSession()
s1, err := session.NewSessionForInialize()
if err != nil {
return false, err
}

return s1.Initialize(ctx, paymentServiceURL, shipmentServiceURL)
}

func sellAndBuy(ctx context.Context, user1, user2 asset.AppUser) error {
s1, err := LoginedSession(ctx, user1)
if err != nil {
return err
}

s2, err := LoginedSession(ctx, user2)
func loadSellNewCategoryBuyWithLoginedSession(ctx context.Context, s1, s2 *session.Session) error {
targetItemID, err := sell(ctx, s1, 100)
if err != nil {
return err
}

targetItemID, err := sell(ctx, s1, 100)
err = loadNewCategoryItemsWithLoginedSession(ctx, s1)
if err != nil {
return err
}
Expand All @@ -43,70 +38,110 @@ func sellAndBuy(ctx context.Context, user1, user2 asset.AppUser) error {
return nil
}

func loadSellNewCategoryBuyWithLoginedSession(ctx context.Context, s1, s2 *session.Session) error {
targetItemID, err := sell(ctx, s1, 100)
func transactionEvidence(ctx context.Context, s1 *session.Session) error {
hasNext, items, err := s1.UsersTransactions(ctx)
if err != nil {
return err
}

err = loadNewCategoryItemsWithLoginedSession(ctx, s1)
if err != nil {
return err
if !hasNext {
return failure.New(fails.ErrApplication, failure.Messagef("/users/transactions.jsonのhas_nextがfalseになっています (user_id: %d)", s1.UserID))
}

err = buyComplete(ctx, s1, s2, targetItemID, 100)
if err != nil {
return err
for _, item := range items {
if item.TransactionEvidenceID == 0 {
// TODO: check
continue
}

ate, ok := asset.GetTransactionEvidence(item.TransactionEvidenceID)
if ok && item.TransactionEvidenceStatus != ate.Status {
return failure.New(fails.ErrApplication, failure.Messagef("/users/transactions.jsonのステータスに誤りがあります (user_id: %d)", s1.UserID))
}
}

return nil
}
targetItemID, targetItemCreatedAt := items[len(items)/2].ID, items[len(items)/2].CreatedAt

func transactionEvidenceWithLoginedSession(ctx context.Context, s1 *session.Session) error {
_, items, err := s1.UsersTransactions(ctx)
_, items, err = s1.UsersTransactionsWithItemIDAndCreatedAt(ctx, targetItemID, targetItemCreatedAt)
if err != nil {
return err
}

for _, item := range items {
if !(item.ID < targetItemID && item.CreatedAt <= targetItemCreatedAt) {
return failure.New(fails.ErrApplication, failure.Messagef("/users/transactions.jsonのitem_idとcreated_atが正しく動作していません (user_id: %d)", s1.UserID))
}

if item.TransactionEvidenceID == 0 {
// TODO: check
continue
}

ate, ok := asset.GetTransactionEvidence(item.TransactionEvidenceID)
if ok && item.TransactionEvidenceStatus != ate.Status {
return failure.New(fails.ErrApplication, failure.Message("/users/transactions.jsonのステータスに誤りがあります"))
return failure.New(fails.ErrApplication, failure.Messagef("/users/transactions.jsonのステータスに誤りがあります (user_id: %d)", s1.UserID))
}
}

targetItemID, targetItemCreatedAt := items[len(items)/2].ID, items[len(items)/2].CreatedAt
return nil
}

_, items, err = s1.UsersTransactionsWithItemIDAndCreatedAt(ctx, targetItemID, targetItemCreatedAt)
func loadTransactionEvidence(ctx context.Context, s1 *session.Session) error {
hasNext, items, err := s1.UsersTransactions(ctx)
if err != nil {
return err
}

for _, item := range items {
if !(item.ID < targetItemID && item.CreatedAt <= targetItemCreatedAt) {
return failure.New(fails.ErrApplication, failure.Message("/users/transactions.jsonのitem_idとcreated_atが正しく動作していません"))
}
if !hasNext {
return failure.New(fails.ErrApplication, failure.Messagef("/users/transactions.jsonのhas_nextがfalseになっています (user_id: %d)", s1.UserID))
}

for _, item := range items {
if item.TransactionEvidenceID == 0 {
// TODO: check
continue
}

ate, ok := asset.GetTransactionEvidence(item.TransactionEvidenceID)
if ok && item.TransactionEvidenceStatus != ate.Status {
return failure.New(fails.ErrApplication, failure.Message("/users/transactions.jsonのステータスに誤りがあります"))
return failure.New(fails.ErrApplication, failure.Messagef("/users/transactions.jsonのステータスに誤りがあります (user_id: %d)", s1.UserID))
}
}

for {
targetItemID, targetItemCreatedAt := items[len(items)/2].ID, items[len(items)/2].CreatedAt

hasNext, items, err = s1.UsersTransactionsWithItemIDAndCreatedAt(ctx, targetItemID, targetItemCreatedAt)
if err != nil {
return err
}

if !hasNext {
// TODO: check
break
}

for _, item := range items {
if !(item.ID < targetItemID && item.CreatedAt <= targetItemCreatedAt) {
return failure.New(fails.ErrApplication, failure.Messagef("/users/transactions.jsonのitem_idとcreated_atが正しく動作していません (user_id: %d)", s1.UserID))
}

if item.TransactionEvidenceID == 0 {
// TODO: check
continue
}

ate, ok := asset.GetTransactionEvidence(item.TransactionEvidenceID)
if ok && item.TransactionEvidenceStatus != ate.Status {
return failure.New(fails.ErrApplication, failure.Messagef("/users/transactions.jsonのステータスに誤りがあります (user_id: %d)", s1.UserID))
}
}
}

return nil
}

func userItemsAndItemWithLoginedSession(ctx context.Context, s1 *session.Session, userID int64) error {
func userItemsAndItem(ctx context.Context, s1 *session.Session, userID int64) error {
_, user, items, err := s1.UserItems(ctx, userID)
if err != nil {
return err
Expand Down
59 changes: 59 additions & 0 deletions bench/scenario/pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package scenario

import (
"sync"

"github.com/isucon/isucon9-qualify/bench/session"
)

var (
ActiveSellerPool *Queue
BuyerPool *Queue
)

type Queue struct {
sync.Mutex

items []*session.Session
}

func InitSessionPool() {
ActiveSellerPool = NewQueue()
BuyerPool = NewQueue()
}

func NewQueue() *Queue {
m := make([]*session.Session, 0, 100)
q := &Queue{
items: m,
}
return q
}

func (q *Queue) Enqueue(s *session.Session) {
q.Lock()
defer q.Unlock()

q.items = append(q.items, s)
}

func (q *Queue) Dequeue() *session.Session {
q.Lock()
defer q.Unlock()

if len(q.items) == 0 {
return nil
}

s := q.items[0]
q.items = q.items[1:]

return s
}

func (q *Queue) Len() int {
q.Lock()
defer q.Unlock()

return len(q.items)
}
Loading

0 comments on commit 725b048

Please sign in to comment.