LiteSpeed Technologies
Download Download     Blog Blog     Wiki Wiki     Forum Forum     Store     Contact Contact    

Go Back   LiteSpeed Support Forums > LiteSpeed Web Server > Install/Configuration > C/C++ FastCGI

Reply
 
Thread Tools Display Modes
  #21  
Old 06-14-2005, 01:21 AM
fryk fryk is offline
Member
 
Join Date: Jun 2005
Posts: 16
Sure.

simple-test-script.php
Code:
<html><head><title>CGI C Example #3</title></head>
<body><h1>CGI C Example #3</h1>
<table border=2>

<?

$db = mysql_pconnect&#40;"localhost","root",""&#41; or die&#40;'konnekt'&#41;;
mysql_select_db&#40;"ht3"&#41; or die&#40;'db'&#41;;

$r = mysql_query&#40;"SELECT * FROM pages"&#41;;
if &#40;$r&#41;	
&#123;
	$nrows = mysql_num_rows&#40;$r&#41;;
	$num_fields = mysql_num_fields&#40;$r&#41;;
	
	printf&#40;"rows&#58; %i, cols&#58; %i

\n",$nrows,$num_fields&#41;;
	printf&#40;"<table border=2>\n"&#41;;

	while &#40; $row = mysql_fetch_array&#40;$r&#41; &#41;
	&#123;
		printf&#40;"<tr>"&#41;;
		for&#40;$j = 0; $j < $num_fields; $j++&#41;
		&#123;
			printf&#40;"<td>%s</td>", $row&#91;$j&#93; ? $row&#91;$j&#93; &#58; NULL"&#41;;
		&#125;
		printf&#40;"</tr>"&#41;;
		printf&#40;"\n"&#41;;
	&#125;

	printf&#40;"</table></body></html>\n"&#41;;
   mysql_free_result&#40;$r&#41;;

&#125;

mysql_close&#40;$db&#41;;

?>

</table></body></html>
simple-test-script.c
Code:
#include	<stdio.h>
#include	<mysql.h>
#include "fcgi_config.h"
#include <stdlib.h>
#include "fcgi_stdio.h"

int main &#40;&#41;
&#123;
	int	err = 0;
	MYSQL	dbase;

	if &#40;mysql_init&#40;&dbase&#41; == NULL&#41; err = 1;
	else
	&#123;		
		if&#40;mysql_real_connect&#40;&dbase,"localhost","root","","ht3",0,NULL,0&#41; == NULL&#41; err = 1;
	&#125;
	if&#40;err&#41;
	&#123;
		printf&#40;"

Error connecting to database</body></html>\n"&#41;;
		exit&#40;0&#41;;
	&#125;


	//
	// B E G I N   R E Q U E S T
	// 


    while &#40;FCGI_Accept&#40;&#41; >= 0&#41;
    &#123;
		char		sqlbuff&#91;255&#93;;
		MYSQL_RES	*result;
		
		printf&#40;"Content-type&#58; text/html\r\n\r\n"&#41;;
		printf&#40;"<html><head><title>CGI C Example #3</title></head>\n"&#41;;
		printf&#40;"<body><h1>CGI C Example #3&#58; %i</h1>\n",getpid&#40;&#41;&#41;;
	
		sprintf&#40;sqlbuff,"SELECT * FROM pages"&#41;;
		if&#40;mysql_real_query&#40;&dbase,sqlbuff,strlen&#40;sqlbuff&#41;&#41;&#41;
		&#123;
			printf&#40;"

SQL error</body></html>\n"&#41;;
			exit&#40;1&#41;;
		&#125;
		
		result = mysql_store_result&#40;&dbase&#41;;
		if&#40;result&#41;
		&#123;
			int			i, j, num_fields, nrows;
			MYSQL_ROW	row;;
			
			nrows = mysql_num_rows&#40;result&#41;;
			num_fields = mysql_num_fields&#40;result&#41;;
			
			printf&#40;"rows&#58; %i, cols&#58; %i

