Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

性能测试疑问 #47

Open
CarlZhang111 opened this issue Dec 24, 2024 · 5 comments
Open

性能测试疑问 #47

CarlZhang111 opened this issue Dec 24, 2024 · 5 comments

Comments

@CarlZhang111
Copy link

CarlZhang111 commented Dec 24, 2024

说明

  • 测试环境: arm笔记本
  • 测试版本:commit: abacb8362f67c07eb1a2b5b7406bab0dbdb5abfe

准备工作

  • 修改shmcache_set.c代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <chrono>
#include <iostream>
#include <thread>
#include "fastcommon/logger.h"
#include "fastcommon/shared_func.h"
#include "shmcache/shmcache.h"

static void usage(const char *prog)
{
    fprintf(stderr, "shmcache set key value.\n"
         "Usage: %s [config_filename] <key> <value> <ttl>\n", prog);
}

int main(int argc, char *argv[])
{
	int result;
    int index;
    char *config_filename;
    struct shmcache_context context;
    struct shmcache_key_info key;
    char *value;
    int value_len;
    int ttl;

    if (argc >= 2 && (strcmp(argv[1], "-h") == 0 ||
                strcmp(argv[1], "help") == 0 ||
                strcmp(argv[1], "--help") == 0))
    {
        usage(argv[0]);
        return 0;
    }
    if (argc < 4) {
        usage(argv[0]);
        return EINVAL;
    }

    config_filename = "/etc/libshmcache.conf";
    if (isFile(argv[1])) {
        if (argc < 5) {
            usage(argv[0]);
            return EINVAL;
        }
        config_filename = argv[1];
        index = 2;
    } else {
        index = 1;
    }

	log_init();
    if ((result=shmcache_init_from_file(&context, config_filename)) != 0) {
        return result;
    }

    key.data = argv[index++];
    key.length = strlen(key.data);
    value = argv[index++];
    value_len = strlen(value);
    ttl = atoi(argv[index++]);
    while(1) {
      auto start_time = std::chrono::high_resolution_clock::now();
      result = shmcache_set(&context, &key, value, value_len, ttl);
      //if (result == 0) {
      //  printf("set key: %s success.\n", key.data);
      //} else {
      //  fprintf(stderr, "set key: %s fail, errno: %d\n", key.data, result);
      //}

      auto end_time = std::chrono::high_resolution_clock::now();
      auto duration =
        std::chrono::duration_cast<std::chrono::microseconds>(end_time - start_time);
      if (duration.count() > 100) {
        std::cout << "Set data with time: " << duration.count() << std::endl;
      }
      std::this_thread::sleep_for(std::chrono::milliseconds(10));
    }
    return result;
}
  • /etc/libshmcache.conf内容:
# shared memory type, shm or mmap
# shm for SystemV pure shared memory
# mmap for POSIX shared memory based file
# default value is mmap
# Note: when type is shm, the shm limit is too small in FreeBSD and MacOS,
#  you should increase following kernel parameters:
#    kern.sysv.shmmax
#    kern.sysv.shmall
#    kern.sysv.shmseg
#  you can refer to http://www.unidata.ucar.edu/software/mcidas/2008/users_guide/workstation.html
type = mmap

# the lock filename
filename = /tmp/libshmcache

# the memory limit
# the oldest memory will be recycled when this max memory reached
max_memory = 32M

# the min memory
# default: 0, means do NOT set min memory
min_memory = 0

# the memory segment size for incremental memory allocation
segment_size = 2M

# the key number limit
max_key_count = 200000

# the size limit for one value
max_value_size = 256K

# the hash function in libfastcommon/src/hash.h
# default: simple_hash
hash_function = simple_hash

# recycle key number once when reach max keys
# <= 0 means recycle one memory striping
# default: 0
recycle_key_once = 0

# if recycle valid entries by FIFO
# default: false
recycle_valid_entries = true

# value allocator policy
# avg. key TTL threshold for recycling memory
# <= 0 for never recycle memory until reach memory limit (max_memory)
# unit: second
# default: 0
value_policy.avg_key_ttl = 5400

# when the remain memory <= this parameter, discard it
value_policy.discard_memory_size = 128

