source: examples/scaling_tests/daisychain/likes_python/base/QUERY.py @ 2b4e740

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

1) test out the performance plotting build

  • Property mode set to 100755
File size: 4.7 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
8"""
9
10import os
11import sys
12import ABAC
13import time
14import datetime
15from datetime import timedelta
16
17debug=0
18
19ctxt = ABAC.Context()
20
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
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"]
53    starttime = datetime.datetime.now()
54    ctxt.load_directory(keystore)
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))
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]
83def goodQuery() :
84    aid="John%s_ID.pem"% #VAL#
85    aID=ABAC.ID(aid)
86    a=aID.id_keyid()
87
88    bID=ABAC.ID("John0_ID.pem")
89    b=bID.id_keyid()
90
91    role = ABAC.Role(a,"likes")
92    p = ABAC.Role(b)
93
94    print "\n===good============ johnN.likes <- john0 "
95    starttime = datetime.datetime.now()
96    out = ctxt.query(role, p)
97    endtime = datetime.datetime.now()
98    if(debug):
99        print "good query start-> %s\n" % starttime
100        print "good query end  -> %s\n" % endtime
101    for c in out[1]:
102        print "%s <- %s" % (c.head_string(), c.tail_string())
103    return extract_delta(starttime, endtime)
104   
105
106##########################################################################
107# Does John0 likes JohnX ?
108# role = [keyid:JohnX].role:after
109# p [Keyid:john0]
110def badQuery() :
111    bid="John%s_ID.pem"% #VAL#
112    bID=ABAC.ID(bid)
113    b=bID.id_keyid()
114
115    aID=ABAC.ID("John0_ID.pem")
116    a=aID.id_keyid()
117   
118    role = ABAC.Role(a,"likes")
119    p = ABAC.Role(b)
120   
121    print "\n===bad============ john0.likes <- johnX "
122    starttime = datetime.datetime.now()
123    out = ctxt.query(role, p)
124    endtime = datetime.datetime.now()
125    for c in out[1]:
126        print "%s <- %s" % (c.head_string(), c.tail_string())
127    return extract_delta(starttime, endtime)
128       
129##############################################################
130
131#skip the first one
132e_time=goodQuery()
133elapsed_micro=get_micro(e_time)
134sys.stderr.write("%d %d GOOD_1(micro)\n" % (cred_count,elapsed_micro))
135
136elapsed_good=timedelta()
137k=10
138while(k):
139    e_time=goodQuery()
140    elapsed_good=elapsed_good+e_time
141    k=k-1
142    if(k==9):
143       elapsed_micro=get_micro(e_time)
144       sys.stderr.write("%d %d GOOD_2(micro)\n" % (cred_count,elapsed_micro))
145
146    if(debug):
147        elapsed_micro=get_micro(e_time)
148        sys.stderr.write("%d %d GOOD_%d(micro)\n" % (cred_count,elapsed_micro,k))
149elapsed_micro=get_micro(elapsed_good)
150sys.stderr.write("%d %d GOOD_t(micro)\n" % (cred_count,elapsed_micro/10))
151
152
153###############################################################
154
155e_time=badQuery()
156elapsed_micro=get_micro(e_time)
157sys.stderr.write("%d %d BAD_1(micro)\n" % (cred_count,elapsed_micro))
158
159elapsed_bad=timedelta()
160k=10
161while(k):
162    e_time=badQuery()
163    elapsed_bad=elapsed_bad+e_time
164    k=k-1
165    if(k==9):
166        elapsed_micro=get_micro(e_time)
167        sys.stderr.write("%d %d BAD_2(micro)\n" % (cred_count,elapsed_micro))
168
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))
172
173elapsed_micro=get_micro(elapsed_bad)
174sys.stderr.write("%d %d BAD_t(micro)\n" % (cred_count,elapsed_micro/10))
Note: See TracBrowser for help on using the repository browser.