1. 有下面这样的一个需求

把下面的两个路径转换为正确的文件目录

1
2
/mfw_project/test/demo/demo/../../demo.js
/mfw_project/test/./demo.js

转换成:

1
/mfw_project/test/demo.js

OK,开始搞!

  • 第一个方案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$str = '/mfw_project/test/demo/demo/../../demo.js';
//$str = '/mfw_project/test/./demo.js';
$arr = explode('/',$str);
//第一种情况
$arr = array_filter($arr,function($value){
if('.' === $value)
{
return false;
}
return true;
});
//第二种情况
while(($index = array_search('..',$arr))!==false)
{
unset($arr[$index]);
unset($arr[$index-1]);
$arr = array_values($arr);
}
$str = implode('/',$arr);
var_dump($str);
  • 第二个方案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
$str = '/mfw_project/test/demo/demo/../../demo.js';
//$str = '/mfw_project/test/./demo.js';
function newStr($str)
{
//第一种情况
$str = preg_replace('/([\w\d])+\/..\//','',$str);
if(strpos($str,'..'))
{
return newStr($str);
}
//第二种情况
if(strpos($str,'.'))
{
$str = str_replace("./",'',$str);
}
return $str;
}
$str = newStr($str);
var_dump($str);

以上两种方案都可以解决我们的需求。但是那种效率更高些呢?我们测试一下

测试执行效率

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
$start_time = microtime(true);
$str = '/mfw_project/test/demo/demo/demo2/demo3/../../../../demo.js';
for($i=0;$i<100000;$i++)
{
/*
$arr = explode('/',$str);
$arr = array_filter($arr,function($value){
if('.' === $value)
{
return false;
}
return true;
});
while(($index = array_search('..',$arr))!==false){
unset($arr[$index]);
unset($arr[$index-1]);
$arr = array_values($arr);
}
$str = implode('/',$arr);
*/
$str = newStr($str);
}
function newStr($str)
{
//第一种情况
$str = preg_replace('/([\w\d])+\/..\//','',$str);
if(strpos($str,'..'))
{
return newStr($str);
}
//第二种情况
if(strpos($str,'.'))
{
$str = str_replace("./",'',$str);
}
return $str;
}
echo $str."\n";
$end_time = microtime(true);
$run_time = $end_time - $start_time;
echo "程序运行".$run_time."秒";
exit;
  • 执行结果
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    //递归方式
    ➜ ~ php 1.php
    /mfw_project/test/demo.js
    程序运行0.57571291923523
    ➜ ~ php 1.php
    /mfw_project/test/demo.js
    程序运行0.58426809310913
    ➜ ~ php 1.php
    /mfw_project/test/demo.js
    程序运行0.58362793922424
    //截取方式
    ➜ ~ php 1.php
    /mfw_project/test/demo.js
    程序运行0.22979879379272
    ➜ ~ php 1.php
    /mfw_project/test/demo.js
    程序运行0.23150992393494
    ➜ ~ php 1.php
    /mfw_project/test/demo.js
    程序运行0.22574400901794

可以看到代码,我们把程序循环执行了10万次,代码执行效率.
截取方式比递归方式,效率快了一倍多。

结论

  • 尽量使用PHP自带函数
  • PHP中,用正则匹配比直接用PHP截取慢,不过第二种方式代码更美观一些

