自动跑测试记录后发送邮件提醒并自动解析数据

打印 上一主题 下一主题

主题 1044|帖子 1044|积分 3132

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  使用脚本自动跑实验(Ubuntu),将实验结果记录在文件中,并在实验结束之后将结果通过邮件发送到邮箱,最后在windows端自动解析成excel表格(因为要在windows的origin上作图)。
SHELL
  1. touch run.sh #创建但不编辑脚本
  2. chmod 777 run.sh #给予脚本执行权限
  3. gedit run.sh #编辑脚本
复制代码

  • 脚本中可以创建变量,如直接x='100'或r='rm -rf'即可,${x}在执行的时候以字符串形式读取。

    • 注意变量创建时使用单引号,等于号前后不要有空格,可以是命令,可以使变量,反正都是字符串。
    • 如何以数字方式解析变量?被以$(())包裹的变量将以数字形式解析,比如在某行中有这个参数:--num=$((${n}*1000000)),这里${n}将被以数字形式解析。
    • 变量为路径:建议使用绝对路径。如想创建用于接收结果文件receive='/home/renjc/easy-test2.txt'之后touch ${receive},当然变量本质上是以字符串形式读取的,用receive='../xx.txt'当然也行,还更灵活,就是程序容易整错。


  • 脚本中若想执行命令,直接新一行然后将命令打上去就可以了,不需要额外的东西,脚本本质上还是一行一行执行的

    • 若命令中想带参数,可以直接用变量替换


  • 数组,volumn='128 512 1024 2048'即是数组,也可以当做一串字符串来解析。
  1. #遍历数组,循环,遍历文件夹
  2. workloads="./works/workloada.spec ./works/workloadb.spec ./works/workloadf.spec"
  3. for file_name in $workloads; do
  4.   ./ycsbc -db redis -threads 1 -P $file_name
  5. done
  6. #下面这个是单纯的for循环
  7. for ((tn=1; tn<=8; tn=tn*2)); do
  8. doneworkload
  9. #下面这个是遍历文件夹workload_dir中所有以workload开头的spec文件
  10. for file_name in $workload_dir/workload*.spec; do
  11. done
复制代码

    • $(date)代表当前时间,带年月日时分秒的那种。
    • shell脚本的函数参数并不需要申明什么的,${1}就代表函数第一个参数,以此类推,其中按照惯例,${0}代表函数名(或者说程序名)。调用函数也很简单,recordstart name ${v}即可调用,函数名后面的都是参数,在函数中使用${1},${2},${3}识别。
    • echo -n "xx" >> ${receive},只有echo的话,会输出到控制台中,加上>> ${receive}会输出到这个文件里面,加上-n代表这一行不换行,因为echo默认是每次都换行的。


使用以上几种组合基本可以完成绝大部分实验与程序。以下作为例子
这个作为例子(已折叠,可展开)
  1. #函数作用是记录name,当前时间,以及初步计算value
  2. recordstart(){
  3. time=$(date)
  4. echo "db_name: ${1}" >> ${receive}
  5. echo -n "start_time: ${time} and Value:" >> ${receive}
  6. echo "scale=2;${3}*${4}/1024/1000"|bc >> ${receive}
  7. #纯shell对除法的支持比较粗糙,如果想要保留两位小数,可以向上面这行一样。
  8. }
复制代码
自动发送邮件

mail如何添加附件没整成功,现在使用mutt命令实现,控制台执行:
sudo apt-get install mutt安装
echo "Body : run over \n file in attach" | mutt -s "Test run over" renjc0816@qq.com -a 'path'即可。
其中,"Body"开头的是邮件内容,-s后面的是邮件标题,-a后面的'path'是附件地址,正常使用不用带单引号,写在脚本里时可以用之前创建的${receive}代替。邮件是立刻发送的,收件箱里找不到请到垃圾箱里面找找。如下:
自动解析实验结果生成excel表格

这个程序自己写去
如何生成excel可读取的文件?csv文件:csv文件可以被excel读取,第一行表示excel的那个列名,用逗号分割,底下的数据也是,每一行代表excel表格内的一行,每一列的内容用逗号分割,英文的逗号。
至于解析实验结果,就逐行读取,然后捕捉自己想要的关键词就行了,性能啥的就不用考虑优化了,咋简单咋来。
下面的是自用的解析rocksdb的参数的,仅供参考
自用rocksdb参数解析(已折叠,可展开)[code] #include #include #include #include #include #include #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& resault,const string& file_path);void trimEmpty(string &s);//用于去除字符串首尾空格void recordString(const string& type, string& s,vector& resault);//用于格式化并记录datavoid recordInFile(const vector& resault);//解析结果记录到文件中,测试用void recordCSVMode(const vector& resault);void printInShell(const vector& resault);//打印到控制台,测试用//按顺序存放需要捕捉的关键词,问题在于只能顺序//如果该行以该关键词结尾,那么就将所有的东西记录下来//注意,这个数组并不是最终参数结果,有的一行记了两三个参数const static vector 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 resault;  readFile(resault,file_path);  // printInShell(resault);  // recordInFile(resault);  recordCSVMode(resault);  // getchar();  return 0;}void readFile(vector& 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& 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& resault){  ofstream record;  remove(resault_file_name);  record.open(resault_file_name, ios::app);  for (size_t i = 0; i < resault.size(); i++) {    record
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

莫张周刘王

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表