Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
ユニットテスト並行化の導入と運用:Goでの事例紹介
Search
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Shoki Hata
September 12, 2023
Programming
940
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
ユニットテスト並行化の導入と運用:Goでの事例紹介
テストパフォーマンス向上のためのテスト戦略 〜自動化の秘訣 Lunch LT〜 の登壇資料になります。
Shoki Hata
September 12, 2023
More Decks by Shoki Hata
See All by Shoki Hata
panicと向き合うGo開発 - nilawayで探る見逃されるnil参照とその対策
shohata
1
840
GoのLanguage Server Protocol実装、 「gopls」の自動補完の仕組みを学ぶ
shohata
8
2.6k
無理なく始めるGoでのユニットテストの並行化戦略
shohata
2
2.4k
Contribute to Hasura
shohata
0
750
Hasura Conference 2022 Shoki_Hata
shohata
0
300
最近話題のGraphQLサーバー、Hasuraを半年使ってみて
shohata
1
540
Other Decks in Programming
See All in Programming
typoなんかねぇよ
raspython3
0
680
Snowflakeで業務アプリを作ろう。 Snowflakeのアプリ機能解説&実践ガイド
ayumu_yamaguchi
1
210
Hello, Hiroshima Geospatial Data! — Exploring DoboX with Python
ra0kley
0
180
新卒PdEのリアル
ryu1013
1
480
Seeing Through Serverless: Observability for AWS Lambda with ADOT and CloudWatch Application Signals
seike460
PRO
1
130
GKE アップグレード前に知っておきたい Blue/Green と PDB の関係
stkk
0
160
Security issues being discussed on Web Platforms
petamoriken
0
500
不幸な GC
chencmd
0
900
Laravelのアプリケーションをどこにデプロイするか #ツナギメオフライン.9
akase244
0
120
高専キャリア LT 発表内容
crysta1221
6
5.6k
PHPプロジェクトの結合バランスを可視化する #php_night
kajitack
0
230
Deep dive into the select statement (GopherCon UK)
jespino
0
170
Featured
See All Featured
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
520
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
10k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
42k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.9k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1k
Building Flexible Design Systems
yeseniaperezcruz
330
41k
Designing Experiences People Love
moore
143
24k
Transcript
ユニットテスト並行化の 導入と運用:Goでの事例紹介 Shoki Hata テストパフォーマンス向上のためのテスト戦略 〜自動化の秘訣 Lunch LT〜 September 13
wed
Copyright Kanmu, Inc. All right reserved. 2 畠 翔紀 software
engineer at Kanmu, Inc. @sho-hata @sho_hata_ 自己紹介
Copyright Kanmu, Inc. All right reserved. 3 本発表の目的 ユニットテストの並行化を 無理なく導入・運用するための情報を提供すること
Copyright Kanmu, Inc. All right reserved. 4 おことわり ユニットテストの並行化を 無理なく導入・運用するための情報を提供すること
・Go言語でのユースケースをお伝えします ・本紹介で紹介するアプローチは、他言語でも 可能ですので適宜読み替えていただければと思います
Copyright Kanmu, Inc. All right reserved. プロダクトの紹介 5 1
Copyright Kanmu, Inc. All right reserved. Poolチーム紹介 6 1 ・PdM:1名
・デザイナー:1名 ・Bizdev(事業開発):1名 ・フロント/バックエンドエンジニア: 4名 ・SRE:1名
Copyright Kanmu, Inc. All right reserved. ユニットテスト並行化とは 2
Copyright Kanmu, Inc. All right reserved. 「個々のユニットテストを独立に実行できる構成に すること」 と本発表では定義します Goのユニットテスト並行化とは
2
Copyright Kanmu, Inc. All right reserved. 並列・並行という用語について 9 2 テキストを入れたり。テキストを入れたり。テキストを入れた
り。テキストを入れたり。テキストを入れたり。テキストを入 れたり。テキストを入れたり。テキストを入れたり。 ・並行:複数の処理を独立に実行できる構成のこと ・並列:複数の処理を同時に実行すること 参考: “Concurrency is not Parallelism”, Heroku’s Waza conference,Rob Pike(2012)
Copyright Kanmu, Inc. All right reserved. テストを並行化すると、どのようなメリットがある? 10 2 テキストを入れたり。テキストを入れたり。テキストを入れた
り。テキストを入れたり。テキストを入れたり。テキストを入 れたり。テキストを入れたり。テキストを入れたり。 ・テスト時間の短縮が期待できる ・テスト対象/テストコードが並列実行に対応している ことを担保できる ・システムリソースの効率的な利用ができる ・CI/CDパイプラインを最適化できる
Copyright Kanmu, Inc. All right reserved. どうやってGoのテストは並列実行される? 11 2 Goでは、テストコマンド実行時オプション
「-parallel」でサポートされている -parallel オプション パッケージ内のテストごとの 並列実行をサポート
Copyright Kanmu, Inc. All right reserved. Aパッケージ パッケージ内のテストの並列実行数を指定する-parallel オプション 12
2 TestX TestY Aパッケージ TestX TestY ※マルチコア環境を想定
Copyright Kanmu, Inc. All right reserved. -parallelは「t.Parallel」を呼んでいるテスト関数のみ対象 13 2 case3
case1 case2 テストコード テスト実行 ※マルチコア環境を想定
Copyright Kanmu, Inc. All right reserved. ユニットテスト並行化に伴う課題 3
Copyright Kanmu, Inc. All right reserved. 課題:並行化コードを記述するのが面倒 15 3 テスト対象に並行化マーカーを
チマチマ記述していかないといけない
Copyright Kanmu, Inc. All right reserved. 16 3 課題:データベース接続テストの並行化 id
name 1 “NIGHT FISHING” 2 “KikUUiki” 3 “DocumentaLy” 4 “GO TO THE FUTURE” 5 ???? album テーブル 並列実行 テストA テストB あるテストの変更が他のテストケースに影響を及ぼしてしまう! ※マルチコア環境を想定
Copyright Kanmu, Inc. All right reserved. 時間も人も有限!プロダクト改善真っ只中! 今はユニットテストの改善に大きなリソースを割けない... 課題をどう解決していくか 3
Copyright Kanmu, Inc. All right reserved. テストの並行化にそんなにリソースを割けない 3 テスト並行化のメリット ・テスト時間の短縮が期待できる
・テスト対象/テストコードが並列実行に対応している ことを担保できるetc…
Copyright Kanmu, Inc. All right reserved. テストの並行化にそんなにリソースを割けない 3 テスト並行化のメリット ・テスト時間の短縮が期待できる
・テスト対象/テストコードが並列実行に対応している ことを担保できるetc… (今は)なるべくプロダクトの成長に直結する施策に 開発リソースを割きたい! メリットはわかる(重要)けど....
Copyright Kanmu, Inc. All right reserved. ・並行化コードを記述するのが面倒! ・データベース接続テストの並行化 テスト並行化の導入・運用のアプローチ 3
人力ではなく、ツールで一気に テストを並行化する
Copyright Kanmu, Inc. All right reserved. Goのテストコードを一気に 並行化するツール「tparagen」 4
Copyright Kanmu, Inc. All right reserved. 22 4 tparagenの紹介 ・https://github.com/sho-hata/tparagen
・対象ディレクトリ配下のGoファイルのテスト関数 に t.Parallel(=並行化マーカー)を埋め込む ・ソースコードを静的解析。テスト関数が並行化 できる条件に一致していればASTをいじって埋め込む
Copyright Kanmu, Inc. All right reserved. 23 4 tparagenの紹介:動作
Copyright Kanmu, Inc. All right reserved. ・並行化コードを記述するのが面倒! ・テストコード上での並行化特有の挙動 ・データベース接続テストの並行化 データベース接続テスト並行化に対してのアプローチ
4 各テストケースごとに独立した データ操作環境にする
Copyright Kanmu, Inc. All right reserved. 事例紹介:Pool開発チーム
Copyright Kanmu, Inc. All right reserved. データベースと接続するテストの並行化には、 テストケースごとに独立した環境の 構築がポイント
Copyright Kanmu, Inc. All right reserved. 27 4 事例紹介:テストケースごとに独立した環境の構築 実現したいこと
・テスト終了後にデータ変更が残らない ・並列実行時に他のテストに影響を与えない 方針 ・テストケースごとにトランザクションを貼る ・テストケース終了後、ロールバック ・テスト対象がトランザクション処理をしている場合、 動作をモック化
Copyright Kanmu, Inc. All right reserved. 28 4 事例紹介:pgtxdbを利用したテスト環境の構築 https://github.com/achiku/pgtxdbを使う
以下の特徴をもつDBコネクションを生成する ・接続が開かれるとトランザクションを開始 ・接続が閉じるとロールバック ・トランザクションがモック化される ・BEGIN -> SAVEPOINT ・COMMIT -> 何もしない ・ROLLBACK -> ROLLBACK TO SAVEPOINT どうやるか?
Copyright Kanmu, Inc. All right reserved. 29 4 事例紹介:pgtxdbを利用したテスト環境 テストA
テストB トランザクションは互いに独立。並列実行時にも問題なし
Copyright Kanmu, Inc. All right reserved. まとめ ・テスト並行化は、開発サイクルの速度向上や並列実行時の動作 担 保が期待できる ・無理なく導入・運用するには、並行化コードの自動挿入ツールの導
入を検討する ・データベースと接続するテストを並行化するには、 テストケースごとに独立したデータ操作環境を整備する
Copyright Kanmu, Inc. All right reserved. - 無理なく始めるGoでのユニットテスト並行化 - カンムテックブログ
- LTではお伝えできなかった部分に踏み込んで、より詳細に説明しています - https://tech.kanmu.co.jp/entry/2023/06/02/172458 - 無理なく始めるGoでのユニットテスト並行化戦略 - Go Conference 2023でお話しした内容です - https://speakerdeck.com/shohata/go-conference-2023 参考資料
Copyright Kanmu, Inc. All right reserved.