evaluator.rs 4.5 KB

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