|
| 1 | +// Copyright 2022 The Matrix.org Foundation C.I.C. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#![feature(test)] |
| 16 | +use synapse::push::{ |
| 17 | + evaluator::PushRuleEvaluator, Condition, EventMatchCondition, FilteredPushRules, PushRules, |
| 18 | +}; |
| 19 | +use test::Bencher; |
| 20 | + |
| 21 | +extern crate test; |
| 22 | + |
| 23 | +#[bench] |
| 24 | +fn bench_match_exact(b: &mut Bencher) { |
| 25 | + let flattened_keys = [ |
| 26 | + ("type".to_string(), "m.text".to_string()), |
| 27 | + ("room_id".to_string(), "!room:server".to_string()), |
| 28 | + ("content.body".to_string(), "test message".to_string()), |
| 29 | + ] |
| 30 | + .into_iter() |
| 31 | + .collect(); |
| 32 | + |
| 33 | + let eval = PushRuleEvaluator::py_new( |
| 34 | + flattened_keys, |
| 35 | + 10, |
| 36 | + 0, |
| 37 | + Default::default(), |
| 38 | + Default::default(), |
| 39 | + true, |
| 40 | + ) |
| 41 | + .unwrap(); |
| 42 | + |
| 43 | + let condition = Condition::Known(synapse::push::KnownCondition::EventMatch( |
| 44 | + EventMatchCondition { |
| 45 | + key: "room_id".into(), |
| 46 | + pattern: Some("!room:server".into()), |
| 47 | + pattern_type: None, |
| 48 | + }, |
| 49 | + )); |
| 50 | + |
| 51 | + let matched = eval.match_condition(&condition, None, None).unwrap(); |
| 52 | + assert!(matched, "Didn't match"); |
| 53 | + |
| 54 | + b.iter(|| eval.match_condition(&condition, None, None).unwrap()); |
| 55 | +} |
| 56 | + |
| 57 | +#[bench] |
| 58 | +fn bench_match_word(b: &mut Bencher) { |
| 59 | + let flattened_keys = [ |
| 60 | + ("type".to_string(), "m.text".to_string()), |
| 61 | + ("room_id".to_string(), "!room:server".to_string()), |
| 62 | + ("content.body".to_string(), "test message".to_string()), |
| 63 | + ] |
| 64 | + .into_iter() |
| 65 | + .collect(); |
| 66 | + |
| 67 | + let eval = PushRuleEvaluator::py_new( |
| 68 | + flattened_keys, |
| 69 | + 10, |
| 70 | + 0, |
| 71 | + Default::default(), |
| 72 | + Default::default(), |
| 73 | + true, |
| 74 | + ) |
| 75 | + .unwrap(); |
| 76 | + |
| 77 | + let condition = Condition::Known(synapse::push::KnownCondition::EventMatch( |
| 78 | + EventMatchCondition { |
| 79 | + key: "content.body".into(), |
| 80 | + pattern: Some("test".into()), |
| 81 | + pattern_type: None, |
| 82 | + }, |
| 83 | + )); |
| 84 | + |
| 85 | + let matched = eval.match_condition(&condition, None, None).unwrap(); |
| 86 | + assert!(matched, "Didn't match"); |
| 87 | + |
| 88 | + b.iter(|| eval.match_condition(&condition, None, None).unwrap()); |
| 89 | +} |
| 90 | + |
| 91 | +#[bench] |
| 92 | +fn bench_match_word_miss(b: &mut Bencher) { |
| 93 | + let flattened_keys = [ |
| 94 | + ("type".to_string(), "m.text".to_string()), |
| 95 | + ("room_id".to_string(), "!room:server".to_string()), |
| 96 | + ("content.body".to_string(), "test message".to_string()), |
| 97 | + ] |
| 98 | + .into_iter() |
| 99 | + .collect(); |
| 100 | + |
| 101 | + let eval = PushRuleEvaluator::py_new( |
| 102 | + flattened_keys, |
| 103 | + 10, |
| 104 | + 0, |
| 105 | + Default::default(), |
| 106 | + Default::default(), |
| 107 | + true, |
| 108 | + ) |
| 109 | + .unwrap(); |
| 110 | + |
| 111 | + let condition = Condition::Known(synapse::push::KnownCondition::EventMatch( |
| 112 | + EventMatchCondition { |
| 113 | + key: "content.body".into(), |
| 114 | + pattern: Some("foobar".into()), |
| 115 | + pattern_type: None, |
| 116 | + }, |
| 117 | + )); |
| 118 | + |
| 119 | + let matched = eval.match_condition(&condition, None, None).unwrap(); |
| 120 | + assert!(!matched, "Didn't match"); |
| 121 | + |
| 122 | + b.iter(|| eval.match_condition(&condition, None, None).unwrap()); |
| 123 | +} |
| 124 | + |
| 125 | +#[bench] |
| 126 | +fn bench_eval_message(b: &mut Bencher) { |
| 127 | + let flattened_keys = [ |
| 128 | + ("type".to_string(), "m.text".to_string()), |
| 129 | + ("room_id".to_string(), "!room:server".to_string()), |
| 130 | + ("content.body".to_string(), "test message".to_string()), |
| 131 | + ] |
| 132 | + .into_iter() |
| 133 | + .collect(); |
| 134 | + |
| 135 | + let eval = PushRuleEvaluator::py_new( |
| 136 | + flattened_keys, |
| 137 | + 10, |
| 138 | + 0, |
| 139 | + Default::default(), |
| 140 | + Default::default(), |
| 141 | + true, |
| 142 | + ) |
| 143 | + .unwrap(); |
| 144 | + |
| 145 | + let rules = |
| 146 | + FilteredPushRules::py_new(PushRules::new(Vec::new()), Default::default(), false, false); |
| 147 | + |
| 148 | + b.iter(|| eval.run(&rules, Some("bob"), Some("person"))); |
| 149 | +} |
0 commit comments