多執行緒資料庫插入速度測試

2021-08-24 18:39:39 字數 3225 閱讀 3006

共享記憶體計數的時候沒有鎖定,會造成少量的更新遺失,不過對整體來說,遺失的資料量沒有大的影響。

<?php

/***

** 每個程序都單獨連線資料庫

* */

//configruation parameters

$tbl_id = 0;

$count_per_process = 10;

$concurrents = 100;

$logpath = "/tmp/s.log";

$host = "10.218.26.75";

$user = "user";

$pwd = "pwd";

$db = "test";

$port = 3306;

$sep = 1000;

$uid_count = 100000;

$uid_path= 100000;

if ($argc != 13) else

$uid_count = $uid_count / $concurrents;

}$shm_key = ftok(__file__, 't');

$shm_id = shmop_open($shm_key, "c", 0644, 100);

shmop_write($shm_id, 0, 0);

$m_start= microtime(true);

$conf = array(

'count' => $count_per_process,

'host' => $host,

'user' => $user,

'pwd' => $pwd,

'db' => $db,

'port' => $port,

'logpath' => $logpath,

'shm_id' => $shm_id,

'tbl_id' => $tbl_id,

'sep' => $sep,

'uid_count' => $uid_count,

'uid_path' => $uid_path,

);error_log("+++++++++++++++++++++begin++++++++++++++++\n",3,$logpath);

error_log("$m_start\n",3,$logpath);

for ($i = 1; $i <= $concurrents; ++$i)

}//main process

while (pcntl_waitpid(0, $status) != -1)

$m_end = microtime(true);

$m_duration = $m_end - $m_start;

echo "total time consumes:".$m_duration."\n";

$shm_size = shmop_size($shm_id);

$counter_str= shmop_read($shm_id, 0, $shm_size);

echo "shm counter :$counter_str\n";

$e =(int)$count_per_process * (int)$concurrents;

echo "theory counter :$e \n";

if (!shmop_delete($shm_id))

shmop_close($shm_id);

error_log("$m_end\n",3,$logpath);

error_log("+++++++++++++++++++++end++++++++++++++++\n",3,$logpath);

class simulator else

$this->domain_size = count($this->domains)-1;

$this->count = $conf['count'];

$this->sep = (int)$conf['sep'];

$this->shm_id = $conf['shm_id'];

$this->logpath = $conf['logpath'];

$this->uid_path = $conf['uid_path'];

$this->uid_count = (int)$conf['uid_count'];

$this->uid_is_ok = false; //關鍵字

$this->uid_tmp_count = 0; //

$this->link = mysql_connect($conf['host'].":".$conf['port'],$conf['user'],$conf['pwd']);

if(!$this->link)

$db_selected = mysql_select_db($conf['db'],$this->link);

if(!$db_selected)

}function __destruct()

if($this->uid_is_ok)

$contents .= $uid."\n";

}fwrite($fp,$contents);

fclose($fp);}}

}private function inc()

shmop_write($this->shm_id, $c, 0);

}public function execute() (uid,name,email) values ($uid,'$name','$email')";

$result = mysql_query($sql,$this->link);

$this->inc();

if(!$result)

}$end = microtime(true);

$duration = $end - $start;

//$line = "process ".$this->pid." ".$count." ".$duration."\n";

//echo "$line";

// error_log($line,3,$this->logpath);

}private function getemail()

private function getname()

private function getuid() else

return $uid;}}

}?>

python使用多執行緒查詢資料庫

當資料量過大時,乙個程式的執行時間就會主要花費在等待單次查詢返回結果,在這個過程中cpu無疑是處於等待io的空閒狀態的,這樣既浪費了cpu資源,又花費了大量時間 當然這裡主要說多執行緒,批量查詢不在考慮範圍,總會存在不能批量查詢的情況 在這種非密集型運算 及大量占用cpu資源 的情況下在python...

資料庫操作 fmdb多執行緒操作

1.匯入 1.三方庫fmdb 2.匯入處理多執行緒的標頭檔案 import fmdatabasequeue.h 3.匯入系統的sqlite 2.修改 1.修改三方庫的非arc改為arc fno objc arc 3.建立 1.建立資料庫建立資料庫例項物件,用於處理多執行緒的問題 fmdatabase...

Mysql多執行緒插入10萬資料

1.excel中匯入資料到mongo暫存 可能多次匯入,防止重複,所以暫存 responsebody apioperation 匯入任務v2 public dggrestresponse importbyexcel param file multipartfile file catch ioexce...