Skip to content

Commit

Permalink
php8
Browse files Browse the repository at this point in the history
  • Loading branch information
wxxiong6 committed Dec 11, 2020
1 parent 9961b84 commit bd1a1be
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 43 deletions.
16 changes: 13 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
language: php
git:
quiet: true
dist: bionic
language: c

compiler:
- gcc
Expand Down Expand Up @@ -29,8 +32,15 @@ before_install:

#Compile
before_script:
- ./travis/compile.sh
- ccache --version
- ccache --zero-stats
- export USE_CCACHE=1
# Compile PHP
- travis_wait ./travis/compile.sh

# Run PHPs run-tests.php
script:
- ./travis/run-tests.sh
- ./travis/run-tests.sh

after_success:
- ccache --show-stats
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
[![Build Status](https://travis-ci.org/wxxiong6/php-ext-snowflake.svg?branch=master)](https://travis-ci.org/wxxiong6/php-ext-snowflake)

- 基于[Twitter SnowFlake](https://github.com/twitter-archive/snowflake "Twitter SnowFlake")分布式ID生成算法,使用c实现的php Extension。
- 默认生成ID是一个64位long型数字。单机每秒内理论上最多可以生成1024*(2^12),也就是409.6万个ID(1024 X 4096 = 4194304)。本机测试生成100万个ID,耗时0.24624609947205秒。
- 默认生成ID是一个64位long型数字。
- 单机每秒内理论上最多可以生成1024*(2^12),也就是409.6万个ID(1024 X 4096 = 4194304)。
- 本机测试生成100万个ID,耗时0.24624609947205秒。
- 可根据自身情况调整bit位数,从而生成长度合适的ID。


|1 标识位|41 时间截(毫秒级)|5 数据中心ID |5 机器ID |12 序列 |
|:-:|-|-|-|-|
|0|0000000000 0000000000 0000000000 0000000000 0| 00000| 00000 | 000000000000 |
Expand Down Expand Up @@ -48,4 +49,11 @@ snowflake.region_bits = 5
snowflake.worker_bits = 5
;序列 默认值12
snowflake.sequence_bits = 12
```
```

#php8执行 100W 次耗时(秒)
/usr/local/php8/bin/php snowflake.php
float(0.245499849319458)
#php7.4执行 100W 次耗时(秒)
/usr/local/php74/bin/php snowflake.php
double(1.2443881034851)
85 changes: 49 additions & 36 deletions config.m4
Original file line number Diff line number Diff line change
@@ -1,44 +1,39 @@
dnl $Id$
dnl config.m4 for extension snowflake

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.
dnl Remove where necessary.

dnl If your extension references something external, use with:
dnl If your extension references something external, use 'with':

dnl PHP_ARG_WITH(snowflake, for snowflake support,
dnl Make sure that the comment is aligned:
dnl [ --with-snowflake Include snowflake support])
dnl PHP_ARG_WITH([snowflake],
dnl [for snowflake support],
dnl [AS_HELP_STRING([--with-snowflake],
dnl [Include snowflake support])])

dnl Otherwise use enable:
dnl Otherwise use 'enable':

PHP_ARG_ENABLE(snowflake, whether to enable snowflake support,
dnl Make sure that the comment is aligned:
[ --enable-snowflake Enable snowflake support])
PHP_ARG_ENABLE([snowflake],
[whether to enable snowflake support],
[AS_HELP_STRING([--enable-snowflake],
[Enable snowflake support])],
[no])

if test "$PHP_SNOWFLAKE" != "no"; then
dnl Write more examples of tests here...

dnl # get library FOO build options from pkg-config output
dnl AC_PATH_PROG(PKG_CONFIG, pkg-config, no)
dnl AC_MSG_CHECKING(for libfoo)
dnl if test -x "$PKG_CONFIG" && $PKG_CONFIG --exists foo; then
dnl if $PKG_CONFIG foo --atleast-version 1.2.3; then
dnl LIBFOO_CFLAGS=`$PKG_CONFIG foo --cflags`
dnl LIBFOO_LIBDIR=`$PKG_CONFIG foo --libs`
dnl LIBFOO_VERSON=`$PKG_CONFIG foo --modversion`
dnl AC_MSG_RESULT(from pkgconfig: version $LIBFOO_VERSON)
dnl else
dnl AC_MSG_ERROR(system libfoo is too old: version 1.2.3 required)
dnl fi
dnl else
dnl AC_MSG_ERROR(pkg-config not found)
dnl fi
dnl PHP_EVAL_LIBLINE($LIBFOO_LIBDIR, SNOWFLAKE_SHARED_LIBADD)
dnl Remove this code block if the library does not support pkg-config.
dnl PKG_CHECK_MODULES([LIBFOO], [foo])
dnl PHP_EVAL_INCLINE($LIBFOO_CFLAGS)
dnl PHP_EVAL_LIBLINE($LIBFOO_LIBS, SNOWFLAKE_SHARED_LIBADD)

dnl If you need to check for a particular library version using PKG_CHECK_MODULES,
dnl you can use comparison operators. For example:
dnl PKG_CHECK_MODULES([LIBFOO], [foo >= 1.2.3])
dnl PKG_CHECK_MODULES([LIBFOO], [foo < 3.4])
dnl PKG_CHECK_MODULES([LIBFOO], [foo = 1.2.3])

dnl # --with-snowflake -> check with-path
dnl Remove this code block if the library supports pkg-config.
dnl --with-snowflake -> check with-path
dnl SEARCH_PATH="/usr/local /usr" # you might want to change this
dnl SEARCH_FOR="/include/snowflake.h" # you most likely want to change this
dnl if test -r $PHP_SNOWFLAKE/$SEARCH_FOR; then # path given as parameter
Expand All @@ -58,24 +53,42 @@ if test "$PHP_SNOWFLAKE" != "no"; then
dnl AC_MSG_ERROR([Please reinstall the snowflake distribution])
dnl fi

dnl # --with-snowflake -> add include path
dnl Remove this code block if the library supports pkg-config.
dnl --with-snowflake -> add include path
dnl PHP_ADD_INCLUDE($SNOWFLAKE_DIR/include)

dnl # --with-snowflake -> check for lib and symbol presence
dnl LIBNAME=snowflake # you may want to change this
dnl LIBSYMBOL=snowflake # you most likely want to change this
dnl Remove this code block if the library supports pkg-config.
dnl --with-snowflake -> check for lib and symbol presence
dnl LIBNAME=SNOWFLAKE # you may want to change this
dnl LIBSYMBOL=SNOWFLAKE # you most likely want to change this

dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
dnl If you need to check for a particular library function (e.g. a conditional
dnl or version-dependent feature) and you are using pkg-config:
dnl PHP_CHECK_LIBRARY($LIBNAME, $LIBSYMBOL,
dnl [
dnl AC_DEFINE(HAVE_SNOWFLAKE_FEATURE, 1, [ ])
dnl ],[
dnl AC_MSG_ERROR([FEATURE not supported by your snowflake library.])
dnl ], [
dnl $LIBFOO_LIBS
dnl ])

dnl If you need to check for a particular library function (e.g. a conditional
dnl or version-dependent feature) and you are not using pkg-config:
dnl PHP_CHECK_LIBRARY($LIBNAME, $LIBSYMBOL,
dnl [
dnl PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $SNOWFLAKE_DIR/$PHP_LIBDIR, SNOWFLAKE_SHARED_LIBADD)
dnl AC_DEFINE(HAVE_SNOWFLAKELIB,1,[ ])
dnl AC_DEFINE(HAVE_SNOWFLAKE_FEATURE, 1, [ ])
dnl ],[
dnl AC_MSG_ERROR([wrong snowflake lib version or lib not found])
dnl AC_MSG_ERROR([FEATURE not supported by your snowflake library.])
dnl ],[
dnl -L$SNOWFLAKE_DIR/$PHP_LIBDIR -lm
dnl ])
dnl
dnl PHP_SUBST(SNOWFLAKE_SHARED_LIBADD)

PHP_NEW_EXTENSION(snowflake, snowflake.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
dnl In case of no dependencies
AC_DEFINE(HAVE_SNOWFLAKE, 1, [ Have snowflake support ])

PHP_NEW_EXTENSION(snowflake, snowflake.c, $ext_shared)
fi
2 changes: 1 addition & 1 deletion snowflake.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

$max = 1000000;
for ($i = 0; $i < $max; $i++) {
snowflake::getId();
Snowflake::getId();
}

var_dump(microtime(true)-$start);
Expand Down
1 change: 1 addition & 0 deletions travis/compile.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#!/bin/sh
set -exv
phpize && ./configure && make clean && make

0 comments on commit bd1a1be

Please sign in to comment.