Package: pgworkload
pgworkload - workload utility for the PostgreSQL protocolOverviewThe goal ofpgworkloadis to ease the creation of workload scripts by providing a utility with the most common functionality already implemented.pgworkloadis run in conjunction with a user supplied Pythonclass. This class defines the workload transactions and flow.The user has complete control of what statements the transactions actually execute, and what transactions are executed in which order.pgworkloadcan seed a database with random generated data, whose definition is supplied in a YAML file.A .sql file can be supplied to create the schema and run any special queries, eg. Zone Configuration changes.ExampleClassBankin fileworkloads/bank.pyis an example of one such user-created workload.
The class defines 3 simple transactions that have to be executed bypgworkload.
Have a look at thebank.py,bank.yamlandbank.sqlin theworkloadfolder in this project.Head to fileworkload/bank.sqlto see what the database schema look like. We have 2 tables:thetransactionstable, where we record the bank payment transactions.theref_datatable.Take a close look at this last table: each column represent a different type, which brings us to the next file.Filebank.yamlis the data generation definition file.
For each column of tableref_data, we deterministically generate random data.
This file is meant as a guide to show what type of data can be generated, and what args are required.Filebank.pydefines the workload.
The workload is defined as a class object.
The class defines 3 methods:init(),run()and the constructor,__init__().
All other methods are part of the application logic of the workload.
Read the comments along the code for more information.Let's run the sampleBankworkload.Step 0 - Create Python env# upgrade pip - must have pip version 20.3+pip3install--upgradepip# we're now inside our virtual envpip3installpgworkload# download the bank workload filesmkdirworkloads# the workload classwget-Pworkloadshttps://raw.githubusercontent.com/fabiog1901/pgworkload/main/workloads/bank.py# the schema filewget-Pworkloadshttps://raw.githubusercontent.com/fabiog1901/pgworkload/main/workloads/bank.sql# the data generation definition filewget-Pworkloadshttps://raw.githubusercontent.com/fabiog1901/pgworkload/main/workloads/bank.yaml# the arguments to pass at runtime, optionalwget-Pworkloadshttps://raw.githubusercontent.com/fabiog1901/pgworkload/main/workloads/bank.args.yamlStep 1 - Init the workloadWe assume that yourCockroachDBcluster orPostgreSQLserver is up and running.Init theBankworkload.# CockroachDBpgworkloadinit-wworkloads/bank.py--url'postgres://root@localhost:26257/postgres?sslmode=disable'# PostgreSQLpgworkloadinit--wworkloads/bank.py--url'postgres://root@localhost:5432/postgres?sslmode=disable'You should see something like below2022-01-28 17:21:47,335 [INFO] (MainProcess 29422) URL: 'postgres://root@localhost:[5432|26257]/defaultdb?sslmode=disable&application_name=Bank'
2022-01-28 17:21:47,480 [INFO] (MainProcess 29422) Database 'bank' created.
2022-01-28 17:21:47,769 [INFO] (MainProcess 29422) Created workload schema
2022-01-28 17:21:47,789 [INFO] (MainProcess 29422) Generating dataset for table 'ref_data'
2022-01-28 17:22:07,088 [INFO] (MainProcess 29422) Importing data for table 'ref_data'
2022-01-28 17:22:21,063 [INFO] (MainProcess 29422) Init completed. Please update your database connection url to 'postgres://root@localhost:26257/bank?sslmode=disable&application_name=Bank'pgworkloadhas read filebank.sqland has created the database and its schema.
It has then read filebank.yamland has generated the CSV files for the tableref_data.
Finally, it imports the CSV files into databasebank.Step 2 - Run the workloadRun the workload using 8 connections for 120 seconds or 100k cycles, whichever comes first.# CockroachDBpgworkloadrun-wworkloads/bank.py-c8--url'postgres://root@localhost:26257/bank?sslmode=disable&application_name=Bank'-d120-i100000# PostgreSQLpgworkloadrun-wworkloads/bank.py-c8--url'postgres://root@localhost:5432/bank?sslmode=disable&application_name=Bank'-d120-i100000pgworkloaduses exclusively the excellentPsycopg 3to connect.
No other ORMs or drivers/libraries are used.
Psycopg has a very simple, neat way tocreate connections and execute statementsandtransactions.pgworkloadwill output something like below2022-01-28 17:22:43,893 [INFO] (MainProcess 29511) URL: 'postgres://root@localhost:26257/bank?sslmode=disable&application_name=Bank'
id               elapsed    tot_ops    tot_ops/s    period_ops    period_ops/s    mean(ms)    p50(ms)    p90(ms)    p95(ms)    p99(ms)    pMax(ms)
-------------  ---------  ---------  -----------  ------------  --------------  ----------  ---------  ---------  ---------  ---------  ----------
__cycle__             10       1342       133.72          1342           134.2       54.9       35.76     165.94     192.89     245.42      333.6
read                  10       1215       121.03          1215           121.5       41.11      19.58     113.21     146.79     208.86      291.02
txn1_new              10        130        12.95           130            13         48.29      53.81      74.7       90.84      95.66      108.37
txn2_verify           10        129        12.85           129            12.9       70.9       73.73      94.3       99.69     137.99      164.96
txn3_finalize         10        127        12.65           127            12.7       67.21      72.48      93.64     105.97     129.57      166 

