1. 程式人生 > 其它 >自動跑測試記錄後傳送郵件提醒並自動解析資料

自動跑測試記錄後傳送郵件提醒並自動解析資料

  使用指令碼自動跑實驗(Ubuntu),將實驗結果記錄在檔案中,並在實驗結束之後將結果通過郵件傳送到郵箱,最後在windows端自動解析成excel表格(因為要在windows的origin上作圖)。

SHELL

touch run.sh #建立但不編輯指令碼
chmod 777 run.sh #給予指令碼執行許可權
gedit run.sh #編輯指令碼
  1. 指令碼中可以建立變數,如直接x='100'r='rm -rf'即可,${x}在執行的時候以字串形式讀取。
    1. 注意變數建立時使用單引號,等於號前後不要有空格,可以是命令,可以使變數,反正都是字串。
    2. 如何以數字方式解析變數?被以
      $(())包裹的變數將以數字形式解析,比如在某行中有這個引數:--num=$((${n}*1000000)),這裡${n}將被以數字形式解析。
    3. 變數為路徑:建議使用絕對路徑。如想建立用於接收結果檔案receive='/home/renjc/easy-test2.txt'之後touch ${receive},當然變數本質上是以字串形式讀取的,用receive='../xx.txt'當然也行,還更靈活,就是程式容易整錯。
  1. 指令碼中若想執行命令,直接新一行然後將命令打上去就可以了,不需要額外的東西,指令碼本質上還是一行一行執行的
    1. 若命令中想帶引數,可以直接用變數替換
  1. 陣列,volumn='128 512 1024 2048'
    即是陣列,也可以當做一串字串來解析。
#遍歷陣列,迴圈,遍歷資料夾
workloads="./works/workloada.spec ./works/workloadb.spec ./works/workloadf.spec"
for file_name in $workloads; do
  ./ycsbc -db redis -threads 1 -P $file_name
done
#下面這個是單純的for迴圈
for ((tn=1; tn<=8; tn=tn*2)); do
doneworkload
#下面這個是遍歷資料夾workload_dir中所有以workload開頭的spec檔案
for file_name in $workload_dir/workload*.spec; do
done
  1. 函式,函式直接寫在執行前就行
#函式作用是記錄name,當前時間,以及初步計算value
recordstart(){
time=$(date) 
echo "db_name: ${1}" >> ${receive}
echo -n "start_time: ${time} and Value:" >> ${receive}
echo "scale=2;${3}*${4}/1024/1000"|bc >> ${receive}
#純shell對除法的支援比較粗糙,如果想要保留兩位小數,可以向上面這行一樣。
}
    1. $(date)代表當前時間,帶年月日時分秒的那種。
    2. shell指令碼的函式引數並不需要申明什麼的,${1}就代表函式第一個引數,以此類推,其中按照慣例,${0}代表函式名(或者說程式名)。呼叫函式也很簡單,recordstart name ${v}即可呼叫,函式名後面的都是引數,在函式中使用${1},${2},${3}識別。
    3. echo -n "xx" >> ${receive},只有echo的話,會輸出到控制檯中,加上>> ${receive}會輸出到這個檔案裡面,加上-n代表這一行不換行,因為echo預設是每次都換行的。

使用以上幾種組合基本可以完成絕大部分實驗與程式。以下作為例子

這個作為例子(已摺疊,可展開)
 receive='/home/renjc/spkv/resault/easy-test2.txt'
touch ${receive}

ulimit -Sn 16384
ulimit -Hn 32768 
#解除堆疊限制,可能需要,先預備

rocksdb_base='rocksdb-base/db_bench'
rocksdb_split='rocksdb-split/db_bench'
wisckey='WiscKey-master/WiscKey/out-static/db_bench'

# database="${rocksdb_base} ${rocksdb_split} ${wisckey}"
rmstore='rm -rf /home/renjc/spkv/db/*'

recordstart(){
    #${1} db_name,${2} number of threads,${3} value_size,${4} number of rows
	time=$(date) 
	echo "db_name: ${1}" >> ${receive}
	echo "thread_num: ${2}" >> ${receive}
	echo "value_size: ${3}" >> ${receive}
	echo "num:${4}" >> ${receive}
	echo "start_time: ${time}" >> ${receive}
	echo -n "resault GB is:" >> ${receive}
	echo "scale=2;${3}*${4}/1024/1000/1000"|bc >> ${receive}
}

