evaluator.rs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. #![feature(test)]
  15. use std::collections::BTreeSet;
  16. use synapse::push::{
  17. evaluator::PushRuleEvaluator, Condition, EventMatchCondition, FilteredPushRules, JsonValue,
  18. PushRules, SimpleJsonValue,
  19. };
  20. use test::Bencher;
  21. extern crate test;
  22. #[bench]
  23. fn bench_match_exact(b: &mut Bencher) {
  24. let flattened_keys = [
  25. (
  26. "type".to_string(),
  27. JsonValue::Value(SimpleJsonValue::Str("m.text".to_string())),
  28. ),
  29. (
  30. "room_id".to_string(),
  31. JsonValue::Value(SimpleJsonValue::Str("!room:server".to_string())),
  32. ),
  33. (
  34. "content.body".to_string(),
  35. JsonValue::Value(SimpleJsonValue::Str("test message".to_string())),
  36. ),
  37. ]
  38. .into_iter()
  39. .collect();
  40. let eval = PushRuleEvaluator::py_new(
  41. flattened_keys,
  42. false,
  43. BTreeSet::new(),
  44. 10,
  45. Some(0),
  46. Default::default(),
  47. Default::default(),
  48. true,
  49. vec![],
  50. false,
  51. false,
  52. false,
  53. )
  54. .unwrap();
  55. let condition = Condition::Known(synapse::push::KnownCondition::EventMatch(
  56. EventMatchCondition {
  57. key: "room_id".into(),
  58. pattern: Some("!room:server".into()),
  59. pattern_type: None,
  60. },
  61. ));
  62. let matched = eval.match_condition(&condition, None, None).unwrap();
  63. assert!(matched, "Didn't match");
  64. b.iter(|| eval.match_condition(&condition, None, None).unwrap());
  65. }
  66. #[bench]
  67. fn bench_match_word(b: &mut Bencher) {
  68. let flattened_keys = [
  69. (
  70. "type".to_string(),
  71. JsonValue::Value(SimpleJsonValue::Str("m.text".to_string())),
  72. ),
  73. (
  74. "room_id".to_string(),
  75. JsonValue::Value(SimpleJsonValue::Str("!room:server".to_string())),
  76. ),
  77. (
  78. "content.body".to_string(),
  79. JsonValue::Value(SimpleJsonValue::Str("test message".to_string())),
  80. ),
  81. ]
  82. .into_iter()
  83. .collect();
  84. let eval = PushRuleEvaluator::py_new(
  85. flattened_keys,
  86. false,
  87. BTreeSet::new(),
  88. 10,
  89. Some(0),
  90. Default::default(),
  91. Default::default(),
  92. true,
  93. vec![],
  94. false,
  95. false,
  96. false,
  97. )
  98. .unwrap();
  99. let condition = Condition::Known(synapse::push::KnownCondition::EventMatch(
  100. EventMatchCondition {
  101. key: "content.body".into(),
  102. pattern: Some("test".into()),
  103. pattern_type: None,
  104. },
  105. ));
  106. let matched = eval.match_condition(&condition, None, None).unwrap();
  107. assert!(matched, "Didn't match");
  108. b.iter(|| eval.match_condition(&condition, None, None).unwrap());
  109. }
  110. #[bench]
  111. fn bench_match_word_miss(b: &mut Bencher) {
  112. let flattened_keys = [
  113. (
  114. "type".to_string(),
  115. JsonValue::Value(SimpleJsonValue::Str("m.text".to_string())),
  116. ),
  117. (
  118. "room_id".to_string(),
  119. JsonValue::Value(SimpleJsonValue::Str("!room:server".to_string())),
  120. ),
  121. (
  122. "content.body".to_string(),
  123. JsonValue::Value(SimpleJsonValue::Str("test message".to_string())),
  124. ),
  125. ]
  126. .into_iter()
  127. .collect();
  128. let eval = PushRuleEvaluator::py_new(
  129. flattened_keys,
  130. false,
  131. BTreeSet::new(),
  132. 10,
  133. Some(0),
  134. Default::default(),
  135. Default::default(),
  136. true,
  137. vec![],
  138. false,
  139. false,
  140. false,
  141. )
  142. .unwrap();
  143. let condition = Condition::Known(synapse::push::KnownCondition::EventMatch(
  144. EventMatchCondition {
  145. key: "content.body".into(),
  146. pattern: Some("foobar".into()),
  147. pattern_type: None,
  148. },
  149. ));
  150. let matched = eval.match_condition(&condition, None, None).unwrap();
  151. assert!(!matched, "Didn't match");
  152. b.iter(|| eval.match_condition(&condition, None, None).unwrap());
  153. }
  154. #[bench]
  155. fn bench_eval_message(b: &mut Bencher) {
  156. let flattened_keys = [
  157. (
  158. "type".to_string(),
  159. JsonValue::Value(SimpleJsonValue::Str("m.text".to_string())),
  160. ),
  161. (
  162. "room_id".to_string(),
  163. JsonValue::Value(SimpleJsonValue::Str("!room:server".to_string())),
  164. ),
  165. (
  166. "content.body".to_string(),
  167. JsonValue::Value(SimpleJsonValue::Str("test message".to_string())),
  168. ),
  169. ]
  170. .into_iter()
  171. .collect();
  172. let eval = PushRuleEvaluator::py_new(
  173. flattened_keys,
  174. false,
  175. BTreeSet::new(),
  176. 10,
  177. Some(0),
  178. Default::default(),
  179. Default::default(),
  180. true,
  181. vec![],
  182. false,
  183. false,
  184. false,
  185. )
  186. .unwrap();
  187. let rules = FilteredPushRules::py_new(
  188. PushRules::new(Vec::new()),
  189. Default::default(),
  190. false,
  191. false,
  192. false,
  193. false,
  194. false,
  195. );
  196. b.iter(|| eval.run(&rules, Some("bob"), Some("person")));
  197. }