# when a value allocator allocate fail times > this parameter,
# means it is almost full, discard it
# default: 5
value_policy.max_fail_times = 5

# sleep time to avoid other processes read dirty data when
# recycle more than one valid (in TTL / not expired) KV entries
# 0 for never sleep
# unit: microsecond (us)
# default: 0
value_policy.sleep_us_when_recycle_valid_entries = 1000

# try lock interval in us, must great than zero
# unit: microsecond (us)
# default value is 200 us
lock_policy.trylock_interval_us = 200

# the interval to detect deadlock caused by the crushed process
# must great than zero
# unit: millisecond (ms)
# default value is 1000 ms
lock_policy.detect_deadlock_interval_ms = 1000

# standard log level as syslog, case insensitive, value list:
## emerg for emergency
## alert
## crit for critical
## error
## warn for warning
## notice
## info
## debug
log_level = info

编译完成后运行:

./shmcache_set test value 1

期望输出

正常情况下不会输出Set data with time

实际输出

[2024-12-24 02:09:19] INFO - file: shmopt.c, line: 86, striping_count: 2, striping.count.current: 0, context->memory->vm_info.segment.size: 2097152, context->memory->vm_info.striping.size: 1048576
[2024-12-24 02:09:19] INFO - file: shmopt.c, line: 109, pid: 4085, create value segment #1, size: 2097152
[2024-12-24 02:09:19] INFO - file: shmcache.c, line: 219, pid: 4085, init share memory first time, hashtable segment size: 4409992
Set data with time: 886μs
Set data with time: 4824μs
Set data with time: 1208μs
Set data with time: 1082μs
Set data with time: 4517μs
Set data with time: 7079μs
Set data with time: 1225μs
Set data with time: 1427μs
Set data with time: 7082μs
Set data with time: 1891μs
Set data with time: 2711μs
Set data with time: 2747μs
Set data with time: 493μs
Set data with time: 2631μs
Set data with time: 1148μs
Set data with time: 1111μs
Set data with time: 1209μs
Set data with time: 1107μs
Set data with time: 1130μs
Set data with time: 2402μs
Set data with time: 4471μs
Set data with time: 5060μs
Set data with time: 1192μs
Set data with time: 824μs
Set data with time: 849μs
Set data with time: 816μs
Set data with time: 2900μs
Set data with time: 1277μs
Set data with time: 1566μs
Set data with time: 7107μs
Set data with time: 6883μs
Set data with time: 212μs
Set data with time: 1088μs
Set data with time: 513μs
Set data with time: 4049μs
Set data with time: 3732μs
Set data with time: 927μs
Set data with time: 715μs
Set data with time: 802μs
Set data with time: 1274μs
Set data with time: 698μs
Set data with time: 2899μs
Set data with time: 2292μs
Set data with time: 777μs
Set data with time: 848μs
Set data with time: 542μs
Set data with time: 6138μs
Set data with time: 908μs
Set data with time: 1144μs
Set data with time: 1642μs
Set data with time: 448μs
Set data with time: 467μs
Set data with time: 791μs
Set data with time: 580μs
Set data with time: 136μs
Set data with time: 267μs
Set data with time: 1582μs
Set data with time: 5285μs
Set data with time: 3284μs
Set data with time: 3466μs
Set data with time: 4630μs

问题

请问这种写入单值是否正常情况下会存在耗时7ms+现象,如果正常的话,此repo的读写性能大概是什么样子?或有什么要求
期待回复,感谢

@happyfish100
Copy link
Owner

从测试结果来看,感觉不太正常。
会不会是你笔记本环境的问题呢?比如笔记本负载比较高,或者大量使用到了交换内存?

@CarlZhang111
Copy link
Author

和在docker里面跑测试程序有关系吗?因为不止我自己的笔记本有这个问题,其他笔记本测试出来也是最多2ms耗时,平时笔记本没有任何操作,只用来测试

@happyfish100
Copy link
Owner

你把 type = mmap 改为 type = shm,采用纯内存缓存方式测试一下看看效果

@CarlZhang111
Copy link
Author

改为shm跑了十几分钟到是没出现,谢谢

@happyfish100
Copy link
Owner

那应该就是采用 mmap方式,操作系统会把写入的数据不定期刷到文件 (磁盘 )导致的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants