平时记录——memcached内存统计脚本

/**
 * Created by tangxx3 on 2015/4/17.
 */
var fs=require('fs');
var M=1024*1024;
var G=1024*1024*1024;

var rawData=fs.readFileSync('C:\\Users\\tangxx3\\Desktop\\mem.txt').toString();
var rowData=rawData.split('\n');

/**
**数据分组
 */
var groupSlab={};
for(var index in rowData){
    var reg=/(STAT \d*:).*?(\d+)/;
    var matchGroup=reg.exec(rowData[index]);
    var key=matchGroup[1];
    var item=matchGroup[2];

    if(groupSlab[key]==undefined){
        groupSlab[key]=[];
    }
    groupSlab[key].push(item);
}

/**
 **分组统计
 */
var totalMemSize=0;
var statGroup={};

var total_chunks_All= 0,total_chunks_Group={};
var unused_Total= 0,unused_Group={};
var canused_Total=0,canused_Group={};
var total_req= 0,total_req_group={};
var left_size_total= 0,left_size_group={};


for(var key in groupSlab){
    //统计每一组的内存大小
    var groupItems=groupSlab[key];

    //统计指标参数
    var chunk_size=parseInt(groupItems[0]);
    var total_chunks=parseInt(groupItems[3]);
    var free_chunks=parseInt(groupItems[5]);
    var free_chunks_end=parseInt(groupItems[6]);
    var mem_requested=parseInt(groupItems[7]);

    //内存大小
    var slabMemSize=chunk_size*total_chunks;
    totalMemSize+=slabMemSize;
    statGroup[key]=slabMemSize;

    //chunck数量
    total_chunks_All+=total_chunks;
    total_chunks_Group[key]=total_chunks;

    //未申请的chunck数量
    unused_Total+=free_chunks_end;

    var can_ree_chunks=free_chunks+free_chunks_end;
    canused_Total+=can_ree_chunks;
    //未申请的chunck数量
    unused_Group[key]=free_chunks_end/total_chunks;
    canused_Group[key]=can_ree_chunks/total_chunks;

    //请求数量
    total_req+=mem_requested;
    total_req_group[key]=mem_requested

    //未申请的chunck大小
    var left_size=chunk_size*free_chunks_end;
    left_size_total+=left_size;
    left_size_group[key]=left_size;
}

var itemKeys=[];

console.log('/**********************统计分组内存情况******************/');
 itemKeys.push('汇总');
itemKeys.push('内存大小,总chuck数,可用chuncks,总未free_chunks_end数,总未free_chunks_end大小,请求总数');

itemKeys.push(formatG(totalMemSize)+','+total_chunks_All+','+canused_Total+','+unused_Total+','+formatM(left_size_total)+','+total_req);
itemKeys.push('');
itemKeys.push('');
itemKeys.push('');
itemKeys.push('分组项,chuck内存(占总比),total_chunks(占总比),free_chunks/total_chunks,free_chunks_end/total_chunks,请求数总比,free_chunks_end大小');
for(var key in statGroup){
    var tmpKey=[];
    var memSize=parseInt(statGroup[key]);
    var memPercent=(memSize/totalMemSize);
    //分组项
    tmpKey.push(key+",");

    //chuck内存(占总比)
    tmpKey.push(formatM(memSize)+'('+formatPercent(memPercent)+')'+',');

    //total_chunks(占总比)
    var total_chunks=total_chunks_Group[key];
    var chunckPercent=total_chunks/total_chunks_All
    tmpKey.push(total_chunks+'('+formatPercent(chunckPercent)+'),');

    //free_chunks/total_chunks
    tmpKey.push(formatPercent(canused_Group[key])+',');
    tmpKey.push(formatPercent(unused_Group[key])+',');

    //请求比
    tmpKey.push(formatPercent(total_req_group[key]/total_req)+',');

    //free_chunks_end大小
    tmpKey.push(formatM(left_size_group[key]));

    itemKeys.push(tmpKey.join(' '))
}

var result=itemKeys.join('\n');
fs.writeFileSync('D:\\mem.csv',result,'utf8');
console.log(result);
console.log('/-----------------------------------------------------/');


function formatPercent(val){
    return (new Number(val*100).toFixed(3))+'%';
}

function formatM(val){
    return (new Number(val/M).toFixed(2))+'M';
}

function formatG(val){
    return (new Number(val/G).toFixed(2))+'G';
}

结果:

/**********************统计分组内存情况******************/                                                                                                            
汇总                                                                                                                                                                  
内存大小,总chuck数,可用chuncks,总未free_chunks_end数,总未free_chunks_end大小,请求总数                                                                                 
14.82G,3869277,2767641,8808,13.47M,2382043858






