[pLog-svn] r3413 - plog/trunk/tools
oscar at devel.lifetype.net
oscar at devel.lifetype.net
Sun May 14 14:51:54 GMT 2006
Author: oscar
Date: 2006-05-14 14:51:54 +0000 (Sun, 14 May 2006)
New Revision: 3413
Added:
plog/trunk/tools/genloadtestdata.php
Log:
script that will output a CSV file the standard output with random requests (both GET and POST) based on the contents of a LT database. The resulting CSV file/script will be "played" by another script that handles the networking part, probably interfacing with apache bench or a similar tool. This script needs to be run in the same server where the tests are going to be executed because it needs to connect to the db.
The GET_RATE and POST_RATE constants do nothing yet, so the script is a random distribution of GET and POST requests (though it'd be nice since I believe in a normal site there are a lot more GET that POST requests)
Added: plog/trunk/tools/genloadtestdata.php
===================================================================
--- plog/trunk/tools/genloadtestdata.php 2006-05-14 14:42:28 UTC (rev 3412)
+++ plog/trunk/tools/genloadtestdata.php 2006-05-14 14:51:54 UTC (rev 3413)
@@ -0,0 +1,152 @@
+<?php
+
+ /**
+ * number of requests to perform
+ */
+ define( "NUM_REQUESTS", 10 );
+
+ /**
+ * base url for these tests
+ */
+ define( "BASE_URL", "http://localhost/plog/" );
+
+ /**
+ * proportion of GET requests
+ */
+ define( "GET_RATE", 70 );
+
+ /**
+ * rate of POST requests
+ */
+ define( "POST_RATE", 30 );
+
+ if (!defined( "PLOG_CLASS_PATH" )) {
+ define( "PLOG_CLASS_PATH", dirname(__FILE__)."/../");
+ }
+
+ include_once( PLOG_CLASS_PATH."class/database/db.class.php" );
+
+ // connect to the db
+ $db =& Db::getDb();
+
+ // load the list of post and blog ids
+ $postsResult = $db->Execute( "SELECT id, blog_id FROM ".Db::getPrefix()."articles WHERE status = 1 LIMIT 1,200" );
+ while( $row = $postsResult->FetchRow()) {
+ if( !isset( $postIds[$row["blog_id"]] ))
+ $postIds[$row["blog_id"]] = Array();
+
+ $postIds[$row["blog_id"]][] = $row["id"];
+ }
+
+ // generate some random links to blogs
+ $numBlogs = count( $postIds );
+ $numReqs = 0;
+
+ while( $numReqs < NUM_REQUESTS ) {
+ // select a random blog
+ $blogId = rand( 1, $numBlogs );
+
+ // and a random article from this blog
+ $articleId = $postIds[$blogId][rand(0,count($postIds[$blogId])-1)];
+
+ // find out whether this is a POST or a GET request
+ $reqType = rand(1,2);
+ if( $reqType == 1 ) {
+ $getType = rand(1,2);
+ if( $getType == 1 ) {
+ // generate a link to the blog
+ $url = BASE_URL."index.php?op=Default&blogId=".$blogId;
+ }
+ else {
+ // generate a permalink
+ $url = BASE_URL."index.php?op=ViewArticle$blogId=".$blogId."&articleId=".$articleId;
+ }
+
+ printGET( $url );
+ }
+ else {
+ // post a comment to any of the articles of the given blog
+ $url = BASE_URL."index.php";
+ // form values
+ $form = Array ( "articleId" => $articleId, "op" => "AddComment", "commentTopic" => generateRandomWords(rand(1,15)),
+ "commentText" => generateRandomParagraphs(rand(1,5)), "blogId" => $blogId,
+ "userName" => "loadtest" );
+ // print the request
+ printPOST( $url, $form );
+ }
+
+ $numReqs++;
+ }
+
+/**
+ * outputs a line to perform a GET request
+ */
+Function printGET( $url )
+{
+ print( "GET, $url\n" );
+}
+
+/**
+ * outputs a line to perform a POST request
+ */
+function printPOST( $url, $formVars )
+{
+ $string = "POST, $url, \"";
+ $form = Array();
+ foreach( $formVars as $key=>$value ) {
+ $form[] = "$key=$value";
+ }
+ $formString = implode( "|", $form );
+ $string .= "$formString\"\n";
+ print($string);
+}
+
+function generateRandomWord($lenght, $uppercase = false) {
+ $newcode_length = 1;
+ $newcode = "";
+ while($newcode_length < $lenght) {
+ $a=97;
+ $b=122;
+ if ($newcode_length == 1) {
+ if (rand(1,4) == 1 || $uppercase) {
+ $a=65;
+ $b=90;
+ }
+ }
+ $code_part=chr(rand($a,$b));
+ $newcode_length++;
+ $newcode = $newcode.$code_part;
+ }
+ return $newcode;
+}
+
+function generateRandomWords( $count, $uppercase = false, $html = true) {
+ $words = generateRandomWord( rand( 3, 12), $uppercase );
+ for ($i = 1; $i < $count; $i++) {
+ $rand = rand(3, 12);
+ $words .= ' ' . generateRandomWord( $rand );
+ }
+ return $words;
+}
+
+function generateRandomParagraph() {
+ $length = rand( 3, 20 );
+ $counter = 0;
+ $text = '<p>' . generateRandomWord( rand( 3, 12 ), true );
+ while ($counter < $length) {
+ $word = generateRandomWords( rand(3, 12) );
+ $text = $text . ' ' . $word;
+ $counter++;
+ }
+ $text .= "</p>";
+ return $text;
+}
+
+function generateRandomParagraphs( $count ) {
+ $text = '';
+ for ($i = 1; $i < $count; $i++) {
+ $text .= generateRandomParagraph();
+ }
+ return $text;
+}
+?>
\ No newline at end of file
More information about the pLog-svn
mailing list