细节:
并没有发现lzo压缩效果比deflate差很多之类(压缩率相差约10%,由于数据极端重复,测试环境中lzo占用总空间也就最多约200MB(有点记不清了,只记得约deflate占用总空间的200%.)
单纯比较time计时,设置为lzo时确实也更快完成运行.
但是无限循环运行时明显感觉到整个系统卡的比deflate厉害,但我找不出数值化的测试方法.
测试步骤:
把系统设置为单核状态(非必须,只是为了方便占满CPU核心)
(2048是以MB为单位,是测试环境中物理内存的总容量)
代码: 全选
time ./zramtest.out 2048 r -1 -1
代码: 全选
//usage:
//indent ./zramswaptest.c;gcc -O0 ./zramswaptest.c
//./a.out max r/w repeat timeout
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int
main (int argc, char **argv)
{
time_t const start_time = time (NULL);
time_t timeout;
int max;
int repeat;
if (argc == 5)
{
max = atoi (argv[1]);
repeat = atoi (argv[3]);
timeout = ((time_t) (atoi (argv[4])));
}
else
return 1;
unsigned char **block_array =
calloc (sizeof (unsigned char *), (size_t) (max));
size_t block_length = (size_t) (1024u * 1024u);
unsigned char data[3];
data[0] = 'a';
data[1] = 'b';
data[2] = 'c';
unsigned i = 0u;
//initialize block_array
for (i = 0u; i < max; i++)
{
do
{
if ((timeout > ((time_t) (0)))
&& ((time (NULL) - start_time) > timeout))
{
printf ("%u:timeouted!\n", i);
return 0;
}
block_array[i] = malloc (block_length);
if (block_array[i] != NULL)
{
unsigned bi = 0u;
for (bi = 0u; bi < block_length; bi++)
block_array[i][bi] = data[bi % ((unsigned) (sizeof (data)))];
}
else
{
printf ("%u error\n", i);
}
}
while (NULL == block_array[i]);
}
puts ("init ok");
unsigned score = 0u;
if (0 == repeat)
goto show_score;
//do page read test
if ('r' == argv[2][0])
for (;;)
{
for (i = 0u; i < max; i++)
{
if ((timeout > ((time_t) (0)))
&& ((time (NULL) - start_time) > timeout))
{
puts ("timeouted!");
goto show_score;
}
unsigned bi = 0u;
for (bi = 0u; bi < block_length; bi++)
{
data[bi % ((unsigned) (sizeof (data)))] = block_array[i][bi];
}
score++;
}
if (repeat > 0)
{
repeat--;
if (0 == repeat)
goto show_score;
}
}
//do page write test
else if ('w' == argv[2][0])
for (;;)
{
for (i = 0u; i < max; i++)
{
if ((timeout > ((time_t) (0)))
&& ((time (NULL) - start_time) > timeout))
{
puts ("timeouted!");
goto show_score;
}
unsigned bi = 0u;
for (bi = 0u; bi < block_length; bi++)
{
block_array[i][bi] = data[bi % ((unsigned) (sizeof (data)))];
}
score++;
}
if (repeat > 0)
{
repeat--;
if (0 == repeat)
goto show_score;
}
}
show_score:
printf ("score:%u\n", score);
return 0;
}