分组项,chuck内存(占总比),total_chunks(占总比),free_chunks/total_chunks,free_chunks_end/total_chunks,请求数总比,free_chunks_end大小                                  
STAT 7:, 81.98M(0.540%), 223860(5.786%), 97.353%, 1.074%, 0.084%, 0.88M
STAT 8:, 38.99M(0.257%), 85176(2.201%), 60.016%, 2.374%, 0.675%, 0.93M
STAT 9:, 33.99M(0.224%), 59398(1.535%), 67.251%, 1.973%, 0.481%, 0.67M
STAT 10:, 108.97M(0.718%), 151946(3.927%), 59.467%, 0.787%, 1.841%, 0.86M
STAT 11:, 939.34M(6.191%), 1043400(26.966%), 46.742%, 0.057%, 18.607%, 0.54M
STAT 12:, 30.98M(0.204%), 27435(0.709%), 0.000%, 0.000%, 1.166%, 0.00M
STAT 13:, 24.98M(0.165%), 17700(0.457%), 64.056%, 1.633%, 0.351%, 0.41M
STAT 14:, 12.98M(0.086%), 7332(0.189%), 26.650%, 0.000%, 0.389%, 0.00M
STAT 15:, 24.95M(0.164%), 11275(0.291%), 78.962%, 1.038%, 0.202%, 0.26M
STAT 16:, 210.95M(1.390%), 76171(1.969%), 83.499%, 0.459%, 1.456%, 0.97M
STAT 17:, 628.46M(4.142%), 181440(4.689%), 31.182%, 0.140%, 17.703%, 0.88M
STAT 18:, 2504.72M(16.509%), 577990(14.938%), 81.017%, 0.037%, 18.451%, 0.93M
STAT 19:, 5262.60M(34.687%), 971520(25.109%), 90.995%, 0.001%, 18.834%, 0.05M
STAT 20:, 1477.93M(9.741%), 218148(5.638%), 86.546%, 0.000%, 7.726%, 0.00M
STAT 21:, 208.85M(1.377%), 24662(0.637%), 36.027%, 0.000%, 5.101%, 0.00M
STAT 22:, 65.70M(0.433%), 6204(0.160%), 92.666%, 0.000%, 0.183%, 0.00M
STAT 23:, 999.72M(6.589%), 75525(1.952%), 97.016%, 0.053%, 1.169%, 0.53M
STAT 24:, 1716.71M(11.315%), 103740(2.681%), 99.444%, 0.054%, 0.368%, 0.93M
STAT 25:, 56.61M(0.373%), 2736(0.071%), 98.501%, 0.950%, 0.031%, 0.54M
STAT 26:, 3.93M(0.026%), 152(0.004%), 98.026%, 3.289%, 0.003%, 0.13M
STAT 27:, 2.91M(0.019%), 90(0.002%), 96.667%, 15.556%, 0.004%, 0.45M
STAT 28:, 2.91M(0.019%), 72(0.002%), 100.000%, 11.111%, 0.000%, 0.32M
STAT 29:, 13.44M(0.089%), 266(0.007%), 99.624%, 6.015%, 0.002%, 0.81M
STAT 30:, 40.74M(0.269%), 645(0.017%), 99.845%, 0.155%, 0.003%, 0.06M
STAT 31:, 10.42M(0.069%), 132(0.003%), 93.182%, 2.273%, 0.030%, 0.24M
STAT 32:, 14.81M(0.098%), 150(0.004%), 97.333%, 4.000%, 0.015%, 0.59M
STAT 33:, 20.73M(0.137%), 168(0.004%), 98.214%, 0.000%, 0.014%, 0.00M
STAT 34:, 24.06M(0.159%), 156(0.004%), 100.000%, 0.641%, 0.000%, 0.15M
STAT 35:, 38.56M(0.254%), 200(0.005%), 99.500%, 0.500%, 0.008%, 0.19M
STAT 36:, 184.12M(1.214%), 764(0.020%), 99.869%, 0.000%, 0.011%, 0.00M
STAT 37:, 56.03M(0.369%), 186(0.005%), 95.161%, 0.538%, 0.115%, 0.30M
STAT 38:, 73.05M(0.481%), 194(0.005%), 53.608%, 0.515%, 1.294%, 0.38M
STAT 39:, 90.37M(0.596%), 192(0.005%), 63.542%, 0.521%, 1.370%, 0.47M
STAT 40:, 77.08M(0.508%), 131(0.003%), 100.000%, 0.000%, 0.000%, 0.00M
STAT 41:, 88.99M(0.587%), 121(0.003%), 31.405%, 0.000%, 2.315%, 0.00M
/-----------------------------------------------------/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值