c中标准输入输出库中函数printf说明

printf(“%3d %6.1f”,a,b);

第一个%表示对应的第一个参数a

第二个%表示对应的第二个参数b

3d表示第一个参数保留三个位置的数字

6.1f 表示保留6个位置的浮点书,并且保留1位小数

对应说明:

%d 按照十进制整型数打印

%6d 按照十进制整型数打印,至少6个字符宽度

%f 按照浮点数打印

%6f 按照浮点数打印,至少6个字符宽度

%.2f按照浮点数打印,小数点后有两位小数

%6.2f 按照浮点数打印,至少6个字符宽度,小数点后有两位小数

 

%O 表示八进制

%X 表示十六进制

%C 表示字符

%s 表示字符串

%% 表示百分号(%)本身

 

 

php.ini文件位置

在类UNIX的系统中,PHP(除了CLI模式)默认在/usr/local/lib路径寻找php.ini文件。为了让它更加“shell式”,CLI模式默认寻找/etc/php-cli.ini,用做代替。这可以让你的web服务器和CLI/shell脚本保持单独的php.ini文件,而无需再你每次运行一个php的脚本时设置-c选项。

不同的Unix/linux系统发布版绑定PHP时,经常使用它们自己默认的php.ini位置;你可以通过你的PHP执行文件加上get_cfg_var(“cfg_file_path”)来找到该文件。

php压力测试和性能分析工具介绍

压力测试:

php的压力测试一般有两种方法法

1.ab工具 。ApacheBench的缩写。它是绑定到apache中的,也可以单独安装。

$ab -n 1000 -c 10  http://localhost/index.php

2.Siege。可以一个测试多个地址。

$ siege -i -t 10S -f urls.txt

 

性能分析:

1.ZendStudio 的Profiler来进行性能分析

2.使用APD(Advanced PHP Debugger)进行性能分析。一般来说用的就是这个。

3.使用Xdebug进行性能分析。这个比较偏向于代码调试。当然如果配置了Ccachegrind还是很好用的。

 

 

.使用APC(Advanced PHP Cache)。opcache 可以大大的加快你php的运行速度。

 

mysql使用存储过程重复/批量 插入测试数据

#创建生成测试数据的存储过程
DROP PROCEDURE IF EXISTS test;
CREATE PROCEDURE test() 
BEGIN 
DECLARE i INT DEFAULT 0; 
SET autocommit = 0; 
WHILE i<100000 DO 

INSERT INTO `test`.`user` (`uid`, `task_id`, `publisher_id`, `expired_at`, `update_at`) 
VALUES ( 450+i, '125', '1', '2','2017-09-08 14:42:23', '2017-09-08 13:42:30');

SET i = i+1; 
IF i%1000 = 0 THEN 
COMMIT; 
END IF; 
END WHILE; 
END;

#执行存储过程生成测试数据
CALL test();

 

ubuntu使用ab测试

安装

sudo apt-get install apache2-utils

测试:

ab -c 20 -n 1000 http://www.example.com/

-c:并发数
-n:总请求数

ab运行完成后会有比较详细的结果

Server Software:        web服务器软件及版本
Server Hostname:        请求的地址
Server Port:            请求的端口

Document Path:          请求的页面路径
Document Length:        页面大小

Concurrency Level:      并发数
Time taken for tests:   测试总共花费的时间
Complete requests:      完成的请求数
Failed requests:        失败的请求数
Write errors:           写入错误
Total transferred:      总共传输字节数,包含http的头信息等
HTML transferred:       html字节数,实际的页面传递字节数
Requests per second:    每秒处理的请求数,服务器的吞吐量(重要)
Time per request:       平均数,用户平均请求等待时间
Time per request:       服务器平均处理时间
Transfer rate:          平均传输速率(每秒收到的速率)

参数说明:

-n即requests,用于指定压力测试总共的执行次数。

-c即concurrency,用于指定压力测试的并发数。

-t即timelimit,等待响应的最大时间(单位:秒)。

-b即windowsize,TCP发送/接收的缓冲大小(单位:字节)。

-p即postfile,发送POST请求时需要上传的文件,此外还必须设置-T参数。

-u即putfile,发送PUT请求时需要上传的文件,此外还必须设置-T参数。

-T即content-type,用于设置Content-Type请求头信息,例如:application/x-www-form-urlencoded,默认值为text/plain

-v即verbosity,指定打印帮助信息的冗余级别。

-w以HTML表格形式打印结果。-i使用HEAD请求代替GET请求。

-x插入字符串作为table标签的属性。-y插入字符串作为tr标签的属性。

-z插入字符串作为td标签的属性。

-C添加cookie信息,例如:”Apache=1234″(可以重复该参数选项以添加多个)。

