From 3ad84dbb4186cbb99a4e90f329dfbe574b770e42 Mon Sep 17 00:00:00 2001 From: Gonzalo Paniagua Javier Date: Mon, 14 Jul 2003 17:58:29 +0000 Subject: [PATCH] Initial revision svn path=/trunk/bench/; revision=16231 --- hb/AssemblyInfo.cs | 21 +++++++ hb/ChangeLog | 4 ++ hb/Makefile | 30 ++++++++++ hb/README | 14 +++++ hb/hb.cs | 146 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 215 insertions(+) create mode 100644 hb/AssemblyInfo.cs create mode 100644 hb/ChangeLog create mode 100644 hb/Makefile create mode 100644 hb/README create mode 100644 hb/hb.cs diff --git a/hb/AssemblyInfo.cs b/hb/AssemblyInfo.cs new file mode 100644 index 0000000..46cbe93 --- /dev/null +++ b/hb/AssemblyInfo.cs @@ -0,0 +1,21 @@ +// +// AssemblyInfo.cs for hb +// +// Authors: +// Gonzalo Paniagua Javier (gonzalo@ximian.com) +// +// (C) 2003 Ximian, Inc (http://www.ximian.com) +// +using System.Reflection; +using System.Runtime.CompilerServices; +using Mono.GetOptions; + +[assembly: AssemblyVersion("0.1.0.0")] +[assembly: AssemblyTitle ("hb")] +[assembly: AssemblyDescription ("HttpWebRequest benchmark.")] +[assembly: AssemblyCopyright ("Copyright (c) 2003 Ximian, Inc.")] +[assembly: AssemblyCompany ("Ximian, Inc.")] +[assembly: Mono.About ("Performs some benchmarks on HttpWebRequest")] +[assembly: Mono.Author ("Gonzalo Paniagua Javier ")] +[assembly: Mono.UsageComplement ("http://hostname[:port]/path")] + diff --git a/hb/ChangeLog b/hb/ChangeLog new file mode 100644 index 0000000..cb2e10c --- /dev/null +++ b/hb/ChangeLog @@ -0,0 +1,4 @@ +14-07-2003 Gonzalo Paniagua Javier + + * Initial import. + diff --git a/hb/Makefile b/hb/Makefile new file mode 100644 index 0000000..aac993c --- /dev/null +++ b/hb/Makefile @@ -0,0 +1,30 @@ +# +# Makefile for hb.exe +# +# Authors: +# Gonzalo Paniagua Javier (gonzalo@ximian.com) +# +topdir=.. + +INSTALL=/usr/bin/install +RUNTIME= +MCS=mcs +MCSFLAGS= /debug+ /debug:full /nologo + +ASSEMBLIES= Mono.GetOptions + +SOURCES= hb.cs \ + AssemblyInfo.cs + +# + +REFS= $(addsuffix .dll, $(addprefix /r:, $(ASSEMBLIES))) + +all: hb.exe + +hb.exe: $(SOURCES) $(RESOURCES) $(STRINGFILES) + $(MCS) $(MCSFLAGS) $(REFS) $(RESS) /out:$@ $(SOURCES) + +clean: + rm -f *~ *.exe *.bak *.temp + diff --git a/hb/README b/hb/README new file mode 100644 index 0000000..be2f815 --- /dev/null +++ b/hb/README @@ -0,0 +1,14 @@ +hb - HttpWebRequest Benchmark +------------------------------------ +hb is a wannabe ab (Apache Benchmark) like benchmarking tool. + +Just type 'make' to compile. + +Then run it like: + + mono hb.exe -n NN http://..... + +where NN is the number of request (GET) to do. + +-Gonzalo + diff --git a/hb/hb.cs b/hb/hb.cs new file mode 100644 index 0000000..5ccd353 --- /dev/null +++ b/hb/hb.cs @@ -0,0 +1,146 @@ +// +// hb.cs: wannabe clone of ab (apache benchmark) +// +// Authors: +// Gonzalo Paniagua Javier (gonzalo@ximian.com) +// +// (c) 2003 Ximian, Inc. (http://www.ximian.com) +// + +using System; +using System.IO; +using System.Net; +using System.Reflection; +using Mono.GetOptions; + +namespace Mono.Bench +{ + class HttpWebRequestBench + { + static Settings settings; + static Uri testUri; + static string server; + static void Connect (string uri, bool stats) + { + HttpWebRequest req = (HttpWebRequest) WebRequest.Create (uri); + req.AllowAutoRedirect = false; + HttpWebResponse wr = (HttpWebResponse) req.GetResponse (); + Stream s = wr.GetResponseStream (); + long length = wr.ContentLength; + if (length >= 0) { + byte [] buffer = new byte [length]; + s.Read (buffer, 0, (int) length); + /*int r = s.Read (buffer, 0, (int) length); + Console.Write (System.Text.Encoding.Default.GetString (buffer, 0, r));*/ + + } else { + byte [] buffer = new byte [4096]; + length = 0; + int read = 0; + while ((read = s.Read (buffer, 0, 4096)) != 0) { + //Console.Write (System.Text.Encoding.Default.GetString (buffer, 0, read)); + length += read; + } + } + s.Close (); + if (stats) { + Counters.TotalBytesRead += length; + Counters.TotalBytesRead += wr.Headers.ToString ().Length; + Counters.TotalBytesRead += wr.Headers.ToString ().Length; + // 17 -> "HTTP/1.x YYY \r\n".Length + Counters.TotalBytesRead += 17 + wr.StatusDescription.Length; + } + + if (server == null) + server = wr.Headers ["Server"]; + } + + static void Report () + { + TimeSpan total = new TimeSpan (Counters.End - Counters.Start); + Console.WriteLine ("Server software:\t{0}", server); + Console.WriteLine ("Server hostname:\t{0}", testUri.Host); + Console.WriteLine ("Server port:\t\t{0}", testUri.Port); + Console.WriteLine (); + Console.WriteLine ("Time taken for tests:\t{0}", total); + Console.WriteLine ("Requests:\t\t{0}", settings.NRequests); + double rps = settings.NRequests / total.TotalSeconds; + Console.WriteLine ("Requests/s:\t\t{0}", rps); + Console.WriteLine ("Bytes received:\t\t{0}", Counters.TotalBytesRead); + Console.WriteLine ("Bytes/request (avg.):\t{0}", Counters.TotalBytesRead / settings.NRequests); + Console.WriteLine ("Transfer rate:\t\t{0}", Counters.TotalBytesRead / total.TotalSeconds); + } + + static int Main (string [] args) + { + settings = new Settings (args); + if (settings.NRequests <= 0) + settings.NRequests = 1; + + args = settings.RemainingArguments; + if (args.Length != 1) { + settings.DoUsage (); + return 1; + } + + testUri = new Uri (args [0]); + /* pre-jit */ + try { + Connect (args [0], false); + } catch (WebException e) { + Console.WriteLine (e.Message); + if (e.Response != null) { + StreamReader r = new StreamReader (e.Response.GetResponseStream ()); + Console.WriteLine (r.ReadToEnd ()); + } + return 0; + } + /**/ + + Counters.Start = DateTime.Now.Ticks; + for (int i = 0; i < settings.NRequests; ++i) { + Connect (args [0], true); + } + + Counters.End = DateTime.Now.Ticks; + Report (); + return 0; + } + } + + sealed class Counters + { + public static long Start; + public static long End; + public static long TotalBytesRead; + } + + sealed class Settings : Options + { + [Option ("Be verbose", 'v', "verbose")] + public bool Verbose = false; + [Option ("Number of requests", 'n', "requests")] + public int NRequests = 1; + + + public Settings (string [] args) + { + this.ParsingMode = OptionsParsingMode.Both; + ProcessArgs (args); + } + + [Option("Show usage syntax", 'h', "usage")] + public override WhatToDoNext DoUsage() + { + base.DoUsage(); + return WhatToDoNext.AbandonProgram; + } + + public override WhatToDoNext DoHelp() // uses parentīs OptionAttribute as is + { + base.DoHelp(); + return WhatToDoNext.AbandonProgram; + } + } +} +