Skip to content
Young Dou edited this page May 17, 2024 · 13 revisions

bash

周期执行

while sleep 2s; do date; echo "do somethings here"; done;

对某个目录下的文件遍历执行某操作

for file in `ls /sys/fs/cgroup`; do ls $file; done;

对 json 文件格式化打印

cat /tmp/your_file | python -m json.tool

对 json 文件进行操作处理(通过 python)

cat /tmp/your_file | python -c 'import json,sys;j=json.load(sys.stdin); print(j)'

批量选择和kill任务

sudo ps -ef | grep process_name_pattern | awk '{print $2}' | sudo xargs kill -9

Performance Tools

构造内存压力和OOM

bash -c "for b in {0..99999999}; do a=$b$a; done"

golang

对指定函数执行单测

go test -v hello_test.go -test.run TestHello

字符串格式化占位符

// https://pkg.go.dev/fmt

python

from datetime import datetime

# 时间戳转datetime对象,再转时间字符串
datetime_float = 1677724839.0
datetime_str2 = datetime.strftime(datetime.fromtimestamp(datetime_float), '%Y-%m-%d %H:%M:%S')
print(datetime_str2)

Java

日期格式化

字符串转日期

CPP

内存带宽压测

#include <pthread.h>
#include <iostream>
#include <memory.h>
#include <stdlib.h>
#include "stdio.h"
#include "unistd.h"
using namespace std;
#define MAX_SIZE sizeof(int)*1024*1024
#define NUM_THREADS     5
void* cpu_mem_comsumer(void *)
{
        int* tmp=(int *)malloc(MAX_SIZE);
        memset(tmp,'a',MAX_SIZE);
    int k=1000;
        while(1)
        {
        k--;
                for(int j=0;j<MAX_SIZE/4;j++)
                {
                        tmp[j] *= tmp[j];
                }
        }
        free(tmp);

}

int main(int argc, char *argv[])
{
        int thread_num=0;
        pthread_t  thread_index[100];
        char ch;
        while((ch = getopt(argc,argv,"c:")) != EOF )
        {
                switch(ch)
                {
                        case 'c' :
                                thread_num=atoi(optarg);
                        break;
                        default:
                                thread_num=1;
                        break;
                }

        }
        for(int i=0;i<thread_num;i++)
        {
                thread_index[i]=(pthread_t )malloc(sizeof(pthread_t));
                pthread_create(&thread_index[i],NULL,cpu_mem_comsumer,&i);
        }
        printf("%d",thread_num);
        pthread_join(thread_index[0],NULL);
        //cpu_mem_comsumer();
        return 0 ;
}
Clone this wiki locally