evaluator.rs 5.1 KB

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