Skip to content

Commit

Permalink
Merge pull request #386 from catatsuy/feature_upload_various_image
Browse files Browse the repository at this point in the history
[bench] upload the various images
  • Loading branch information
catatsuy authored Aug 30, 2019
2 parents cd341e8 + ae6015b commit 3829d1d
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 15 deletions.
33 changes: 33 additions & 0 deletions bench/asset/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ var (
userItems map[int64][]int64
transactionEvidences map[int64]AppTransactionEvidence
keywords []string
imageFiles []string
muItem sync.RWMutex
muUser sync.RWMutex
muImageFile sync.Mutex
indexImageFile int
indexActiveSellerID int32
indexBuyerID int32
)
Expand All @@ -106,6 +109,7 @@ func Initialize(dataDir string) {
childCategories = make([]AppCategory, 0, 50)
userItems = make(map[int64][]int64)
transactionEvidences = make(map[int64]AppTransactionEvidence)
imageFiles = make([]string, 0, 10000)

f, err := os.Open(filepath.Join(dataDir, "result/users_json.txt"))
if err != nil {
Expand Down Expand Up @@ -203,8 +207,24 @@ func Initialize(dataDir string) {
}
f.Close()

d, err := os.Open(filepath.Join(dataDir, "images"))
if err != nil {
log.Fatal(err)
}
defer d.Close()

files, err := d.Readdir(-1)
if err != nil {
log.Fatal(err)
}

for _, file := range files {
imageFiles = append(imageFiles, filepath.Join(dataDir, "images", file.Name()))
}

rand.Shuffle(len(activeSellerIDs), func(i, j int) { activeSellerIDs[i], activeSellerIDs[j] = activeSellerIDs[j], activeSellerIDs[i] })
rand.Shuffle(len(buyerIDs), func(i, j int) { buyerIDs[i], buyerIDs[j] = buyerIDs[j], buyerIDs[i] })
rand.Shuffle(len(imageFiles), func(i, j int) { imageFiles[i], imageFiles[j] = imageFiles[j], imageFiles[i] })
}

func (u1 *AppUser) Equal(u2 *AppUser) bool {
Expand Down Expand Up @@ -309,6 +329,19 @@ func SetItemCreatedAt(sellerID int64, itemID int64, createdAt int64) {
items[key] = item
}

func GetRandomImageFileName() string {
muImageFile.Lock()
defer muImageFile.Unlock()

indexImageFile--

if indexImageFile < 0 {
indexImageFile = len(imageFiles) - 1
}

return imageFiles[indexImageFile]
}

func GetRandomRootCategory() AppCategory {
return rootCategories[rand.Intn(len(rootCategories))]
}
Expand Down
4 changes: 2 additions & 2 deletions bench/scenario/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ func loginedSession(ctx context.Context, user1 asset.AppUser) (*session.Session,
}

func sell(ctx context.Context, s1 *session.Session, price int) (int64, error) {
name, description, categoryID := asset.GenText(8, false), asset.GenText(200, true), 32
fileName, name, description, categoryID := asset.GetRandomImageFileName(), asset.GenText(8, false), asset.GenText(200, true), 32

targetItemID, err := s1.Sell(ctx, name, price, description, categoryID)
targetItemID, err := s1.Sell(ctx, fileName, name, price, description, categoryID)
if err != nil {
return 0, err
}
Expand Down
8 changes: 4 additions & 4 deletions bench/scenario/wrong.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,22 @@ func irregularLoginWrongPassword(ctx context.Context, user1 asset.AppUser) error
}

func irregularSellAndBuy(ctx context.Context, s1, s2 *session.Session, user3 asset.AppUser) error {
name, description := asset.GenText(8, false), asset.GenText(200, true)
fileName, name, description := asset.GetRandomImageFileName(), asset.GenText(8, false), asset.GenText(200, true)

price := priceStoreCache.Get()

err := s1.SellWithWrongCSRFToken(ctx, name, price, description, 32)
err := s1.SellWithWrongCSRFToken(ctx, fileName, name, price, description, 32)
if err != nil {
return err
}

// 変な値段で買えない
err = s1.SellWithWrongPrice(ctx, name, session.ItemMinPrice-1, description, 32)
err = s1.SellWithWrongPrice(ctx, fileName, name, session.ItemMinPrice-1, description, 32)
if err != nil {
return err
}

err = s1.SellWithWrongPrice(ctx, name, session.ItemMaxPrice+1, description, 32)
err = s1.SellWithWrongPrice(ctx, fileName, name, session.ItemMaxPrice+1, description, 32)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions bench/session/webapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,16 +311,16 @@ func (s *Session) SetSettings(ctx context.Context) error {
return nil
}

func (s *Session) Sell(ctx context.Context, name string, price int, description string, categoryID int) (int64, error) {
file, err := os.Open("webapp/public/upload/sample.jpg")
func (s *Session) Sell(ctx context.Context, fileName, name string, price int, description string, categoryID int) (int64, error) {
file, err := os.Open(fileName)
if err != nil {
return 0, failure.Wrap(err, failure.Message("POST /sell: 画像のOpenに失敗しました"))
}
defer file.Close()

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("image", "sample.jpg")
part, err := writer.CreateFormFile("image", "upload.jpg")
if err != nil {
return 0, failure.Wrap(err, failure.Message("POST /sell: リクエストに失敗しました"))
}
Expand Down
12 changes: 6 additions & 6 deletions bench/session/wrongapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ func (s *Session) LoginWithWrongPassword(ctx context.Context, accountName, passw
return nil
}

func (s *Session) SellWithWrongCSRFToken(ctx context.Context, name string, price int, description string, categoryID int) error {
file, err := os.Open("webapp/public/upload/sample.jpg")
func (s *Session) SellWithWrongCSRFToken(ctx context.Context, fileName, name string, price int, description string, categoryID int) error {
file, err := os.Open(fileName)
if err != nil {
return failure.Wrap(err, failure.Message("POST /sell: 画像のOpenに失敗しました"))
}
defer file.Close()

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("image", "sample.jpg")
part, err := writer.CreateFormFile("image", "upload.jpg")
if err != nil {
return failure.Wrap(err, failure.Message("POST /sell: リクエストに失敗しました"))
}
Expand Down Expand Up @@ -125,16 +125,16 @@ func (s *Session) SellWithWrongCSRFToken(ctx context.Context, name string, price
return nil
}

func (s *Session) SellWithWrongPrice(ctx context.Context, name string, price int, description string, categoryID int) error {
file, err := os.Open("webapp/public/upload/sample.jpg")
func (s *Session) SellWithWrongPrice(ctx context.Context, fileName, name string, price int, description string, categoryID int) error {
file, err := os.Open(fileName)
if err != nil {
return failure.Wrap(err, failure.Message("POST /sell: 画像のOpenに失敗しました"))
}
defer file.Close()

body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
part, err := writer.CreateFormFile("image", "sample.jpg")
part, err := writer.CreateFormFile("image", "upload.jpg")
if err != nil {
return failure.Wrap(err, failure.Message("POST /sell: リクエストに失敗しました"))
}
Expand Down
1 change: 1 addition & 0 deletions initial-data/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
result/*.sql
result/*.txt
!result/category_json.txt
images/
Binary file added initial-data/images/sample.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3829d1d

Please sign in to comment.