You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

150 lines
4.1 KiB

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