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

mei_rt2
Last change on this file since 7751094 was 646e57e, checked in by Mei <mei@…>, 12 years ago

1) add partial proof

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