evaluator.rs 4.2 KB

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