diff --git a/bench/asset/asset.go b/bench/asset/asset.go index afe4f3933..f214a2409 100644 --- a/bench/asset/asset.go +++ b/bench/asset/asset.go @@ -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 ) @@ -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 { @@ -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 { @@ -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))] } diff --git a/bench/scenario/action.go b/bench/scenario/action.go index 9c0b22d6d..2f940330a 100644 --- a/bench/scenario/action.go +++ b/bench/scenario/action.go @@ -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 } diff --git a/bench/scenario/wrong.go b/bench/scenario/wrong.go index b25aca2cd..e26340f06 100644 --- a/bench/scenario/wrong.go +++ b/bench/scenario/wrong.go @@ -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 } diff --git a/bench/session/webapp.go b/bench/session/webapp.go index 143b4e159..91c4f6f22 100644 --- a/bench/session/webapp.go +++ b/bench/session/webapp.go @@ -311,8 +311,8 @@ 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に失敗しました")) } @@ -320,7 +320,7 @@ func (s *Session) Sell(ctx context.Context, name string, price int, description 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: リクエストに失敗しました")) } diff --git a/bench/session/wrongapp.go b/bench/session/wrongapp.go index b320642d9..0cccbebce 100644 --- a/bench/session/wrongapp.go +++ b/bench/session/wrongapp.go @@ -66,8 +66,8 @@ 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に失敗しました")) } @@ -75,7 +75,7 @@ func (s *Session) SellWithWrongCSRFToken(ctx context.Context, name string, price 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: リクエストに失敗しました")) } @@ -125,8 +125,8 @@ 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に失敗しました")) } @@ -134,7 +134,7 @@ func (s *Session) SellWithWrongPrice(ctx context.Context, name string, price int 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: リクエストに失敗しました")) } diff --git a/initial-data/.gitignore b/initial-data/.gitignore index e9d7e6de2..6cb4dd996 100644 --- a/initial-data/.gitignore +++ b/initial-data/.gitignore @@ -1,3 +1,4 @@ result/*.sql result/*.txt !result/category_json.txt +images/ diff --git a/initial-data/images/sample.jpg b/initial-data/images/sample.jpg new file mode 100644 index 000000000..96a8d43c7 Binary files /dev/null and b/initial-data/images/sample.jpg differ