php如何实现简单的进程管理?

  1. 首先定义一个要执行的文件work.php,比较简单,死循环输入当前的时间.当然你也可以自己写一个.
    1
    2
    3
    4
    5
    6
    <?php
    while (true)
    {
    echo date('Y-m-d H:i:s').PHP_EOL ;
    sleep(3);
    }
  2. 开始写进程管理文件workProcess.php,根据自己的需要进行优化.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    <?php
    /**
    * 管理php进程
    */
    class workProcess {
    const PHP_PATH = '/usr/bin/php';
    const PHP_LOG = './php.log';
    private static $process = null;
    //命令必须在这个里面
    private static $cmd_str = array('start','stop','restart');
    //脚本执行次数
    private static $times = 0;
    /**
    * main
    * 主要程序,用来控制php进程开启,结束,重启
    * @param mixed $argv
    * @static
    * @access public
    * @return void
    */
    public static function main($argv) {
    //需要执行的php文件
    $filename = isset($argv[1]) ? $argv[1] : '';
    $cmd = isset($argv[2]) ? $argv[2] : '';
    $num = isset($argv[3]) ? $argv[3] : 0;
    if(empty($filename)) {
    echo "process is not null \n";
    exit;
    }
    if(empty($cmd)) {
    echo "cmd not exists \n";
    exit;
    }
    self::$process = $filename;
    self::$times = $num;
    //检测命令
    if(!in_array($cmd,self::$cmd_str)) {
    echo "cmd is not wrong! \n";
    exit;
    }
    self::$cmd();
    }
    /**
    * start
    * 启动程序
    * @static
    * @access public
    * @return void
    */
    public static function start() {
    for($i=0; $i <=self::$times; $i++ ) {
    self::runWork();
    }
    echo "start done~!\n";
    exit;
    }
    /**
    * stop
    * 终止程序
    * @static
    * @access public
    * @return void
    */
    public static function stop() {
    $pids = self::getPid();
    foreach($pids as $pid){
    self::killWork($pid);
    sleep(1);
    }
    echo "stop done~!\n";
    exit;
    }
    /**
    * restart
    * 重启一个php程序
    * @static
    * @access public
    * @return void
    */
    public static function restart() {
    $pids = self::getPid();
    $count = count($pids);
    for($i=0; $i < $count; $i++ ) {
    self::killWork($pids[$i]);
    self::runWork();
    sleep(1);
    }
    $times = self::$times;
    if( $count < $times) {
    for($j=0; $j< $times-$count; $j++) {
    self::runWork();
    sleep(1);
    }
    }
    echo "restart done~!";
    exit;
    }
    /**
    * runWork
    * 运行程序
    * @static
    * @access private
    * @return void
    */
    private static function runWork() {
    $cmd_str = sprintf("%s %s >> %s &", self::PHP_PATH, self::$process, self::PHP_LOG);
    echo $cmd_str."\n";
    shell_exec($cmd_str);
    }
    /**
    * killWork
    * 杀死一个程序
    * @static
    * @access private
    * @return void
    */
    private static function killWork($pid) {
    $cmd_str = "kill -9 ".$pid;
    shell_exec($cmd_str);
    echo "kill {$pid} \n";
    }
    /**
    * getPid
    * 获取进程id
    * @static
    * @access private
    * @return void
    */
    private static function getPid(){
    $cmd_str = "ps -ef |grep ".basename(self::$process)." |grep -v grep |grep -v ".basename(__FILE__)." |awk '{print $2}'";
    $pid = shell_exec($cmd_str);
    $pids = array_filter(explode("\n",$pid));
    return $pids;
    }
    }
    workProcess::main($argv);
  3. 程序执行

    • 启动5个进程:php workProcess.php work.php start 5
    • 重启进程:php workProcess.php work.php restart
    • 停止进程:php workProcess.php work.php stop

参考来源:
看了阿印的博客,觉得思路不错,自己也模仿写了一个,嘿嘿..

Apache附带的ab,它非常容易使用,ab可以直接在Web服务器本地发起测试请求。这至关重要,因为我们希望测试的服务器的处理时间,而不包含数据的网络传输时间以及用户PC本地的计算时间。

需要清楚的是,ab进行一切测试的本质都是基于HTTP,所以可以说它是对于Web服务器软件的黑盒性能测试,它获得的一切数据和计算结果,都可以通过HTTP来解释。

如果没有安装,在运行时会提示安装。

  • 查看ab版本:
1
2
3
4
wangkongming@Vostro /etc/apache2 $ ab -V
This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
  • 举个例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
