Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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}/...
Expand Down
31 changes: 28 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
```
$ make run-local
```

## テストデータについて

`scripts/insert-test-data.sh` を実行すると、以下のテストデータが投入されます:
- 5 genres(アニメ、ドラマ、バラエティ、映画、格闘技)
- 5 series(ONE PIECE、名探偵コナン、ドラゴンボール、NARUTO、進撃の巨人)
- 3 seasons
- 3 episodes
36 changes: 36 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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:
8 changes: 4 additions & 4 deletions k6/load-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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分間維持
],
},
},
Expand All @@ -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}`);

Expand Down
2 changes: 1 addition & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 0 additions & 12 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion pkg/repository/database/episode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
53 changes: 53 additions & 0 deletions scripts/insert-test-data.sh
Original file line number Diff line number Diff line change
@@ -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"