mirror of
https://github.com/element-hq/synapse.git
synced 2024-12-14 11:57:44 +00:00
11bc9a1b3a
Some checks failed
Tests / check-lockfile (push) Has been cancelled
/ Check locked dependencies have sdists (push) Has been cancelled
Build release artifacts / Calculate list of debian distros (push) Has been cancelled
Tests / changes (push) Has been cancelled
Build docker images / build (push) Has been cancelled
Deploy the documentation / Calculate variables for GitHub Pages deployment (push) Has been cancelled
Build release artifacts / Build wheels on ubuntu-20.04 for aarch64 (push) Has been cancelled
Build release artifacts / Build wheels on macos-12 for x86_64 (push) Has been cancelled
Build release artifacts / Build wheels on ubuntu-20.04 for x86_64 (push) Has been cancelled
Build release artifacts / Build sdist (push) Has been cancelled
Tests / lint-crlf (push) Has been cancelled
Tests / lint-newsfile (push) Has been cancelled
Tests / check-schema-delta (push) Has been cancelled
Tests / lint (push) Has been cancelled
Tests / calculate-test-jobs (push) Has been cancelled
Build release artifacts / Build .deb packages (push) Has been cancelled
Tests / check-sampleconfig (push) Has been cancelled
Deploy the documentation / GitHub Pages (push) Has been cancelled
Build release artifacts / Attach assets to release (push) Has been cancelled
Tests / Typechecking (push) Has been cancelled
Tests / lint-pydantic (push) Has been cancelled
Tests / lint-clippy (push) Has been cancelled
Tests / lint-clippy-nightly (push) Has been cancelled
Tests / lint-rustfmt (push) Has been cancelled
Tests / lint-readme (push) Has been cancelled
Tests / linting-done (push) Has been cancelled
Tests / trial (push) Has been cancelled
Tests / trial-olddeps (push) Has been cancelled
Tests / trial-pypy (all, pypy-3.8) (push) Has been cancelled
Tests / sytest (push) Has been cancelled
Tests / export-data (push) Has been cancelled
Tests / portdb (11, 3.8) (push) Has been cancelled
Tests / portdb (15, 3.11) (push) Has been cancelled
Tests / complement (monolith, Postgres) (push) Has been cancelled
Tests / complement (monolith, SQLite) (push) Has been cancelled
Tests / complement (workers, Postgres) (push) Has been cancelled
Tests / cargo-test (push) Has been cancelled
Tests / cargo-bench (push) Has been cancelled
Tests / tests-done (push) Has been cancelled
216 lines
5.4 KiB
Rust
216 lines
5.4 KiB
Rust
/*
|
|
* This file is licensed under the Affero General Public License (AGPL) version 3.
|
|
*
|
|
* Copyright 2022 The Matrix.org Foundation C.I.C.
|
|
* Copyright (C) 2023 New Vector, Ltd
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
* License, or (at your option) any later version.
|
|
*
|
|
* See the GNU Affero General Public License for more details:
|
|
* <https://www.gnu.org/licenses/agpl-3.0.html>.
|
|
*
|
|
* Originally licensed under the Apache License, Version 2.0:
|
|
* <http://www.apache.org/licenses/LICENSE-2.0>.
|
|
*
|
|
* [This file includes modifications made by New Vector Limited]
|
|
*
|
|
*/
|
|
|
|
#![feature(test)]
|
|
|
|
use std::borrow::Cow;
|
|
|
|
use synapse::push::{
|
|
evaluator::PushRuleEvaluator, Condition, EventMatchCondition, FilteredPushRules, JsonValue,
|
|
PushRules, SimpleJsonValue,
|
|
};
|
|
use test::Bencher;
|
|
|
|
extern crate test;
|
|
|
|
#[bench]
|
|
fn bench_match_exact(b: &mut Bencher) {
|
|
let flattened_keys = [
|
|
(
|
|
"type".to_string(),
|
|
JsonValue::Value(SimpleJsonValue::Str(Cow::Borrowed("m.text"))),
|
|
),
|
|
(
|
|
"room_id".to_string(),
|
|
JsonValue::Value(SimpleJsonValue::Str(Cow::Borrowed("!room:server"))),
|
|
),
|
|
(
|
|
"content.body".to_string(),
|
|
JsonValue::Value(SimpleJsonValue::Str(Cow::Borrowed("test message"))),
|
|
),
|
|
]
|
|
.into_iter()
|
|
.collect();
|
|
|
|
let eval = PushRuleEvaluator::py_new(
|
|
flattened_keys,
|
|
false,
|
|
10,
|
|
Some(0),
|
|
Default::default(),
|
|
Default::default(),
|
|
true,
|
|
vec![],
|
|
false,
|
|
false,
|
|
)
|
|
.unwrap();
|
|
|
|
let condition = Condition::Known(synapse::push::KnownCondition::EventMatch(
|
|
EventMatchCondition {
|
|
key: "room_id".into(),
|
|
pattern: "!room:server".into(),
|
|
},
|
|
));
|
|
|
|
let matched = eval.match_condition(&condition, None, None).unwrap();
|
|
assert!(matched, "Didn't match");
|
|
|
|
b.iter(|| eval.match_condition(&condition, None, None).unwrap());
|
|
}
|
|
|
|
#[bench]
|
|
fn bench_match_word(b: &mut Bencher) {
|
|
let flattened_keys = [
|
|
(
|
|
"type".to_string(),
|
|
JsonValue::Value(SimpleJsonValue::Str(Cow::Borrowed("m.text"))),
|
|
),
|
|
(
|
|
"room_id".to_string(),
|
|
JsonValue::Value(SimpleJsonValue::Str(Cow::Borrowed("!room:server"))),
|
|
),
|
|
(
|
|
"content.body".to_string(),
|
|
JsonValue::Value(SimpleJsonValue::Str(Cow::Borrowed("test message"))),
|
|
),
|
|
]
|
|
.into_iter()
|
|
.collect();
|
|
|
|
let eval = PushRuleEvaluator::py_new(
|
|
flattened_keys,
|
|
false,
|
|
10,
|
|
Some(0),
|
|
Default::default(),
|
|
Default::default(),
|
|
true,
|
|
vec![],
|
|
false,
|
|
false,
|
|
)
|
|
.unwrap();
|
|
|
|
let condition = Condition::Known(synapse::push::KnownCondition::EventMatch(
|
|
EventMatchCondition {
|
|
key: "content.body".into(),
|
|
pattern: "test".into(),
|
|
},
|
|
));
|
|
|
|
let matched = eval.match_condition(&condition, None, None).unwrap();
|
|
assert!(matched, "Didn't match");
|
|
|
|
b.iter(|| eval.match_condition(&condition, None, None).unwrap());
|
|
}
|
|
|
|
#[bench]
|
|
fn bench_match_word_miss(b: &mut Bencher) {
|
|
let flattened_keys = [
|
|
(
|
|
"type".to_string(),
|
|
JsonValue::Value(SimpleJsonValue::Str(Cow::Borrowed("m.text"))),
|
|
),
|
|
(
|
|
"room_id".to_string(),
|
|
JsonValue::Value(SimpleJsonValue::Str(Cow::Borrowed("!room:server"))),
|
|
),
|
|
(
|
|
"content.body".to_string(),
|
|
JsonValue::Value(SimpleJsonValue::Str(Cow::Borrowed("test message"))),
|
|
),
|
|
]
|
|
.into_iter()
|
|
.collect();
|
|
|
|
let eval = PushRuleEvaluator::py_new(
|
|
flattened_keys,
|
|
false,
|
|
10,
|
|
Some(0),
|
|
Default::default(),
|
|
Default::default(),
|
|
true,
|
|
vec![],
|
|
false,
|
|
false,
|
|
)
|
|
.unwrap();
|
|
|
|
let condition = Condition::Known(synapse::push::KnownCondition::EventMatch(
|
|
EventMatchCondition {
|
|
key: "content.body".into(),
|
|
pattern: "foobar".into(),
|
|
},
|
|
));
|
|
|
|
let matched = eval.match_condition(&condition, None, None).unwrap();
|
|
assert!(!matched, "Didn't match");
|
|
|
|
b.iter(|| eval.match_condition(&condition, None, None).unwrap());
|
|
}
|
|
|
|
#[bench]
|
|
fn bench_eval_message(b: &mut Bencher) {
|
|
let flattened_keys = [
|
|
(
|
|
"type".to_string(),
|
|
JsonValue::Value(SimpleJsonValue::Str(Cow::Borrowed("m.text"))),
|
|
),
|
|
(
|
|
"room_id".to_string(),
|
|
JsonValue::Value(SimpleJsonValue::Str(Cow::Borrowed("!room:server"))),
|
|
),
|
|
(
|
|
"content.body".to_string(),
|
|
JsonValue::Value(SimpleJsonValue::Str(Cow::Borrowed("test message"))),
|
|
),
|
|
]
|
|
.into_iter()
|
|
.collect();
|
|
|
|
let eval = PushRuleEvaluator::py_new(
|
|
flattened_keys,
|
|
false,
|
|
10,
|
|
Some(0),
|
|
Default::default(),
|
|
Default::default(),
|
|
true,
|
|
vec![],
|
|
false,
|
|
false,
|
|
)
|
|
.unwrap();
|
|
|
|
let rules = FilteredPushRules::py_new(
|
|
PushRules::new(Vec::new()),
|
|
Default::default(),
|
|
false,
|
|
false,
|
|
false,
|
|
false,
|
|
false,
|
|
);
|
|
|
|
b.iter(|| eval.run(&rules, Some("bob"), Some("person")));
|
|
}
|