發表文章

[PHP] 物件化方式的讀檔

一般讀檔的方式 $handler = fopen("file.csv"); while (($line = fgets($handler)) !== false) { // 執行相關邏輯 fwrite($handler, "test\r\n"); } fclose($handler); 物件化讀檔的方式 $filepath = "test.tsv"; $file = new SplFileObject($filepath); while (!$file->eof()) { // 執行相關邏輯 $line = rtrim($file->fgets()); $file->fwrite("12345"); } $file = null; 這樣感覺 code 有比較好看一些 XD Ref: https://stackoverflow.com/questions/13246597/how-to-read-a-large-file-line-by-line https://www.php.net/manual/en/splfileobject.fwrite.php https://www.php.net/manual/en/class.splfileobject.php

[Laravel] 如何自己做套件

圖片
剛好最近公司內部有提需求,希望我自己可以把一些功能包成套件 這樣後面的人需要的話,就可以直接掛在 vendors 底下 就不需要重工了! 有鑑於此,我去看了一下 官方文件 ,好像沒有寫得很清楚 如果有人看到官方哪邊有提供,可以在底下回覆我,我會非常感謝QQ 於是乎,我跟著別人寫好的文章 1. 可以先產一個 laravel 的專案,或是用原有的專案都行,目的只是可以用指令產一些需要的檔案,比較方便 composer composer create-project laravel/laravel TestPackage 2. 在這個專案內新增一個 packages 的目錄,存放要做的套件,我這裡用 neil/text2address 當範例,先建立目錄之後,執行 composer init,初始化套件 mkdir -p packages/neil/text2address && composer init composer 範例可能會像下面 { "name": "neil/text2address", "description": "Translate text to address component of google", "license": "MIT", "authors": [ { "name": "Neil", "email": "neil@test.com" } ], "minimum-stability": "dev", "require": { "php": "^7.1", "illuminate/support": "~5.4|~6.0" }, "autoload...

[wrk] 壓力測試工具 - wrk

Ref:  https://github.com/wg/wrk 基本的指令就不太多闡述,github裡面有 主要是需要在 wrk 送出時新增 header, 可以透過 -s 去呼叫自己寫的 .lua 腳本 現在有個情境,驗證時需要將其他變數md5並放入 header 中,該怎麼做? 範例: -- 這行必須設定,因為 md5.lua 會放在同層目錄,方便引入 package.path = '/Users/neil/Desktop/all_projects/project/wrk/?.lua' -- 引入同層的 md5.lua md5 = require 'md5' a = "a" b = "b" -- 取得系統時間 time = os.time() -- 做 md5, 字串串接是使用 .. checksum = md5.sumhexa(a .. b .. time) wrk.method = "POST" wrk.headers["time"] = time wrk.headers["checksum"] = checksum wrk.headers["a"] = a wrk.headers["b"] = b wrk.headers["Content-Type"] = "application/json" -- 以下是將 response 寫入到檔案中 -- file = io.open('/Users/neil/Desktop/all_projects/project/test.log', 'a') -- io.output(file) -- response = function(status, header, body) -- -- io.write("status:" .. status .. "\n"); -- io.write(body .. "\n"); -- end -- done = function(summa...

[Mac] 使用 wrk 時,出現 Too many open files

Ref:  https://superuser.com/questions/433746/is-there-a-fix-for-the-too-many-open-files-in-system-error-on-os-x-10-7-1 可以使用 sysctl -w kern.maxfiles=60000 sysctl -w kern.maxfilesperproc=60000 來增加可開啟檔案的上限, 如果是一般 linux 的話,可以使用 ulimit -HSn 60000

[MySQL] MySQL 系統參數設定

sql_mode="" 主要為sql內部特殊規則限制,通常方便工程師會特別設定成"",代表空值不做任何限制,因為不保證工程師都能寫出高品質的SQL語句 sync_binlog=0 innodb_flush_log_at_trx_commit = 0 兩個特別是影響資料庫刷盤的參數,第一個指binlog刷盤,第二個指資料異動的刷盤,都設定為0速度最優,通常只有Master需要改成雙0,雙1的話安全性最高但最慢 max_connections = 5000 連線數上限 connect_timeout=10 連線timeout秒數 open_files_limit = 65535 能同時開啟檔案的上限 max_allowed_packet = 500M 能接受的封包最大值 transaction-isolation = REPEATABLE-READ 資料安全性隔離等級 RR為預設 query_cache_size = 0 query_cache_type = 0 過時的快取設定,兩組都設定為0,才能徹底關閉 expire_logs_days = 7 binlog保留天數,如果有硬碟空間問題,可以嘗試減少,但是相對備份檔案的有效保存期限 slow_query_log=1 long_query_time=1 slow_query_log_file=/var/lib/mysql/slow.log log_throttle_queries_not_using_indexes=1000 min_examined_row_limit=1000 log-slow-admin-statements = TRUE 慢查詢相關記錄設定,基本上有點能力的DBA才能處理優化,目前只需要開啟紀錄即可 innodb_file_per_table = 1 讓innodb的data實體檔案 .ibd文件從ibdata1獨立出來,分散寫入改善整體效能 innodb_buffer_pool_size = 3G *最重要影響資料庫讀取效能沒有之一 innodb引擎的快取層吃多少記憶體的設定,通常試情況設定為實體80%左右佔成

[Node.js] require 的機制

最近在面試的時候, 寫了一些基本的題目, 但是卻有些地方是我自以為清楚, 但是不清楚的地方, 那就是這次要寫的 require! 其實一般人使用應該都會知道 require 完後, Node.js 會把 require 的物件, 會快取在記憶體內, 但是由於我真的太久沒寫, 所以在沒有理解的情況下, 也會忘記是不是真的每次 require 後就會存在記憶體中, 所以這次來分析一下原始碼 https://github.com/nodejs/node/blob/master/lib/internal/modules/cjs/loader.js // Loads a module at the given file path. Returns that module's // `exports` property. Module.prototype.require = function(id) { validateString(id, 'id'); if (id === '') { throw new ERR_INVALID_ARG_VALUE('id', id, 'must be a non-empty string'); } requireDepth++; try { return Module._load(id, this, /* isMain */ false); } finally { requireDepth--; } }; 從上面就可以看出, 關鍵是在 Module._load 那我們再看一下 Module._load 的程式碼 // Check the cache for the requested file. // 1. 假如一個 module 已經存在快取中, 直接返回其 exports 的物件 // 2. 假如 module 是原生系統內部的 module (ex: file, path), // 呼叫 NativeModule.prototype.compileForPublicLoader() 並且返回其 exports...

[Laravel] 讓你的 Helper 更有彈性 (使用 facades)

在 Laravel 中,有些常使用的 class 像是: Cache 在進行單元測試的時候,因為 Cache 本身繼承 Facade 所以可以很容易地在單元測試中 mock Cache ex: Cache::shouldReceive('get') ->once() ->with('key') ->andReturn('value'); 如果像是我們自己實作的 Helper,也想要在靜態類別也可以有 mockery 的 shouldReceive 該如何實作? 官方文件有提到可以使用 Facades 舉我們自己建立的 HttpHelper 為例 1. 建立 HttpHelper class <?php namespace App\Helpers; use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; use Illuminate\Http\Request; class HttpHelper { // ... 功能略 } 2. 建立 Provider,並且將 HttpHelper 綁定到這個 HttpServiceProvider php artisan make:provider HttpServiceProvider <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\App; class HttpHelperServiceProvider extends ServiceProvider { public function boot() { } /** * Register the application services. * * @return void */ public function register() { App::bind('Ht...