1 | #!/usr/bin/env python |
---|
2 | |
---|
3 | """ |
---|
4 | Run the queries described in README |
---|
5 | |
---|
6 | cmd: env keystore=`pwd` ./query.py |
---|
7 | |
---|
8 | """ |
---|
9 | |
---|
10 | import os |
---|
11 | import sys |
---|
12 | import ABAC |
---|
13 | import time |
---|
14 | import datetime |
---|
15 | import math |
---|
16 | |
---|
17 | from test_util import runTest |
---|
18 | |
---|
19 | debug=0 |
---|
20 | |
---|
21 | ctxt = ABAC.Context() |
---|
22 | |
---|
23 | cred_count = 2 + 2 * #VAL# |
---|
24 | |
---|
25 | def get_msec(e_time) : |
---|
26 | msec_delta=0 |
---|
27 | if( int(e_time.seconds) !=0 ) : |
---|
28 | msec_delta= int(e_time.seconds) *1000 |
---|
29 | if( int(e_time.microseconds) !=0) : |
---|
30 | msec_delta = msec_delta + int(e_time.microseconds)/1000 |
---|
31 | return msec_delta |
---|
32 | |
---|
33 | def get_micro(e_time) : |
---|
34 | micro_delta=0 |
---|
35 | if( int(e_time.seconds) !=0 ) : |
---|
36 | micro_delta= int(e_time.seconds) *1000000 |
---|
37 | if( int(e_time.microseconds) !=0) : |
---|
38 | micro_delta = micro_delta + int(e_time.microseconds) |
---|
39 | return micro_delta |
---|
40 | |
---|
41 | def extract_delta(starttime, endtime) : |
---|
42 | """ given a start time, and a endtime, extract delta """ |
---|
43 | elapsed_time = (endtime - starttime) |
---|
44 | # Only handle in seconds/microseconds |
---|
45 | if ( int(elapsed_time.days) != 0 ) : |
---|
46 | sys.stderr,write("%s is longer than a day !!!" % msg) |
---|
47 | exit(1) |
---|
48 | return elapsed_time |
---|
49 | |
---|
50 | |
---|
51 | # Keystore is the directory containing the principal credentials. |
---|
52 | # Load existing principals and/or policy credentials |
---|
53 | if (os.environ.has_key("keystore")) : |
---|
54 | keystore=os.environ["keystore"] |
---|
55 | starttime = datetime.datetime.now() |
---|
56 | ctxt.load_directory(keystore) |
---|
57 | endtime = datetime.datetime.now() |
---|
58 | elapsed_load=extract_delta(starttime, endtime) |
---|
59 | elapsed_msec=get_msec(elapsed_load) |
---|
60 | sys.stderr.write("%d %d LOAD(msec)\n" % (cred_count,elapsed_msec)) |
---|
61 | else: |
---|
62 | print("keystore is not set...") |
---|
63 | exit(1) |
---|
64 | |
---|
65 | ########################################################################## |
---|
66 | # dump the loaded principals/policies |
---|
67 | # |
---|
68 | |
---|
69 | fd=os.open("creds_dump",os.O_WRONLY|os.O_CREAT) |
---|
70 | credentials = ctxt.credentials() |
---|
71 | for cred in credentials: |
---|
72 | string="%s <- %s" % (cred.head().string(), cred.tail().string()) |
---|
73 | os.write(fd,string) |
---|
74 | os.write(fd,"\n") |
---|
75 | os.close(fd) |
---|
76 | |
---|
77 | ########################################################################## |
---|
78 | # Does JohnX likes John0 ? |
---|
79 | # role = [keyid:JohnX].role:after |
---|
80 | # p [Keyid:john0] |
---|
81 | def goodQuery() : |
---|
82 | aid="John%s_ID.pem"% #VAL# |
---|
83 | aID=ABAC.ID(aid) |
---|
84 | bID=ABAC.ID("John0_ID.pem") |
---|
85 | |
---|
86 | print "\n===good============ johnX.likes <- john0 " |
---|
87 | starttime = datetime.datetime.now() |
---|
88 | |
---|
89 | # (success, credentials) = ctxt.query("%s.likes" % aID.keyid(), bID.keyid()) |
---|
90 | # if success: |
---|
91 | # print "success" |
---|
92 | # else: |
---|
93 | # print "failure" |
---|
94 | |
---|
95 | runTest("scaling_tests/daisychain/#VAL#","test1",ctxt,"%s.likes" % aID.keyid(), bID.keyid(), 1, "cascaing johnX.likes<-john0,expect success") |
---|
96 | |
---|
97 | endtime = datetime.datetime.now() |
---|
98 | if(debug): |
---|
99 | print "good query start-> %s\n" % starttime |
---|
100 | print "good query end -> %s\n" % endtime |
---|
101 | for cred in credentials: |
---|
102 | print "%s <- %s" % (cred.head().string(), cred.tail().string()) |
---|
103 | return extract_delta(starttime, endtime) |
---|
104 | |
---|
105 | |
---|
106 | ########################################################################## |
---|
107 | # Does John0 likes JohnX ? |
---|
108 | # role = [keyid:JohnX].role:after |
---|
109 | # p [Keyid:john0] |
---|
110 | def badQuery() : |
---|
111 | bid="John%s_ID.pem"% #VAL# |
---|
112 | bID=ABAC.ID(bid) |
---|
113 | aID=ABAC.ID("John0_ID.pem") |
---|
114 | |
---|
115 | print "\n===bad============ john0.likes <- johnX " |
---|
116 | starttime = datetime.datetime.now() |
---|
117 | # (success, credentials) = ctxt.query("%s.likes" % aID.keyid(), bID.keyid()) |
---|
118 | # if success: |
---|
119 | # print "success" |
---|
120 | # else: |
---|
121 | # print "failure" |
---|
122 | runTest("scaling_tests/daisychain/#VAL#","test2",ctxt,"%s.likes" % aID.keyid(), bID.keyid(), 1, "cascaing john0.likes<-johnX,expect failure") |
---|
123 | endtime = datetime.datetime.now() |
---|
124 | for cred in credentials: |
---|
125 | print "%s <- %s" % (cred.head().string(), cred.tail().string()) |
---|
126 | return extract_delta(starttime, endtime) |
---|
127 | |
---|
128 | ############################################################## |
---|
129 | |
---|
130 | #skip the first one |
---|
131 | e_time=goodQuery() |
---|
132 | elapsed_micro=get_micro(e_time) |
---|
133 | sys.stderr.write("%d %d GOOD_f(micro)\n" % (cred_count,elapsed_micro)) |
---|
134 | |
---|
135 | tlist=[] |
---|
136 | k=100 |
---|
137 | while(k): |
---|
138 | e_time=goodQuery() |
---|
139 | elapsed_micro=get_micro(e_time) |
---|
140 | k=k-1 |
---|
141 | tlist.append(elapsed_micro) |
---|
142 | if(k==99): |
---|
143 | sys.stderr.write("%d %d GOOD_s(micro)\n" % (cred_count,elapsed_micro)) |
---|
144 | |
---|
145 | if(debug): |
---|
146 | sys.stderr.write("%d %d GOOD_%d(micro)\n" % (cred_count,elapsed_micro,k)) |
---|
147 | |
---|
148 | sum=0 |
---|
149 | for i in tlist: |
---|
150 | sum=sum+i |
---|
151 | ave=sum/100 |
---|
152 | dlist = [(x-ave) for x in tlist ] |
---|
153 | slist = [ (x-ave)*(x-ave) for x in tlist] |
---|
154 | sum=0 |
---|
155 | for i in slist: |
---|
156 | sum=sum+i |
---|
157 | sd=math.sqrt(sum/99) |
---|
158 | sys.stderr.write("%d %d %d GOOD_t(micro)\n" % (cred_count,ave,sd)) |
---|
159 | sys.stderr.write("%d 100 %s GOOD_list(micro)\n" % (cred_count,tlist)) |
---|
160 | |
---|
161 | |
---|
162 | ############################################################### |
---|
163 | |
---|
164 | e_time=badQuery() |
---|
165 | elapsed_micro=get_micro(e_time) |
---|
166 | sys.stderr.write("%d %d BAD_f(micro)\n" % (cred_count,elapsed_micro)) |
---|
167 | |
---|
168 | tlist=[] |
---|
169 | k=100 |
---|
170 | while(k): |
---|
171 | e_time=badQuery() |
---|
172 | elapsed_micro=get_micro(e_time) |
---|
173 | tlist.append(elapsed_micro) |
---|
174 | k=k-1 |
---|
175 | if(k==99): |
---|
176 | sys.stderr.write("%d %d BAD_s(micro)\n" % (cred_count,elapsed_micro)) |
---|
177 | |
---|
178 | if(debug): |
---|
179 | sys.stderr.write("%d %d BAD_%d(micro)\n" % (cred_count,elapsed_micro,k)) |
---|
180 | |
---|
181 | sum=0 |
---|
182 | for i in tlist: |
---|
183 | sum=sum+i |
---|
184 | ave=sum/100 |
---|
185 | dlist = [(x-ave) for x in tlist ] |
---|
186 | slist = [ (x-ave)*(x-ave) for x in tlist] |
---|
187 | sum=0 |
---|
188 | for i in slist: |
---|
189 | sum=sum+i |
---|
190 | sd=math.sqrt(sum/99) |
---|
191 | sys.stderr.write("%d %d %d BAD_t(micro)\n" % (cred_count,ave, sd)) |
---|
192 | sys.stderr.write("%d 100 %s BAD_list(micro)\n" % (cred_count,tlist)) |
---|