From 3c8b8b6ee06a8e369d545f18e80c4e8009032493 Mon Sep 17 00:00:00 2001 From: Yosi Attias Date: Mon, 3 Oct 2022 20:48:05 +0300 Subject: [PATCH] chore: introduce benchmarks --- flattener_bench_test.go | 156 +++++++++++++++++++++++ status.json | 267 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 423 insertions(+) create mode 100644 flattener_bench_test.go create mode 100644 status.json diff --git a/flattener_bench_test.go b/flattener_bench_test.go new file mode 100644 index 0000000..aec7d1c --- /dev/null +++ b/flattener_bench_test.go @@ -0,0 +1,156 @@ +package quamina + +import ( + "os" + "strings" + "testing" +) + +var ( + topMatches []X + topFields []Field +) + +type tracker struct { + names map[string]bool +} + +func (t tracker) IsNameUsed(label []byte) bool { + _, ok := t.names[string(label)] + return ok +} + +const PatternContext = `{ "context": { "user_id": [9034], "friends_count": [158] } }` +const PatternMiddleNestedField = `{ "payload": { "user": { "id_str": ["903487807"] } } }` +const PatternLastField = `{ "payload": { "lang_value": ["ja"] } }` + +func Benchmark_JsonFlattener_ContextFields(b *testing.B) { + RunBehcmarkWithJSONFlattener(b, "context", "user_id", "friends_count") +} + +func Benchmark_JsonFlattener_MiddleNestedField(b *testing.B) { + RunBehcmarkWithJSONFlattener(b, "payload", "user", "id_str") +} + +func Benchmark_JsonFlattener_LastField(b *testing.B) { + RunBehcmarkWithJSONFlattener(b, "payload", "lang_value") +} + +func RunBehcmarkWithJSONFlattener(b *testing.B, fields ...string) { + b.Helper() + var localFields []Field + + event, err := os.ReadFile("./status.json") + if err != nil { + b.Fatal(err) + } + + flattener := newJSONFlattener() + + t := tracker{names: make(map[string]bool)} + for _, field := range fields { + t.names[field] = true + } + results, err := flattener.Flatten(event, t) + if err != nil { + b.Fatal(err) + } + PrintFields(b, results) + + b.ResetTimer() + b.ReportAllocs() + + for i := 0; i < b.N; i++ { + fields, err := flattener.Flatten(event, t) + if err != nil { + b.Fatal(err) + } + + localFields = fields + } + + topFields = localFields +} + +func Benchmark_JsonFlattner_Evaluate_ContextFields(b *testing.B) { + q, err := New() + + if err != nil { + b.Fatal(err) + } + + RunBenchmarkEvaluate(b, q, PatternContext) +} + +func Benchmark_JsonFlattner_Evaluate_MiddleNestedField(b *testing.B) { + q, err := New() + + if err != nil { + b.Fatal(err) + } + + RunBenchmarkEvaluate(b, q, PatternMiddleNestedField) +} + +func Benchmark_JsonFlattner_Evaluate_LastField(b *testing.B) { + q, err := New() + + if err != nil { + b.Fatal(err) + } + + RunBenchmarkEvaluate(b, q, PatternLastField) +} + +func RunBenchmarkEvaluate(b *testing.B, q *Quamina, pattern string) { + b.Helper() + var localMatches []X + + err := q.AddPattern(1, pattern) + if err != nil { + b.Fatalf("Failed adding pattern: %+v", err) + } + + event, err := os.ReadFile("./status.json") + if err != nil { + b.Fatal(err) + } + + matches, err := q.MatchesForEvent(event) + if err != nil { + b.Fatalf("failed matching: %s", err) + } + + if len(matches) != 1 { + b.Fatalf("in-correct matching: %+v", matches) + } + + b.ReportAllocs() + b.ResetTimer() + + for i := 0; i < b.N; i++ { + matches, err := q.MatchesForEvent(event) + if err != nil { + b.Fatalf("failed matching: %s", err) + } + + if len(matches) != 1 { + b.Fatalf("in-correct matching: %+v", matches) + } + + localMatches = matches + } + + topMatches = localMatches +} + +func PrintFields(b *testing.B, fields []Field) { + b.Helper() + + b.Logf("> Fields\n") + + for _, field := range fields { + b.Logf("Path [%s] Val [%s] ArrayTrail [%+v]\n", strings.ReplaceAll(string(field.Path), "\n", "->"), field.Val, field.ArrayTrail) + } + b.Logf("\n") +} diff --git a/status.json b/status.json new file mode 100644 index 0000000..f5d3ab5 --- /dev/null +++ b/status.json @@ -0,0 +1,267 @@ +{ + "context": { + "user_id": 9034, + "friends_count": 158 + }, + "payload": { + "metadata": { + "result_type": "recent", + "iso_language_code": "ja" + }, + "created_at": "Sun Aug 31 00:29:14 +0000 2014", + "id": 505874922023837700, + "id_str": "505874922023837696", + "text": "RT @KATANA77: えっそれは・・・(一同) http://t.co/PkCJAcSuYK", + "source": "Twitter for iPhone", + "truncated": false, + "in_reply_to_status_id": null, + "in_reply_to_status_id_str": null, + "in_reply_to_user_id": null, + "in_reply_to_user_id_str": null, + "in_reply_to_screen_name": null, + "user": { + "id": 903487807, + "id_str": "903487807", + "name": "RT&ファボ魔のむっつんさっm", + "screen_name": "yuttari1998", + "location": "関西 ↓詳しいプロ↓", + "description": "無言フォローはあまり好みません ゲームと動画が好きですシモ野郎ですがよろしく…最近はMGSとブレイブルー、音ゲーをプレイしてます", + "url": "http://t.co/Yg9e1Fl8wd", + "entities": { + "url": { + "urls": [ + { + "url": "http://t.co/Yg9e1Fl8wd", + "expanded_url": "http://twpf.jp/yuttari1998", + "display_url": "twpf.jp/yuttari1998", + "indices": [0, 22] + } + ] + }, + "description": { + "urls": ["http://empty.bug.com"] + } + }, + "protected": false, + "followers_count": 95, + "friends_count": 158, + "listed_count": 1, + "created_at": "Thu Oct 25 08:27:13 +0000 2012", + "favourites_count": 3652, + "utc_offset": null, + "time_zone": null, + "geo_enabled": false, + "verified": false, + "statuses_count": 10276, + "lang": "ja", + "contributors_enabled": false, + "is_translator": false, + "is_translation_enabled": false, + "profile_background_color": "C0DEED", + "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", + "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", + "profile_background_tile": false, + "profile_image_url": "http://pbs.twimg.com/profile_images/500268849275494400/AoXHZ7Ij_normal.jpeg", + "profile_image_url_https": "https://pbs.twimg.com/profile_images/500268849275494400/AoXHZ7Ij_normal.jpeg", + "profile_banner_url": "https://pbs.twimg.com/profile_banners/903487807/1409062272", + "profile_link_color": "0084B4", + "profile_sidebar_border_color": "C0DEED", + "profile_sidebar_fill_color": "DDEEF6", + "profile_text_color": "333333", + "profile_use_background_image": true, + "default_profile": true, + "default_profile_image": false, + "following": false, + "follow_request_sent": false, + "notifications": false + }, + "geo": null, + "coordinates": null, + "place": null, + "contributors": null, + "retweeted_status": { + "metadata": { + "result_type": "recent", + "iso_language_code": "ja" + }, + "created_at": "Sat Aug 30 23:49:35 +0000 2014", + "id": 505864943636197400, + "id_str": "505864943636197376", + "text": "えっそれは・・・(一同) http://t.co/PkCJAcSuYK", + "source": "Twitter Web Client", + "truncated": false, + "in_reply_to_status_id": null, + "in_reply_to_status_id_str": null, + "in_reply_to_user_id": null, + "in_reply_to_user_id_str": null, + "in_reply_to_screen_name": null, + "user": { + "id": 77915997, + "id_str": "77915997", + "name": "(有)刀", + "screen_name": "KATANA77", + "location": "", + "description": "プリキュア好きのサラリーマンです。好きなプリキュアシリーズはハートキャッチ、最愛のキャラクターは月影ゆりさんです。 http://t.co/QMLJeFmfMTご質問、お問い合わせはこちら http://t.co/LU8T7vmU3h", + "url": null, + "entities": { + "description": { + "urls": [ + { + "url": "http://t.co/QMLJeFmfMT", + "expanded_url": "http://www.pixiv.net/member.php?id=4776", + "display_url": "pixiv.net/member.php?id=…", + "indices": [58, 80] + }, + { + "url": "http://t.co/LU8T7vmU3h", + "expanded_url": "http://ask.fm/KATANA77", + "display_url": "ask.fm/KATANA77", + "indices": [95, 117] + } + ] + } + }, + "protected": false, + "followers_count": 1095, + "friends_count": 740, + "listed_count": 50, + "created_at": "Mon Sep 28 03:41:27 +0000 2009", + "favourites_count": 3741, + "utc_offset": 32400, + "time_zone": "Tokyo", + "geo_enabled": true, + "verified": false, + "statuses_count": 19059, + "lang": "ja", + "contributors_enabled": false, + "is_translator": false, + "is_translation_enabled": false, + "profile_background_color": "C0DEED", + "profile_background_image_url": "http://pbs.twimg.com/profile_background_images/808597451/45b82f887085d32bd4b87dfc348fe22a.png", + "profile_background_image_url_https": "https://pbs.twimg.com/profile_background_images/808597451/45b82f887085d32bd4b87dfc348fe22a.png", + "profile_background_tile": true, + "profile_image_url": "http://pbs.twimg.com/profile_images/480210114964504577/MjVIEMS4_normal.jpeg", + "profile_image_url_https": "https://pbs.twimg.com/profile_images/480210114964504577/MjVIEMS4_normal.jpeg", + "profile_banner_url": "https://pbs.twimg.com/profile_banners/77915997/1404661392", + "profile_link_color": "0084B4", + "profile_sidebar_border_color": "FFFFFF", + "profile_sidebar_fill_color": "DDEEF6", + "profile_text_color": "333333", + "profile_use_background_image": true, + "default_profile": false, + "default_profile_image": false, + "following": false, + "follow_request_sent": false, + "notifications": false + }, + "geo": null, + "coordinates": null, + "place": null, + "contributors": null, + "retweet_count": 82, + "favorite_count": 42, + "entities": { + "hashtags": ["#empty"], + "symbols": ["#empty"], + "urls": ["empty.bug.com"], + "user_mentions": ["empty_user"], + "media": [ + { + "id": 505864942575034400, + "id_str": "505864942575034369", + "indices": [13, 35], + "media_url": "http://pbs.twimg.com/media/BwUxfC6CIAEr-Ye.jpg", + "media_url_https": "https://pbs.twimg.com/media/BwUxfC6CIAEr-Ye.jpg", + "url": "http://t.co/PkCJAcSuYK", + "display_url": "pic.twitter.com/PkCJAcSuYK", + "expanded_url": "http://twitter.com/KATANA77/status/505864943636197376/photo/1", + "type": "photo", + "sizes": { + "medium": { + "w": 600, + "h": 338, + "resize": "fit" + }, + "small": { + "w": 340, + "h": 192, + "resize": "fit" + }, + "thumb": { + "w": 150, + "h": 150, + "resize": "crop" + }, + "large": { + "w": 765, + "h": 432, + "resize": "fit" + } + } + } + ] + }, + "favorited": false, + "retweeted": false, + "possibly_sensitive": false, + "lang": "ja" + }, + "retweet_count": 82, + "favorite_count": 0, + "entities": { + "hashtags": ["#empty"], + "symbols": ["empty"], + "urls": ["empty.bug.com"], + "user_mentions": [ + { + "screen_name": "KATANA77", + "name": "(有)刀", + "id": 77915997, + "id_str": "77915997", + "indices": [3, 12] + } + ], + "media": [ + { + "id": 505864942575034400, + "id_str": "505864942575034369", + "indices": [27, 49], + "media_url": "http://pbs.twimg.com/media/BwUxfC6CIAEr-Ye.jpg", + "media_url_https": "https://pbs.twimg.com/media/BwUxfC6CIAEr-Ye.jpg", + "url": "http://t.co/PkCJAcSuYK", + "display_url": "pic.twitter.com/PkCJAcSuYK", + "expanded_url": "http://twitter.com/KATANA77/status/505864943636197376/photo/1", + "type": "photo", + "sizes": { + "medium": { + "w": 600, + "h": 338, + "resize": "fit" + }, + "small": { + "w": 340, + "h": 192, + "resize": "fit" + }, + "thumb": { + "w": 150, + "h": 150, + "resize": "crop" + }, + "large": { + "w": 765, + "h": 432, + "resize": "fit" + } + }, + "source_status_id": 505864943636197400, + "source_status_id_str": "505864943636197376" + } + ] + }, + "favorited": false, + "retweeted": false, + "possibly_sensitive": false, + "lang_value": "ja" + } +}