recordend(){
	time=$(date) 
	echo "done_time: ${time},----clean store and sleep 10s-------------------" >> ${receive}
	${rmstore}
	sleep 10s
}

#rocksdb 用一個槓兩個槓都行,leveldb/wisckey只能用兩個,統一採用兩個槓槓。
const_arg='--db=/home/renjc/spkv/db --bloom_bits=0 --write_buffer_size=134217728'
rocksdb_arg='--compression_type=none'   #Wisckey do not support but rocksdb need
sperate_arg='--enable_blob_files=true'  #rokcsdb的鍵值分離版本就是原來的版本上加一個引數,所以使用相同路徑加一個引數
benchmarks='--benchmarks=fillrandom,stats,readrandom,stats'
volumn='128 512 1024 2048'
num='5 10'
echo "test start" >> ${receive}

for v in ${volumn}
do
	for n in ${num}
	do
		recordstart r_sperate 1 ${v} 10000000
		./${rocksdb_base} ${const_arg} ${rocksdb_arg} --num=$((${n}*1000000)) --value_size=${v} ${benchmarks} ${sperate_arg} >> ${receive}
		recordend
		recordstart r_split 1 ${v} 10000000
		./${rocksdb_split} ${const_arg} ${rocksdb_arg} --num=$((${n}*1000000)) --value_size=${v} ${benchmarks} >> ${receive}
		recordend
		recordstart wisckey 1 ${v} 10000000
		./${wisckey} ${const_arg} --num=$((${n}*1000000)) --value_size=${v} ${benchmarks} >> ${receive}
		recordend
	done
done

echo "all done" >> ${receive}

echo "run over \n file in attach" | mutt -s "Test run over" [email protected] -a ${receive}

自動傳送郵件

mail如何新增附件沒整成功,現在使用mutt命令實現,控制檯執行:

sudo apt-get install mutt安裝

echo "Body : run over \n file in attach" | mutt -s "Test run over" [email protected] -a 'path'即可。

其中,"Body"開頭的是郵件內容,-s後面的是郵件標題,-a後面的'path'是附件地址,正常使用不用帶單引號,寫在腳本里時可以用之前建立的${receive}代替。郵件是立刻傳送的,收件箱裡找不到請到垃圾箱裡面找找。如下:

自動解析實驗結果生成excel表格

這個程式自己寫去

如何生成excel可讀取的檔案?csv檔案:csv檔案可以被excel讀取,第一行表示excel的那個列名,用逗號分割,底下的資料也是,每一行代表excel表格內的一行,每一列的內容用逗號分割,英文的逗號。

至於解析實驗結果,就逐行讀取,然後捕捉自己想要的關鍵詞就行了,效能啥的就不用考慮優化了,咋簡單咋來。

下面的是自用的解析rocksdb的引數的,僅供參考

自用rocksdb引數解析(已摺疊,可展開)
 #include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
#include <iomanip>

#define default_file_path "E:\\Code\\Windows\\FileOperate\\easy-test.txt"
#define resault_file_name "E:\\Code\\Windows\\FileOperate\\resault.csv"
using namespace std;

struct arguments {
  arguments(string s,string t):name(s),data(t){}
  string name;
  string data;
};

void readFile(vector<arguments>& resault,const string& file_path);
void trimEmpty(string &s);//用於去除字串首尾空格
void recordString(const string& type, string& s,vector<arguments>& resault);//用於格式化並記錄data
void recordInFile(const vector<arguments>& resault);//解析結果記錄到檔案中,測試用
void recordCSVMode(const vector<arguments>& resault);
void printInShell(const vector<arguments>& resault);//列印到控制檯,測試用

//按順序存放需要捕捉的關鍵詞,問題在於只能順序
//如果該行以該關鍵詞結尾,那麼就將所有的東西記錄下來
//注意,這個陣列並不是最終引數結果,有的一行記了兩三個引數
const static vector<string> key_words = {"db_name","thread_num","start_time","Values","Entries","RawSize","FileSize",
"fillrandom","Cumulative compaction","Cumulative writes","Cumulative stall",
"readrandom","Cumulative compaction","Cumulative writes","Cumulative stall","done_time"};
const static int length = key_words.size();