-H添加任意的请求头,例如:”Accept-Encoding: gzip”,请求头将会添加在现有的多个请求头之后(可以重复该参数选项以添加多个)。

-A添加一个基本的网络认证信息,用户名和密码之间用英文冒号隔开。

-P添加一个基本的代理认证信息,用户名和密码之间用英文冒号隔开。

-X指定使用的代理服务器和端口号,例如:”126.10.10.3:88″。

-V打印版本号并退出。

-k使用HTTP的KeepAlive特性。

-d不显示百分比。

-S不显示预估和警告信息。

-g输出结果信息到gnuplot格式的文件中。

-e输出结果信息到CSV格式的文件中。

-r指定接收到错误信息时不退出程序。

-h显示用法信息,其实就是ab -help

开启mysql执行日志

在mysql命令行或者客户端管理工具中执行:SHOW VARIABLES LIKE “general_log%”;

结果:

general_log OFF
general_log_file /var/lib/mysql/localhost.log

OFF说明没有开启日志记录

分别执行开启日志以及日志路径和日志文件名

SET GLOBAL general_log_file = ‘/var/lib/mysql/localhost.log’;
SET GLOBAL general_log = ‘ON’;

还要注意

这时执行的所有sql都会别记录下来,方便查看,但是如果重启mysql就会停止记录需要重新设置

SHOW VARIABLES LIKE “log_output%”;

如果是NONE,需要设置

SET GLOBAL log_output=’TABLE,FILE’

在php中使用正则表达式

<?php
/**
 * php 正则表达式
 * 在php中想使用正则表达式,请先确认已经安装pcre扩展,可以输出phpinfo查看
 * author: mrluchanglong@163.com
 * 201709
 * */

/**
 * 一个正则表达式通常使用如下格式:
 * <delimiter><pattern><delimiter>[<modifiers>]
 * <定界符><匹配式><定界符>[修饰符(可选)]
 * 修饰符是可选的。界定符把正则式与修饰符分离开来。PCRE将正则表达式的第一个字符作为定界符。
 * 所以你应该使用一个不会出现在正则式本身中的字符。
 * 或者,你可以使用一个在表达式中存在的字符,但是你必须使用\对它进行转义.
 * 传统来说,字符/被用来当作定界符,但是你也可以选择其他常用的定界符,例如|或者@。
 * 从个人角度来说,大多数情况下,我们会选择@,除非我们需要匹配一个Email地址或者类似的包含@的正则式,那种情况下我们将用/。
 *
 * php函数preg_match()被用来匹配正则表达式。
 * 传递到函数的第一个参数式正则式。
 * 第二个参数是需要跟正则式匹配的字符串而且它也叫做标题。函数返回TRUE(正则式匹配)或者FALSE(正则式不匹配)。
 * 你还可以传递第三个参数————一个变量名。匹配的文本将通过引用存放到这个名字命名的数组中。如果不需要使用匹配的文本而只是想知道是否存在一个匹配,你可以不使用第三个参数并留空。
 */

//简而言之,格式如下,其中$matches是可选的:
$result=preg_match($pattern,$subject,$matches);




?>

 

仿linux权限 设置内容发布平台

以下仿照linux权限算法,对内容发布平台进行设置

 /**
     * 推送平台
     *
     * 值     显示
     * 0      全部
     * 1      web平台
     * 2      android平台
     * 4      iOS平台
     *
     * 3      web+android
     * 5      web+iOS
     * 6      android+iOS
     * 7      web+android+iOS
     *
     * 仿unix权限规则,平台码按等比数列形式增加,如 1,2,4,8,16,32....
     * 两种平台的状态码相加等于的值就表示这两种平台都显示,多个平台也一样
     *
     * @return array([],[]...)
     * */
    static function platForm()
    {
        return array(
            // 推送平台码 、文字
            ['code' => '0', 'text' => '全部'],
            ['code' => '1', 'text' => '网站'],
            ['code' => '2', 'text' => '安卓'],
            ['code' => '4', 'text' => '苹果']
        );
    }

    /**
     * 获取当前任务推送的平台
     * 为后台编辑提供
     * 状态码,根据保存的platfrom的值获取任务推送的平台
     * 如:传入7,返回 array(1,2,4),表示这个任务在1,2,4 平台开启推送
     * @code int 所有平台码:如0,1,2,3,4,5,6,7
     * @return array(1,2,4...)
     */
    static function getTaskPlatForm($code = 0)
    {
        $plat = [];
        if ($code > 0) {
            $platFrom = array_reverse(self::platForm());//从大到小开始取推送的平台
            foreach ($platFrom as $k => $v) {
                if ($v['code'] == 0) break;
                if ($code&$v['code'])$plat[] = (int)$v['code']; //使用按位与运算
            }
        } else {
            $plat = [0];
        }
        return $plat;
    }

    /**
     * 获取传入状态内的所有任务对应的platfrom的值
     * 为前台筛选提供
     * 如:传入2 返回 array(0,2,3,6,7),表示数据库中platfrom 为0,2,3,7,6的都允许显示
     * @param $code int 唯一平台码:如0,1,2,4
     * @return array(2,3,7...)
     * */
    static function inTaskPlatForm($code = 0)
    {
        $plat = [];
        if ($code>0) {
            $platFrom = self::platForm();
            $plat_code = [];
            foreach ($platFrom as $v) {
                $plat_code[] = $v['code'];
            }
            $sum = array_sum($plat_code);
            $plat = [];
            for ($i = $sum; $i >= 0; $i--) {
                if ($i & $code) $plat[] = $i;
            }
        }
        $plat[]=0;
        return $plat;
    }

 

