|
| 1 | +use criterion::{Criterion, black_box, criterion_group, criterion_main}; |
| 2 | + |
| 3 | +use aegis_core::engine::GraphEngine; |
| 4 | +use aegis_core::storage::sqlite::{SqliteConfig, SqliteStorage}; |
| 5 | +use aegis_core::storage::{StorageBackend, TupleFilter}; |
| 6 | +use aegis_core::types::schema::{PermissionDef, RelationDef, Schema, TypeDef}; |
| 7 | +use aegis_core::types::*; |
| 8 | +use std::collections::HashMap; |
| 9 | + |
| 10 | +fn setup_engine() -> GraphEngine { |
| 11 | + let schema = Schema { |
| 12 | + schema_version: 1, |
| 13 | + namespace: "bench".to_string(), |
| 14 | + types: { |
| 15 | + let mut types = HashMap::new(); |
| 16 | + let mut relations = HashMap::new(); |
| 17 | + relations.insert( |
| 18 | + "owner".to_string(), |
| 19 | + RelationDef { |
| 20 | + inherit_from: vec![], |
| 21 | + description: None, |
| 22 | + }, |
| 23 | + ); |
| 24 | + relations.insert( |
| 25 | + "viewer".to_string(), |
| 26 | + RelationDef { |
| 27 | + inherit_from: vec![], |
| 28 | + description: None, |
| 29 | + }, |
| 30 | + ); |
| 31 | + let mut permissions = HashMap::new(); |
| 32 | + permissions.insert( |
| 33 | + "read".to_string(), |
| 34 | + PermissionDef { |
| 35 | + union_of: vec!["viewer".to_string(), "owner".to_string()], |
| 36 | + ..Default::default() |
| 37 | + }, |
| 38 | + ); |
| 39 | + types.insert( |
| 40 | + "repo".to_string(), |
| 41 | + TypeDef { |
| 42 | + relations, |
| 43 | + permissions, |
| 44 | + ..Default::default() |
| 45 | + }, |
| 46 | + ); |
| 47 | + types |
| 48 | + }, |
| 49 | + }; |
| 50 | + let mut storage = SqliteStorage::new(SqliteConfig::in_memory()).unwrap(); |
| 51 | + storage.initialize().unwrap(); |
| 52 | + let engine = GraphEngine::new(Box::new(storage), schema); |
| 53 | + |
| 54 | + for i in 0..1000 { |
| 55 | + let subject = SubjectId::new(format!("user:{}", i)).unwrap(); |
| 56 | + let repo = ResourceId::new(format!("repo:list{}", i)).unwrap(); |
| 57 | + engine |
| 58 | + .write(&RelationshipTuple::new( |
| 59 | + subject, |
| 60 | + Relation::new("owner").unwrap(), |
| 61 | + repo, |
| 62 | + )) |
| 63 | + .unwrap(); |
| 64 | + } |
| 65 | + |
| 66 | + let resource = ResourceId::new("repo:populated").unwrap(); |
| 67 | + for i in 0..100 { |
| 68 | + let subject = SubjectId::new(format!("user:pop{}", i)).unwrap(); |
| 69 | + engine |
| 70 | + .write(&RelationshipTuple::new( |
| 71 | + subject, |
| 72 | + Relation::new("viewer").unwrap(), |
| 73 | + resource.clone(), |
| 74 | + )) |
| 75 | + .unwrap(); |
| 76 | + } |
| 77 | + |
| 78 | + let subject = SubjectId::new("user:manyrels").unwrap(); |
| 79 | + for i in 0..50 { |
| 80 | + let repo = ResourceId::new(format!("repo:many{}", i)).unwrap(); |
| 81 | + engine |
| 82 | + .write(&RelationshipTuple::new( |
| 83 | + subject.clone(), |
| 84 | + Relation::new("owner").unwrap(), |
| 85 | + repo, |
| 86 | + )) |
| 87 | + .unwrap(); |
| 88 | + } |
| 89 | + engine |
| 90 | +} |
| 91 | + |
| 92 | +fn bench_list_by_object(c: &mut Criterion) { |
| 93 | + let engine = setup_engine(); |
| 94 | + let resource = ResourceId::new("repo:populated").unwrap(); |
| 95 | + |
| 96 | + c.bench_function("list_by_object", |b| { |
| 97 | + b.iter(|| { |
| 98 | + let result = engine.list_by_object(black_box(&resource), None, None); |
| 99 | + black_box(result) |
| 100 | + }) |
| 101 | + }); |
| 102 | +} |
| 103 | + |
| 104 | +fn bench_list_by_subject(c: &mut Criterion) { |
| 105 | + let engine = setup_engine(); |
| 106 | + let subject = SubjectId::new("user:manyrels").unwrap(); |
| 107 | + |
| 108 | + c.bench_function("list_by_subject", |b| { |
| 109 | + b.iter(|| { |
| 110 | + let result = engine.list_by_subject(black_box(&subject), None, None); |
| 111 | + black_box(result) |
| 112 | + }) |
| 113 | + }); |
| 114 | +} |
| 115 | + |
| 116 | +fn bench_list_pagination(c: &mut Criterion) { |
| 117 | + let engine = setup_engine(); |
| 118 | + let filter = TupleFilter::default(); |
| 119 | + let pagination = PaginationParams::new(10, None); |
| 120 | + |
| 121 | + c.bench_function("list_pagination", |b| { |
| 122 | + b.iter(|| { |
| 123 | + let result = engine.query(black_box(&filter), black_box(&pagination), None); |
| 124 | + black_box(result) |
| 125 | + }) |
| 126 | + }); |
| 127 | +} |
| 128 | + |
| 129 | +fn bench_list_pagination_cursor(c: &mut Criterion) { |
| 130 | + let engine = setup_engine(); |
| 131 | + let filter = TupleFilter::default(); |
| 132 | + let first_page = engine |
| 133 | + .query(&filter, &PaginationParams::new(10, None), None) |
| 134 | + .unwrap(); |
| 135 | + let cursor = first_page.next_cursor.unwrap(); |
| 136 | + |
| 137 | + c.bench_function("list_pagination_cursor", |b| { |
| 138 | + b.iter(|| { |
| 139 | + let pagination = PaginationParams::new(10, Some(black_box(cursor.clone()))); |
| 140 | + let result = engine.query(black_box(&filter), black_box(&pagination), None); |
| 141 | + black_box(result) |
| 142 | + }) |
| 143 | + }); |
| 144 | +} |
| 145 | + |
| 146 | +criterion_group!( |
| 147 | + benches, |
| 148 | + bench_list_by_object, |
| 149 | + bench_list_by_subject, |
| 150 | + bench_list_pagination, |
| 151 | + bench_list_pagination_cursor, |
| 152 | +); |
| 153 | +criterion_main!(benches); |
0 commit comments