6月 25

如果我做双色球报表系统性能会如何

  双色球有人中了5.7亿,各种怀疑的声音也此起彼伏。有人说停售到开奖直播中间有一段时间是在算出现概率。我对是否在算并不感兴趣,我倒是想知道,如果在算要用什么样子的服务器

,会有多大压力。实际值2012072期销售349310588元,共174655294注。我用一台很老的dell 2850 来试验,双核3.2主频的单cpu,4G内存,ide接口硬盘。 Centos 5.4 64位系统,mysql5。

  写个脚本产生随机数来模拟彩票投注。根据投注号产生MD5码,取MD5的前两位作为分表依据。
  需要速度,所以开始用C来作,因学艺不精,C写的东西速度很慢,并且也没有找到原因,我将c的代码放在最后,如有朋友能帮忙解决烦请告知。最后还是用python来做。通过初步试验,

产生100万数据并入库需要3分多钟,如果产生1.8亿需要12个多小时。最后决定只产生一张表的数据,一张表数据100万。取MD5前两位,如果每张表都是100万,总共能产生2.56亿注,这个值

可以满足测试要求。

  产生表的SQL如下:

CREATE TABLE `dt_00` ( \
             `Id` int(11) NOT NULL AUTO_INCREMENT, \
             `CreateUnixTime` varchar(13) NOT NULL, \
             `RedBlue` varchar(21) DEFAULT NULL, \
             `HashValues` varchar(32) DEFAULT NULL, \
             `SaleId` int(11) DEFAULT NULL, \
             PRIMARY KEY (`Id`), \
             KEY `i_RedBlue` (`RedBlue`), \
             KEY `i_HashValues` (`HashValues`) \
             ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

  产生投注的脚本如下,因为开始考虑到要放到256张表里,所以就没有做成批量入库,而是做成生成SQL语句,5000条提交一次。:

#!/bin/env python
# ----------------------------------------
# Filename:    rand.py
# Revision:    1.0
# Date:        2012-06-24
# Web:        http://www.simonzhang.net
# Author:      simonzhang
# Email:       simon-zzm@163.com
# ----------------------------------------
import random
import time
import MySQLdb
import hashlib


def mysql_run(sql):
    try:
        db = MySQLdb.connect(host = '192.168.1.41', user = 'double', passwd = 'ball', db = 'doubleball')
        cursor = db.cursor()
    except MySQLdb.Error, e:
        print "Error %d:%s" % (e.args[0], e.args[1])
        exit(1)
    try:
        result_set = ''
        cursor.execute('%s' % sql)
        result_set = cursor.fetchall()
        cursor.close()
        db.close()
        return  result_set
    except MySQLdb.Error, e:
        print "Error %d:%s" % (e.args[0], e.args[1])
        cursor.close()
        db.close()


def create_sql():
    z = 0
    _tmp_di = []
    while (z < 6):
        red = random.randint(1, 33)
        count_rand_di = 0
        if len(_tmp_di) > 0:
            for i in _tmp_di:
                if red == i:
                    count_rand_di += 1
        if count_rand_di == 0:
            _tmp_di.append(red)
            z += 1
    _tmp_di.sort()
    blue = random.randint(1, 16)
    _result_di = ''
    for j in xrange(6):
        if j == 1:
            _result_di = "%s" % _tmp_di[j]
        else:
            _result_di += " %s" % _tmp_di[j]
    _get_md5 = hashlib.md5("%s+%s" % (_result_di, blue)).hexdigest()
    if _get_md5[0:2] == "00":
       _create_sql = "INSERT INTO  dt_00(CreateUnixTime,RedBlue,HashValues) VALUES (\'%s\',\'%s+%s\',\'%s\');" % (int(time.time()), _result_di, blue, _get_md5)
    else:
       _create_sql = ""
    return _create_sql


def write_time():
    _open_file = open("history.txt", "ab")
    get_time = time.localtime()
    _open_file.write("%s-%s-%s %s:%s:%s\n" % (get_time[0], get_time[1], get_time[2], get_time[3], get_time[4], get_time[5]))
    _open_file.close()


def main():
    _get_sql = ''
    for i in xrange(1000000):
        _loop_status = 0
        while _loop_status == 0:
            _tmp_sql = create_sql()
            if len(_tmp_sql) >2:
                _loop_status = 1
        _get_sql += _tmp_sql
        if i % 5000  == 0:
            print i
            mysql_run("%scommit;" % _get_sql)
            _get_sql = ''


if __name__ == "__main__":
    write_time()
    main()
    write_time()

  运行完毕使用sql分组统计,查看分组和排序结果SQL如下,运行一次用时6.693秒。

SELECT
	count(*) AS c,
	RedBlue,
	HashValues
FROM
	dt_00 d
GROUP BY
	d.HashValues
ORDER BY
	c DESC

  结论:因为要保证速度和稳定,所以购买系统应该是各省有各省的一套系统,然后准实时向中心汇数据,所以汇总需要的时间基本可以忽略。统计是按照分表的方式进行计算,然后在合

并结果,从计算效率来看,如果使用Dell R420高配的服务器,即使只有一台,系统也可以在3-5分钟产生出所有的分析报表(这个是估算,因为不知道需要多少统计表,也不知道需要统计的

逻辑)。如果是用oracle数据库应该会更安全快速。

  我写失败的C代码:

#include 
#include 
#include 

/*快速排序*/
void quickSort(int a[],int left,int right)
{
  int i,j,temp;
  i=left;
  j=right;
  temp=a[left];
  if (left>right)
    return;
  while(i!=j)
  {
    while(a[j]>=temp && j>i)
      j--;
    if(j>i)
      a[i++]=a[j];
    while (a[i]<=temp && j>i)
      i++;
    if(j>i)
      a[j--]=a[i];
  }
  a[i]=temp;
  quickSort(a,left,i-1);
  quickSort(a,i+1,right);
}

void main()
{
   int i,z;
   int red,blue;
   int count_rand_di;
   int di[6];
   char result_di[30]="";
   z=0;
   while(z<6)
   {
      srand(time(0)); 
      red=1+(int)(33.0*rand()/(RAND_MAX+1.0));
      count_rand_di=0;
      for(i=0;i

  编译命令:
gcc rand.c -o rand -Wall

参考资料:

http://baike.baidu.com/view/19016.htm

6月 24

读《常青藤式的精英教育》

  本书介绍了美国对于家庭教育的理念。其实其中的很多理念大家已知并接受,但是还有些理念确实值得学习。我在此
  在47也中讲“对于孩子来说,即使是一件小事,哪怕做这件小事时会花费很多的时间,只要孩子自己能够做到的,就要给孩子自己去做的机会,这一点是非常重要的。”。有的家庭已经接受了这个理念,但是在高速生活和工作忙碌中很难有空闲时间陪孩子做这些事情。上一代的老人则是希望自己能为孩子做好一切。我对这个观点非常赞同,我小的时候以至于现在我很多想做的事情父母都会替我做好。有时候感觉自己已经失去了生存的能力。我自由自力的个性与细心地照顾碰撞,让我无比的不适应。我自己在教育孩子的时候要努力改正。
  还有一点。让孩子早期就接受古典音乐。很多家长认为孩子还小什么都不动,所以不会注意听音乐、玩耍和交流的问题。还有很多家长认为小时候也没有审美观点,并且孩子长得太快,所以衣服方面有什么就穿什么。我觉得也不要奢侈浪费,买一大堆新衣服,旧衣服也可以,重要的是男孩子还是要穿男孩的衣服,女孩子要穿女孩子的衣服。不要认为小孩子不懂、不说就无所谓。
  教育是点点滴滴的教育。推荐要为人父母的朋友有空来读读这本书。

image

6月 23

  好想出去走走,当然不是到楼下遛弯的那种,但是一直没有出去过。我经常和朋友说,我是因为要到北京上学、工作,才第一次走出了我的家乡。但是我不是一个没有最求的人。
  小时在等我大了自己赚钱了要出去见见世面。大学毕业了,等工作稳定了。工作稳定了,等把房子搞定了。房子搞定了,等结了婚。结婚了,又有小孩了,现在要等小孩大了。再等等吧,也许我闭眼的时候会说,等我下辈子……
  我很奇怪那些旅行者天天就在旅游,怎么吃饭,怎么能放得下那么多的东西。佩服呀

6月 20

nginx 自动封 ip 过高连接

  用命令查看web连接过高的IP地址,但是需要人工智能去封,太麻烦了,直接写个脚本自动解决。web服务器是用nginx,python为2.6
  首先在nignx的config中建立空文件deny.ip, 然后在nginx.conf 的http标签中添加“include deny.ip;”。在nginx下sbin的目录中放入自动脚本。脚本可以查到连接最大的IP,并插入屏蔽列表中,验证正确性后导入配置。全部完成或者出错后发送邮件。被封ip再次访问会报403错误,如果不希望报错可以跳转到其它页面。源码如下:

check_deny_up.py

#!/bin/python
#-*- coding:utf-8 -*-
# Filename:    main.py
# Revision:    1.0
# Date:        2012-06-20
# Author:      simonzhang
# web:         www.simonzhang.net
# Email:       simon-zzm@163.com
### END INIT INFO
import os
from string import strip
from email.mime.text import MIMEText
import smtplib

####
check_comm = "/bin/netstat -antp|grep :80|awk ' ''{print $5}'|awk -F: '{print $1}'|sort -r|uniq -c|sort -n -k1 -r"
max_ip = 100
mail_host = ‘’;
mail_user = ‘’;
mail_pwd = ‘’;
mail_to = ‘’;
mail_cc = ‘’;


def reboot_nginx_sendmail(ip_list):
    #### reboot nginx
    _get_check_confile = os.popen('./nginx -t').readlines()
    if str(_get_check_confile.find('ok')) != '-1':
        os.system('./nginx -s reload')
        _mail_content = ip_list
    else:
        _mail_content = 'Error'
    #### send mail
    msg = MIMEText(_mail_content)
    msg['From'] = mail_user
    msg['Subject'] = ' force ip.'
    msg['To'] = mail_to
    try:
        s = smtplib.SMTP()
        s.connect(mail_host)
        s.login(mail_user, mail_pwd)
        s.sendmail(mail_user, [mail_to, mail_cc], msg.as_string())
        s.close()
    except Exception, e:
        print e


#### force out IP
def force_out(_deny_ip):
    _write_status = 0
    _read_force_file = open('../conf/deny.ip', 'rb').read()
    if  str(_read_force_file.find(_deny_ip)) == '-1':
        try:
            _get_force_file = open('../conf/deny.ip', 'ab')
            _get_force_file.write('deny %s ;\n' % _deny_ip)
            _get_force_file.close()
            _write_status = 1
            return _write_status
        except:
            return _write_status
            reboot_nginx_sendmail("Error !")
    return _write_status


def main():
    get_high_ip = os.popen('%s' % check_comm).readlines()
    _count_force_ip = 0
    _force_ip_list = ''
    for i in xrange(3):
        try:
            _get_count = strip(get_high_ip[i]).split(' ')[0]
            _get_ip = strip(strip(get_high_ip[i]).split(' ')[1])
        except:
            _get_count = 0
            _get_ip = ''
        # Maximum connection IP is Beyond the limit value
        if (int(_get_count) > max_ip) and (len(_get_ip) > 0):
            force_ip = _get_ip
            _get_status = force_out(force_ip)
            # check maximum is added in the deny.ip file
            if str(_get_status) == '1':
                _count_force_ip += 1
                _force_ip_list += ' %s ' % force_ip
#    if _count_force_ip > 0:
#        reboot_nginx_sendmail(_force_ip_list)


if __name__ == '__main__':
    main()

启动i脚本
check_deny_up.sh

#! /bin/bash
#
# make simon-zzm@163.com
#
#
### END INIT INFO

# Source function library.
. /etc/profile
cd /Data/apps/nginx/sbin/

# See how we were called.
case "$1" in
  start)
      /usr/local/bin/python check_ip_deny.py
      ;;
  *)
        echo $"Usage: $0 {start}"
        exit 1
esac

exit

将启动脚本放在crontab中运行。