evaluator.rs 5.2 KB

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