[...]

2022-01-28 17:24:44,765 [INFO] (MainProcess 29511) Requested iteration/duration limit reached. Printing final stats
id               elapsed    tot_ops    tot_ops/s    period_ops    period_ops/s    mean(ms)    p50(ms)    p90(ms)    p95(ms)    p99(ms)    pMax(ms)
-------------  ---------  ---------  -----------  ------------  --------------  ----------  ---------  ---------  ---------  ---------  ----------
__cycle__            121      14519       120.12            66             6.6       94.08      96.68     203.74     216.83     242.24      262.69
read                 121      13050       107.96            54             5.4       70.6       62.7      127.88     151.29     203.52      203.62
txn1_new             121       1469        12.15             7             0.7       51.08      51.07      71.71      73.66      75.23       75.62
txn2_verify          121       1469        12.15            11             1.1       70.52      76.92     102.31     102.32     102.33      102.33
txn3_finalize        121       1469        12.15            12             1.2       81.19      98.97     103.88     103.97     103.98      103.98There are many built-in options.
Check them out withpgworkload--helpConcurrency - processes and threadspgworkload uses both themultiprocessingandthreadinglibrary to achieve high concurrency, that is, opening multiple connections to the DBMS.There are 2 parameters that can be used to configure how many processes you want to create, and for each process, how many threads:--procs, or-x, to configure the count of processes (defaults to the CPU count)--concurrency, or-c, to configure the total number of executing workloads to run (also referred to asexecuting threads)pgworkload will spread the load across the processes, so that each process has an even amount of threads.Example: if we set--procs 4and--concurrency 10, pgworkload will create as follows:Process-1: MainThread + 2 extra threads. Total = 3Process-2: MainThread + 2 extra threads. Total = 3Process-3: MainThread + 1 extra thread.  Total = 2Process-3: MainThread + 1 extra thread.  Total = 2Total workloads = 10This allows you to fine tune the count of Python processes and threads to fit your system.Built-in Workloadspgworkloadhas the following workload already built-in and can be called without the need to pass a class fileQuerybenchQuerybench runs a list of SQL Statements sequentially and iteratively.
It assumes the schema and data have been created and loaded.SQL statements filemystmts.sql-- Query 1select1;selectversion();-- select now();-- Query 2SELECT*FROMmy_tableWHEREid=1234;RunQuerybenchlike this:pgworkloadrun--builtin-workloadQuerybench--argsmystmts.sql--url<conn-string>HovrComing soonAcknowledgmentsSome methods and classes have been taken and modified from, or inspired by,https://github.com/cockroachdb/movr