From c7c22e3db1c826bcfb2bc66651ec480aae0d4ae0 Mon Sep 17 00:00:00 2001 From: yo mama Date: Sat, 4 Apr 2015 01:00:59 -0700 Subject: first --- .../benchmark/b00_fibonacci_server_no_threads.js | 27 + .../benchmark/b01_fibonacci_server_threads.js | 42 ++ .../benchmark/b02_fibonacci_server_threads_pool.js | 36 ++ .../benchmark/b03_fibonacci_server_clustered.js | 39 ++ .../webworker-threads/benchmark/b04_only_quick.js | 14 + node_modules/webworker-threads/benchmark/doubles.c | 637 +++++++++++++++++++++ node_modules/webworker-threads/benchmark/pi.c | 155 +++++ node_modules/webworker-threads/benchmark/pi.js | 50 ++ node_modules/webworker-threads/benchmark/pi.rb | 39 ++ .../webworker-threads/benchmark/pi_precompiled.js | 50 ++ 10 files changed, 1089 insertions(+) create mode 100644 node_modules/webworker-threads/benchmark/b00_fibonacci_server_no_threads.js create mode 100644 node_modules/webworker-threads/benchmark/b01_fibonacci_server_threads.js create mode 100644 node_modules/webworker-threads/benchmark/b02_fibonacci_server_threads_pool.js create mode 100644 node_modules/webworker-threads/benchmark/b03_fibonacci_server_clustered.js create mode 100644 node_modules/webworker-threads/benchmark/b04_only_quick.js create mode 100644 node_modules/webworker-threads/benchmark/doubles.c create mode 100644 node_modules/webworker-threads/benchmark/pi.c create mode 100755 node_modules/webworker-threads/benchmark/pi.js create mode 100755 node_modules/webworker-threads/benchmark/pi.rb create mode 100644 node_modules/webworker-threads/benchmark/pi_precompiled.js (limited to 'node_modules/webworker-threads/benchmark') diff --git a/node_modules/webworker-threads/benchmark/b00_fibonacci_server_no_threads.js b/node_modules/webworker-threads/benchmark/b00_fibonacci_server_no_threads.js new file mode 100644 index 0000000..74c74fc --- /dev/null +++ b/node_modules/webworker-threads/benchmark/b00_fibonacci_server_no_threads.js @@ -0,0 +1,27 @@ + + +function fib (n) { + return (n < 2) ? 1 : fib(n-2)+ fib(n-1); +} + +var i= 0; +var n= 35; +function ƒ (req, res) { + if ((++i) % 10) { + res.end(" QUICK"); + process.stdout.write(" QUICK"); + } + else { + var txt= ' '+ fib(n); + res.end(txt); + process.stdout.write(txt); + } +} + + +var port= +process.argv[2] || 1234; +var http= require('http'); +http.globalAgent.maxSockets= 8192+2048; +http.createServer(ƒ).listen(port); +console.log('Fibonacci server (NO THREADS) running @port: '+ port); + diff --git a/node_modules/webworker-threads/benchmark/b01_fibonacci_server_threads.js b/node_modules/webworker-threads/benchmark/b01_fibonacci_server_threads.js new file mode 100644 index 0000000..dbedc87 --- /dev/null +++ b/node_modules/webworker-threads/benchmark/b01_fibonacci_server_threads.js @@ -0,0 +1,42 @@ + + +function fib (n) { + return (n < 2) ? 1 : fib(n-2)+ fib(n-1); +} + +//We're going to use n threads +var numThreads= +process.argv[3] || 1; +console.log("Using "+ numThreads+ " threads"); + +var threads= []; +var round_robin= 0; +var t= require('threads_a_gogo'); +while (numThreads--) { + threads.push(t.create().eval(fib)); +} + +var i= 0; +var n= 35; +function ƒ (req, res) { + if ((++i) % 10) { + res.end(" QUICK"); + process.stdout.write(" QUICK"); + } + else { + round_robin= (++round_robin) % threads.length; + threads[round_robin].eval('fib('+ n+ ')', function cb (err, data) { + if (err) throw err; + var txt= ' '+ data; + res.end(txt); + process.stdout.write(txt); + }); + } +} + + +var port= +process.argv[2] || 1234; +var http= require('http'); +http.globalAgent.maxSockets= 8192+2048; +http.createServer(ƒ).listen(port); +console.log('Fibonacci server (WITH THREADS) running @port: '+ port); + diff --git a/node_modules/webworker-threads/benchmark/b02_fibonacci_server_threads_pool.js b/node_modules/webworker-threads/benchmark/b02_fibonacci_server_threads_pool.js new file mode 100644 index 0000000..7b2eef5 --- /dev/null +++ b/node_modules/webworker-threads/benchmark/b02_fibonacci_server_threads_pool.js @@ -0,0 +1,36 @@ + + +function fib (n) { + return (n < 2) ? 1 : fib(n-2)+ fib(n-1); +} + +//We're going to use n threads +var numThreads= +process.argv[3] || 1; +console.log("Using a POOL of "+ numThreads+ " threads"); + +var pool= require('threads_a_gogo').createPool(numThreads).all.eval(fib); + +var i= 0; +var n= 35; +function ƒ (req, res) { + if ((++i) % 10) { + res.end(" QUICK"); + process.stdout.write(" QUICK"); + } + else { + pool.any.eval('fib('+ n+ ')', function cb (err, data) { + if (err) throw err; + var txt= ' '+ data; + res.end(txt); + process.stdout.write(txt); + }); + } +} + + +var port= +process.argv[2] || 1234; +var http= require('http'); +http.globalAgent.maxSockets= 8192+2048; +http.createServer(ƒ).listen(port); +console.log('Fibonacci server (WITH A THREAD POOL) running @port: '+ port); + diff --git a/node_modules/webworker-threads/benchmark/b03_fibonacci_server_clustered.js b/node_modules/webworker-threads/benchmark/b03_fibonacci_server_clustered.js new file mode 100644 index 0000000..d6609b9 --- /dev/null +++ b/node_modules/webworker-threads/benchmark/b03_fibonacci_server_clustered.js @@ -0,0 +1,39 @@ + + +function fib (n) { + return (n < 2) ? 1 : fib(n-2)+ fib(n-1); +} + +var i= 0; +var n= 35; +function ƒ (req, res) { + if ((++i) % 10) { + res.end(" QUICK"); + process.stdout.write(" QUICK"); + } + else { + var txt= ' '+ fib(n); + res.end(txt); + process.stdout.write(txt); + } +} + +var cluster = require('cluster'); +if (cluster.isMaster) { + require('http').globalAgent.maxSockets= 8192+2048; + var numCPUs = process.argv[3] || 1; + for (var i = 0; i < numCPUs; i++) { + cluster.fork(); + } + + cluster.on('death', function(worker) { + console.log('worker ' + worker.pid + ' died'); + }); +} else { + var port= + process.argv[2] || 1234; + var http= require('http'); + http.globalAgent.maxSockets= 8192+2048; + http.createServer(ƒ).listen(port); + console.log('Fibonacci server (CLUSTERED) listening: ' + port); +} + diff --git a/node_modules/webworker-threads/benchmark/b04_only_quick.js b/node_modules/webworker-threads/benchmark/b04_only_quick.js new file mode 100644 index 0000000..105ebb2 --- /dev/null +++ b/node_modules/webworker-threads/benchmark/b04_only_quick.js @@ -0,0 +1,14 @@ + + +function ƒ (req, res) { + res.end(" QUICK"); + process.stdout.write(" QUICK"); +} + + +var port= +process.argv[2] || 1234; +var http= require('http'); +http.globalAgent.maxSockets= 8192+2048; +http.createServer(ƒ).listen(port); +console.log('Fibonacci server (NO THREADS) running @port: '+ port); + diff --git a/node_modules/webworker-threads/benchmark/doubles.c b/node_modules/webworker-threads/benchmark/doubles.c new file mode 100644 index 0000000..3210a51 --- /dev/null +++ b/node_modules/webworker-threads/benchmark/doubles.c @@ -0,0 +1,637 @@ +/* + simple thread/process benchmark + + Copyright (C) Andrew Tridgell 2003 + + Released under the GNU GPL version 2 or later +*/ + +/* + this program is designed to test the relative performance of threads/processes + for operations typically performed by fileserving applications. +*/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef NO_THREADS +#include +#endif + +/* this contains per-task data */ +static struct { + char *dname; + char *fname; +} *id_data; + +/* these pipes are used for synchronised startup of the tasks */ +static struct barrier { + int fd1[2]; + int fd2[2]; +} barriers[2]; + +/* setup a barrier */ +static void barrier_setup(struct barrier *b) +{ + if (pipe(b->fd1) != 0 || pipe(b->fd2) != 0) { + fprintf(stderr,"Barrier setup failed\n"); + exit(1); + } +} + +/* cleanup the barrier pipes */ +static void barrier_cleanup(struct barrier *b) +{ + close(b->fd1[0]); + close(b->fd1[1]); + close(b->fd2[0]); + close(b->fd2[1]); +} + +/* wait for the parent to signal startup */ +static void barrier_wait(struct barrier *b) +{ + char c = 0; + + if (write(b->fd1[1], &c, 1) != 1 || + read(b->fd2[0], &c, 1) != 1) { + fprintf(stderr, "Barrier wait failed\n"); + exit(1); + } +} + +/* synchronise children. Return the amount of time since the last + barrier */ +static double barrier_parent(struct barrier *b, int nprocs) +{ + char *s = calloc(nprocs, 1); + int i, nwritten=0; + char c = 0; + double t; + static struct timeval tp1; + struct timeval tp2; + + for (i=0;ifd1[0], &c, 1) != 1) ; + } + + /* putting the timer here prevents problems with the parent getting + rescheduled after the write */ + gettimeofday(&tp2,NULL); + t = (tp2.tv_sec + (tp2.tv_usec*1.0e-6)) - + (tp1.tv_sec + (tp1.tv_usec*1.0e-6)); + gettimeofday(&tp1,NULL); + + while (nwritten != nprocs) { + int n = write(b->fd2[1], s, nprocs-nwritten); + if (n <= 0) { + fprintf(stderr, "Barrier parent failed\n"); + exit(1); + } + nwritten += n; + } + free(s); + return t; +} + +#ifndef NO_THREADS +/* + create a thread with initial function fn(private) +*/ +static pthread_t thread_start(void *(*fn)(int), int id) +{ + pthread_t thread_id; + pthread_attr_t thread_attr; + int rc; + typedef void *(*thread_fn_t)(void *); + pthread_attr_init(&thread_attr); + pthread_attr_setdetachstate(&thread_attr, 0); + rc = pthread_create(&thread_id, &thread_attr, (thread_fn_t)fn, (void *)(intptr_t)id); + pthread_attr_destroy(&thread_attr); + + if (rc != 0) { + fprintf(stderr,"Thread create failed for id %d\n", id); + exit(1); + } + + return thread_id; +} + +/* wait for a thread to exit */ +static int thread_join(pthread_t id) +{ + return pthread_join(id, NULL); +} +#endif + + +/* + create a process with initial function fn(private) +*/ +static pid_t proc_start(void *(*fn)(int), int id) +{ + pid_t pid; + + pid = fork(); + if (pid == (pid_t)-1) { + fprintf(stderr,"Fork failed for id %d\n", id); + return pid; + } + if (pid == 0) { + fn(id); + exit(0); + } + return pid; +} + +/* wait for a process to exit */ +static int proc_join(pid_t id) +{ + if (waitpid(id, NULL, 0) != id) { + return -1; + } + return 0; +} + +#ifndef NO_THREADS +/* run a function under a set of threads */ +static double run_threads(int nthreads, void *(*fn)(int )) +{ + int i; + pthread_t *ids = calloc(sizeof(*ids), nthreads); + double t; + + barrier_setup(&barriers[0]); + barrier_setup(&barriers[1]); + + for (i=0;i=0;i--) { + kill(ids[i], SIGTERM); + } + exit(1); + } + } + + barrier_parent(&barriers[0], nprocs); + t = barrier_parent(&barriers[1], nprocs); + + for (i=0;i maxt) maxt = t[i]; + total += t[i]; + } + printf("%s %5.2f +/- %.2f seconds\n", name, total/nrepeats, (maxt-mint)/2); +} + + +/* lock a byte range in a open file */ +int main(int argc, char *argv[]) +{ + int nprocs, i; + char *tname = "ALL"; +#define NREPEATS 10 + struct { + const char *name; + void *(*fn)(int ); + } tests[] = { + {"noop", test_noop}, + {"malloc", test_malloc}, + {"setreuid", test_setreuid}, + {"readwrite", test_readwrite}, + {"stat", test_stat}, + {"fstat", test_fstat}, + {"dir", test_dir}, + {"dirsingle", test_dirsingle}, + {"create", test_create}, + {"lock", test_lock}, + {NULL, NULL} + }; + + if (argc <= 1) { + printf("thread_perf NPROCS\n"); + exit(1); + } + + nprocs = atoi(argv[1]); + + if (argc > 2) { + tname = argv[2]; + } + + id_data = calloc(nprocs, sizeof(*id_data)); + if (!id_data) { + exit(1); + } + +#ifndef NO_THREADS + printf("NOTE! for accurate process results please compile with -DNO_THREADS and don't link to -lpthread\n\n"); +#endif + + for (i=0;i +#include +#include +#include +#include +#include +#include +#include +#include + +#define kPI 5e7 + +typedef struct { + int id; + pid_t pid; + pthread_t thread; +} threadData; + + + + +struct timeval t0; +void timer_start () { + gettimeofday(&t0, NULL); +} + +double timer_end () { + struct timeval t1; + gettimeofday(&t1, NULL); + return (double) (((double)t1.tv_sec)+ ((double)t1.tv_usec*1.0e-6))- (((double)t0.tv_sec)+ ((double)t0.tv_usec*1.0e-6)); +} + + + + +double pi (double n) { + double pi= 0; + double num= 4; + double den= 1; + int plus= 1; + + while (den < n) { + if (plus) { + pi+= num/den; + plus= 0; + } + else { + pi-= num/den; + plus= 1; + } + den+= 2; + } + return pi; +} + + + + +void* thread_proc (void* arg) { + fprintf(stdout, "%.16f -> TID[%d]\n", pi(kPI), ((threadData*) arg)->id); + return NULL; +} + + + + +int main (int argc, char *argv[]) { + + double tThreads= 0, tProcs= 0; + int numThreads= (argc > 1) ? atoi(argv[1]): 33; + threadData* threads= (threadData*) malloc(numThreads* sizeof(threadData)); + + + + // USING PROCESSES + + fprintf(stdout, "\n**** Using %d processes\n", numThreads); + + timer_start(); + + { + pid_t pid; + int i= numThreads; + while (i--) { + threads[i].id= numThreads- i; + threads[i].pid= pid= fork(); + if (!pid) { + //child + fprintf(stdout, "%.16f -> PID[%d]\n", pi(kPI), threads[i].id); + exit(0); + } + else if (pid < 0) { + fprintf(stdout, "**** Error: fork(): -1\n"); + while (++i < numThreads) { + kill(threads[i].pid, SIGKILL); + } + exit(-1); + } + } + + i= numThreads; + while (i--) { + waitpid(WAIT_ANY, NULL, 0); + } + } + + tProcs= timer_end(); + + + + + // USING THREADS + + fprintf(stdout, "\n**** Using %d threads\n", numThreads); + + timer_start(); + + { + int i= numThreads; + while (i--) { + threads[i].id= numThreads- i; + pthread_create(&threads[i].thread, NULL, thread_proc, &threads[i]); + } + + i= numThreads; + while (i--) { + pthread_join(threads[i].thread, NULL); + } + } + + tThreads= timer_end(); + + + fprintf(stdout, "\n*** Procesos:\n"); + fprintf(stdout, "Tiempo total (ms) -> %.0f\n", tProcs*1e3); + fprintf(stdout, "Procesos por segundo -> %.1f\n", (double)(numThreads/tProcs)); + fprintf(stdout, "Total de procesos ejecutadas -> %d\n", numThreads); + + fprintf(stdout, "\n*** Threads:\n"); + fprintf(stdout, "Tiempo total (ms) -> %.0f\n", tThreads*1e3); + fprintf(stdout, "Threads por segundo -> %.1f\n", (double)(numThreads/tThreads)); + fprintf(stdout, "Total de threads ejecutadas -> %d\n", numThreads); + + fprintf(stdout, "\nRatio t_Threads/t_Procesos -> %.4f\n", (double)(tThreads/tProcs)); + free(threads); +} diff --git a/node_modules/webworker-threads/benchmark/pi.js b/node_modules/webworker-threads/benchmark/pi.js new file mode 100755 index 0000000..fe30668 --- /dev/null +++ b/node_modules/webworker-threads/benchmark/pi.js @@ -0,0 +1,50 @@ + + +var Threads= require('threads_a_gogo'); + + +function cb (err, msg) { + this.destroy(); + ++i; + process.stdout.write('\n'+ msg + ' -> '+ this.id); +} + +function pi () { + var π= 0; + var num= 4; + var den= 1; + var plus= true; + + while (den < 5e7) { + if (plus) { + π+= num/den; + plus= false; + } + else { + π-= num/den; + plus= true; + } + den+= 2; + } + return π; +} + + +var i= +process.argv[2] || 1; +console.log('Using '+ i+ ' threads'); + + +var t= Date.now(); +while (i--) { + Threads.create().eval('('+ pi+ ')()', cb); +} + + +i= 0; +process.on('exit', function () { + t= Date.now()- t; + var tps= (i*1e3/t).toFixed(1); + console.log('\nTiempo total (ms) -> '+ t); + console.log('Threads por segundo -> '+ tps); + console.log('Total de threads ejecutadas -> '+ i); +}); diff --git a/node_modules/webworker-threads/benchmark/pi.rb b/node_modules/webworker-threads/benchmark/pi.rb new file mode 100755 index 0000000..5f85965 --- /dev/null +++ b/node_modules/webworker-threads/benchmark/pi.rb @@ -0,0 +1,39 @@ +#!/usr/bin/env ruby + +def pi(i) + pi= 0 + num= 4.0 + den= 1 + plus= true + + while den < 5e7 + if plus + pi+= num/den + plus= false + else + pi-= num/den + plus= true + end + den+= 2 + end + puts "#{pi} -> #{i}" +end + +threads=[] +count=ARGV.shift || 1 +puts "Using #{count} threads" + +t= Time.new +count.to_i.times do |i| + threads << Thread.new{pi(i)} +end + +threads.each do |j| + j.join +end + +t= (Time.new- t)* 1e3 +tps= Integer(count) * 1e3 / t +puts "\nTiempo total (ms) -> %.0f" % t +puts "Threads por segundo -> %.1f" % tps +puts "Total de threads ejecutadas -> #{count}" diff --git a/node_modules/webworker-threads/benchmark/pi_precompiled.js b/node_modules/webworker-threads/benchmark/pi_precompiled.js new file mode 100644 index 0000000..7139ac1 --- /dev/null +++ b/node_modules/webworker-threads/benchmark/pi_precompiled.js @@ -0,0 +1,50 @@ + + +var Threads= require('threads_a_gogo'); + + +function cb (err, msg) { + this.destroy(); + ++i; + process.stdout.write('\n'+ msg + ' -> '+ this.id); +} + +function pi () { + var π= 0; + var num= 4; + var den= 1; + var plus= true; + + while (den < 1e7) { + if (plus) { + π+= num/den; + plus= false; + } + else { + π-= num/den; + plus= true; + } + den+= 2; + } + return π; +} + + +var i= +process.argv[2] || 1; +console.log('Using '+ i+ ' threads'); + +var precompiled= Threads.preCompile('('+ pi+ ')()'); +var t= Date.now(); +while (i--) { + Threads.create().eval(precompiled, cb); +} + + +i= 0; +process.on('exit', function () { + t= Date.now()- t; + var tps= (i*1e3/t).toFixed(1); + console.log('\nTiempo total (ms) -> '+ t); + console.log('Threads por segundo -> '+ tps); + console.log('Total de threads ejecutadas -> '+ i); +}); -- cgit v1.2.3-70-g09d2