from locust import HttpLocust, TaskSet, task, between, events, Locust, User, HttpUser
import vertica_python
import time, logging
def get_sample_query():
query = '''
SELECT COUNT(*) FROM tst.test_table'''
conn = {
'host': os.environ['vertica_host'],
'port': os.environ['vertica_port'],
'database': os.environ['vertica_database'],
'user': os.environ['vertica_user'],
'password': os.environ['vertica_password'],
'read_timeout': 600,
'unicode_error': 'strict',
'ssl': False
}
return conn, query
def execute_query(conn_info, query):
with vertica_python.connect(**conn_info) as conn:
cur = conn.cursor()
cur.execute(query)
return [x for x in cur.iterate()]
class VerticaClient:
def __getattr__(self, name):
def wrapper(*args, **kwargs):
start_time = time.time()
try:
res = execute_query(*args, **kwargs)
self._locust_environment.events.request_success.fire(request_type="vertica",
name=name,
response_time=int((time.time() - start_time) * 1000),
response_length=len(res))
except Exception as e:
self._locust_environment.events.request_failure.fire(request_type="vertica",
name=name,
response_time=int((time.time() - start_time) * 1000),
exception=e)
logging.info('error {}'.format(e))
return wrapper
class VerticaTaskSet(TaskSet):
@task
def execute_query(self):
self.client.execute_query(get_sample_query()[0], get_sample_query()[1])
class VerticaLocust(HttpUser):
tasks = [VerticaTaskSet]
wait_time = between(0.1, 1)
def __init__(self):
super(VerticaLocust, self).__init__()
self.client = VerticaClient()
您好,我正在尝试使用上面的代码对Vertica数据库进行压力测试,但是,当我运行python文件时,出现以下错误:'TypeError: init ()需要1位置参数,但给出了2个”。我已经进行了一些研究,但还无法确定解决方法,有人可以提出建议/帮助吗?谢谢!
[2020-10-08 14:53:29,338] LAPTOP/WARNING/locust.main: System open file limit setting is not high enough for load testing, and the OS didn't allow locust to increase it by itself. See https://github.com/locustio/locust/wiki/Installation#increasing-maximum-number-of-open-files-limit for more info.
[2020-10-08 14:53:29,339] LAPTOP/INFO/locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
[2020-10-08 14:53:29,358] LAPTOP/INFO/locust.main: Starting Locust 1.2.3
[2020-10-08 14:53:31,799] LAPTOP/INFO/locust.runners: Spawning 2 users at the rate 2 users/s (0 users already running)...
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 854, in gevent._gevent_cgreenlet.Greenlet.run
File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 417, in <lambda>
lambda: super(LocalRunner, self).start(user_count, spawn_rate, wait=wait)
File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 299, in start
self.spawn_users(user_count, spawn_rate=spawn_rate, wait=wait)
File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 194, in spawn_users
spawn()
File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 187, in spawn
new_user = user_class(self.environment)
TypeError: __init__() takes 1 positional argument but 2 were given
2020-10-08T13:53:31Z <Greenlet at 0x203ee5c68c0: <lambda>> failed with TypeError
[2020-10-08 14:53:31,820] LAPTOP/CRITICAL/locust.runners: Unhandled exception in greenlet: <Greenlet at 0x203ee5c68c0: <lambda>>
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 854, in gevent._gevent_cgreenlet.Greenlet.run
File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 417, in <lambda>
lambda: super(LocalRunner, self).start(user_count, spawn_rate, wait=wait)
File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 299, in start
self.spawn_users(user_count, spawn_rate=spawn_rate, wait=wait)
File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 194, in spawn_users
spawn()
File "c:\users\john\appdata\local\programs\python\python39\lib\site-packages\locust\runners.py", line 187, in spawn
new_user = user_class(self.environment)
TypeError: __init__() takes 1 positional argument but 2 were given
答案 0 :(得分:0)
我收到以下错误:'TypeError:init()接受1个位置参数,但给出了2个位置参数'。
@Test
public void testRateService1() throws Exception
{
RateResponse response = new RateResponse();
List<Map<String, String>> rowList = new ArrayList<>();
Map<String, String> rowMap = new HashMap<>();
rowMap.put("ORIGIN_ID", "123");
rowMap.put("DESTINATION ID", "DEST1");
rowList.add(rowMap);
Map<String, Object> rowsMap = new HashMap<>();
rowsMap.put("rows", rowList);
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("query", rowsMap);
response.setData(dataMap);
response.setStatus("success");
ResponseEntity<RateResponse> templateResponse = new ResponseEntity<>(response, HttpStatus.OK);
when(restTemplate.exchange(
ArgumentMatchers.anyString(),
any(HttpMethod.class),
any(),
ArgumentMatchers.<Class<RateResponse>>any()))
.thenReturn(templateResponse);
List<Rate> rateList = rateService.getRates();
Assert.assertEquals(1, rateList.size());
Assert.assertEquals("DEST1", rateList.get(0).getDestinationId());
}
扩展了locust.HttpUser
,该参数接受一个locust.User
参数。无论期望初始化蝗虫的对象是什么,用户对象都可能会传递上述环境参数,该参数会爆炸,因为您的environment
不需要任何参数。
此外,Python错误具有回溯,回溯通常对事后分析很有用。
答案 1 :(得分:0)
实际上你想创建你自己的locust类,所以你需要继承以前版本User
中的Locust
。所以你需要像这样改变你的类:
class VerticaLocus(User):
wait_time = between(0.1, 1)
tasks = [VerticaTask]
def __init__(self, environment):
super().__init__(environment)
self.client = VerticaClient()
为什么,因为我相信我们遵循相同的教程。