source: examples/scaling_tests/daisychain/likes_python/base/QUERY.py @ 41d99fb

mei_rt2mei_rt2_fix_1
Last change on this file since 41d99fb 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: 4.6 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
[373bf68]7
8"""
9
10import os
[08b8da7]11import sys
[373bf68]12import ABAC
[08b8da7]13import time
14import datetime
15from datetime import timedelta
16
17debug=0
[373bf68]18
19ctxt = ABAC.Context()
20
[08b8da7]21cred_count = 2 + 2 * #VAL#
22
23def get_msec(e_time) :
24    msec_delta=0
25    if( int(e_time.seconds) !=0 ) :
26        msec_delta= int(e_time.seconds) *1000
27    if( int(e_time.microseconds) !=0) :
28        msec_delta = msec_delta + int(e_time.microseconds)/1000
29    return msec_delta
30
31def get_micro(e_time) :
32    micro_delta=0
33    if( int(e_time.seconds) !=0 ) :
34        micro_delta= int(e_time.seconds) *1000000
35    if( int(e_time.microseconds) !=0) :
36        micro_delta = micro_delta + int(e_time.microseconds)
37    return micro_delta
38
39def extract_delta(starttime, endtime) :
40    """ given a start time, and a endtime, extract delta """
41    elapsed_time = (endtime - starttime)
42# Only handle in seconds/microseconds
43    if ( int(elapsed_time.days) != 0 ) :
44        sys.stderr,write("%s is longer than a day !!!" % msg)
45        exit(1)
46    return elapsed_time
47
48
[373bf68]49# Keystore is the directory containing the principal credentials.
50# Load existing principals and/or policy credentials
51if (os.environ.has_key("keystore")) :
52    keystore=os.environ["keystore"]
[08b8da7]53    starttime = datetime.datetime.now()
[373bf68]54    ctxt.load_directory(keystore)
[08b8da7]55    endtime = datetime.datetime.now()
56    elapsed_load=extract_delta(starttime, endtime)
57    elapsed_msec=get_msec(elapsed_load)
58    sys.stderr.write("%d %d LOAD(msec)\n" % (cred_count,elapsed_msec))
[373bf68]59else:
60    print("keystore is not set...")
61    exit(1)
62
63##########################################################################
64# dump the loaded principals/policies
65#
66
67fd=os.open("creds_dump",os.O_WRONLY|os.O_CREAT)
68out = ctxt.context_principals()
69for x in out[1]:
70    os.write(fd, x.string())
71    os.write(fd,"\n")
72out = ctxt.context_credentials()
73for c in out[1]:
74    string="%s <- %s" % (c.head_string(), c.tail_string())
75    os.write(fd,string) 
76    os.write(fd,"\n")
77os.close(fd)
78
79##########################################################################
80# Does JohnX likes John0 ?
81# role = [keyid:JohnX].role:after
82# p [Keyid:john0]
[08b8da7]83def goodQuery() :
84    aid="John%s_ID.pem"% #VAL#
85    aID=ABAC.ID(aid)
86    a=aID.id_keyid()
[373bf68]87
[08b8da7]88    bID=ABAC.ID("John0_ID.pem")
89    b=bID.id_keyid()
[373bf68]90
[08b8da7]91    role = ABAC.Role(a,"likes")
92    p = ABAC.Role(b)
[373bf68]93
[08b8da7]94    print "\n===good============ johnN.likes <- john0 "
95    starttime = datetime.datetime.now()
96    out = ctxt.query(role, p)
97    endtime = datetime.datetime.now()
98    for c in out[1]:
99        print "%s <- %s" % (c.head_string(), c.tail_string())
100    return extract_delta(starttime, endtime)
101   
102
103##########################################################################
104# Does John0 likes JohnX ?
105# role = [keyid:JohnX].role:after
106# p [Keyid:john0]
107def badQuery() :
108    bid="John%s_ID.pem"% #VAL#
109    bID=ABAC.ID(bid)
110    b=bID.id_keyid()
111
112    aID=ABAC.ID("John0_ID.pem")
113    a=aID.id_keyid()
114   
115    role = ABAC.Role(a,"likes")
116    p = ABAC.Role(b)
117   
118    print "\n===bad============ john0.likes <- johnX "
119    starttime = datetime.datetime.now()
120    out = ctxt.query(role, p)
121    endtime = datetime.datetime.now()
122    for c in out[1]:
123        print "%s <- %s" % (c.head_string(), c.tail_string())
124    return extract_delta(starttime, endtime)
125       
126##############################################################
127
128#skip the first one
129e_time=goodQuery()
130elapsed_micro=get_micro(e_time)
131sys.stderr.write("%d %d GOOD_1(micro)\n" % (cred_count,elapsed_micro))
132
133elapsed_good=timedelta()
134k=10
135while(k):
136    e_time=goodQuery()
137    elapsed_good=elapsed_good+e_time
138    k=k-1
139    if(k==9):
140       elapsed_micro=get_micro(e_time)
141       sys.stderr.write("%d %d GOOD_2(micro)\n" % (cred_count,elapsed_micro))
142
143    if(debug):
144        elapsed_micro=get_micro(e_time)
145        sys.stderr.write("%d %d GOOD_%d(micro)\n" % (cred_count,elapsed_micro,k))
146elapsed_micro=get_micro(elapsed_good)
147sys.stderr.write("%d %d GOOD_t(micro)\n" % (cred_count,elapsed_micro/10))
148
149
150###############################################################
151
152e_time=badQuery()
153elapsed_micro=get_micro(e_time)
154sys.stderr.write("%d %d BAD_1(micro)\n" % (cred_count,elapsed_micro))
155
156elapsed_bad=timedelta()
157k=10
158while(k):
159    e_time=badQuery()
160    elapsed_bad=elapsed_bad+e_time
161    k=k-1
162    if(k==9):
163        elapsed_micro=get_micro(e_time)
164        sys.stderr.write("%d %d BAD_2(micro)\n" % (cred_count,elapsed_micro))
165
166    if(debug):
167        elapsed_micro=get_micro(e_time)
168        sys.stderr.write("%d %d BAD_%d(micro)\n" % (cred_count,elapsed_micro,k))
[373bf68]169
[08b8da7]170elapsed_micro=get_micro(elapsed_bad)
171sys.stderr.write("%d %d BAD_t(micro)\n" % (cred_count,elapsed_micro/10))
Note: See TracBrowser for help on using the repository browser.