diff --git a/Makefile b/Makefile index 26cff48..88fc85c 100644 --- a/Makefile +++ b/Makefile @@ -8,10 +8,6 @@ test: run-local: @ENV_ENVIRONMENT=local go run main.go -.PHONY: run-cloud9 -run-cloud9: - @ENV_ENVIRONMENT=cloud9 go run main.go - .PHONY: generate generate: go generate ${CURDIR}/... diff --git a/README.md b/README.md index dce1743..5d4c643 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,32 @@ # server-performance-tuning-2023 -## Cloud9で実行する場合 +## 実行方法 + +### Docker を使用する場合(推奨) +```shell +# MySQL と Redis を起動 +$ docker-compose up -d + +# テストデータを投入(オプション) +$ ./scripts/insert-test-data.sh + +# アプリケーションを起動 +$ make run-local + +# 停止する場合 +$ docker-compose down +``` + +### Docker を使用しない場合 ```shell $ make setup -$ make run-cloud9 -``` \ No newline at end of file +$ make run-local +``` + +## テストデータについて + +`scripts/insert-test-data.sh` を実行すると、以下のテストデータが投入されます: +- 5 genres(アニメ、ドラマ、バラエティ、映画、格闘技) +- 5 series(ONE PIECE、名探偵コナン、ドラゴンボール、NARUTO、進撃の巨人) +- 3 seasons +- 3 episodes \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b0d09ae --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,36 @@ +services: + mysql: + image: mysql:8.0 + container_name: wsperf-mysql + environment: + MYSQL_ROOT_PASSWORD: "" + MYSQL_ALLOW_EMPTY_PASSWORD: "yes" + MYSQL_DATABASE: wsperf + ports: + - "3306:3306" + volumes: + - mysql-data:/var/lib/mysql + - ./pkg/db/sql/ddl.sql:/docker-entrypoint-initdb.d/01-schema.sql + command: --default-authentication-plugin=mysql_native_password + healthcheck: + test: ["CMD", "mysqladmin", "ping", "-h", "localhost"] + interval: 5s + timeout: 3s + retries: 10 + + redis: + image: redis:7-alpine + container_name: wsperf-redis + ports: + - "6379:6379" + volumes: + - redis-data:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + +volumes: + mysql-data: + redis-data: diff --git a/k6/load-test.js b/k6/load-test.js index e4c103e..ffdee5a 100644 --- a/k6/load-test.js +++ b/k6/load-test.js @@ -13,12 +13,12 @@ export const options = { gracefulStop: '10s', - preAllocatedVUs: 20, + preAllocatedVUs: 50, stages: [ // target: 1 秒あたりの load_test 関数の実行回数の目標値 // duration: target 到達までにかかる時間 - { target: 5, duration: '1m' }, // 1分かけてload_test関数の実行回数を5まで大きくする - { target: 5, duration: '1m' }, // 1秒あたりの実行回数5回を1分間維持する + { target: 20, duration: '1m' }, // 1分かけて target 20 に到達 + { target: 20, duration: '1m' }, // target 20 を1分間維持 ], }, }, @@ -27,7 +27,7 @@ export const options = { export function load_test() { const seriesURL = new URL(`${__ENV.API_BASE_URL}/series`); - const offset = Math.floor(Math.random() * 20) + const offset = Math.floor(Math.random() * 100) seriesURL.searchParams.append(`limit`, `20`); seriesURL.searchParams.append(`offset`, `${offset}`); diff --git a/pkg/app/app.go b/pkg/app/app.go index 5a47239..542df3f 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -22,7 +22,7 @@ import ( type App struct { logger *zap.Logger Level string `default:"debug"` - Environment string `default:"cloud9"` + Environment string `default:"local"` Port int `default:"8080"` DbSecretName string RedisEndpoint string diff --git a/pkg/config/config.go b/pkg/config/config.go index 8851316..1874572 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -28,18 +28,6 @@ func NewConfig(env string, dbSecretName string, redisEndpoint string) (*Config, EnableTracing: true, }, } - case "cloud9": - cfg = &Config{ - DBConfig: &config.DBConfig{ - SecretsManagerDBConfig: &config.SecretsManagerDBConfig{ - SecretID: dbSecretName, - }, - }, - RedisEndpoint: redisEndpoint, - TraceConfig: &TraceConfig{ - EnableTracing: false, - }, - } case "local": cfg = &Config{ DBConfig: &config.DBConfig{ diff --git a/pkg/repository/database/episode.go b/pkg/repository/database/episode.go index 8c1f5f7..6518208 100644 --- a/pkg/repository/database/episode.go +++ b/pkg/repository/database/episode.go @@ -44,7 +44,7 @@ func (e *Episode) List(ctx context.Context, params *repository.ListEpisodesParam } if params.SeriesID != "" { clauses = append(clauses, "seriesID = ?") - args = append(args, params.SeasonID) + args = append(args, params.SeriesID) } var whereClause string diff --git a/scripts/insert-test-data.sh b/scripts/insert-test-data.sh new file mode 100755 index 0000000..c4ef534 --- /dev/null +++ b/scripts/insert-test-data.sh @@ -0,0 +1,53 @@ +#!/bin/bash + +set -e + +echo "Inserting test data into MySQL..." + +# genres +docker exec -i wsperf-mysql mysql -u root wsperf << 'EOF' +INSERT INTO genres (genreID, displayName) VALUES +('976-755', 'アニメ'), +('878-285', 'ドラマ'), +('51-872', 'バラエティ'), +('816-680', '映画'), +('719-306', '格闘技'); +EOF + +echo "✓ Inserted 5 genres" + +# series +docker exec -i wsperf-mysql mysql -u root wsperf << 'EOF' +INSERT INTO series (seriesID, displayName, description, imageURL, genreID) VALUES +('374-745', 'ONE PIECE', 'これは作品 ONE PIECE の説明文です。様々なジャンルの面白いコンテンツをお楽しみください。', 'https://image.p-c2-x.abema-tv.com/image/series/374-745', '976-755'), +('77-257', '名探偵コナン', 'これは作品 名探偵コナン の説明文です。様々なジャンルの面白いコンテンツをお楽しみください。', 'https://image.p-c2-x.abema-tv.com/image/series/77-257', '878-285'), +('937-848', 'ドラゴンボール', 'これは作品 ドラゴンボール の説明文です。様々なジャンルの面白いコンテンツをお楽しみください。', 'https://image.p-c2-x.abema-tv.com/image/series/937-848', '51-872'), +('703-400', 'NARUTO', 'これは作品 NARUTO の説明文です。様々なジャンルの面白いコンテンツをお楽しみください。', 'https://image.p-c2-x.abema-tv.com/image/series/703-400', '816-680'), +('565-598', '進撃の巨人', 'これは作品 進撃の巨人 の説明文です。様々なジャンルの面白いコンテンツをお楽しみください。', 'https://image.p-c2-x.abema-tv.com/image/series/565-598', '719-306'); +EOF + +echo "✓ Inserted 5 series" + +# seasons +docker exec -i wsperf-mysql mysql -u root wsperf << 'EOF' +INSERT INTO seasons (seasonID, seriesID, displayName, imageURL, displayOrder) VALUES +('374-745_s1', '374-745', 'シーズン1', 'https://image.p-c2-x.abema-tv.com/image/series/374-745/season1', 1), +('374-745_s2', '374-745', 'シーズン2', 'https://image.p-c2-x.abema-tv.com/image/series/374-745/season2', 2), +('77-257_s1', '77-257', 'シーズン1', 'https://image.p-c2-x.abema-tv.com/image/series/77-257/season1', 1); +EOF + +echo "✓ Inserted 3 seasons" + +# episodes +docker exec -i wsperf-mysql mysql -u root wsperf << 'EOF' +INSERT INTO episodes (episodeID, seasonID, seriesID, displayName, description, imageURL, displayOrder) VALUES +('374-745_s1_e1', '374-745_s1', '374-745', '第1話', 'ONE PIECE 第1話の説明', 'https://image.p-c2-x.abema-tv.com/image/episode/374-745_s1_e1', 1), +('374-745_s1_e2', '374-745_s1', '374-745', '第2話', 'ONE PIECE 第2話の説明', 'https://image.p-c2-x.abema-tv.com/image/episode/374-745_s1_e2', 2), +('77-257_s1_e1', '77-257_s1', '77-257', '第1話', '名探偵コナン 第1話の説明', 'https://image.p-c2-x.abema-tv.com/image/episode/77-257_s1_e1', 1); +EOF + +echo "✓ Inserted 3 episodes" + +echo "" +echo "Test data inserted successfully!" +echo "Total: 5 genres, 5 series, 3 seasons, 3 episodes"