ubuntu 16.04 安装laravel 出错

当安装laravel 时出现这堆错误:

Your requirements could not be resolved to an installable set of packages.

Problem 1
– phpunit/phpunit 5.7.9 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.8 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.7 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.6 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.5 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.4 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.3 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.21 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.20 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.2 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.19 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.18 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.17 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.16 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.15 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.14 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.13 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.12 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.11 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.10 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.1 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– phpunit/phpunit 5.7.0 requires ext-dom * -> the requested PHP extension dom is missing from your system.
– Installation request for phpunit/phpunit ~5.7 -> satisfiable by phpunit/phpunit[5.7.0, 5.7.1, 5.7.10, 5.7.11, 5.7.12, 5.7.13, 5.7.14, 5.7.15, 5.7.16, 5.7.17, 5.7.18, 5.7.19, 5.7.2, 5.7.20, 5.7.21, 5.7.3, 5.7.4, 5.7.5, 5.7.6, 5.7.7, 5.7.8, 5.7.9].

To enable extensions, verify that they are enabled in your .ini files:
– /etc/php/7.0/cli/php.ini
– /etc/php/7.0/cli/conf.d/10-mysqlnd.ini
– /etc/php/7.0/cli/conf.d/10-opcache.ini
– /etc/php/7.0/cli/conf.d/10-pdo.ini
– /etc/php/7.0/cli/conf.d/20-calendar.ini
– /etc/php/7.0/cli/conf.d/20-ctype.ini
– /etc/php/7.0/cli/conf.d/20-exif.ini
– /etc/php/7.0/cli/conf.d/20-fileinfo.ini
– /etc/php/7.0/cli/conf.d/20-ftp.ini
– /etc/php/7.0/cli/conf.d/20-gd.ini
– /etc/php/7.0/cli/conf.d/20-gettext.ini
– /etc/php/7.0/cli/conf.d/20-iconv.ini
– /etc/php/7.0/cli/conf.d/20-json.ini
– /etc/php/7.0/cli/conf.d/20-mbstring.ini
– /etc/php/7.0/cli/conf.d/20-mysqli.ini
– /etc/php/7.0/cli/conf.d/20-pdo_mysql.ini
– /etc/php/7.0/cli/conf.d/20-phar.ini
– /etc/php/7.0/cli/conf.d/20-posix.ini
– /etc/php/7.0/cli/conf.d/20-readline.ini
– /etc/php/7.0/cli/conf.d/20-shmop.ini
– /etc/php/7.0/cli/conf.d/20-sockets.ini
– /etc/php/7.0/cli/conf.d/20-sysvmsg.ini
– /etc/php/7.0/cli/conf.d/20-sysvsem.ini
– /etc/php/7.0/cli/conf.d/20-sysvshm.ini
– /etc/php/7.0/cli/conf.d/20-tokenizer.ini
– /etc/php/7.0/cli/conf.d/20-zip.ini
You can also run `php –ini` inside terminal to see which files are used by PHP in CLI mode.

 

执行命令:

sudo apt-get install php-xml

 

后重新安装就可以了。

请确定你已将 ~/.composer/vendor/bin 路径加到 PATH,只有这样系统才能找到 laravel 的执行文件

安装larave时,有这个要求:请确定你已将 ~/.composer/vendor/bin 路径加到 PATH,只有这样系统才能找到 laravel 的执行文件

那这个怎么做呢?

执行以下命令

export PATH="$PATH:$HOME/.composer/vendor/bin"

添加路径到 PATH, 当然,这个只在当前会话生效。

如果你想让它一直生效,可以修改 $HOME/.bashrc 这个文件,将变量写入

echo 'export PATH="$PATH:$HOME/.composer/vendor/bin"' >> ~/.bashrc

执行以下命令,生效。

source ~/.bashrc

 

PS: 在一些系统中不是文件 ~/.bashrc, 可以修改文件 ~/.bash_profile也是一样的。