Created
June 10, 2019 20:17
-
-
Save vasartori/1ecb373bfe9722d665fe92bb0b17d1ca to your computer and use it in GitHub Desktop.
Docker Oracle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM debian:stable-slim | |
COPY instantclient-basic-linux.x64-12.2.0.1.0.zip / | |
COPY test_pool.py / | |
RUN apt-get update ; \ | |
apt-get install -y build-essential python3 python3-dev python3-pip \ | |
libaio1 unzip ; \ | |
mkdir -p /opt/oracle ; \ | |
unzip /instantclient-basic-linux.x64-12.2.0.1.0.zip -d /opt/oracle ; \ | |
rm -rf /instantclient-basic-linux.x64-12.2.0.1.0.zip ; \ | |
pip3 install cx_Oracle ; \ | |
apt-get purge -y build-essential python3-dev python3-pip unzip ; \ | |
apt autoremove -y ;\ | |
chmod +x /test_pool.py | |
ENV LD_LIBRARY_PATH=/opt/oracle/instantclient_12_2:$LD_LIBRARY_PATH | |
CMD ['/usr/bin/python3', '/test_pool.py'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cx_Oracle | |
import threading | |
import os | |
import logging | |
import time | |
log = logging.getLogger() | |
log.setLevel(logging.DEBUG) | |
formatter = logging.Formatter('%(asctime)s - [%(levelname)s] - %(message)s') | |
console = logging.StreamHandler() | |
console.setFormatter(formatter) | |
log.addHandler(console) | |
try: | |
USERNAME = os.environ['USERNAME'] | |
except KeyError: | |
print("Environment Variable USERNAME must be defined.") | |
try: | |
PASSWORD = os.environ['PASSWORD'] | |
except KeyError: | |
print("Environment Variable PASSWORD must be defined.") | |
try: | |
ORACLE_SERVER = os.environ['ORACLE_SERVER'] | |
except KeyError: | |
print("Environment Variable ORACLE_SERVER must be defined.") | |
try: | |
SERVICE_NAME = os.environ['SERVICE_NAME'] | |
except KeyError: | |
print("Environment Variable SERVICE_NAME must be defined.") | |
MIN_CONNECTIONS = os.getenv("MIN_CONNECTIONS", "2") | |
MAX_CONNECTIONS = os.getenv("MAX_CONNECTIONS", "10") | |
INCREMENT_CONNECTIONS = os.getenv("INCREMENT_CONNECTIONS", "1") | |
QUERY = os.getenv("QUERY", "SELECT 1 FROM DUAL") | |
N_THREADS = os.getenv("THREADS", "5") | |
I_PER_THREAD = os.getenv("I_PER_THREAD", "100") | |
try: | |
db_pool = cx_Oracle.SessionPool(USERNAME, PASSWORD, "/".join([ | |
ORACLE_SERVER, SERVICE_NAME]), int(MIN_CONNECTIONS), int(MAX_CONNECTIONS), | |
int(INCREMENT_CONNECTIONS), threaded=True) | |
except NameError: | |
log.error("ERROR CREATING CONNECTION POOL....") | |
os._exit(1) | |
def perform_query(query): | |
connection = db_pool.acquire() | |
cursor = connection.cursor() | |
cursor.execute(query) | |
result = cursor.fetchall() | |
cursor.close() | |
db_pool.release(connection) | |
return result | |
def do_query(): | |
for i in range(int(I_PER_THREAD)): | |
result = perform_query(QUERY) | |
log.info("{} - {}".format(threading.current_thread().getName(),result)) | |
time.sleep(0.1) | |
if __name__ == '__main__': | |
log.info("Starting test on oracle server: {}".format(ORACLE_SERVER)) | |
log.info("In service name: {}".format(SERVICE_NAME)) | |
log.info("With {} Threads".format(N_THREADS)) | |
log.info("Number of interactions per Thread: {}".format(I_PER_THREAD)) | |
log.info("ThreadPool. Start Value: {}, Increment Value: {}, Max Value: {}". | |
format(MIN_CONNECTIONS, INCREMENT_CONNECTIONS, MAX_CONNECTIONS)) | |
log.info("Starting test in 5 seconds...") | |
for i in reversed(range(1,5)): | |
log.info("In {}".format(i)) | |
time.sleep(1) | |
for i in range(int(N_THREADS)): | |
t = threading.Thread(target=do_query) | |
t.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment