设为首页收藏本站

LinuxTone | 运维专家网论坛 - 最棒的Linux运维与开源架构技术交流社区!

 找回密码
 注册

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

查看: 902|回复: 4

[性能测试] [原创]:发布个AB插件 [复制链接]

论坛元老

反正我是信了

Rank: 8Rank: 8

签到
81
注册时间
2011-6-1
最后登录
2011-12-9
在线时间
125 小时
阅读权限
90
积分
8080
帖子
439
主题
80
精华
0
UID
13993
发表于 2011-7-22 22:39:31 |显示全部楼层
本帖最后由 anxu 于 2011-7-22 22:52 编辑

        来这里一个月多了,得到了很多的帮助,也学到了很多东西。这两天在做对项目做压力测试,使用工具AB.EXE。
在使用的过程来回测试记录实在让人很烦,于是想写一个批处理。发现WIN批处理不支持浮点运算~
随手写个小插件,方便测试使用。也算是为论坛做贡献吧。
C#写的,代码很简单。如果你喜欢你扩展吧,目前只是满足了我的需求。
由于时间问题,没有时间重构代码。见谅吧~
几个参数---//0:采样次数、1:客户端数量、2:请求数量、3:网址、4:下一次请求等待时间(默认5秒)自己修改吧~得出多次采样的平均值。
#win的nagios监控程序,我写了很多安全模块~目前在使用中,回头我会发出来共享的#



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Text.RegularExpressions;
using System.IO;

namespace abPulg
{
    /// <summary>
    /// ab插件
    /// </summary>
    /// http://bbs.linuxtone.org
    /// 创建人:anxu
    /// 2011-7-21 14:41
    class Program
    {
        static void Main(string[] args)
        {
            if(string.IsNullOrEmpty(GetPath()))
            {   
                Console.WriteLine("请COPY ab.exe到当前目录");
                return;
            }
            try
            {
                //0:采样次数、1:客户端数量、2:请求数量、3:网址、4:下一次请求等待时间
                if (!string.IsNullOrEmpty(args[0]) && !string.IsNullOrEmpty(args[1]) && !string.IsNullOrEmpty(args[2]) && !string.IsNullOrEmpty(args[3]) && !string.IsNullOrEmpty(args[4]))
                {
                    decimal _tCount = 0;    //吞吐率
                    decimal _cCount = 0;    //客户端等待
                    decimal _sCount = 0;    //服务端处理时间
                    //采样次数
                    int RunNum = Convert.ToInt32(args[0]);
                    for (int i = 1; i <= RunNum; i++)
                    {
                        string Result = GetAbString(Convert.ToInt32(args[1]), Convert.ToInt32(args[2]), args[3]);
                        ABPlugs ab = new ABPlugs();
                        ab.TimetakenTest = decimal.Parse(GetRegexResult(Result, @"Timetakenfortests.+)seconds"));
                        ab.Requests_second = decimal.Parse(GetRegexResult(Result, @"Requestspersecond.+)\[\#\/sec\]\(mean\)"));
                        ab.ClientTimerequest = decimal.Parse(GetRegexResult(Result, @"Timeperrequest.+)\[ms\]\(mean\)"));
                        string _ts = GetRegexResult(Result, @"Timeperrequest.+)\(mean,acrossallconcurrentrequests\)");
                        ab.ServerTimerequest = decimal.Parse(GetRegexResult(_ts, @"(.+)\[ms\]"));
                        ab.TotalTransferred = decimal.Parse(GetRegexResult(Result, @"Totaltransferred.+)bytes"));
                        ab.Transfer_rate = decimal.Parse(GetRegexResult(Result, @"Transferrate.+)\[Kbytes\/sec\]received"));
                        decimal[] rs = PrintRs(ab, i);
                        _sCount += rs[0];
                        _tCount += rs[2];
                        _cCount += rs[1];
                        //Thread.Sleep(1000 * (Convert.ToInt16(args[4]) * 60)); 分钟
                        Thread.Sleep(5000); //秒
                    }
                    Console.WriteLine("============共使用{0}采样,并得出平均值===========", RunNum);
                    Console.WriteLine("吞吐量平均:{0}", _tCount / RunNum);
                    Console.WriteLine("客户端平均等待时间:{0}", _cCount / RunNum);
                    Console.WriteLine("服务端平均处理时间:{0}", _sCount / RunNum);
                }
                else
                {
                    Console.WriteLine("Usage: abPulg [options] [http://]hostname[:port]/path");
                    Console.WriteLine("This args 0:采样次数、1:客户端数量、2:请求数量、3:网址、4:下一次请求等待时间");
                }

            }
            catch (Exception)
            {

                Console.WriteLine("This args 0:采样次数、1:客户端数量、2:请求数量、3:网址、4:下一次请求等待时间");
            }
        }

        /// <summary>
        /// 获取当前AB路径
        /// </summary>
        /// <returns></returns>
        private static string GetPath()
        {
            string path = string.Format("{0}\\ab.exe", System.Environment.CurrentDirectory);
            if (!File.Exists(path))
                return "";
            return string.Format("{0}\\ab.exe", System.Environment.CurrentDirectory);
        }

