Skip to content

Instantly share code, notes, and snippets.

@rmboyle
Last active January 20, 2021 15:30
Show Gist options
  • Save rmboyle/2fa79539c319b04664bd6e794ae10d32 to your computer and use it in GitHub Desktop.
Save rmboyle/2fa79539c319b04664bd6e794ae10d32 to your computer and use it in GitHub Desktop.
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act, eft
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow)) && !some(where (p.eft == deny))
[matchers]
m = g(r.sub, p.sub) && (r.obj == p.obj || p.obj == "*") && r.act == p.act
export const backgroundAnnotation = {
uniqueId: '8ceccd67-bc73-4a38-b19a-54ba7aa97fd0',
last_sync_time: '12-25-2020',
index: 0,
page_id: 123,
creation_mode: 'EDIT',
creation_permission: 'SELLER',
type: 'BackgroundImage',
metadata: {
fabric_object: JSON.stringify({
context: { mode: 'EDIT', permission: 'SELLER', userId: 'system' },
type: 'BackgroundImage',
imageData: {
type: 'image',
version: '3.6.3',
originX: 'left',
originY: 'top',
left: 175,
top: 20,
width: 1240,
height: 1755,
fill: 'rgb(0,0,0)',
stroke: null,
strokeWidth: 0,
strokeDashArray: null,
strokeLineCap: 'butt',
strokeDashOffset: 0,
strokeLineJoin: 'miter',
strokeMiterLimit: 4,
scaleX: 0.28,
scaleY: 0.28,
angle: 0,
flipX: false,
flipY: false,
opacity: 1,
shadow: null,
visible: true,
clipTo: null,
backgroundColor: '',
fillRule: 'nonzero',
paintFirst: 'fill',
globalCompositeOperation: 'source-over',
transformMatrix: null,
skewX: 0,
skewY: 0,
crossOrigin: '',
cropX: 0,
cropY: 0,
src:
'https://tpt-digital-tool-dev.s3.amazonaws.com/assets/images/61/222b90bcea4da2982f54a8e1890a9b/page_-3.png',
filters: []
},
studentData: null
})
}
};
Perf script
Diff 23.715888
Casbin 2024.049177
const penAnnotation = {
uniqueId: '8ceccd67-bc73-4a38-b19a-54ba7aa97fd1',
last_sync_time: '12-26-2020',
index: 1,
page_id: 123,
creation_mode: 'EDIT',
creation_permission: 'TEACHER',
type: 'Pen',
metadata: {
fabric_object: JSON.stringify({
pathData: {
type: 'path',
version: '3.6.3',
originX: 'left',
originY: 'top',
left: 61.84,
top: 97.33,
width: 236.17,
height: 123.47,
fill: null,
stroke: '#0F6EEE',
strokeWidth: 5,
strokeDashArray: null,
strokeLineCap: 'round',
strokeDashOffset: 0,
strokeLineJoin: 'round',
strokeMiterLimit: 10,
scaleX: 1,
scaleY: 1,
angle: 0,
flipX: false,
flipY: false,
opacity: 1,
shadow: null,
visible: true,
clipTo: null,
backgroundColor: '',
fillRule: 'nonzero',
paintFirst: 'fill',
globalCompositeOperation: 'source-over',
transformMatrix: null,
skewX: 0,
skewY: 0,
path: [
['M', 64.33843434343434, 189.49149027663358],
[
'Q',
64.34343434343434,
189.4864902766336,
64.34343434343434,
189.13297070522196
],
[
'Q',
64.34343434343434,
188.7794511338103,
64.69696969696969,
187.7188924195754
],
[
'Q',
65.05050505050505,
186.65833370534054,
65.4040404040404,
185.59777499110567
],
[
'Q',
65.75757575757575,
184.53721627687077,
67.52525252525251,
181.00202056275447
],
[
'Q',
69.29292929292929,
177.46682484863814,
70.7070707070707,
174.9921878487567
],
[
'Q',
72.12121212121212,
172.51755084887532,
75.65656565656566,
166.154198563466
],
[
'Q',
79.1919191919192,
159.79084627805668,
81.66666666666667,
155.90213099252875
],
[
'Q',
84.14141414141415,
152.01341570700083,
88.73737373737373,
142.82190685029843
],
[
'Q',
93.33333333333333,
133.63039799359606,
95.8080808080808,
129.03464356524486
],
[
'Q',
98.28282828282828,
124.43888913689368,
102.87878787878788,
117.72201728007272
],
[
'Q',
107.47474747474747,
111.00514542325176,
109.24242424242425,
108.88402799478197
],
[
'Q',
111.01010101010101,
106.7629105663122,
112.77777777777777,
104.64179313784243
],
[
'Q',
114.54545454545455,
102.52067570937264,
115.25252525252526,
101.46011699513775
],
[
'Q',
115.95959595959596,
100.39955828090285,
116.31313131313132,
100.04603870949123
],
[
'Q',
116.66666666666667,
99.69251913807959,
116.66666666666667,
100.75307785231448
],
[
'Q',
116.66666666666667,
101.81363656654938,
115.95959595959596,
107.46994970913545
],
[
'Q',
115.25252525252526,
113.12626285172153,
114.54545454545455,
117.01497813724946
],
[
'Q',
113.83838383838383,
120.90369342277738,
113.13131313131314,
127.97408485100999
],
[
'Q',
112.42424242424242,
135.0444762792426,
112.07070707070707,
138.5796719933589
],
[
'Q',
111.71717171717172,
142.11486770747518,
112.07070707070707,
147.064141707238
],
[
'Q',
112.42424242424242,
152.01341570700083,
112.77777777777777,
154.48805270688223
],
[
'Q',
113.13131313131314,
156.96268970676363,
113.83838383838383,
160.85140499229155
],
[
'Q',
114.54545454545455,
164.74012027781947,
115.60606060606061,
167.56827684911252
],
[
'Q',
116.66666666666667,
170.39643342040557,
117.02020202020202,
172.16403127746372
],
[
'Q',
117.37373737373737,
173.93162913452184,
118.08080808080808,
175.34570742016837
],
[
'Q',
118.78787878787878,
176.7597857058149,
120.20202020202021,
180.64850099134281
],
[
'Q',
121.61616161616162,
184.53721627687077,
121.96969696969697,
186.65833370534054
],
[
'Q',
122.32323232323232,
188.7794511338103,
123.03030303030303,
192.3146468479266
],
[
'Q',
123.73737373737374,
195.8498425620429,
124.0909090909091,
197.61744041910106
],
[
'Q',
124.44444444444444,
199.38503827615918,
125.15151515151516,
201.50615570462895
],
[
'Q',
125.85858585858585,
203.62727313309875,
125.85858585858585,
204.68783184733365
],
[
'Q',
125.85858585858585,
205.74839056156853,
126.21212121212122,
206.8089492758034
],
[
'Q',
126.56565656565657,
207.8695079900383,
126.56565656565657,
208.22302756144995
],
[
'Q',
126.56565656565657,
208.57654713286158,
127.97979797979798,
206.4554297043918
],
[
'Q',
129.3939393939394,
204.334312275922,
131.16161616161617,
201.15263613321736
],
[
'Q',
132.92929292929293,
197.9709599905127,
137.52525252525254,
191.60760770510336
],
[
'Q',
142.12121212121212,
185.244255419694,
145.65656565656565,
181.70905970557772
],
[
'Q',
149.1919191919192,
178.17386399146142,
153.7878787878788,
173.22458999169862
],
[
'Q',
158.3838383838384,
168.2753159919358,
167.57575757575756,
158.37676799241015
],
[
'Q',
176.76767676767676,
148.4782199928845,
182.07070707070707,
142.82190685029843
],
[
'Q',
187.37373737373738,
137.16559370771236,
193.73737373737373,
129.03464356524486
],
[
'Q',
200.1010101010101,
120.90369342277738,
202.57575757575756,
118.07553685148434
],
[
'Q',
205.05050505050505,
115.24738028019132,
207.17171717171718,
112.41922370889827
],
[
'Q',
209.2929292929293,
109.59106713760524,
209.64646464646466,
108.88402799478197
],
[
'Q',
210,
108.17698885195871,
210.35353535353534,
107.46994970913545
],
[
'Q',
210.7070707070707,
106.7629105663122,
210.7070707070707,
107.11643013772382
],
['Q', 210.7070707070707, 107.46994970913545, 210, 109.59106713760524],
[
'Q',
209.2929292929293,
111.71218456607502,
208.23232323232324,
114.89386070877968
],
[
'Q',
207.17171717171718,
118.07553685148434,
205.05050505050505,
131.5092805651263
],
[
'Q',
202.92929292929293,
144.9430242787682,
202.57575757575756,
153.42749399264733
],
[
'Q',
202.22222222222223,
161.91196370652645,
201.86868686868686,
176.75978570581492
],
[
'Q',
201.5151515151515,
191.60760770510336,
201.86868686868686,
196.9104012762778
],
[
'Q',
202.22222222222223,
202.21319484745223,
203.2828282828283,
208.57654713286155
],
[
'Q',
204.34343434343435,
214.9398994182709,
204.6969696969697,
217.0610168467407
],
[
'Q',
205.05050505050505,
219.18213427521047,
205.40404040404042,
220.94973213226862
],
[
'Q',
205.75757575757575,
222.71732998932677,
205.75757575757575,
223.0708495607384
],
[
'Q',
205.75757575757575,
223.42436913215005,
206.11111111111111,
222.7173299893268
],
[
'Q',
206.46464646464648,
222.01029084650352,
207.17171717171718,
220.596212560857
],
[
'Q',
207.87878787878788,
219.18213427521047,
211.4141414141414,
214.23286027544765
],
[
'Q',
214.94949494949495,
209.28358627568483,
218.83838383838383,
205.04135141874525
],
[
'Q',
222.72727272727272,
200.7991165618057,
226.26262626262627,
196.9104012762778
],
[
'Q',
229.7979797979798,
193.0216859907499,
240.75757575757575,
181.70905970557772
],
[
'Q',
251.7171717171717,
170.39643342040557,
258.0808080808081,
163.6795615635846
],
[
'Q',
264.44444444444446,
156.96268970676363,
273.2828282828283,
147.77118085006126
],
[
'Q',
282.1212121212121,
138.5796719933589,
285.65656565656565,
134.69095670783096
],
[
'Q',
289.19191919191917,
130.80224142230304,
293.4343434343434,
126.20648699395184
],
[
'Q',
297.67676767676767,
121.61073256560064,
299.0909090909091,
120.19665427995412
],
['L', 300.5100505050505, 118.77757599430761]
]
}
})
}
};
const { performance, PerformanceObserver } = require('perf_hooks');
const { newEnforcer } = require('casbin');
const get = require('lodash.get');
const textAnnotation = require('tests/textAnnotation');
const penAnnotation = require('tests/penAnnotation');
const backgroundAnnotation = require('tests/backgroundAnnotation');
const obs = new PerformanceObserver((items) => {
items.getEntries().forEach((item) => {
console.log(item.name, +' ' + item.duration);
});
});
obs.observe({ entryTypes: ['measure'] });
console.log('Perf script');
const annotations = [backgroundAnnotation, textAnnotation, penAnnotation];
// Diff Performance
// Trivial Diffing
// 1.) Runs for DIFF_ITERATIONS count
// 2.) Chooses 1 of the 3 annotation types and diffs it against itself with a fabric object change
const DIFF_ITERATIONS = 10000;
// Casbin Performance
// Trivial Casbin Enforcements
// 1.) Runs for CASBIN_ITERATIONS count
// 2.) Create a new enforcer and run the current subject + change through the enforcer
// Note this will mimic a student changing the student data
const CASBIN_ITERATIONS = 10000;
const getObjectDiff = (o1, o2) => {
// Targeted Approach to Diffing
const KEYS_TO_DIFF = [
'index',
'creation_mode',
'creation_permission',
'metadata.fabric_object',
'studentAnswer'
];
return KEYS_TO_DIFF.filter((k) => {
return get(o1, k) !== get(o2, k);
});
};
const run = async () => {
performance.mark('Starting Diff');
for (let i = 0; i < DIFF_ITERATIONS; i++) {
// Run diff on current iterations annotation
const annotation = annotations[i % 3];
// Assumes it is just an annotation change
const annotationCopy = {
...annotation,
metadata: { fabric_object: '123' }
};
// Basic Diffing process
const difference = getObjectDiff(annotation, annotationCopy);
console.log(difference);
}
performance.mark('Ending Diff');
// Casbin Performance
performance.mark('Starting Casbin');
for (let i = 0; i < CASBIN_ITERATIONS; i++) {
// Run diff on current iterations annotation
// Creating a new enforcer each time to avoid any caching that might occur
const enforcer = await newEnforcer(
'tests/annotation_model.conf',
'tests/teacher_policy.csv'
);
const sub = 'student'; // the user that wants to access a resource.
const obj = 'studentData'; // the resource that is going to be accessed.
const act = 'write'; // the operation that the user performs on the resource.
const res = await enforcer.enforce(sub, obj, act);
if (res) {
// permit student to write studentData
} else {
// deny the request, show an error
}
}
performance.mark('Ending Casbin');
performance.measure('Diff', 'Starting Diff', 'Ending Diff');
performance.measure('Casbin', 'Starting Casbin', 'Ending Casbin');
};
run();
p student * write allow
p teacher * write allow
p student * read allow
p teacher * read allow
p teacher studentData write deny
export const textAnnotation = {
uniqueId: '8ceccd67-bc73-4a38-b19a-54ba7aa97fd2',
last_sync_time: '12-27-2020',
index: 1,
page_id: 123,
creation_mode: 'EDIT',
creation_permission: 'TEACHER',
type: 'Text',
metadata: {
fabric_object: JSON.stringify({
context: {
mode: 'EDIT',
permission: 'TEACHER',
userId: '10882193'
},
type: 'Text',
textboxData: {
type: 'textbox',
version: '3.6.3',
originX: 'left',
originY: 'top',
left: 108.89,
top: 271.82,
width: 100,
height: 13.56,
fill: '#0F6EEE',
stroke: null,
strokeWidth: 1,
strokeDashArray: null,
strokeLineCap: 'butt',
strokeDashOffset: 0,
strokeLineJoin: 'miter',
strokeMiterLimit: 4,
scaleX: 1,
scaleY: 1,
angle: 0,
flipX: false,
flipY: false,
opacity: 1,
shadow: null,
visible: true,
clipTo: null,
backgroundColor: '',
fillRule: 'nonzero',
paintFirst: 'fill',
globalCompositeOperation: 'source-over',
transformMatrix: null,
skewX: 0,
skewY: 0,
text: 'Hello text',
fontSize: 12,
fontWeight: 'normal',
fontFamily: 'arial',
fontStyle: 'normal',
lineHeight: 1.16,
underline: false,
overline: false,
linethrough: false,
textAlign: 'left',
textBackgroundColor: '',
charSpacing: 0,
minWidth: 20,
splitByGrapheme: false,
styles: {}
},
shouldAutoScale: true
})
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment