feat:添加 cpu 相关的测试实验源码

This commit is contained in:
yanfeizhang
2022-05-08 22:48:46 +08:00
parent cee57dcda4
commit 473a7be0ca
6 changed files with 182 additions and 9 deletions

16
tests/cpu/test02/main.c Normal file
View File

@@ -0,0 +1,16 @@
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int in;
int i;
in = open("in.txt", O_RDONLY);
for(i=0; i<100; i++){
read(in,&c,1);
}
return 0;
}

41
tests/cpu/test03/main.c Normal file
View File

@@ -0,0 +1,41 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <sched.h>
#include <sys/types.h>
#include <unistd.h> //pipe()
int main()
{
int x, i, fd[2], p[2];
char send = 's';
char receive;
pipe(fd);
pipe(p);
struct timeval tv;
struct sched_param param;
param.sched_priority = 0;
while ((x = fork()) == -1);
if (x==0) {
sched_setscheduler(getpid(), SCHED_FIFO, &param);
gettimeofday(&tv, NULL);
printf("Before Context Switch Time%u s, %u us\n", tv.tv_sec, tv.tv_usec);
for (i = 0; i < 10000; i++) {
read(fd[0], &receive, 1);
write(p[1], &send, 1);
}
exit(0);
}
else {
sched_setscheduler(getpid(), SCHED_FIFO, &param);
for (i = 0; i < 10000; i++) {
write(fd[1], &send, 1);
read(p[0], &receive, 1);
}
gettimeofday(&tv, NULL);
printf("After Context SWitch Time%u s, %u us\n", tv.tv_sec, tv.tv_usec);
}
return 0;
}

30
tests/cpu/test04/main.go Normal file
View File

@@ -0,0 +1,30 @@
package main
import (
"fmt"
"time"
"runtime"
)
func cal() {
for i :=0 ; i<1000000 ;i++{
//fmt.Printf("call:%d\n",i)
runtime.Gosched()
}
}
func main() {
runtime.GOMAXPROCS(1)
currentTime:=time.Now()
fmt.Println(currentTime)
go cal()
for i :=0 ; i<1000000 ;i++{
//fmt.Printf("main:%d\n",i)
runtime.Gosched()
}
currentTime=time.Now()
fmt.Println(currentTime)
}

91
tests/cpu/test05/main.c Normal file
View File

@@ -0,0 +1,91 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <pthread.h>
int pipes[20][3];
char buffer[10];
int running = 1;
void inti()
{
int i =20;
while(i--)
{
if(pipe(pipes[i])<0)
exit(1);
pipes[i][2] = i;
}
}
void distroy()
{
int i =20;
while(i--)
{
close(pipes[i][0]);
close(pipes[i][1]);
}
}
double self_test()
{
int i =20000;
struct timeval start, end;
gettimeofday(&start, NULL);
while(i--)
{
if(write(pipes[0][1],buffer,10)==-1)
exit(1);
read(pipes[0][0],buffer,10);
}
gettimeofday(&end, NULL);
return (double)(1000000*(end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec)/20000;
}
void *_test(void *arg)
{
int pos = ((int *)arg)[2];
int in = pipes[pos][0];
int to = pipes[(pos + 1)%20][1];
while(running)
{
read(in,buffer,10);
if(write(to,buffer,10)==-1)
exit(1);
}
}
double threading_test()
{
int i = 20;
struct timeval start, end;
pthread_t tid;
while(--i)
{
pthread_create(&tid,NULL,_test,(void *)pipes[i]);
}
i = 10000;
gettimeofday(&start, NULL);
while(i--)
{
if(write(pipes[1][1],buffer,10)==-1)
exit(1);
read(pipes[0][0],buffer,10);
}
gettimeofday(&end, NULL);
running = 0;
if(write(pipes[1][1],buffer,10)==-1)
exit(1);
return (double)(1000000*(end.tv_sec-start.tv_sec)+ end.tv_usec-start.tv_usec)/10000/20;
}
int main()
{
inti();
printf("%6.6f\n",self_test());
printf("%6.6f\n",threading_test());
distroy();
exit(0);
}

View File

@@ -1,6 +1,10 @@
## CPU相关实验
- [likely 和 unlikely 汇编结果对比](tests/cpu/test01)
- [read 系统调用开销](tests/cpu/test02)
- [read 进程上下文切换开销](tests/cpu/test03)
- [read 协程上下文切换开销](tests/cpu/test04)
- [read 线程上下文切换开销](tests/cpu/test05)
## 磁盘相关实验
- [使用fio磁盘压测工具进行性能压测分析](tests/disk/test01)

View File

@@ -1,9 +0,0 @@
- [PHP单语言的百万连接测试源码](tests/network/test01)
- [通过多 IP 达成单机百万连接支持c、java、php三种语言](tests/network/test02)
- [通过端口重用达成单机百万连接支持c、java、php三种语言](tests/network/test03)
- [一个模拟 tcpdump 的简单抓包程序](tests/network/test04)
- [用 bridge 连接本机上的多组 veth使其可以互相通信](tests/network/test05)
- [命令行使用 namespace 的简单实验](tests/network/test06)
- [手工模拟实现一个可以和外部通信的容器网络](tests/network/test07)
- [socket端口复用SO_REUSEPORT测试](tests/network/test08)