wangkongming@Vostro /etc/apache2 $ ab -n 10 -c 10 http://www.baidu.com/
This is ApacheBench, Version 2.3 <$Revision: 1528965 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking www.baidu.com (be patient).....done
Server Software: Apache-Coyote/1.1
Server Hostname: www.baidu.com
Server Port: 80
Document Path: /
Document Length: 521 bytes // 请求的页面大小
Concurrency Level: 10 //并发量
Time taken for tests: 3.467 seconds //测试总共耗时
Complete requests: 10 //完成的请求
Failed requests: 9 //失败的请求
(Connect: 0, Receive: 0, Length: 9, Exceptions: 0)
Total transferred: 880759 bytes //总共传输数据量
HTML transferred: 871360 bytes
Requests per second: 2.88 [#/sec] (mean) //每秒钟的请求量。(仅仅是测试页面的响应速度)
Time per request: 3466.517 [ms] (mean) //等于 Time taken for tests/(complete requests/concurrency level) 即平均请求等待时间(用户等待的时间)
Time per request: 346.652 [ms] (mean, across all concurrent requests) //等于 Time taken for tests/Complete requests 即服务器平均请求响应时间 在并发量为1时 用户等待时间相同
Transfer rate: 248.12 [Kbytes/sec] received //平均每秒多少K,即带宽速率
Connection Times (ms)
min mean[+/-sd] median max
Connect: 31 34 2.6 35 39
Processing: 2 1962 909.4 2298 3432
Waiting: 2 336 528.4 67 1528
Total: 33 1996 910.9 2337 3466
Percentage of the requests served within a certain time (ms)
50% 2337
66% 2467
75% 2497
80% 2588
90% 3466
95% 3466
98% 3466
99% 3466
100% 3466 (longest request)
  • 参数说明:

-n 10 表示总请求数为10,共发出了10次请求
-c 10 表示并发用户数为10,同时有10个用户访问
http://www.baidu.com/ 表示这些请求的目标URL (注意,目标地址后面一定要加结束的反斜杠/)

  • 关注的参数:
1
2
3
4
5
6
Requests per second:每秒的请求量,所谓的吞吐率。【这个值越小越好
Time per request: 3466.517 [ms] (mean)  即平均请求等待时间,也是吞吐率(用户等待的时间) mean表示平均值
Time per request: 346.652 [ms] (mean, across all concurrent requests) //服务器平均请求响应时间 在并发量为1时 用户等待时间相同 【这个值越大越好】

简单总结下:

Requests per second 的值越小越好,Time per request 的值越大越好

参考资料:

http://blog.itpub.net/29773961/viewspace-1470071/

https://blog.linuxeye.com/124.html

用hexo搭建博客需要安装下面两个工具

  • git
  • node.js
    步骤
  1. 安装node.js
  • 下载node.js软件

    1
    2
    cd /home/wangkongming/software
    git clone git@github.com:nodejs/node.git
  • 编译安装

    1
    2
    3
    4
    cd node
    ./configure
    make
    sudo make install (必须用sudo)
  • 查看node.js的版本

    1
    2
    nodejs -v
    v0.10.25
  1. 安装hexo

    1
    sudo npm install -g hexo-cli
  2. 安装成功后,初始化hexo

    1
    2
    3
    hexo init blog_github
    cd blog_github
    npm install

安装成功后:

1
2
3
4
5
6
dwxr-xr-x 189 wangkongming wangkongming 4096 12月 7 18:56 node_modules
-rw-r--r-- 1 wangkongming wangkongming 1477 12月 7 18:53 _config.yml
-rw-r--r-- 1 wangkongming wangkongming 442 12月 7 18:53 package.json
drwxr-xr-x 2 wangkongming wangkongming 4096 12月 7 18:53 scaffolds
drwxr-xr-x 3 wangkongming wangkongming 4096 12月 7 18:53 source
drwxr-xr-x 3 wangkongming wangkongming 4096 12月 7 18:53 themes

  1. 用hexo生成文章
    新建文章 hexo new "My first article",简写hexo n "My first article"
    生成文章 hexo generate,简写hexo g
    部署到github hexo deploy,简写hexo d

    1
    2
    3
    4
    hexo n == hexo new
    hexo g == hexo generate
    hexo s == hexo server
    hexo d == hexo deploy
  2. 如何推送博客到github上
    修改_config.yml文件

    1
    2
    3
    4
    # Deployment
    ## Docs: http://hexo.io/docs/deployment.html
    deploy:
    type:

修改成

1
2
3
4
5
6
# Deployment
## Docs: http://hexo.io/docs/deployment.html
deploy:
type: github
repository: git@github.com:KoMiles/komiles.github.com.git
branch: master

这样修改可能会出现

在hexo deploy 时,出现deployer not found:github
原因是:
在hexo更新到3.0之后,deploy 的 type 需要改成 git。如果没有安装Git发布工具,使用 npm 命令安装:
npm install hexo-deployer-git --save
最后清除工程(hexo clean),重新生成(hexo generate)、发布文件(hexo deploy),检查是否发布成功。
最后改成这样:

1
2
3
4
5
6
# Deployment
## Docs: http://hexo.io/docs/deployment.html
deploy:
type: git
repository: git@github.com:KoMiles/komiles.github.com.git
branch: master

执行:npm install hexo-deployer-git --save

参考资料

如何使用,请参考PHP官网 switch的用法
我在开发过程中遇到了下面的问题。

请看下面这段代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
$type = 0;
switch($type) {
case $type >= 0 && $type < 2:
$price = 100;
break;
case $type>= 3 && $type < 5:
$price = 200;
break;
case $type>= 6 && $type < 9:
$price = 300;
break;
default:
$price = 800;
break;
}
echo $price;

乍一看,很简单呀,因为满足条件 $type >= 0 && $type < 2 ,所以答案是100 呀;
其实不是这样子的,返回的是200.是不是很坑啊?

分析代码

  • 官网中特别提示,switch/case作的是松散比较,什么是松散比较,请参考松散比较
  • $type >= 0 && $type < 2 这个返回 true;
  • $type>= 3 && $type < 5 这个返回 false;
    so,上面代码可以转化为这样:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <?php
    $type = 0;
    switch(0) {
    case true:
    $price = 100;
    break;
    case false:
    $price = 200;
    break;
    case false:
    $price = 300;
    break;
    default:
    $price = 800;
    break;
    }
    echo $price;
  • 这样你就容易理解了。
    看到这里,你就明白为什么返回是200。
    but,我想要的是100呀,怎么办呢?

解决问题

  • 可以按照下面这样的方式解决:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <?php
    $type = 0;
    switch(true) {
    case $type >= 0 && $type < 2:
    $price = 100;
    break;
    case $type>= 3 && $type < 5:
    $price = 200;
    break;
    case $type>= 6 && $type < 9:
    $price = 300;
    break;
    default:
    $price = 800;
    break;
    }
    echo $price;
  • 看到了吧,switch 后边的变量改成true后,程序就正常了,终于输出了自己想要的100.
    泪奔 啊~~

本文为自己收集的技术博客网址,不断更新中…

Google网址

大牛博客

架构

前端

php

redis

git

github

其他博客

收集的文章

sublime_text 被称为’神器’,自然有它的长处,但是在linux系统安装sublime_text 后,你会发现,无法输入中文,本文会解决 sublime_txt 无法输入中文的问题.

测试系统:Linux Mint 17.2
输入法:搜狗输入法
Sublime Text版本:Sublime Text 3 Build 3083

关于如何安装输入法和sublime,在这里不做叙述,自行百度.

  1. 保存下面的代码为sublime-imfix.c 文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    /*
    sublime-imfix.c
    Use LD_PRELOAD to interpose some function to fix sublime input method support for linux.
    By Cjacker Huang
    gcc -shared -o libsublime-imfix.so sublime-imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC
    LD_PRELOAD=./libsublime-imfix.so subl
    */
    #include <gtk/gtk.h>
    #include <gdk/gdkx.h>
    typedef GdkSegment GdkRegionBox;
    struct _GdkRegion
    {
    long size;
    long numRects;
    GdkRegionBox *rects;
    GdkRegionBox extents;
    };
    GtkIMContext *local_context;
    void
    gdk_region_get_clipbox (const GdkRegion *region,
    GdkRectangle *rectangle)
    {
    g_return_if_fail (region != NULL);
    g_return_if_fail (rectangle != NULL);
    rectangle->x = region->extents.x1;
    rectangle->y = region->extents.y1;
    rectangle->width = region->extents.x2 - region->extents.x1;
    rectangle->height = region->extents.y2 - region->extents.y1;
    GdkRectangle rect;
    rect.x = rectangle->x;
    rect.y = rectangle->y;
    rect.width = 0;
    rect.height = rectangle->height;
    //The caret width is 2;
    //Maybe sometimes we will make a mistake, but for most of the time, it should be the caret.
    if(rectangle->width == 2 && GTK_IS_IM_CONTEXT(local_context)) {
    gtk_im_context_set_cursor_location(local_context, rectangle);
    }
    }
    //this is needed, for example, if you input something in file dialog and return back the edit area
    //context will lost, so here we set it again.
    static GdkFilterReturn event_filter (GdkXEvent *xevent, GdkEvent *event, gpointer im_context)
    {
    XEvent *xev = (XEvent *)xevent;
    if(xev->type == KeyRelease && GTK_IS_IM_CONTEXT(im_context)) {
    GdkWindow * win = g_object_get_data(G_OBJECT(im_context),"window");
    if(GDK_IS_WINDOW(win))
    gtk_im_context_set_client_window(im_context, win);
    }
    return GDK_FILTER_CONTINUE;
    }
    void gtk_im_context_set_client_window (GtkIMContext *context,
    GdkWindow *window)
    {
    GtkIMContextClass *klass;
    g_return_if_fail (GTK_IS_IM_CONTEXT (context));
    klass = GTK_IM_CONTEXT_GET_CLASS (context);
    if (klass->set_client_window)
    klass->set_client_window (context, window);
    if(!GDK_IS_WINDOW (window))
    return;
    g_object_set_data(G_OBJECT(context),"window",window);
    int width = gdk_window_get_width(window);
    int height = gdk_window_get_height(window);
    if(width != 0 && height !=0) {
    gtk_im_context_focus_in(context);
    local_context = context;
    }
    gdk_window_add_filter (window, event_filter, context);
    }
  2. 安装 C/C++ 的编译环境和 gtk libgtk2.0-dev

    1
    2
    sudo apt-get install build-essential
    sudo apt-get install libgtk2.0-dev
  3. 编译共享内库

    1
    gcc -shared -o libsublime-imfix.so sublime-imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC
  4. 设置 LD_PRELOAD 并启动 Sublime Text

    1
    LD_PRELOAD=./libsublime-imfix.so subl
  5. 修改 /usr/share/applications/sublime_text.desktop 为

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    [Desktop Entry]
    Version=1.0
    Type=Application
    Name=Sublime Text
    GenericName=Text Editor
    Comment=Sophisticated text editor for code, markup and prose
    Exec=env LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so /opt/sublime_text/sublime_text %F
    Terminal=false
    MimeType=text/plain;
    Icon=sublime-text
    Categories=TextEditor;Development;
    StartupNotify=true
    Actions=Window;Document;
    [Desktop Action Window]
    Name=New Window
    Exec=env LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so /opt/sublime_text/sublime_text -n
    OnlyShowIn=Unity;
    [Desktop Action Document]
    Name=New File
    Exec=env LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so /opt/sublime_text/sublime_text --command new_file
    OnlyShowIn=Unity;
  6. 把 libsublime-imfix.so 移动到 /opt/sublime_text/ 中

    1
    mv libsublime-imfix.so /opt/sublime_text/
  7. 修改 /usr/bin/subl 为

    1
    2
    3
    4
    #!/bin/sh
    export LD_PRELOAD=/opt/sublime_text/libsublime-imfix.so
    exec /opt/sublime_text/sublime_text "$@"
  8. 重启电脑

到此,就可以在sublime中输入中文,可以愉快的码字啦~

文章参考: https://www.sinosky.org/linux-sublime-text-fcitx.html

by komiles 2015-12-21

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment