Skip to content

Instantly share code, notes, and snippets.

View xjdrew's full-sized avatar
:octocat:

xjdrew xjdrew

:octocat:
View GitHub Profile
@xjdrew
xjdrew / ping_client.py
Last active December 21, 2015 13:49
ping -> pong server 效率: 单连接: python 2.1W每秒 erlang 4.6W每秒 redis 2.7W每秒 多连接(100)个: python 3W每秒 erlang 11W 每秒 redis 13W每秒 CPU: Intel i5-2400 CPU@3.1GHz
#!/usr/bin/python
# -*- coding: utf-8 -*-
import gevent
from gevent import monkey;monkey.patch_all()
import sys, argparse, socket, time
def ping(sock, requests):
fileobj = sock.makefile()
for i in xrange(requests):
sock.sendall("PING\r\n")
@xjdrew
xjdrew / co1.lua
Last active December 21, 2015 15:29
coroutine比较,lua的实现更科学一点。
function test1(co2)
print(12)
coroutine.resume(co2)
print(34)
coroutine.resume(co2)
end
function test2()
print(56)
@xjdrew
xjdrew / async_call.py
Last active December 21, 2015 22:29
用gevent变异步调用为同步
import gevent
from gevent.queue import Queue
q = Queue()
session = {}
def producer():
i = 1
while True:
q.put(i)
@xjdrew
xjdrew / lua-utf8.c
Created January 15, 2014 16:05
lualib, 把字符串从utf8解成utf-16;并返回长度,解码失败,返回nil;
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include "lua.h"
#include "lauxlib.h"
inline int
_bytes(uint8_t c) {
if(c < 0x80) return 1;
@xjdrew
xjdrew / crc32_table.lua
Created January 25, 2014 11:44
生成crc32表
--[[
-- polynomial: 0x104c11db7
-- 根据MSB-first的crc polynomial,生成单字节表
-- http://en.wikipedia.org/wiki/Cyclic_redundancy_check
-- http://www.cnblogs.com/esestt/archive/2007/08/09/848856.html
-- 这个算法对于crc64应该也是一样的
--]]
local polynomial = 0x04c11db7
local length = 32
@xjdrew
xjdrew / echo.go
Created March 2, 2014 09:22
echo service in go
package main
import (
"fmt"
"net"
"bufio"
)
func handleConnection(conn net.Conn) {
reader := bufio.NewReader(conn)
@xjdrew
xjdrew / conns.go
Created June 10, 2014 04:16
go并发连接读
package main
import "net"
import "fmt"
import "sync"
import "os"
var wg sync.WaitGroup
var addr *net.TCPAddr
@xjdrew
xjdrew / echo.py
Created August 5, 2014 04:08
gevent echo service
import gevent
from gevent import monkey;monkey.patch_all()
import socket
def handle_client(csock):
while True:
data = csock.recv(1024)
csock.sendall(data)
csock.close()
@xjdrew
xjdrew / printf.cpp
Created November 25, 2014 11:23
vs printf/std:cout bug
#include <stdlib.h>
#include <process.h>
#include <iostream>
void work1(void* p)
{
while (1) {
printf("hello world");
_sleep(1);
}
@xjdrew
xjdrew / vpnsetup.sh
Last active August 29, 2015 14:13 — forked from hwdsl2/.MOVED.md
#!/bin/sh
#
# Amazon EC2 user-data file for automatic configuration of IPsec/L2TP VPN
# on a Ubuntu server instance. Tested with 14.04 (Trusty) AND 12.04 (Precise).
# With minor modifications, this script *can also be used* on dedicated servers
# or any KVM- or XEN-based Virtual Private Server (VPS) from other providers.
#
# DO NOT RUN THIS SCRIPT ON YOUR PC OR MAC! THIS IS MEANT TO BE RUN WHEN
# YOUR AMAZON EC2 INSTANCE STARTS!
#