        /// <summary>
        /// 打印结果
        /// </summary>
        /// <param name="ab"></param>
        /// <param name="number"></param>
        /// <returns></returns>
        private static decimal[] PrintRs(ABPlugs ab, int number)
        {
            Console.WriteLine("============第一次{0}采样,并得出结果===========", number);
            Console.WriteLine("请求总时间: {0}", ab.TimetakenTest);
            Console.WriteLine("吞吐量:{0}", ab.Requests_second);
            Console.WriteLine("客户端等待时间:{0}", ab.ClientTimerequest);
            Console.WriteLine("服务端处理时间:{0}", ab.ServerTimerequest);
            Console.WriteLine("传输大小:{0}", ab.Transfer_rate);
            return new decimal[] { ab.ServerTimerequest, ab.ClientTimerequest, ab.Requests_second };
        }

        /// <summary>
        /// 这个 GetRegexResult 是个集合
        /// </summary>
        /// <param name="startT">The start T.</param>
        /// 创建人:anxu
        /// 2011-7-21 14:46
        public static string GetRegexResult(string regexResult , string pattern)
        {
            Match m = Regex.Match(regexResult,pattern);
            if (!m.Success)
                return "";
            return m.Groups[1].ToString();
        }

        /// <summary>
        /// 这个 GetAbString 是个集合
        /// </summary>
        /// <param name="ccount">The ccount.</param>
        /// <param name="rcount">The rcount.</param>
        /// <param name="url">The URL.</param>
        /// <returns></returns>
        /// 创建人:anxu
        /// 2011-7-21 14:41
        public static string GetAbString(int ccount , int rcount , string url)
        {
            Process process = new Process();
            process.StartInfo.FileName = string.Format("{0} ", GetPath());
            process.StartInfo.Arguments = string.Format("-c{0} -n{1} {2} ",ccount,rcount,url);
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.CreateNoWindow = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.Start();
            string output = process.StandardOutput.ReadToEnd();
            string str =string.Empty;
            List<int> ls = new List<int>();
            for (int i = 0; i < output.Length; i++)
            {
                if (output != (char)32)
                    str+=output.ToString();
            }
            process.WaitForExit();
            return str;
        }

        /// <summary>
        /// 删除空格
        /// </summary>
        /// <param name="str">The STR.</param>
        /// <returns></returns>
        public static string RemoveNull(string str)
        {
            string rs = string.Empty;
            CharEnumerator  _tchar = str.GetEnumerator();
            while (_tchar.MoveNext())
            {
                byte[] array = new byte[1];
                array = System.Text.Encoding.ASCII.GetBytes(_tchar.Current.ToString());
                int asciicode=(short)(array[0]);
                if(asciicode!=32)
                {
                    rs += _tchar.Current.ToString();
                }
            }
            return rs;
        }
    }


    /// <summary>
    /// 插件类
    /// </summary>
    public class ABPlugs
    {
        public string Software { get; set; }
        public string Hostname { get; set; }
        public string Port  {get;set;}
        public string DocumentPath { get; set; }
        public long DocumentLenght { get; set; }
        public string Curr_User { get; set; }
        public decimal TimetakenTest { get; set; }
        public string CompleeRequests { get; set; }
        public string FailedRequests { get; set; }
        public string WriteErrors { get; set; }
        public decimal  TotalTransferred { get; set; }
        public string HTMLTransferred { get; set; }
        public decimal Requests_second { get; set; }
        public decimal ClientTimerequest {get;set;}
        public decimal ServerTimerequest { get; set; }
        public decimal Transfer_rate { get; set; }
    }
}


提供项目源文件


附件: 你需要登录才可以下载或查看附件。没有帐号?注册

LT管理团队

守住每一天

Rank: 9Rank: 9Rank: 9

注册时间
2008-5-30
最后登录
2012-5-22
在线时间
641 小时
阅读权限
200
积分
16214
帖子
1273
主题
176
精华
11
UID
31
发表于 2011-7-22 23:09:27 |显示全部楼层
支持原创
<-sina_sign,1054764633,7->

使用道具 举报

论坛元老

~~IT公司打杂的&&LT社区划水的~~

Rank: 8Rank: 8

注册时间
2010-11-4
最后登录
2012-5-22
在线时间
459 小时
阅读权限
90
积分
19479
帖子
511
主题
7
精华
0
UID
10809
发表于 2011-7-23 11:57:34 |显示全部楼层
顶一个~不错~~
~~开心快乐每一天~~

使用道具 举报

Rank: 4

注册时间
2010-5-28
最后登录
2012-1-14
在线时间
17 小时
阅读权限
50
积分
571
帖子
12
主题
2
精华
0
UID
9385
发表于 2012-1-13 17:15:08 |显示全部楼层

顶一个~不错~~

使用道具 举报

Rank: 8Rank: 8

注册时间
2011-10-20
最后登录
2012-5-9
在线时间
27 小时
阅读权限
90
积分
6032
帖子
95
主题
2
精华
0
UID
15807
发表于 2012-2-14 11:55:31 |显示全部楼层
支持中, 留个标记,等待不时之需.
K.I.S.S

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

IT运维专家网感谢红之盟网络提供带宽支持

合作联系: QQ:67888954/MSN:cnseek@msn.com/mail:netseek@linuxtone.org

Archiver|手机版|感谢所有关心和支持过LinuxTone的朋友们 转载本站内容请注明原作者名及出处 ( 京ICP备08103151 )   |

GMT+8, 2012-5-22 21:01 , Processed in 0.149316 second(s), 12 queries , Memcache On.

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部