Skip to content

Instantly share code, notes, and snippets.

@pibi
Created July 12, 2011 10:54
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pibi/1077769 to your computer and use it in GitHub Desktop.
Save pibi/1077769 to your computer and use it in GitHub Desktop.
Embedding V8 on GWAN
Tested with Ubuntu 11.04 32bit
build v8 from source code with:
$ scons mode=release library=shared snapshot=on
If you run it on 64bit cpu try this:
$ scons mode=release library=shared snapshot=on arch=x64
copy the library and the include directory in $GWANHOME/libraries/v8
$ mkdir $GWANHOME/libraries/v8
$ cp -R libv8.so include $GWANHOME/libraries/v8
put in $GWANHOME/libraries/v8 v8wrapper.h andv8wrapper.cc.
build libv8wrapper by:
$ cd $GWANHOME/libraries/v8
$ g++ -c -fPIC -I./include v8wrapper.cc
$ ld -G -o libv8wrapper.so libv8.so v8wrapper.o
copy v8.c on $GWANHOME/*/csp/
start GWAN with:
$ cd $GWANHOME/
$ export LD_LIBRARY_PATH=$GWANHOME/libraries/v8/:$LD_LIBRARY_PATH
$ ./gwan
point your browser to: http://127.0.0.1:8080/csp/v8
Hello World!
Copyright (c) 2011 pibi, http://pianobit.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
$ ab -n 10000 -c 100 -k http://192.168.11.150:8080/csp/v8
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.11.150 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: G-WAN/2.1.20
Server Hostname: 192.168.11.150
Server Port: 8080
Document Path: /csp/v8
Document Length: 12 bytes
Concurrency Level: 100
Time taken for tests: 0.680 seconds
Complete requests: 10000
Failed requests: 39
(Connect: 0, Receive: 0, Length: 39, Exceptions: 0)
Write errors: 0
Keep-Alive requests: 9960
Total transferred: 3406657 bytes
HTML transferred: 119532 bytes
Requests per second: 14705.88 [#/sec] (mean)
Time per request: 6.800 [ms] (mean)
Time per request: 0.068 [ms] (mean, across all concurrent requests)
Transfer rate: 4892.37 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 1.9 0 20
Processing: 0 7 8.4 10 80
Waiting: 0 7 8.4 10 80
Total: 0 7 9.7 10 100
Percentage of the requests served within a certain time (ms)
50% 10
66% 10
75% 10
80% 10
90% 10
95% 20
98% 20
99% 50
100% 100 (longest request)
$ ab -n 10000 -c 100 -k http://192.168.11.150:8080/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking 192.168.11.150 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: G-WAN/2.1.20
Server Hostname: 192.168.11.150
Server Port: 8080
Document Path: /
Document Length: 143 bytes
Concurrency Level: 100
Time taken for tests: 0.630 seconds
Complete requests: 10000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 10000
Total transferred: 4730000 bytes
HTML transferred: 1430000 bytes
Requests per second: 15873.02 [#/sec] (mean)
Time per request: 6.300 [ms] (mean)
Time per request: 0.063 [ms] (mean, across all concurrent requests)
Transfer rate: 7331.97 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.9 0 10
Processing: 0 6 6.2 10 40
Waiting: 0 6 6.2 10 40
Total: 0 6 6.3 10 40
Percentage of the requests served within a certain time (ms)
50% 10
66% 10
75% 10
80% 10
90% 10
95% 20
98% 20
99% 20
100% 40 (longest request)
#pragma link "./libraries/v8/libv8wrapper.so"
#pragma include "./libraries/v8"
#include <stdlib.h>
#include <stdio.h>
#include <gwan.h>
// ============================================================================
// main
// ----------------------------------------------------------------------------
int main(int argc, char **argv)
{
xbuf_t *reply = get_reply(argv);
// build the top of our HTML page
xbuf_cat(reply, runv8("'Hello'+' world!'")); //runs v8
return 200;
}
// ============================================================================
// End of Source Code
// ============================================================================
#include <v8.h>
#include <string.h>
#include "v8wrapper.h"
using namespace v8;
char * runv8(const char *jssrc)
{
// Create a stack-allocated handle scope.
HandleScope handle_scope;
// Create a new context.
Persistent<Context> context = Context::New();
// Enter the created context for compiling and
// running the script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Handle<String> source = String::New(jssrc);
// Compile the source code.
Handle<Script> script = Script::Compile(source);
// Run the script
Handle<Value> result = script->Run();
// Dispose the persistent context.
context.Dispose();
// return result as string, must be deallocated in cgo wrapper
String::AsciiValue ascii(result);
return strdup(*ascii);
}
#ifndef _V8WRAPPER_H
#define _V8WRAPPER_H
#ifdef __cplusplus
extern "C" {
#endif
// compiles and executes javascript and returns the script return value as string
char * runv8(const char *jssrc);
#ifdef __cplusplus
}
#endif
#endif // _V8WRAPPER_H
@G-WAN
Copy link

G-WAN commented Mar 8, 2012

G-WAN is an event-based multithreaded server.

AB is a single-threaded client (so it can't saturate G-WAN).

To test more than a CPU Core, use a multithreaded client like weighttp, see:

http://gwan.ch/en_apachebench_httperf.html#weighttp

Hope it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment