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

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

1) update new performance graphs with standard deviations

  • Property mode set to 100755
File size: 6.2 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))
135
136##########################################################################
137# Would Mary eat kiwi ?
138# oset = [keyid:mary].oset:what2eat
139# p [string:'kiwi']
140oset = ABAC.Oset(mary,"what2eat")
141term=ABAC.DataTerm("string", "'kiwi'")
142p = ABAC.Oset(term)
143
144print "\n===good============ mary.what2eat <- kiwi"
145out = ctxt.query(oset, p)
146for c in out[1]:
147    print "%s <- %s" % (c.head_string(), c.tail_string())
148
149##########################################################################
150# Would Bob eat navel orange ?
151# oset = [keyid:bob].oset:what2eat
152# p [string:'navel orange']
153def bobQuery(pr) :
154    oset = ABAC.Oset(bob,"what2eat")
155    term=ABAC.DataTerm("string", "'navel orange'")
156    p = ABAC.Oset(term)
157
158    if(pr) :
159        print "\n===bad============ bob.what2eat <- navel orange"
160    starttime = datetime.datetime.now()
161    out = ctxt.query(oset, p)
162    endtime = datetime.datetime.now()
163    if(pr) :
164        for c in out[1]:
165            print "%s <- %s" % (c.head_string(), c.tail_string())
166    return extract_delta(starttime, endtime)
167
168# skip the first one
169e_time=bobQuery(1)
170elapsed_micro=get_micro(e_time)
171sys.stderr.write("%d %d BAD_f(micro)\n" % (cred_count,elapsed_micro))
172
173k=100
174tlist=[]
175while(k):
176    e_time=bobQuery(0)
177    elapsed_micro=get_micro(e_time)
178    tlist.append(elapsed_micro)
179    k=k-1
180    if(debug):
181        sys.stderr.write("%d %d BAD_%d(micro)\n" % (cred_count,elapsed_micro,k))
182
183sum=0
184for i in tlist:
185    sum=sum+i
186ave=sum/100
187dlist = [(x-ave) for x in tlist ]
188slist = [ (x-ave)*(x-ave) for x in tlist]
189sum=0
190for i in slist:
191    sum=sum+i
192sd=math.sqrt(sum/99)
193sys.stderr.write("%d %d %d BAD_t(micro)\n" % (cred_count,ave, sd))
194
195##########################################################################
196# Is Apple 1.50 at Ralphs ?
197# oset = [keyid:$ralphs].oset:fruitprice([float:1.50])
198# p = [string:'apple']
199param=ABAC.DataTerm("float", "1.50")
200oset = ABAC.Oset(ralphs,"fruitprice")
201oset.oset_add_data_term(param)
202term=ABAC.DataTerm("string", "'apple'")
203p = ABAC.Oset(term)
204
205print "\n===good============ ralphs.fruitprice(1.50) <- apple"
206out = ctxt.query(oset, p)
207for c in out[1]:
208    print "%s <- %s" % (c.head_string(), c.tail_string())
209
210##########################################################################
211# Is green apple 1.50 at Ralphs ?
212# oset = [keyid:$ralphs].oset:fruitprice([float:1.50])
213# p = [string:'green apple']
214param=ABAC.DataTerm("float", "1.50")
215oset = ABAC.Oset(ralphs,"fruitprice")
216oset.oset_add_data_term(param)
217term=ABAC.DataTerm("string", "'green apple'")
218p = ABAC.Oset(term)
219
220print "\n===bad============ ralphs.fruitprice(1.50) <- green apple"
221out = ctxt.query(oset, p)
222for c in out[1]:
223    print "%s <- %s" % (c.head_string(), c.tail_string())
224
225##########################################################################
226# dump the yap dB
227#
228#ctxt.dump_yap_db()
229
Note: See TracBrowser for help on using the repository browser.