source: examples/scaling_tests/haystack/fruit_python/base/QUERY.py @ abf8d5d

mei_rt2mei_rt2_fix_1
Last change on this file since abf8d5d was 1e51e1b, checked in by Mei <mei@…>, 12 years ago

1) update plotting to include standard deviation and increase

repeticion to 100

  • Property mode set to 100755
File size: 6.4 KB
Line 
1#!/usr/bin/env python
2
3"""
4Run the queries described in README
5
6cmd: env ABAC_CN=1 keystore=`pwd` ./query.py
7
8number of credentials,
9     3 + 6 + 3 x #VAL#
10
11"""
12
13import os
14import sys
15import ABAC
16import time
17import datetime
18import math
19
20debug=0
21
22ctxt = ABAC.Context()
23
24cred_count = 3 + 6 + 3 * #VAL#
25
26def get_msec(e_time) :
27    msec_delta=0
28    if( int(e_time.seconds) !=0 ) :
29        msec_delta= int(e_time.seconds) *1000
30    if( int(e_time.microseconds) !=0) :
31        msec_delta = msec_delta + int(e_time.microseconds)/1000
32    return msec_delta
33
34def get_micro(e_time) :
35    micro_delta=0
36    if( int(e_time.seconds) !=0 ) :
37        micro_delta= int(e_time.seconds) *1000000
38    if( int(e_time.microseconds) !=0) :
39        micro_delta = micro_delta + int(e_time.microseconds)
40    return micro_delta
41
42def extract_delta(starttime, endtime) :
43    """ given a start time, and a endtime, extract delta """
44    elapsed_time = (endtime - starttime)
45# Only handle in seconds/microseconds
46    if ( int(elapsed_time.days) != 0 ) :
47        sys.stderr.write("%s is longer than a day !!!" % msg)
48        exit(1)
49    return elapsed_time
50
51# Keystore is the directory containing the principal credentials.
52# Load existing principals and/or policy credentials
53if (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))
61else:
62    print("keystore is not set...")
63    exit(1)
64
65# retrieve principals' keyid value from local credential files
66ralphsID=ABAC.ID("Ralphs_ID.pem");
67ralphs=ralphsID.id_keyid()
68
69bobID=ABAC.ID("Bob_ID.pem");
70bob=bobID.id_keyid()
71
72maryID=ABAC.ID("Mary_ID.pem");
73mary=maryID.id_keyid()
74
75##########################################################################
76# dump the loaded principals/policies
77#
78
79fd=os.open("creds_dump",os.O_WRONLY|os.O_CREAT)
80out = ctxt.context_principals()
81for x in out[1]:
82    os.write(fd, x.string())
83    os.write(fd,"\n")
84out = ctxt.context_credentials()
85for c in out[1]:
86    string="%s <- %s\n" % (c.head_string(), c.tail_string())
87    os.write(fd,string) 
88os.close(fd)
89
90##########################################################################
91# Would Mary eat navel orange ?
92# oset = [keyid:mary].oset:what2eat
93# p [string:'navel orange']
94def maryQuery(pr) :
95    oset = ABAC.Oset(mary,"what2eat")
96    term=ABAC.DataTerm("string", "'navel orange'")
97    p = ABAC.Oset(term)
98
99    if(pr) :
100        print "\n===good============ mary.what2eat <- navel orange"
101    starttime = datetime.datetime.now()
102    out = ctxt.query(oset, p)
103    endtime = datetime.datetime.now()
104    if(pr) :
105        for c in out[1]:
106            print "%s <- %s" % (c.head_string(), c.tail_string())
107    return extract_delta(starttime, endtime)
108
109# skip the first one
110e_time=maryQuery(1)
111elapsed_micro=get_micro(e_time)
112sys.stderr.write("%d %d GOOD_f(micro)\n" % (cred_count,elapsed_micro))
113
114k=100
115tlist=[]
116while(k):
117    e_time=maryQuery(0)
118    elapsed_micro=get_micro(e_time)
119    tlist.append(elapsed_micro)
120    k=k-1
121    if(debug):
122        sys.stderr.write("%d %d GOOD_%d(micro)\n" % (cred_count,elapsed_micro,k))
123
124sum=0
125for i in tlist:
126    sum=sum+i
127ave=sum/100
128dlist = [(x-ave) for x in tlist ]
129slist = [ (x-ave)*(x-ave) for x in tlist]
130sum=0
131for i in slist:
132    sum=sum+i
133sd=math.sqrt(sum/99)
134sys.stderr.write("%d %d %d GOOD_t(micro)\n" % (cred_count,ave,sd))
135sys.stderr.write("%d 100 %s GOOD_list(micro)\n" % (cred_count,tlist))
136
137##########################################################################
138# Would Mary eat kiwi ?
139# oset = [keyid:mary].oset:what2eat
140# p [string:'kiwi']
141oset = ABAC.Oset(mary,"what2eat")
142term=ABAC.DataTerm("string", "'kiwi'")
143p = ABAC.Oset(term)
144
145print "\n===good============ mary.what2eat <- kiwi"
146out = ctxt.query(oset, p)
147for c in out[1]:
148    print "%s <- %s" % (c.head_string(), c.tail_string())
149
150##########################################################################
151# Would Bob eat navel orange ?
152# oset = [keyid:bob].oset:what2eat
153# p [string:'navel orange']
154def bobQuery(pr) :
155    oset = ABAC.Oset(bob,"what2eat")
156    term=ABAC.DataTerm("string", "'navel orange'")
157    p = ABAC.Oset(term)
158
159    if(pr) :
160        print "\n===bad============ bob.what2eat <- navel orange"
161    starttime = datetime.datetime.now()
162    out = ctxt.query(oset, p)
163    endtime = datetime.datetime.now()
164    if(pr) :
165        for c in out[1]:
166            print "%s <- %s" % (c.head_string(), c.tail_string())
167    return extract_delta(starttime, endtime)
168
169# skip the first one
170e_time=bobQuery(1)
171elapsed_micro=get_micro(e_time)
172sys.stderr.write("%d %d BAD_f(micro)\n" % (cred_count,elapsed_micro))
173
174k=100
175tlist=[]
176while(k):
177    e_time=bobQuery(0)
178    elapsed_micro=get_micro(e_time)
179    tlist.append(elapsed_micro)
180    k=k-1
181    if(debug):
182        sys.stderr.write("%d %d BAD_%d(micro)\n" % (cred_count,elapsed_micro,k))
183
184sum=0
185for i in tlist:
186    sum=sum+i
187ave=sum/100
188dlist = [(x-ave) for x in tlist ]
189slist = [ (x-ave)*(x-ave) for x in tlist]
190sum=0
191for i in slist:
192    sum=sum+i
193sd=math.sqrt(sum/99)
194sys.stderr.write("%d %d %d BAD_t(micro)\n" % (cred_count,ave, sd))
195sys.stderr.write("%d 100 %s BAD_list(micro)\n" % (cred_count,tlist))
196
197##########################################################################
198# Is Apple 1.50 at Ralphs ?
199# oset = [keyid:$ralphs].oset:fruitprice([float:1.50])
200# p = [string:'apple']
201param=ABAC.DataTerm("float", "1.50")
202oset = ABAC.Oset(ralphs,"fruitprice")
203oset.oset_add_data_term(param)
204term=ABAC.DataTerm("string", "'apple'")
205p = ABAC.Oset(term)
206
207print "\n===good============ ralphs.fruitprice(1.50) <- apple"
208out = ctxt.query(oset, p)
209for c in out[1]:
210    print "%s <- %s" % (c.head_string(), c.tail_string())
211
212##########################################################################
213# Is green apple 1.50 at Ralphs ?
214# oset = [keyid:$ralphs].oset:fruitprice([float:1.50])
215# p = [string:'green apple']
216param=ABAC.DataTerm("float", "1.50")
217oset = ABAC.Oset(ralphs,"fruitprice")
218oset.oset_add_data_term(param)
219term=ABAC.DataTerm("string", "'green apple'")
220p = ABAC.Oset(term)
221
222print "\n===bad============ ralphs.fruitprice(1.50) <- green apple"
223out = ctxt.query(oset, p)
224for c in out[1]:
225    print "%s <- %s" % (c.head_string(), c.tail_string())
226
227##########################################################################
228# dump the yap dB
229#
230#ctxt.dump_yap_db()
231
Note: See TracBrowser for help on using the repository browser.