evaluator.rs 5.2 KB

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