int main(int arg_count, char *arg_value[]){
  string file_path = default_file_path;   //預設路徑
  if(arg_count > 1) file_path = arg_value[1]; 
  //argv[0]為程式名,這點由shell傳統決定,argv[1]是程式名的輸入

  vector<arguments> resault;
  readFile(resault,file_path);

  // printInShell(resault);
  // recordInFile(resault);
  recordCSVMode(resault);

  // getchar();
  return 0;
}

void readFile(vector<arguments>& resault,const string& file_path){
  int flag = 0;
  string line;//按行讀取
  ifstream fp(file_path);

  while(getline(fp, line)) {
    string head = line.substr(0,line.find(":"));
    trimEmpty(head);
    if(key_words[flag] == head) {
      string tail = line.substr(line.find(":")+1);
      recordString(key_words[flag],tail,resault);
      // resault.push_back(arguments(key_words[flag],tail));
      flag = flag == length - 1 ? 0 : flag + 1;
    }
  }
}

void trimEmpty(string &s){
  //刪除字串的前後空格
  if (!s.empty()){
    s.erase(0, s.find_first_not_of(" "));
    s.erase(s.find_last_not_of(" ") + 1);
  }
}

void recordString(const string& type, string& s,vector<arguments>& resault){
  //為什麼不使用精確的下表標位置
  //因為資料的長度不一定,所以必須使用標點符號等來相對定位。
  trimEmpty(s);
  if(type == "fillrandom" || type == "readrandom"){
    int start = s.find("op")+3;
    int len = s.find(";") - start;
    string kops = s.substr(start,len);
    resault.push_back(arguments("Kops",kops));
    start = s.find(";")+1;
    len = s.find_last_of("s")+1 - start;
    s = s.substr(start,len);
    trimEmpty(s);
    resault.push_back(arguments("Speed",s));
    return;
  }

  if(type == "Cumulative stall") {
    string stall_time = s.substr(0,s.find(" "));
    resault.push_back(arguments("Cumulative stall",stall_time));
    s = s.substr(s.find(",") + 2);
    resault.push_back(arguments("Average stall",s));
    return;
  }

  if(type == "start_time" || type == "done_time"){
    s = s.substr(s.find_last_of(":")-5,8);
  }else if(type == "Values"){
    s = s.substr(0,s.find(" "));
  }else if(type == "RawSize" || type == "FileSize"){
    s = s.substr(0,s.find_last_of(" "));
  }else if(type == "Cumulative compaction"){
    s = s.substr(0,s.find(" ")+3);
  }else if(type == "Cumulative writes"){
    int start = s.find_last_of(":") + 2;
    int length = s.find_last_of(",") - start;
    s = s.substr(start,length);
  }
  resault.push_back(arguments(type,s));
  return;
}

void recordInFile(const vector<arguments>& resault){
  ofstream record;
  remove(resault_file_name);
  record.open(resault_file_name, ios::app);
  for (size_t i = 0; i < resault.size(); i++) {
    record << left << setw(25) << setfill(' ') << resault[i].name; 
    record << resault[i].data << endl;
  }
}

void recordCSVMode(const vector<arguments>& resault){
  //key_words的長度並不是最終引數的數量
  ofstream record;
  remove(resault_file_name);
  record.open(resault_file_name, ios::app);
  size_t point = 0,point_t = 0;
  string head = resault[0].name;
  while(resault[point+1].name != head){
    record << resault[point++].name << ",";
  }
  record << resault[point].name << endl;
  
  for (size_t i = 0; i < resault.size(); i++) {
    if(point_t < point) {
      ++point_t;
      record << resault[i].data << ",";
    } else {
      point_t = 0;
      record << resault[i].data << endl;
    }
  }
}

void printInShell(const vector<arguments>& resault){
  // 格式化輸出,left表示偏左,setw是設定寬度,stefill是設定填充字元
  for (size_t i = 0; i < resault.size(); i++) {
    cout << left << setw(25) << setfill(' ') << resault[i].name; 
    cout << resault[i].data << endl;
  }
}

解析結果如下(這個花花綠綠的顏色是vscode的外掛,建立csv檔案之後就會提示安裝)

excel可以直接開啟,wps沒下就沒試。excel開啟如下:

剛開啟可能資料顯得有點擁擠,可以另存為excel正常的格式進行調整,要不然調整了這csv檔案也儲存不下來的。

好了,這波整完就可以開始摸魚啦把實驗放那跑,跑完手機會收到郵件提示,回去自動解析一波就行,至於更多的分類啥的功能,都可以在程式中實現。

(>.o)