<commit_before>/*
 * Copyright (c) 2013, <NAME> <evilsocket at gmail dot com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Gibson nor the names of its contributors may be used
 *     to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
var util = require('util');
var Gibson = require('./lib/gibson');

function microtime(get_as_float) {  
    var now = new Date().getTime() / 1000;  
    var s = parseInt(now);
    return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;  
}

var benchmark = function() {
	var count = 10000;
    var delta, start = microtime(true);
	var x = 0;

    console.log( 'Connected, starting benchmark ...' );

	for (var i = 1; i <= count; i++) {
		c.ping( function( err ) {
			x += 1;

            if( x == count ){
                delta = microtime(true) - start;
                
                console.log( 'Benchmark finished: ' + ( x / delta ).toFixed(2)  + ' requests/second ( time: ' + delta.toFixed(2)  + ' s )' );

                c.close();
            }
		});
	}
};

var c = new Gibson.Client( 'unix:///var/run/gibson.sock' );

console.log( 'Connecting ...' );

c.connect();

c.on( 'connect', /* function(){ 
    c.set( 0, 'foo', 'bar', function( e, d ){
        console.log( 'Reply: ' + d );
    });
}*/ benchmark );

c.on( 'error', function(e){
    console.log( 'ERROR: ' + e );
});

c.on( 'close', function(){
    console.log( 'Connection closed.' );
});

c.on( 'timeout', function(){
    console.log( 'Connection timed out.' );
});
<commit_msg>better examples<commit_after>/*
 * Copyright (c) 2013, <NAME> <evilsocket at gmail dot com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Gibson nor the names of its contributors may be used
 *     to endorse or promote products derived from this software without
 *     specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS'
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
var util = require('util');
var Gibson = require('./lib/gibson');

function microtime() {  
    return new Date().getTime() / 1000; 
}

var benchmark = function() {
	var count = 10000;
    var delta, start = microtime(true);
	var x = 0;

    console.log( 'Starting benchmark ...' );

	for (var i = 1; i <= count; i++) {
		c.get( 'foo', function( err, data ) {
            if( data != 'bar' ){
                console.log( 'Unexpected data "' + data + '" ( error: "' + err + '" )' ); 
            }

			x += 1;
        
            if( x == count ){
                delta = microtime(true) - start;
                
                console.log( 'Benchmark finished: ' + ( x / delta ).toFixed(2)  + 
                             ' requests/second ( time: ' + delta.toFixed(2)  + ' s )' );

                c.close();
            }
		});
	}
};

var c = new Gibson.Client( 'unix:///var/run/gibson.sock' );

console.log( 'Connecting ...' );

c.connect();

c.on( 'connect', function(){
    // SET 0 foo bar
    c.set( 0, 'foo', 'bar', function(e,d){
        console.log( 'set reply: ' + d );
        // GET foo
        c.get( 'foo', function(e,d){
            console.log( 'get reply: ' + d );
            // PING
            c.ping( function(){
                console.log( 'got ping reply' );
                benchmark(); 
            });
        }); 
    });
});

c.on( 'error', function(e){
    console.log( 'ERROR: ' + e );
});

c.on( 'close', function(){
    console.log( 'Connection closed.' );
});

c.on( 'timeout', function(){
    console.log( 'Connection timed out.' );
});

{'fraction_non_alphanumeric': 0.11609709358032577, 'fraction_numerical': 0.0075055892686042795, 'mean_word_length': 2.4544953116381687, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1135768', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1687, 'n_words': 688}