def __init__(self, min_property, max_property): # def __init__(self, min_property='min_msisdn', max_property='max_msisdn'): ''' ''' properties = grinder.getProperties() self.nbThread = properties.getInt('grinder.threads', 0) if self.nbThread <= 0: raise 'Problem with your grinder.threads parameter definition - Got [%d]' % ( self.nbThread) self.min_msisdn = properties.getInt(min_property, 0) self.max_msisdn = properties.getInt(max_property, 0) if not self.min_msisdn or not self.max_msisdn: raise 'properties min_msisdn and max_msisdn MUST BE DEFINED in your grinder.properties file' self.generation_mode = (properties.get('msisdn_generation') or 'random').upper() if not self.generation_mode in ('RANDOM', 'SEQUENTIAL', 'MIXED'): raise 'Invalid msisdn generation mode - Found %s - required RANDOM, SEQUENTIAL or MIXED' % ( self.generation_mode) self.counter = 0 # # Drawback: Great Memory cost but limited to one class variable # if self.generation_mode == 'RANDOM': # We protect this call - only one initialization if debug: print 'BEFORE LOCK()' cv_msisdn.acquire() if len(self.__class__.msisdn) == 0: if debug: print 'Shuffling MSISDN' self.__class__.msisdn = range(self.min_msisdn, self.max_msisdn) random.shuffle(self.__class__.msisdn) if debug: print 'Shuffling done' cv_msisdn.release() if debug: print 'len=%d' % (len(self.__class__.msisdn))
def setup_config(self, grinder): #Parse the properties file and update default_config dictionary for entry in grinder.getProperties().entrySet(): if entry.value.startswith(".."): continue if entry.key == "grinder.threads": self.tot_threads = self.convert(entry.value) if entry.key.find("concurrency") >= 0: self.concurrent_threads += self.convert(entry.value) if not entry.key.startswith("grinder.bf."): continue k = entry.key.replace("grinder.bf.","") default_config[k] = self.convert(entry.value)
def __init__(self): global first log("TestRunner created") startingpid = grinder.getProperties().getProperty('firstpid') if (startingpid is not None and startingpid > 0): first = startingpid else: pid = establishFirst() number = pid.replace(PIDPREFIX,"") first = number log("found "+first+" as the first nonused pid") self.initialisationTime = System.currentTimeMillis() log("New thread started at time %s" % self.initialisationTime)
connectionDefaults = HTTPPluginControl.getConnectionDefaults() # Decode gzipped responses connectionDefaults.useContentEncoding = 1 # in ms connectionDefaults.setTimeout(60000) httpUtilities = HTTPPluginControl.getHTTPUtilities() log = grinder.logger.output # Properties read from the grinder .properties file. # After how many 'send' requests should we perform oauth? This is to # simulate cookie expiry or session timeouts. sends_per_oauth = grinder.getProperties().getInt("linkdrop.sends_per_oauth", 0) # The URL of the server we want to hit. linkdrop_host = grinder.getProperties().getProperty("linkdrop.host", 'http://127.0.0.1:5000') # Service we want to test linkdrop_service = grinder.getProperties().getProperty("linkdrop.service", 'twitter.com') # Static URL we want to hit linkdrop_static_url = grinder.getProperties().getProperty("linkdrop.static_url", '/share/') # How often we want to hit the static page per send linkdrop_static_per_send = grinder.getProperties().getInt("linkdrop.static_per_send", 0) # *sob* - failed to get json packages working. Using 're' is an option, # although it requires you install jython2.5 (which still doesn't have
def establishFirstFromConfig(): global PIDPREFIX number = grinder.getProperties().getProperty('firstpid') return PIDPREFIX+number
from net.grinder.plugin.http import HTTPRequest, HTTPPluginControl from HTTPClient import NVPair from threading import Condition from java.lang import System from java.io import FileInputStream import random ingestCount = 0 first = 0 cv = Condition() # Used to synchronise thread activity. runs = 0 RETRIEVECOUNT=grinder.getProperties().getProperty('RetrieveCount') INGEST_PROP=grinder.getProperties().getProperty('ingest_prop') SERVER = 'http://' + grinder.getProperties().getProperty('fcrepo.targetHost') PIDPREFIX = grinder.getProperties().getProperty('pidprefix') log = grinder.logger.output # alias testCycle = Test(0,"Full Cycle") test1 = Test(1, "RetrieveDC") test2 = Test(2, "Ingest") test3 = Test(3, "modifyRELSEXT")
from net.grinder.script.Grinder import grinder from net.grinder.script import Test from net.grinder.plugin.http import HTTPRequest from com.wandoujia.hbase.load.test import HBaseLoadTest cacheBlocks = grinder.getProperties().getBoolean("hbase.test.cacheblocks", True) scanRowNum = grinder.getProperties().getInt("hbase.test.scan.rownum", 1) putPercent = grinder.getProperties().getDouble("hbase.test.put.percent", 0.0) scanPercent = grinder.getProperties().getDouble("hbase.test.scan.percent", 0.0) threadNum = grinder.getProperties().getInt("grinder.threads", -1) putThreadNum = int(round(threadNum * putPercent)) scanThreadNum = int(round(threadNum * scanPercent)) print "cacheBlocks: %s, scanRowNum: %d" % (cacheBlocks, scanRowNum) print "total thread number: %d, put thread number: %d, scan thread number: %d" % (threadNum, putThreadNum, scanThreadNum) hbaseLoadTest = HBaseLoadTest(cacheBlocks, scanRowNum) def put(): hbaseLoadTest.put() def scan(): hbaseLoadTest.scan() hbasePutTest = Test(1, "HBase Put").wrap(put) hbaseScanTest = Test(2, "HBase Scan").wrap(scan) class TestRunner: def __init__(self): self.threadId = grinder.getThreadNumber() print "current thread id: %d" % (self.threadId)
from net.grinder.plugin.http import HTTPRequest from net.grinder.script.Grinder import grinder from org.picocontainer import DefaultPicoContainer from org.picocontainer.injectors import AnnotatedFieldInjection from com.syamantakm.annotation import Resource def load_class(class_name): m = __import__(class_name) for comp in class_name.split('.')[1:]: m = getattr(m, comp) return m test_runner = load_class( grinder.getProperties().getProperty('java_test_runner')) dependent_class_prop = grinder.getProperties().getProperty('resource_classes') print dependent_class_prop dependent_classes = [] for dep_c in dependent_class_prop.split(';'): dep_class = load_class(dep_c) print dep_class dependent_classes.append(dep_class) class TestRunner: def __init__(self): pico = DefaultPicoContainer(AnnotatedFieldInjection(Resource))
TEST_NAME = 'http_get_test' # 设置时间格式为yyyy-mm-dd hh24:mi:ss ISOTIMEFORMAT = '%Y-%m-%d %X' # 根据实际情况选择方案1或2,可以重复利用的测试数据可以选择1或者2,不可重复利用的测试数据选择2。 # 方案1 random = Random() # 方案2 # processNum = int(grinder.getProperties().get('grinder.processes')) # threadNum = int(grinder.getProperties().get('grinder.threads')) # 一般无需修改 ERR_LOG = 'err.log' logfile = open(ERR_LOG, 'w') is_open = AtomicInteger(int(grinder.getProperties().get('grinder.threads'))) # 需要修改列表名称 param_file = "http_get_test.txt" infile = open(param_file, 'r') keyword_list = [] for line in infile.readlines(): keyword_list.append(line.strip()) infile.close() # 可能需要修改url和headers等值 url = 'https://www.baidu.com' headers = [ NVPair( 'User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'
from net.grinder.plugin.http import HTTPRequest from net.grinder.script.Grinder import grinder from org.picocontainer import DefaultPicoContainer; from org.picocontainer.injectors import AnnotatedFieldInjection; from com.syamantakm.annotation import Resource def load_class(class_name): m = __import__(class_name) for comp in class_name.split('.')[1:]: m = getattr(m, comp) return m test_runner = load_class(grinder.getProperties().getProperty('java_test_runner')) dependent_class_prop = grinder.getProperties().getProperty('resource_classes') print dependent_class_prop dependent_classes = [] for dep_c in dependent_class_prop.split(';'): dep_class = load_class(dep_c) print dep_class dependent_classes.append(dep_class) class TestRunner: def __init__(self): pico = DefaultPicoContainer(AnnotatedFieldInjection(Resource)); for dep_class in dependent_classes: pico.addComponent(dep_class)
from net.grinder.script.Grinder import grinder from net.grinder.script import Test from net.grinder.plugin.http import HTTPRequest from net.grinder.plugin.http import HTTPPluginControl from HTTPClient import NVPair from com.xhaus.jyson import JysonCodec as json import random from datetime import datetime import base64 USERNAME = grinder.getProperties()["grinder.username"] PASSWORD = grinder.getProperties()["grinder.password"] WEBROOT = grinder.getProperties()["grinder.webroot"] test1 = Test(1, "Request resource") request = HTTPRequest() test1.record(request) control = HTTPPluginControl.getConnectionDefaults() control.setDefaultHeaders([ NVPair("Content-Type", "application/json"), NVPair("Authorization", "Basic " + base64.b64encode('%s:%s' % (USERNAME,PASSWORD)).decode()) ]) student_ids = set() problems={} start_time = datetime.now().isoformat()
# A simple example using the HTTP plugin that shows the retrieval of a # single file via HTTP. # from java.util import Random from net.grinder.script import Test from net.grinder.plugin.http import HTTPRequest from net.grinder.script.Grinder import grinder seed = grinder.getProperties()['grinder.seed'] if seed: random = Random(int(seed)) else: random = Random() test1 = Test(1, "Localhost") request1 = test1.wrap(HTTPRequest()) class TestRunner: def __call__(self): i = random.nextInt(100) url = 'http://localhost?%s' % i request1.GET(url)
from net.grinder.script.Grinder import grinder from net.grinder.script import Test from java.util import Random from java.sql import DriverManager from com.mysql.jdbc import Driver DriverManager.registerDriver(Driver()) p = grinder.getProperties() p.setLong("grinder.threads", 8) p.setLong("grinder.runs", 100000000) p.setLong("grinder.duration", 120 * 1000) t = Test(1, "Query") def getConnection(): return DriverManager.getConnection("jdbc:mysql://server/geoip", "geoip", "geoip") class TestRunner: def __init__(self): self.connection = getConnection() def __call__(self): r = Random() s = self.connection.createStatement()
import re from net.grinder.script.Grinder import grinder from net.grinder.script import Test from net.grinder.plugin.http import HTTPRequest baseURL = os.environ.get('BASE_URL', 'http://search-test.library.wisconsin.edu/') format_pat= re.compile( r'.*\s"(.+?)"\s' ) request_pat= re.compile( r"(\S+)\s(.+?)\sHTTP" ) # Read in the logfile logfile = grinder.getProperties()["grinder.apachelog"] try: file = open(logfile, 'r') except IOError, e: sys.stderr.write(e) sys.exit(1) else: lines = file.readlines() file.close() requests = [] for line in lines: match = format_pat.match(line) if match: req = match.groups()[0] match2 = request_pat.match(req)
# 一般无需修改 from net.grinder.script import Test from net.grinder.script.Grinder import grinder from net.grinder.plugin.http import HTTPRequest from HTTPClient import NVPair from java.util.concurrent.atomic import AtomicInteger import time TEST_ID = 2 TEST_NAME = 'http_post_test' ISOTIMEFORMAT = '%Y-%m-%d %X' # post数据要注意确认是否可以复用,测试数据要足够多,不然会从头开始用 processNum = int(grinder.getProperties().get('grinder.processes')) threadNum = int(grinder.getProperties().get('grinder.threads')) ERR_LOG = 'err.log' logfile = open(ERR_LOG, 'w') is_open = AtomicInteger(int(grinder.getProperties().get('grinder.threads'))) param_file = "http_post_test.txt" infile = open(param_file, 'r') keyword_list = [] for line in infile.readlines(): keyword_list.append(line.strip()) infile.close() url = 'https://www.google.com.hk' # 只设置Content-Type即可
from net.grinder.script.Grinder import grinder from net.grinder.script import Test from net.grinder.plugin.http import HTTPRequest # Getting the path to the file holding the URLs from our test configuration url_file_path = grinder.getProperties().getProperty('task2.urls') class TestRunner: def __init__(self): pass # TODO: # 1. Read the file (tip: the `open()` function might come in handy). # 2. Then, create test objects for each URL (tip: you can iterate over the # file object as if it was a list of lines). def __call__(self): pass # 3. Make GET requests for each of the URLs
from net.grinder.script.Grinder import grinder from net.grinder.script import Test from org.glite.authz.pep.client import PEPClient, PEPClientException from org.glite.authz.pep.client.config import PEPClientConfiguration, PEPClientConfigurationException from org.glite.authz.pep.profile import CommonXACMLAuthorizationProfile from org.glite.authz.common.security import PEMFileReader from org.glite.authz.common.model import Request, Response, Result import os import traceback from random import randint from utils import * log = grinder.logger props = grinder.getProperties() TESTID = 201 def parallel_test(self): id_range = self.range_permit.split(",") resource_id = "%s%03d" % (self.resource_id, int(id_range[0])) endpoint = get_endpoint(self, grinder.threadNumber) method_info = "parallel_test - resourceid=[%s] actionid=[%s] endpoint=[%s]" % (resource_id, self.action_id, endpoint) log.debug("START %s" % method_info) pep_client = create_client(endpoint, self.ca_dirname, self.client_cert, self.client_key, self.client_passwd) certs = get_user_cert(self.proxy_path, self.client_passwd) request = create_request(certs, resource_id, self.action_id) self.request_barrier.await() response = pep_client.authorize(request) result = response.getResults().get(0) decision = result.getDecision() check_result(decision, Result.DECISION_PERMIT)
from net.grinder.script.Grinder import grinder from net.grinder.script import Test from net.grinder.plugin.http import HTTPRequest url_file_path = grinder.getProperties().getProperty("task3.urls") class TestRunner: 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): for request, url, checks in self.tests: response = request.GET(url) if not self.is_valid(response, checks): self.fail() grinder.statistics.report() def fail(self): grinder.statistics.getForLastTest().setSuccess(False) def is_valid(self, response, checks):
#Java/SQL stuff from java.lang import * #Grinder stuff from net.grinder.script.Grinder import grinder from net.grinder.script import Test #misc import time import sys #project specific from framework import TSdata_w, TSdata_h, importstrs #import relevant t_DATABASENAME depending on settings in grinder.properties inp = grinder.getProperties()["grinder.inp"] inp = importstrs(inp) exec(inp) class TestRunner: def __init__(self): self.testdb = DBAccess(*dbargs) self.numstreams = 100 logstr = self.testdb.init_insert(100000, self.numstreams, False) grinder.logger.info(logstr) #this has a crazy amount of overhead in python, need to figure out #what's up def __call__(self): try: res = self.testdb.run_insert_h()
def __maxThread(): return grinder.getProperties().getInt('grinder.threads', 0)