In release 2023.2, an API for quickly creating a large number of employees was implemented. During testing, we described the algorithm for generating new employees in ChatGPT and conducted stress testing for creating 700 random accounts in different languages. It took about 1 minute to complete the load test, and it was successful.
To perform this task, a request to ChatGPT was written with the following content.
If the result parameter in the response is true, we will take the data for creating a new user from the data parameter and make a POST request to the address http://127.0.0.1:8081/pbxcore/api/extensions/saveRecord, using the modified array of form fields in x-www-form-urlencoded format.
The script should be written in Python without using NodeJS and should allow creating 100 different employees. In the user_username field, enter the name and surname of a random person so that national alphabets are used. In the user_email field, enter any email address. In the mobile_number field, enter random mobile phone numbers, only digits, without duplicates. In the fwd_forwarding and fwd_forwardingonbusy fields, fill in with mobile_number values.
When creating an employee, print user_username, user_email, and number to the console and report the result.
If the result of the saveRecord or getRecord request is false, also print the messages parameter from the received response. If the response results in an error of another type or cannot be parsed, print the complete text received from the API server to the console.
As a result, a Python script was generated that allowed us to perform this test.
CreateRandomExtensions.py
import requests
import random
from faker import Faker
def generate_random_mobile():
return ''.join(random.choice("1234567890") for _ in range(11))
def main():
faker_ru = Faker('ru_RU')
faker_en = Faker('en_US')
faker_zh = Faker('zh_CN')
faker_ja = Faker('ja_JP')
for number in range(1, 699):
# Retrieving the data template for a new employee
get_response = requests.get("http://127.0.0.1:8081/pbxcore/api/extensions/getRecord?id=")
if get_response.json().get('result', False) == False:
print(f"getRecord error: {get_response.json().get('messages', 'Unknown error')}")
continue
data = get_response.json().get('data', {})
# Generating random data
locale = random.choice(['ru_RU', 'en_US', 'zh_CN', 'ja_JP'])
faker = locals()[f"faker_{locale.split('_')[0].lower()}"]
data['user_username'] = faker.name()
data['user_email'] = faker.email()
data['mobile_number'] = generate_random_mobile()
data['fwd_forwarding'] = data['mobile_number']
data['fwd_forwardingonbusy'] = data['mobile_number']
# Creating a new employee
post_response = requests.post("http://127.0.0.1:8081/pbxcore/api/extensions/saveRecord", data=data, headers={'Content-Type': 'application/x-www-form-urlencoded'})
if post_response.json().get('result', False):
print(f"Created user {number} -> Username: {data['user_username']}, Email: {data['user_email']}, Mobile: {data['mobile_number']}")
else:
print(f"saveRecord error for user {number}: {post_response.json().get('messages', 'Unknown error')}")
if post_response.status_code != 200 or not isinstance(post_response.json(), dict):
print(f"Unknown error: {post_response.text}")
if __name__ == "__main__":
main()
Save file content in /tmp/CreateRandomExtensions.py and run in with the following approach.
As a result of the script's execution, you will see a list of created employees in the console, and new user accounts will appear in the web interface.
It's important to note that since the system is read-only, you need to install Python libraries after each reboot, and the /tmp folder will be cleared. Development and refinement of the script should be done in an external system.
When making REST API requests from external addresses, it's necessary to authenticate with an administrative username and password or create a user with the necessary permissions in the employee rights management module and authenticate with it.
When making requests, as in this example, from the address 127.0.0.1, no authentication is required.
Last updated
Was this helpful?
MikoPBX stress testing process for the interface and REST API
MikoPBX result of the script for generating a random set of employees