Blog Cover Image

Inspire you to have New thinking, Walk out your unique Road.

後端小菜鳥學習記錄(4)

Posted on Sep 20, 2018

9/17

#看看資料庫Table內有幾筆資料
SELECT COUNT(*) FROM User;

#刪除Table內全部資料
truncate TABLE User;
<code>$git rm <filename></code>
$git commit -m "filename"

測試 Pymysql 及 SQLAlchemy

SQLAlchemy 可處理大量資料

Pymysql 會掉資料

測試為什麼 Pymysql 會掉資料(4h)

$git pull
$git fetch
$git push

嘗試用其他方式寫(午 4 點) (3 Hours)

https://bigdatafinance.tw/index.php/tech/data-processing/500-python-mysql

connector(30min)

http://blog.topspeedsnail.com/archives/6018

peewee

http://xiaorui.cc/2015/10/09/%E8%81%8A%E8%81%8Apython%E7%9A%84%E8%BD%BB%E9%87%8F%E7%BA%A7orm-peewee%E5%8F%8A%E5%AF%B9%E6%AF%94sqlalchemy/

http://docs.peewee-orm.com/projects/flask-peewee/en/latest/database.html


實測 5000 筆資料 Local >> DB

connector-python: 4min34s
pymysql: 1min41s
SQLAlchemy: 49s

測 Jmeter 1 秒輸入 DB Docker > DB

pymysql: 70筆資料
SQLAlchemy: 233筆資料

9/18

STUDY Python(早 2 hours)

API Performance Test

10000筆
ORM: SQLAlchemy >> 1min43s
DRIVER: Connector-python >> 9min12s
DRIVER: Pymysql >> 58s (非常不穩)

將conn.close寫進迴圈: 50000筆資料 16min22s

4 hours for API performance Test


3 hours for learning DEBUG

**Debug 流程**

重建新環境

於 docker 建立 local database

Docker >> Schema

$docker-compose up -d
$./init-mysql.sh (裡面會寫指令)
$ifconfig
$ifconfig | grep inet (找到很像ip的)
$brew install telnet
$telnet ip port (確認可連線)

Trying xx.x.x.xx... Connected to xx.x.x.xx. Escape character is '^]'. J


將 flask-app.py 的 mysql connect ip 改成找到的 ip

於 project 資料夾中

$docker-compose up

#這邊跟 schema 不同,於 schema 跑 docker-compose up 是開啟 mysql

#這邊是代表在 local 端將 docker server 開啟

再測試一次資料

於 Jmeter 測試,ip 要填 localhost: 127.0.0.1

$grep

http://man.linuxde.net/grep


*Debug *注意*

由於之前在學校建立寫程式的壞習慣

使得現在將這些壞習慣帶來公司後,有點水土不服

其實等於是將自己全部打掉重新矯正自己一次

以往,我都是遇到錯誤就複製貼上,然後亂改一通

經過昨下午被高手訓唸了一番後!!!!

告訴你們後端工程師不好當,真不好當!!

Debug首先看“官方手冊”
config檔內 看到的是重點
再來看“google”,  "Stackoverflow"
最後是找人家的source code來看

Debug 拆解資料流程 >>> 優先檢查自己錯誤的地方 >>因為最容易錯

分為

整合測試 >> 使用者來用的整個環境(就是加伺服器整體Run起的環境測試)
單元測試 >> 拆解成 網路 >> APP >> 資料庫,一層一層測
供應商測試 >> Lib提供者,通常除非是很新的函數庫,不然通常不大會錯





我的重修科目:
資訊網路,資料結構,python,資料流通,debug

會用到的相關指令

#忘了是什麼姑且先記得
ps 
ps aus
lsof





docker log 查詢指令
$docker-compose logs | grep [-v] [檢索字串] 

#像我是想把所有logs裡有GET成功的正確log消掉,剩下Error的訊息
$docker-compose logs | grep -v GET

9/19

4Hours for Connection Pool Making

通過這種檢查方式在 Jmeter 測試中抓出 Bugs

def usr():

try:
    insert_usr()

except BaseException as e:
    return str(e)

return"OK"

昨日 Bug 原因懷疑是 uwsgi 內參數設置有誤

今日透過 Jmeter 找出問題為 Mysql Time Out

透過大神指引,問題點出在 Pymysql 沒有 Connection Pool

經過四小時奮鬥(早 10 點~午 3 點) 扣午餐時間

沒能做出 Connection Pool

請示主管後,決定棄坑,投往 connector/python 的懷抱!


寫好兩種丟資料方法 mysql-connector/SQLAlchemy(午 3:30-6:30 約 3 小時)

消失了好一回,露出頭來我寫了兩個新的丟資料的方法

所以目前就是三個利用 driver 丟資料的方法

  1. Pymysql >> 可是會掉資料

  2. Mysql-Connector + Connection Pool

  3. SQLAlchemy Pool(不用 orm 只用他的 pool 做連結)

mysql-connector

https://pynative.com/python-database-connection-pooling-with-mysql/

https://dev.mysql.com/doc/connectors/en/connector-python-connection-pooling.html

SQLAlchemy

http://docs.sqlalchemy.org/en/latest/core/pooling.html

https://stackoverflow.com/questions/10770377/howto-create-db-mysql-with-sqlalchemy

http://sunnyingit.github.io/book/section_python/SQLalchemy-engine.html

#上傳資料用SQLAlchemy的Connection Pool

def insert_usr_by_sqlalchemy():
    engine = create_engine('mysql+pymysql://usr:pw@ip:port/db')
    connection = engine.connect()
    engine.execute("INSERT INTO ******") 
    # continue with your work...
    connection.close()

#上傳資料用pymysql(壓測會掉資料,需要另外寫Connection Pool)

def insert_usr_by_pymysql():
    conn = pymysql.connect(host='ip', port=port, user='usr', passwd='pw', db='db', charset='utf8', autocommit=True)
    cur = conn.cursor()
    usr = cur.execute("INSERT INTO*********")
    conn.commit()
    cur.close()
    conn.close()

#上傳資料用mysql-connector

def connector_python():
    try:
        connection_pool = mysql.connector.pooling.MySQLConnectionPool(pool_name="pynative_pool",
                                                                      pool_size=5,
                                                                      pool_reset_session=True,
                                                                      host='ip',
                                                                      port=port,
                                                                      database='db',
                                                                      user='usr',
                                                                      password='pw')

        connection_object = connection_pool.get_connection()
        cursor = connection_object.cursor()
        cursor.execute("INSERT INTO********")
        connection_object.commit()
        cursor.close()
        connection_object.close()

    except Error as e :
        print ("Error while connecting to MySQL using Connection pool ", e)

將碼改一改 git 上去