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

mei_rt2mei_rt2_fix_1
Last change on this file since 2be9809 was 08b8da7, checked in by Mei <mei@…>, 12 years ago

1) rework examples directory with Makefile
2) update scaling with plotting scripts
3) add more doc in there

  • Property mode set to 100755
File size: 6.0 KB
RevLine 
[373bf68]1#!/usr/bin/env python
2
3"""
4Run the queries described in README
5
[08b8da7]6cmd: env ABAC_CN=1 keystore=`pwd` ./query.py
7
8number of credentials,
9     3 + 6 + 3 x #VAL#
[373bf68]10
11"""
12
13import os
[08b8da7]14import sys
[373bf68]15import ABAC
[08b8da7]16import time
17import datetime
18from datetime import timedelta
19
20debug=0
[373bf68]21
22ctxt = ABAC.Context()
23
[08b8da7]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
[373bf68]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"]
[08b8da7]55    starttime = datetime.datetime.now()
[373bf68]56    ctxt.load_directory(keystore)
[08b8da7]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))
[373bf68]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']
[08b8da7]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_1(micro)\n" % (cred_count,elapsed_micro))
113
114elapsed_good=timedelta()
115k=20
116while(k):
117    e_time=maryQuery(0)
118    elapsed_good=elapsed_good+e_time
119    k=k-1
120    if(debug):
121        elapsed_micro=get_micro(e_time)
122        sys.stderr.write("%d %d GOOD_%d(micro)\n" % (cred_count,elapsed_micro,k))
123elapsed_micro=get_micro(elapsed_good)
124sys.stderr.write("%d %d GOOD_t(micro)\n" % (cred_count,elapsed_micro/20))
[373bf68]125
126##########################################################################
127# Would Mary eat kiwi ?
128# oset = [keyid:mary].oset:what2eat
129# p [string:'kiwi']
130oset = ABAC.Oset(mary,"what2eat")
131term=ABAC.DataTerm("string", "'kiwi'")
132p = ABAC.Oset(term)
133
134print "\n===good============ mary.what2eat <- kiwi"
135out = ctxt.query(oset, p)
136for c in out[1]:
137    print "%s <- %s" % (c.head_string(), c.tail_string())
138
139##########################################################################
140# Would Bob eat navel orange ?
141# oset = [keyid:bob].oset:what2eat
142# p [string:'navel orange']
[08b8da7]143def bobQuery(pr) :
144    oset = ABAC.Oset(bob,"what2eat")
145    term=ABAC.DataTerm("string", "'navel orange'")
146    p = ABAC.Oset(term)
147
148    if(pr) :
149        print "\n===bad============ bob.what2eat <- navel orange"
150    starttime = datetime.datetime.now()
151    out = ctxt.query(oset, p)
152    endtime = datetime.datetime.now()
153    if(pr) :
154        for c in out[1]:
155            print "%s <- %s" % (c.head_string(), c.tail_string())
156    return extract_delta(starttime, endtime)
157
158# skip the first one
159e_time=bobQuery(1)
160elapsed_micro=get_micro(e_time)
161sys.stderr.write("%d %d BAD_1(micro)\n" % (cred_count,elapsed_micro))
162
163elapsed_bad=timedelta()
164k=20
165while(k):
166    e_time=bobQuery(0)
167    elapsed_bad=elapsed_bad+e_time
168    k=k-1
169    if(debug):
170        elapsed_micro=get_micro(e_time)
171        sys.stderr.write("%d %d BAD_%d(micro)\n" % (cred_count,elapsed_micro,k))
172elapsed_micro=get_micro(elapsed_bad)
173sys.stderr.write("%d %d BAD_t(micro)\n" % (cred_count,elapsed_micro/20))
[373bf68]174
175##########################################################################
176# Is Apple 1.50 at Ralphs ?
177# oset = [keyid:$ralphs].oset:fruitprice([float:1.50])
178# p = [string:'apple']
179param=ABAC.DataTerm("float", "1.50")
180oset = ABAC.Oset(ralphs,"fruitprice")
181oset.oset_add_data_term(param)
182term=ABAC.DataTerm("string", "'apple'")
183p = ABAC.Oset(term)
184
185print "\n===good============ ralphs.fruitprice(1.50) <- apple"
186out = ctxt.query(oset, p)
187for c in out[1]:
188    print "%s <- %s" % (c.head_string(), c.tail_string())
189
190##########################################################################
191# Is green apple 1.50 at Ralphs ?
192# oset = [keyid:$ralphs].oset:fruitprice([float:1.50])
193# p = [string:'green apple']
194param=ABAC.DataTerm("float", "1.50")
195oset = ABAC.Oset(ralphs,"fruitprice")
196oset.oset_add_data_term(param)
197term=ABAC.DataTerm("string", "'green apple'")
198p = ABAC.Oset(term)
199
200print "\n===bad============ ralphs.fruitprice(1.50) <- green apple"
201out = ctxt.query(oset, p)
202for c in out[1]:
203    print "%s <- %s" % (c.head_string(), c.tail_string())
204
205##########################################################################
206# dump the yap dB
207#
208#ctxt.dump_yap_db()
209
Note: See TracBrowser for help on using the repository browser.