\n",nrows,num_fields&#41;;
			printf&#40;"<table border=2>\n"&#41;;

			while &#40; row = mysql_fetch_row&#40;result&#41; &#41;
			&#123;
				printf&#40;"<tr>"&#41;;
				for&#40;j = 0; j < num_fields; j++&#41;
				&#123;
					printf&#40;"<td>%s</td>", row&#91;j&#93; ? row&#91;j&#93; &#58; "NULL"&#41;;
				&#125;
				printf&#40;"</tr>"&#41;;
				printf&#40;"\n"&#41;;
			&#125;

			printf&#40;"</table></body></html>\n"&#41;;

			mysql_free_result&#40;result&#41;;
			free&#40;result&#41;;
		&#125;
		else
		&#123;
			printf&#40;"No entries</body></html>\n"&#41;;
		&#125;

    &#125; /* while */
    
    mysql_close&#40;&dbase&#41;;

    return 0;
&#125;
"SELECT * FROM pages" returns 4 rows / 5 short columns each.

I got ubuntu:
Code:
root@fryk&#58;/var/www # uname -a
Linux fryk 2.6.10-5-386 #1 Tue Apr 5 12&#58;12&#58;40 UTC 2005 i686 GNU/Linux
on p4 2.xghz 512ram

Apache2 run just from apt-get install + mod_php + mod_fastcgi (-processes 10 - same as lsws).
Lsws standard with c fastcgi as described in previous posts (php fastcgi is built in - as I get it).

I typically measured it with
Code:
ab -n 20000 -c 50 PATH
Reply With Quote
  #22  
Old 06-14-2005, 10:18 AM
mistwang mistwang is offline
LiteSpeed Staff
 
Join Date: May 2003
Location: New Jersey
Posts: 7,590
I think your test case is DB bounded.

Even though, lsws should be slightly faster than Apache, you may want to try tuning the concurrent level of lsws fcgi. In your test, Apache made 50 concurrent queries to MySQL, but lsws fcgi only made 10, according to your configuration posted in previous post. Change the "instances" to match "max connections", try different value between 10-50.

The C fastCGI could be optimized a little bit:
To use prepared SQL statement if you are using MySQL 4.1.X
To combime multiple printf to one, or buffer the result in a buffer with snprintf.
To cache the result if it does not change very often.

All in all, the faster you make the FCGI run, the bigger difference you will see between lsws and Apache.
Reply With Quote
  #23  
Old 06-14-2005, 11:00 AM
xing xing is offline
LiteSpeed Staff
 
Join Date: Oct 2003
Location: Los Angeles, California
Posts: 380
1) Add db_close to line right before exit(1). Just making sure the db connects are freed or are in clean state for the load test.

2) This test result you would get for this is only realistic in a clean-room environment. Even if you have mysql query cache disabled, subsequent reqsults will be purely from myisam/innodb buffer and or linux buffer ram so after the first few requests, you are dealing with mysql through pure memory non disk i/o calls.

I would suggest that the scripts are run with a companion program which modifies a record in the page table once every second. This forces mysql cache flush and gives you a more accurate data on how your performance is when injected with index/cache busting write queries.

In addition, instead of pure select * from pages. You should use a more common compounded query such as select * from pages where pageid = value order by priority asc, etc.

3) Don't how it applies to C but in javascript, it is magnitudes faster to buffer output like so..

var buffer = new Array();
buffer[buffer.length] = "<html>";
buffer[buffer.length] = "</html>";

document.write(buffer.join(''));

A) one big write = faster
B) Intead of appending growing data to a string, use an dynamic array and
join the data at the end = faster

the buffer.join appends all the elements into one string separated by the separator, empty char in this case.

Not sure how to do this in C but the same concept shoudl apply to most languages.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
FastCGI spawning more processes jooray Bug Reports 7 01-02-2007 03:50 PM
FastCGI > LSAPI ? Jetro Feedback/Feature Requests 6 10-19-2005 09:48 AM
503 Service Unavailable - while setting up Perl FastCGI nigel CGI/Perl/Python 1 02-15-2005 06:15 AM
FastCGI : SCRIPT_FILENAME vs. PATH_TRANSLATED bogus Install/Configuration 2 03-21-2004 11:06 AM
FastCGI responders bogus Install/Configuration 3 12-08-2003 09:31 AM


All times are GMT -7. The time now is 10:20 AM.



- Archive - Top
© Copyright 2003-2011 LiteSpeed Technologies, Inc. All rights reserved. Privacy Policy.