def __call__(self): try: test = Test(TestID.PTG_ASYNC, "StoRM ptg-async without release or file transfer") test.record(do_prepare_to_get) do_prepare_to_get(self.SRMclient) except Exception: error("Error executing ptg-async: %s" % traceback.format_exc())
def __init__(self, partition): grinder.logger.output('Selected dataset %s' % dataset_url) self.test = Test(Instrumented.next_test, 'Partition into %d slices' % partition) self.test_tot = Test(100 + Instrumented.next_test, 'Total for %d slices' % partition) Instrumented.next_test += 1 self.requests = generate_subset_requests(variable, {'time': partition}) # Create a fresh callable to instrument def f(req): return req() self.test.record(f) self.f = f def f2(): for req in self.requests: grinder.logger.output('Requesting %s' % req) data = self.f(req) grinder.logger.output('Data returned of shape %s' % data.shape) self.test_tot.record(f2) self.f2 = f2
def __call__(self): # Turn off automatic reporting for the current worker thread. # Having done this, the script can modify or set the statistics # before they are sent to the log and the console. grinder.statistics.delayReports = 1 # Creates a Test Suite. suite = TestSuite(VoCardRequestServiceTest().getClass()); suite.addTestSuite(ElectoralRollRegistrationRequestServiceTest().getClass()); suite.addTestSuite(RequestServiceTest().getClass()); suite.addTestSuite(DocumentServiceTest().getClass()); suite.addTestSuite(FakeExternalServiceTest().getClass()); suite.addTestSuite(LocalReferentialServiceTest().getClass()); suite.addTestSuite(CategoryServiceTest().getClass()); #suite.addTestSuite(HomeFolderModificationRequestServiceTest().getClass()); # Returns the tests as an enumeration. tests = suite.tests(); # Iterate over the tests. testNumber = 0 for test in tests: testNumber += 1 testCase = Test(testNumber, test.getName()).wrap(suite) testResult = TestResult() testCase.runTest(test, testResult) if testResult.errorCount() > 0: grinder.statistics.success = 0 elif testResult.failureCount() > 0: grinder.statistics.success = 0
def __call__(self): request = Test(1, "Basic request").wrap( HTTPRequest(url = "http://localhost:7001")) # Example 1. You can get the time of the last test as follows. result = request.GET("index.html") grinder.logger.info("The last test took %d milliseconds" % grinder.statistics.forLastTest.time) # Example 2. Normally test results are reported automatically # when the test returns. If you want to alter the statistics # after a test has completed, you must set delayReports = 1 to # delay the reporting before performing the test. This only # affects the current worker thread. grinder.statistics.delayReports = 1 result = request.GET("index.html") if grinder.statistics.forLastTest.time > 5: # We set success = 0 to mark the test as a failure. The test # time will be reported to the data log, but not included # in the aggregate statistics sent to the console or the # summary table. grinder.statistics.forLastTest.success = 0 # With delayReports = 1 you can call report() to explicitly. grinder.statistics.report() # You can also turn the automatic reporting back on. grinder.statistics.delayReports = 0 # Example 3. # getForCurrentTest() accesses statistics for the current test. # getForLastTest() accesses statistics for the last completed test. def page(self): resourceRequest = Test(2, "Request resource").wrap( HTTPRequest(url = "http://localhost:7001")) resourceRequest.GET("index.html"); resourceRequest.GET("foo.css"); grinder.logger.info("GET foo.css returned a %d byte body" % grinder.statistics.forLastTest.getLong( "httpplugin.responseLength")) grinder.logger.info("Page has taken %d ms so far" % grinder.statistics.forCurrentTest.time) if grinder.statistics.forLastTest.time > 10: grinder.statistics.forCurrentTest.success = 0 resourceRequest.GET("image.gif"); instrumentedPage = Test(3, "Page").wrap(page) instrumentedPage(self)
def __call__(self): test = Test(1, "StoRM srmLs") client = SRMClientFactory.newSRMClient(SRM_ENDPOINT, PROXY_FILE) surl_range = range(1, random.randrange(NUM_FILES)) surls = [] for i in surl_range: surls.append(SURL_PREFIX + "/f" + str(i)) test.record(client) log("Listing %d surls..." % surl_range[-1]) res = client.srmLs(surls, MAX_WAITING_TIME_IN_MSEC) log("List result: %s: %s" % (res.returnStatus.statusCode, res.returnStatus.explanation)) statuses = res.getDetails().getPathDetailArray() for s in statuses: log("%s -> %s" %(s.getPath(), s.getStatus().getStatusCode()))
def __init__(self): """ Initialize tests """ Logger.getLogger("net.spy.memcached").setLevel(Level.DEBUG) self.clients = [] if USE_GLOBAL_CLIENT: # use global client self.client = global_client else: cfb = ConnectionFactoryBuilder() self.client = ArcusClient.createArcusClient( arcus_cloud, service_code, cfb) print 'Wait for per-thread client to be connected to Arcus cloud (%d seconds)' % DEFAULT_CONNECTION_WAIT Thread.currentThread().sleep(DEFAULT_CONNECTION_WAIT * 1000) self.flush_counter = 0 self.tests = [] # insert operations self.tests.append(Test(1, "KeyValue").wrap(self.KeyValue)) self.tests.append( Test(2, "Collection_Btree").wrap(self.Collection_Btree)) self.tests.append(Test(3, "Collection_Set").wrap(self.Collection_Set)) self.tests.append( Test(4, "Collection_List").wrap(self.Collection_List))
def __call__(self): # Turn off automatic reporting for the current worker thread. # Having done this, the script can modify or set the statistics # before they are sent to the log and the console. grinder.statistics.delayReports = 1 # Creates a Test Suite. suite = TestSuite(VoCardRequestServiceTest().getClass()) suite.addTestSuite( ElectoralRollRegistrationRequestServiceTest().getClass()) suite.addTestSuite(RequestServiceTest().getClass()) suite.addTestSuite(DocumentServiceTest().getClass()) suite.addTestSuite(FakeExternalServiceTest().getClass()) suite.addTestSuite(LocalReferentialServiceTest().getClass()) suite.addTestSuite(CategoryServiceTest().getClass()) #suite.addTestSuite(HomeFolderModificationRequestServiceTest().getClass()); # Returns the tests as an enumeration. tests = suite.tests() # Iterate over the tests. testNumber = 0 for test in tests: testNumber += 1 testCase = Test(testNumber, test.getName()).wrap(suite) testResult = TestResult() testCase.runTest(test, testResult) if testResult.errorCount() > 0: grinder.statistics.success = 0 elif testResult.failureCount() > 0: grinder.statistics.success = 0
def __call__(self): grinder.statistics.delayReports = 1 error = grinder.logger.error log = grinder.logger.output twitterTest = Test(201, "REST twittersoek") twitterRequest = twitterTest.wrap(HTTPRequest(url=baseUrl)) profilbildeTest = Test(202, "REST profilbilder") profilbildeRequest = profilbildeTest.wrap(HTTPRequest()) try: log("Soek etter 'Grinder' paa twitter og henter JSON-resultater") twitterJson = twitterRequest.GET("search.json?q=" + sokeord).text twitterObjekt = JSONObject(twitterJson) twitterResultater = twitterObjekt.getJSONArray("results") for i in range(0,twitterResultater.length()): enkeltTweet = twitterResultater.getJSONObject(i) brukerID = enkeltTweet.getString("from_user_id_str") log("Henter profilbilde for bruker:" + brukerID) profilbildeUrl = enkeltTweet.getString("profile_image_url") profilbildeRequest.GET(profilbildeUrl) except JSONException, ex: grinder.statistics.forLastTest.setSuccess(0) error("EXCEPTION HENDTE:") error(str(ex))
def __call__(self): try: test = Test(TestID.MIX_DAV, "StoRM Mix WebDAV test") test.record(mix_dav) mix_dav(self.HTTP_Client, self.local_file_path) except Exception, e: error("Error executing mix-dav: %s" % traceback.format_exc())
def __call__(self): try: test = Test(TestID.TXFER_OUT, "StoRM file-transfer OUT") test.record(file_transfer_out) file_transfer_out(self.SRMclient, self.HTTPclient, self.local_file_path, self.target_file_surl, self.transfer_protocol) cleanup(self.SRMclient, self.target_file_surl) except Exception, e: error("Error executing file-transfer-out: %s" % traceback.format_exc())
def __call__(self): test = Test(TestID.ATLAS_RENAMING, "Atlas renaming") test.record(atlas_renaming) debug("WEBDAV_ENDPOINT: %s" % WEBDAV_ENDPOINT) atlas_renaming(HTTP_CLIENT)
def __init__(self): url_file = open(url_file_path) self.tests = [] for num, url in enumerate(url_file): url = url.strip() test = Test(num, url) request = test.wrap(HTTPRequest()) self.tests.append((request, url)) url_file.close()
def __call__(self): if (self.isFirstRun()): self.initBarrier.await() try: test = Test(TestID.PUT_DAV, "StoRM PUT WebDAV test") test.record(put_dav) put_dav(self.remote_URL) except Exception, e: error("Error executing put-dav: %s" % traceback.format_exc())
def __call__(self): if (grinder.runNumber == 0): self.initBarrier.await() try: test = Test(TestID.GET_DAV, "StoRM GET WebDAV test") test.record(get_dav) get_dav(random.choice(FILE_URLS)) except Exception, e: error("Error executing get-dav: %s" % traceback.format_exc())
def __call__(self, src_url, dest_url, client): test = Test(TestID.MOVE, "MOVE") test.record(move) try: return move(src_url, dest_url, client) except Exception: error("Error executing MOVE: %s" % traceback.format_exc()) raise
def __init__(self): url_file = open(url_file_path) self.tests = [] for num, url in enumerate(url_file): url = url.strip() test = Test(num, url) request = test.wrap(HTTPRequest()) self.tests.append((request, url)) url_file.close() grinder.statistics.setDelayReports(True)
def __init__(self): url_file = open(url_file_path) self.requests = [] for num, line in enumerate(url_file): url, description = line.split(' ', 1) test = Test(num, description.strip()) request_fn = request_factory(url) wrapped_request_fn = test.wrap(request_fn) self.requests.append(wrapped_request_fn) url_file.close()
def __call__(self): test = Test(TestID.ATLAS_RENAMING, "Atlas renaming") test.record(atlas_renaming) (base_dir, src_file, dest_file, dir_level1, dir_level2) = setup(SRM_CLIENT) atlas_renaming(src_file, dest_file, dir_level1, dir_level2, HTTP_CLIENT) cleanup(SRM_CLIENT, base_dir)
def __call__(self): try: test = Test(TestID.RM_MULTI, "StoRM srmRm multiple calls") test.record(rm_files) surls = setup(SRM_CLIENT, TEST_NUMFILES) rm_files(SRM_CLIENT, surls) except Exception, e: error("Error executing rm-multi: %s" % traceback.format_exc())
def __call__(self, url, client): test = Test(TestID.DELETE, "DELETE") test.record(delete) try: return delete(url, client) except Exception: error("Error executing DELETE: %s" % traceback.format_exc()) raise
def __call__(self, url, client): test = Test(TestID.MKCOL, "MKCOL") test.record(mkcol) try: return mkcol(url, client) except Exception: error("Error executing MKCOL: %s" % traceback.format_exc()) raise
def __call__(self): (ks,cert,key) = get_random_certificate() log("Cert: %s" % ks) grinder.SSLControl.setKeyStoreFile(ks,KEYSTORE_PASSWORD) test = Test(1, "Admin legacy connector request") req = HTTPRequest() test.record(req) url="https://"+HOST+":"+str(PORT)+"/generate-ac" req.GET(url) req2= HTTPRequest() test2 = Test(2, "Admin 8443 connector request") test2.record(req2) url2="https://"+HOST+":8443/voms/mysql/generate-ac" req2.GET(url2) legacy = LegacyVOMSRequestResponse(HOST,PORT,"mysql") test3 = Test(3, "Admin legacy request") test3.record(legacy) output = legacy() log("legacy response: %s" % output)
def test(function): """ Decorator that creates a Grinder Test object for each function and instruments it. """ global _testCounter description = dedent(function.__doc__).strip() testObject = Test(_testCounter, description) _testCounter = _testCounter + 1 testObject.record(function) return function
def __call__(self): try: test = Test(TestID.PTP_SYNC_PD, "StoRM PTP Sync test") test.record(ptp_sync) file_name = ptp_sync(SRM_CLIENT) cleanup(SRM_CLIENT, file_name) except Exception, e: error("Error executing ptp-sync: %s" % traceback.format_exc()) raise
def __call__(self): try: if (SETUP_ONCE != "yes"): self.target_file_surl = setup(self.SRMclient) test = Test(TestID.TXFER_IN, "StoRM file-transfer IN") test.record(file_transfer_in) file_transfer_in(self.SRMclient, self.HTTPclient, self.target_file_surl, TRANSFER_PROTOCOL) if (SETUP_ONCE != "yes"): cleanup(self.SRMclient, self.target_file_surl) except Exception, e: error("Error executing file-transfer-in: %s" % traceback.format_exc())
def __init__(self): url_file = open(url_file_path, "rb") self.tests = [] for num, line in enumerate(url_file): line = [val.strip() for val in line.split("|")] url, description, checks = line[0], line[1], line[2:] test = Test(num, description) request = test.wrap(HTTPRequest()) self.tests.append((request, url, checks)) url_file.close() grinder.statistics.setDelayReports(True)
def __call__(self, surls, transport_protocols, client): if client is None: raise Exception("Please set a non-null SRM client!") test = Test(TestID.PTG, "StoRM PTG") test.record(ptg) try: return ptg(surls, transport_protocols, client) except Exception: error("Error executing ptg: %s" % traceback.format_exc()) raise
def __call__(self,url,http_client): if http_client is None: raise Exception("Please set a non-null HTTP client!") test = Test(TestID.HTTP_GET, "HTTP GET") test.record(http_get) try: return http_get(url,http_client) except Exception: error("Error executing HTTP GET: %s" % traceback.format_exc()) raise
def create_metrics(cls, agent_number): #divide the total number of each query type into the ones need by this worker total_queries = default_config[cls.query_interval_name] start_job, end_job = generate_job_range(total_queries, default_config['num_nodes'], agent_number) cls.num_queries_for_current_node = end_job - start_job # Grinder test infrastructure test = Test(cls.test_number, cls.query_name) cls.query_request = HTTPRequest() test.record(cls.query_request) return [cls] * cls.num_queries_for_current_node
def __call__(self): try: test = Test(TestID.MKRMDIR, "StoRM MkDir+RmDir") test.record(mkrmdir) setup(SRM_CLIENT) mkrmdir(SRM_CLIENT) except Exception, e: error("Error executing mkdir-rmdir: %s" % traceback.format_exc()) raise
def __call__(self): method_info = "call" log.debug("START %s" % method_info) try: test = Test(TESTID, "Argus PEPD parallel test") test.record(parallel_test) parallel_test(self) except Exception, e: log.error("Error executing PEPD test: %s" % traceback.format_exc())
def __init__(self, partition): self.test = Test(Instrumented.next_test, 'Partition into %d slices' % partition) Instrumented.next_test += 1 self.requests = generate_subset_requests(variable, {'time': partition}) # Create a fresh callable to instrument def f(req): return req() self.test.record(f) self.f = f
def __call__(self, dirname, client): if client is None: raise Exception("Please set a non-null SRM client!") test = Test(TestID.LS, "StoRM LS") test.record(ls) try: return ls(dirname, client) except Exception: error("Error executing ls: %s" % traceback.format_exc()) raise
def __call__(self, surls, client): if client is None: raise Exception("Please set a non-null SRM client!") test = Test(TestID.RM, "StoRM RM") test.record(rm) try: return rm(surls, client) except Exception: error("Error executing rmdir: %s" % traceback.format_exc()) raise
class TestRunner: """A TestRunner instance is created for each worker thread.""" def __init__(self): self.linkdrop_cookie = None self.userid = None def doit(self): if linkdrop_static_per_send: for i in range(0,linkdrop_static_per_send): getStatic(linkdrop_static_url) if (sends_per_oauth and grinder.getRunNumber() % sends_per_oauth==0): self.linkdrop_cookie = None self.userid = None if self.userid is None: self.userid, self.linkdrop_cookie = authService() # cookies are reset by the grinder each test run - re-inject the # linkdrop session cookie. threadContext = HTTPPluginControl.getThreadHTTPClientContext() CookieModule.addCookie(self.linkdrop_cookie, threadContext) send(self.userid) # wrap the work in a grinder 'Test' - the unit where stats are collected. doit = Test(1, "send with oauth").wrap(doit) def __call__(self): """This method is called for every run performed by the worker thread.""" TestRunner.doit(self)
def _add_webtest_file(cls, filename): """Add all requests in the given ``.webtest`` filename to the class. """ # Parse the Webtest file webtest = parser.Webtest(filename) # Create an HTTPRequest and Test wrapper for each request, # numbered sequentially test_requests = [] for index, request in enumerate(webtest.requests): # First request is test_number+1, then test_number+2 etc. test = Test(cls.test_number + index + 1, str(request)) wrapper = test.wrap(HTTPRequest()) test_requests.append((test, wrapper, request)) # Add the (test, request) list to class for this filename cls.webtest_requests[filename] = test_requests # Skip ahead to the next test_number cls.test_number += cls.test_number_skip
def __init__(self, testNumber, properties): self.sleepTime = properties["sleepTime"] headers = [] seenContentType = 0 for e in properties.getPropertySubset("parameter.header.").entrySet(): headers.append(NVPair(e.key, e.value)) if not seenContentType and e.key.lower() == "content-type": seenContentType = 1 postDataFilename = properties["parameter.post"] if postDataFilename: file = open(postDataFilename) self.postData = file.read() file.close() if not seenContentType: headers.append( NVPair("Content-type", "application/x-www-form-urlencoded")) else: self.postData = None self.okString = properties["parameter.ok"] self.url = properties["parameter.url"] realm = properties["basicAuthenticationRealm"] user = properties["basicAuthenticationUser"] password = properties["basicAuthenticationPassword"] if realm and user and password: self.basicAuthentication = (realm, user, password) elif not realm and not user and not password: self.basicAuthentication = None else: raise "If you specify one of { basicAuthenticationUser, basicAuthenticationRealm, basicAuthenticationPassword } you must specify all three." self.request = HTTPRequest(headers=headers) self.test = Test(testNumber, properties["description"]) self.test.record(self.request)
def getProxy(self, url): #log("in getProxy with url: %s" % url) try: s = Test(self.querynum, "xmlrpc search").wrap(ServerProxy(url, None)) except: print "Service returned an error:", sys.exc_info()[0] return s
def page(self): resourceRequest = Test(2, "Request resource").wrap( HTTPRequest(url = "http://localhost:7001")) resourceRequest.GET("index.html"); resourceRequest.GET("foo.css"); grinder.logger.info("GET foo.css returned a %d byte body" % grinder.statistics.forLastTest.getLong( "httpplugin.responseLength")) grinder.logger.info("Page has taken %d ms so far" % grinder.statistics.forCurrentTest.time) if grinder.statistics.forLastTest.time > 10: grinder.statistics.forCurrentTest.success = 0 resourceRequest.GET("image.gif");
def __call__(self): t1 = System.currentTimeMillis() test = Test(1, "HTTP post") request = test.wrap(HTTPRequest()) timestamp = "<foo>|timestamp=" + str(System.currentTimeMillis()) + "|" padding = 'X' * (MSG_SIZE - len(timestamp) - len("</foo>")) data = timestamp + padding + "</foo>" result = request.POST(URL, data) if not result.statusCode == 204: raise Exception("Unexpected HTTP response; " + result.getText()) if sleepTime > 0: # Adjust sleep time based on test time adjustedSleeptime = sleepTime - (System.currentTimeMillis() - t1) grinder.sleep(long(adjustedSleeptime))
def __call__(self): log = grinder.logger.info log("Creating queue session") session = connection.createQueueSession(0, Session.AUTO_ACKNOWLEDGE) sender = session.createSender(queue) instrumentedSender = Test(1, "Send a message").wrap(sender) message = createBytesMessage(session, 100) log("Sending ten messages") for i in range(0, 10): instrumentedSender.send(message) grinder.sleep(100) log("Closing queue session") session.close()
def create_request_obj(test_num, test_name, tgroup_name=None, auth_user=None): grinder.logger.debug('Creating %s request object' % test_name) test = Test(test_num, test_name) request = HTTPRequest() request = ResponseCheckingRequest(request) test.record(request) request = ExceptionHandlingRequest(request) request = ErrorLoggingRequest(request, grinder.logger.info) if auth_user: grinder.logger.debug('%s request object will authenticate with ' 'username "%s".' % (test_name, auth_user.username)) request = AuthenticatingRequest(request, auth_user) if tgroup_name: grinder.logger.debug('%s request object will throttle with group ' '"%s".' % (test_name, tgroup_name)) tgroup = throttling_groups[tgroup_name] request = ThrottlingRequest(tgroup, request) return request
def __call__(self): t1Ms = time.time() * 1000 doTest = Test(1, "Send message").wrap(self._doTest) doTest() if sleepTime > 0: # Adjust sleep time based on test time adjustedSleeptime = sleepTime - ((time.time() * 1000) - t1Ms) grinder.sleep(long(adjustedSleeptime))
def __call__(self): grinder.SSLControl.setKeyStoreFile("mykeystore.jks", "123456") for index in range(len(vo_keys)): vo = vo_keys[index] test = Test(1, "Simple VOMS AC request") test.record(request) port=list_vo[vo] url="https://"+host+":"+str(port)+resource grinder.statistics.delayReports = 1 result = str(request.GET(url)) if not '200 OK' in result: grinder.statistics.forLastTest.success = 0 grinder.statistics.report() grinder.statistics.delayReports = 0
class Instrumented(object): next_test = 1 def __init__(self, partition): self.test = Test(Instrumented.next_test, 'Partition into %d slices' % partition) Instrumented.next_test += 1 self.requests = generate_subset_requests(variable, {'time': partition}) # Create a fresh callable to instrument def f(req): return req() self.test.record(f) self.f = f def __call__(self): for req in self.requests: grinder.logger.output('Requesting %s' % req) data = self.f(req) grinder.logger.output('Data returned of shape %s' % data.shape)
class TestRunner: """A TestRunner instance is created for each worker thread.""" # 构造方法 def __init__(self): pass # 获取参数 def getParam(self, keyword): param = "/baidu?wd=" + keyword return param # 发送请求 def sendRequest(self, param): """A method is called for each recorded page.""" # print url + param response = request.GET(url + param) code = response.getStatusCode() print "code:", code data = response.getText().encode('utf-8') # print "data:", type(data) if code != 200 or data.find('_百度搜索') == -1: info = time.strftime(ISOTIMEFORMAT, time.localtime(time.time( ))) + ' Test:' + TEST_NAME + ' [[]] Param:' + param.encode( 'utf-8') + ' [[]] StatusCode:' + str( code) + ' [[]] Content:' + data logfile.write(info + '\n') grinder.getStatistics().getForCurrentTest().setSuccess(False) # 一般无需修改 request1 = Test(TEST_ID, TEST_NAME).wrap(sendRequest) # 初始化休眠 def initialSleep(self): sleepTime = grinder.threadNumber * 100 grinder.sleep(sleepTime, 0) # 入口方法 def __call__(self): """This method is called for every run performed by the worker thread.""" if grinder.runNumber == 0: self.initialSleep() idx = random.nextInt(len(keyword_list)) # idx = (processNum * threadNum * grinder.getRunNumber() + threadNum * grinder.getProcessNumber() + grinder.getThreadNumber()) % len(keyword_list) keyword = keyword_list[idx] param = self.getParam(keyword) self.request1(param) # 析构方法 def __del__(self): if is_open.decrementAndGet() == 0: logfile.close()
def test_spec(test_name, func, maximum=None): def wrapper(ctx): response = func(context=ctx, logString=test_name, template=test_name + ".vm") stats = grinder.statistics.getForCurrentTest() if maximum and stats.time > maximum: stats.success = 0 if test_name in TestRunner.testNumberMap: number = TestRunner.testNumberMap[test_name] else: number = TestRunner.testNumberMap[test_name] = TestRunner.lastNumber TestRunner.lastNumber += 1 return Test(number, test_name).wrap(wrapper)
class TestRunner: """A TestRunner instance is created for each worker thread.""" def __init__(self): pass def getParam(self, keyword): param = "async=translate,sl:en,tl:zh-CN,st:" + keyword + ",id:1464065770244,_id:tw-async-translate,_pms:s" return param def sendRequest(self, param): """A method is called for each recorded page.""" # print url + param response = request.POST( '/async/translate?vet=10ahUKEwjKntXo1vHMAhUKH5QKHU2DDoQQqDgIJzAA..i&ei=Nb5DV4qwBIq-0ATNhrqgCA&yv=2', param) code = response.getStatusCode() print "code:", code data = response.getText().encode('utf-8') print "data:", data if code != 200 or data.find('tw-async-translate') == -1: info = time.strftime(ISOTIMEFORMAT, time.localtime(time.time( ))) + ' Test:' + TEST_NAME + ' [[]] Param:' + param.encode( 'utf-8') + ' [[]] StatusCode:' + str( code) + ' [[]] Content:' + data logfile.write(info + '\n') grinder.getStatistics().getForCurrentTest().setSuccess(False) request1 = Test(TEST_ID, TEST_NAME).wrap(sendRequest) def initialSleep(self): sleepTime = grinder.threadNumber * 100 grinder.sleep(sleepTime, 0) def __call__(self): """This method is called for every run performed by the worker thread.""" if grinder.runNumber == 0: self.initialSleep() idx = (processNum * threadNum * grinder.getRunNumber() + \ threadNum * grinder.getProcessNumber() + \ grinder.getThreadNumber()) % len(keyword_list) keyword = keyword_list[idx] param = self.getParam(keyword) self.request1(param) def __del__(self): if is_open.decrementAndGet() == 0: logfile.close()
class TestRunner: """A TestRunner instance is created for each worker thread.""" def __init__(self): self.app = App() def getParam(self): fir_idx = random.randint(0, len(number_list) - 1) param1 = number_list[fir_idx] sec_idx = random.randint(0, len(number_list) - 1) param2 = number_list[sec_idx] param = [param1, param2] return param def sendRequest(self, param1, param2): """A method is called for each recorded page.""" time.sleep(random.randint(70, 110) / 1000.0) print param1, param2 result = self.app.callAdd(param1, param2) print "result:", result # 注意param1和param2都是int类型,需要转换成str if result.find(str(param1) + ' + ' + str(param2)) == -1: print 'Failed!' info = time.strftime(ISOTIMEFORMAT, time.localtime(time.time())) + ' Test:' + TEST_NAME + ' [[]] Params:' + str(param1) + \ ',' + str(param2) + ' [[]] Result:' + result logfile.write(info + '\n') grinder.getStatistics().getForCurrentTest().setSuccess(False) request1 = Test(TEST_ID, TEST_NAME).wrap(sendRequest) def initialSleep(self): sleepTime = grinder.threadNumber * 100 grinder.sleep(sleepTime, 0) def __call__(self): """This method is called for every run performed by the worker thread.""" if grinder.runNumber == 0: self.initialSleep() (param1, param2) = self.getParam() self.request1(param1, param2) def __del__(self): if is_open.decrementAndGet() == 0: logfile.close()
class TestRunner: """A TestRunner instance is created for each worker thread.""" # 构造方法 def __init__(self): pass # 发送请求 def sendRequest(self): """A method is called for each recorded page.""" # print url time.sleep(0.4) response = request.GET(url) code = response.getStatusCode() print "code:", code data = response.getText().encode('utf-8') # print "data:", type(data) if code != 200 or data.find('Apache Tomcat') == -1: info = time.strftime(ISOTIMEFORMAT, time.localtime(time.time( ))) + ' Test:' + TEST_NAME + ' [[]] StatusCode:' + str( code) + ' [[]] Content:' + data logfile.write(info + '\n') grinder.getStatistics().getForCurrentTest().setSuccess(False) # 一般无需修改 request1 = Test(TEST_ID, TEST_NAME).wrap(sendRequest) # 初始化休眠 def initialSleep(self): sleepTime = grinder.threadNumber * 100 grinder.sleep(sleepTime, 0) # 入口方法 def __call__(self): """This method is called for every run performed by the worker thread.""" if grinder.runNumber == 0: self.initialSleep() self.request1() # 析构方法 def __del__(self): if is_open.decrementAndGet() == 0: logfile.close()
def __createTest(self): ''' pre-creation of all test instrumentation for this local thread. please note that each protocol are initialized only one time -------------------------------------------------------------- TODO: re-implement in a proper way ... ''' if grinder.threadNumber == 0: logger.info('######### Initializing test ...') procNumber = Configuration.processNumber rangeTest = BaseConfiguration.testRangeSize if logger.isTraceEnabled(): if grinder.threadNumber == 0 and Configuration.oneFileByProcess: logger.trace( 'CreateTest - [processNumber=%d][testRangeSize=%d]' % (procNumber, rangeTest)) for scenario in self.scenarioList: for line in scenario.get_array_lines(): # always instantiate the protocol/implementation self.testImpl[line.getTestId()] = line.getImplementation()( Configuration.dataFilePath, Configuration.templateFilePath) Test(line.getTestId(), line.getTestName()).record( self.testImpl[line.getTestId()].sendData) if logger.isDebugEnabled(): if grinder.threadNumber == 0: logger.debug( 'Initialized test [scenarioId=%d][name=%s][testId=%d][testname=%s][implementation=%s] - object: %s' % (scenario.scenarioId, scenario.name, line.getTestId(), line.getTestName(), line.getImplementation(), self.testImpl[line.getTestId()])) if grinder.threadNumber == 0: logger.info('######### Tests initialized ...') if not self.testImpl: logger.error('No tests were create ! Review your configuration') raise SyntaxError( 'No tests were create ! Review your configuration')
def test_spec(test_name, func, maximum=None): def wrapper(ctx): response = func(context=ctx, logString=test_name, template=test_name + ".vm") stats = grinder.statistics.getForCurrentTest() if maximum and stats.time > maximum: grinder.getLogger().error( String.format( Locale.ENGLISH, "Request took too long (%s ms), while maximum was %s ms", [stats.time, maximum])) stats.success = 0 if test_name in TestRunner.testNumberMap: number = TestRunner.testNumberMap[test_name] else: number = TestRunner.testNumberMap[test_name] = TestRunner.lastNumber TestRunner.lastNumber += 1 return Test(number, test_name).wrap(wrapper)
# control.followRedirects = 1 # if you want to increase the timeout, please modify the following option. control.timeout = 600000 #超时时间10分钟 def tenant(): statusCode = [0L, 0L, 0L, 0L] result7 = HTTPRequest().GET( 'https://cmdb.uyuntest.cn/crab/api/v1/cis?classCode=Y_Router') PTS.addHttpCode(result7.getStatusCode(), statusCode) return statusCode # Make any method call on request1 increase TPS Test(1, 'test').record(tenant) class TestRunner: # initlialize a thread def __init__(self): grinder.statistics.delayReports = True headers = [ NVPair('Content-Type', 'application/json'), NVPair('Accept', 'application/json'), ] data = '{"email": "*****@*****.**","passwd": "e10adc3949ba59abbe56e057f20f883e"}' result = HTTPRequest().POST( 'https://cmdb.uyuntest.cn/tenant/api/v1/user/login', data, headers) self.threadContext = HTTPPluginControl.getThreadHTTPClientContext() self.login_cookies = CookieModule.listAllCookies(self.threadContext)
# this script to generate SPAM or other unwanted email traffic is # violating the law and should be exiled to a very bad place for a # very long time. # # Copyright (C) 2004 Tom Pittard # Copyright (C) 2004-2008 Philip Aston # Distributed under the terms of The Grinder license. from net.grinder.script.Grinder import grinder from net.grinder.script import Test from java.lang import System from javax.mail import Message, Session from javax.mail.internet import InternetAddress, MimeMessage emailSendTest1 = Test(1, "Email Send Engine") class TestRunner: def __call__(self): smtpHost = "mailhost" properties = System.getProperties() properties["mail.smtp.host"] = smtpHost session = Session.getInstance(System.getProperties()) session.debug = 1 message = MimeMessage(session) message.setFrom(InternetAddress("*****@*****.**")) message.addRecipient(Message.RecipientType.TO, InternetAddress("*****@*****.**"))
# # A server should be running on the localhost. This script uses the # example from # http://xmlrpc-c.sourceforge.net/xmlrpc-howto/xmlrpc-howto-java-server.html # # Copyright (C) 2004 Sebasti�n Fontana # Distributed under the terms of The Grinder license. from java.util import Vector from java.lang import Integer from net.grinder.script.Grinder import grinder from net.grinder.script import Test from org.apache.xmlrpc import XmlRpcClient test1 = Test(1, "XML-RPC example test") server_url = "http://localhost:8080/RPC2" client = XmlRpcClient(server_url) test1.record(client) class TestRunner: def __call__(self): params = Vector() params.addElement(Integer(6)) params.addElement(Integer(3)) result = client.execute("sample.sumAndDifference", params) sum = result.get("sum")
from net.grinder.script.Grinder import grinder from net.grinder.script import Test from net.grinder.plugin.http import HTTPRequest from HTTPClient import NVPair from java.util import Random newCellTest = Test(1, "Make a new Cell") evaluationTest = Test(2, "Evaluate") updateTest = Test(3, "Poll until evaluated") deleteCellTest = Test(4, "Delete Cell") class TestRunner: def __call__(self): sheets = 10 random = Random() worksheet = str(40 + random.nextInt(sheets)) base_url = 'http://localhost:8000/home/admin/%s' % worksheet request = newCellTest.wrap( HTTPRequest(url=base_url + "/new_cell_after")) result = request.POST((NVPair("id", "0"), )) new_cell = result.text.split()[0].rstrip('___S_A_G_E___') request = evaluationTest.wrap(HTTPRequest(url=base_url + "/eval")) a = random.nextInt(2**30) b = random.nextInt(2**30) evalData = ( NVPair("id", new_cell), NVPair("input", "factor(%s%s)" % (a, b)), NVPair("newcell", "0"),
class AnnotationsIngestThread(AbstractThread): # The list of metric numbers for all threads in this worker annotations = [] # Grinder test reporting infrastructure test1 = Test(2, "Annotations Ingest test") request = HTTPRequest() test1.record(request) @classmethod def create_metrics(cls, agent_number): """ Generate all the annotations for this worker """ cls.annotations = generate_metrics_tenants( default_config['annotations_num_tenants'], default_config['annotations_per_tenant'], agent_number, default_config['num_nodes'], cls.generate_annotations_for_tenant) @classmethod def num_threads(cls): return default_config['annotations_concurrency'] @classmethod def generate_annotations_for_tenant(cls, tenant_id, annotations_per_tenant): l = [] for x in range(annotations_per_tenant): l.append([tenant_id, x]) return l def __init__(self, thread_num): AbstractThread.__init__(self, thread_num) # Initialize the "slice" of the metrics to be sent by this thread start, end = generate_job_range(len(self.annotations), self.num_threads(), thread_num) self.slice = self.annotations[start:end] def generate_annotation(self, time, metric_id): metric_name = generate_metric_name(metric_id) return { 'what': 'annotation ' + metric_name, 'when': time, 'tags': 'tag', 'data': 'data' } def generate_payload(self, time, metric_id): payload = self.generate_annotation(time, metric_id) return json.dumps(payload) def ingest_url(self, tenant_id): return "%s/v2.0/%s/events" % (default_config['url'], tenant_id) def make_request(self, logger): if len(self.slice) == 0: logger("Warning: no work for current thread") self.sleep(1000000) return None self.check_position(logger, len(self.slice)) batch = self.slice[self.position] tenant_id = batch[0] metric_id = batch[1] payload = self.generate_payload(int(self.time()), metric_id) self.position += 1 result = self.request.POST(self.ingest_url(tenant_id), payload) return result