【问题标题】:How to disable G-WAN servlet internal cache?如何禁用 G-WAN servlet 内部缓存?
【发布时间】:2012-12-13 01:09:33
【问题描述】:

gwan 版本:3.12.26

servlet 类型:C 和 Perl

问题:

gwan 内部缓存使请求不重新读取脚本

测试:

  1. 创建“日志”目录:

    [bash]# mkdir -p /dev/shm/random-c
    [bash]# chmod 777 /dev/shm/random-c
    
  2. 创建/path/to/gwan/0.0.0.0_8080/#0.0.0.0/csp/random.c

    // ============================================================================
    // C servlet sample for the G-WAN Web Application Server (http://trustleap.ch/)
    // ----------------------------------------------------------------------------
    // hello.c: just used with Lighty's Weighttp to benchmark a minimalist servlet
    // ============================================================================
    // imported functions:
    //   get_reply(): get a pointer on the 'reply' dynamic buffer from the server
    //    xbuf_cat(): like strcat(), but it works in the specified dynamic buffer
    // ----------------------------------------------------------------------------
    #include <sys/time.h>
    #include "gwan.h" // G-WAN exported functions
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    //------------------
    void init_random(){
        struct /*sys/time.h->*/timeval res;
        /*sys/time.h->*/gettimeofday(&res,NULL);
       /*stdlib.h->*/srand( (unsigned int)/*stdlib.h->*/time(NULL) + res.tv_usec);
    }
    
    //------------------
    char *get_rnd_char(int num){
        char *char_list = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int  char_list_len = 62;
        char *ret = (char *)/*stdlib.h->*/malloc((num * sizeof(char)) + 1);
        int i,r;
    
    
        for(i=0;i<num;i++){
            r=(int) (/*stdlib.h->*/rand() % char_list_len);
            ret[i] = char_list[r==char_list_len ? r-1 : r];
        }
        ret[num] = '\0';
        return ret;
    }
    
    //------------------
    int main(int argc, char *argv[])
    {
        char *rnd_out; //-- random data for browser output and file input
        char *rnd_file; //-- random file
        char *rnd_path; //-- for speed let's make on ramdisk /dev/shm/random-c/
        char *t;
        FILE *F;
    
        int num_char=10;
        int arg_cnt=1;
    
        if(argc>0){
            //-- why nobody love C ? one of the reason is these kind parsing thing
            while ((t = /*string.h->*/strtok(argv[0], "=")) != NULL) {
                argv[0] = NULL;
                if(arg_cnt == 2){
                    num_char = /*stdlib.h->*/atoi(t);
                }
                arg_cnt++;
            }
        }else{
            //-- get random number betwen 1 to 1000
            num_char = (rand() % 1000)+1;
        }
    
        init_random();
    
    
       //-- create random data
        rnd_out = get_rnd_char(num_char);
    
    
        //-- creating "log" path
        //-- why nobody love C ? more reason
       rnd_file = get_rnd_char(20);
       // "/dev/shm/random-c/xxxxxxxxxxxxxxxxxxxx" -> 38 chars + 1 for \0
       rnd_path = (char *)/*stdlib.h->*/malloc((38 * sizeof(char)) + 1);
       rnd_path[0] = '\0';
       /*string.h->*/strcat(rnd_path,"/dev/shm/random-c/");
       /*string.h->*/strcat(rnd_path,rnd_file);
    
        //-- save to file
        F = /*stdio.h->*/fopen(rnd_path,"w");
            /*stdio.h->*/fprintf(F,"%s",rnd_out);
        /*stdio.h->*/fclose(F);
    
    
       //-- send output to browser
       /*gwan.h->*/xbuf_cat(get_reply(argv), rnd_out);
    
    
        //-- cleanup memory
        //-- why nobody love C ? MAIN reason: no easy way of memory management
       /*stdlib.h->*/free(rnd_file);
       /*stdlib.h->*/free(rnd_out);
       /*stdlib.h->*/free(rnd_path);
    
       return 200; // return an HTTP code (200:'OK')
    }
    
    // ============================================================================
    // End of Source Code
    // ============================================================================
    
  3. 在浏览器上运行:

    http://localhost:8080/?random.c 
    

    那么你应该在 /dev/shm/random-c/ 有一个 20char 的随机文件

  4. 这里是“问题”,运行:

    ab -n 1000 'http://localhost:8080/?random.c'
    

    我的 ubuntu 有输出:

    Finished 1000 requests
    
    
    Server Software:        G-WAN
    Server Hostname:        localhost
    Server Port:            8080
    
    Document Path:          /?random.c
    Document Length:        440 bytes
    
    Concurrency Level:      1
    Time taken for tests:   0.368 seconds
    Complete requests:      1000
    Failed requests:        361
       (Connect: 0, Receive: 0, Length: 361, Exceptions: 0)
    Write errors:           0
    Total transferred:      556492 bytes
    HTML transferred:       286575 bytes
    Requests per second:    2718.73 [#/sec] (mean)
    Time per request:       0.368 [ms] (mean)
    Time per request:       0.368 [ms] (mean, across all concurrent requests)
    Transfer rate:          1477.49 [Kbytes/sec] received
    

    试试:

    [bash]# ls /dev/shm/random-c/
    

    该目录仅列出 4 或 5 个随机文件,预计为 1000 个文件

  5. 在 random.c 和 perl 的版本 random.pl 上测试

所以回到开头的问题,如何禁用 GWAN 内部缓存,我尝试阅读 gwan 用户指南以在处理程序中设置某些内容,但什么也没找到(或者我错过了该指南中的某些内容)。

感谢 GWAN 团队提供这款出色的产品。 欢迎任何答案..谢谢

【问题讨论】:

  • 我对存储在数据库中的数据有同样的问题...... 100000 个请求,但数据库中只填充了 30 个项目......
  • @isenkSaja - 为什么你(再次)将一个记录在案的特性作为一个 bug 呈现仍然是一个谜......直到有人读到你反复写的 “为什么没有人喜欢 C”您的代码示例:C 与任何语言一样,可能会被严重误用(您发布的代码将小 10 倍,快 10 倍,因此编写和阅读容易 10 倍……如果您只是费心阅读 G-广域网 API)。
  • @gil,啊,好吧……看到你的回答并删除了我的帖子……现在我知道为什么没有人使用 GWAN……而且不是每个“报告”或“输入”都是 FUD。 ..你已经失去了一位未来的用户/客户……无论如何谢谢……
  • @isenkSaja (a) 我没有足够的权限来删除任何帖子,但是 (b) 你所指的帖子和我评论的帖子在传播技术上不准确的陈述;此外,它(c)与您的问题无关。 Stackoverflow 是一个问答平台,而不是论坛。如果您想开始讨论 G-WAN 如何更好地满足您的需求,那么您应该联系 G-WAN。

标签: g-wan


【解决方案1】:

我认为您所说的功能是微缓存。要禁用它,URI 需要在 200 毫秒内对每个请求都是唯一的。 (比如在 URI 上添加随机数)

G-WAN FAQ 状态:

“为了免除对前端缓存服务器的需求(并让 G-WAN 用作缓存反向代理)G-WAN 支持微缓存,这是一种 RESTful 功能。当调用给定的 URI 时在高并发和生成payload需要大量时间时,G-WAN会自动缓存一个页面200毫秒(互联网上的平均延迟),以确保缓存是最新的:200毫秒内, 连续请求提供预期结果。为防止触发微缓存,请对并发请求使用不断变化的查询参数(每个用户会话 id、随机数、计数器等)。”

请注意,v4.10+ 缓存默认禁用,请查看gwan/init.c 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-10
    相关资源
    最近更新 更多