├── .gitignore ├── LICENSE ├── LICENSE.html ├── Makefile ├── README.html ├── README.md ├── etc ├── README.html ├── README.md ├── css │ └── style.css ├── images │ ├── fleece-footer-1.1.png │ ├── fleece-footer-1.2.png │ ├── fleece-footer.png │ ├── fleece-head-1.2.2-background.jpg │ ├── fleece-head-1.2.2-letters.png │ └── fleece-head-1.2.2.2-letters.png ├── json4 │ └── json.lua ├── luajson │ ├── Makefile │ ├── luajson.c │ ├── luajson.lua │ ├── readme.txt │ └── test.lua ├── lunit │ ├── ANNOUNCE │ ├── CHANGES │ ├── DOCUMENTATION │ ├── LICENSE │ ├── PATCHES │ ├── README │ ├── example.lua │ ├── lunit │ ├── lunit-console.lua │ ├── lunit-tests.lua │ └── lunit.lua └── markdown.lua ├── hello.lua ├── include ├── README.md ├── ldebug.h ├── ldo.h ├── lgc.h ├── llimits.h ├── lmem.h ├── lobject.h ├── lstate.h ├── ltable.h ├── ltm.h ├── lua.h └── lzio.h ├── index.html ├── src ├── Makefile ├── README.html ├── README.md ├── fleece-buffers.c ├── fleece-buffers.h ├── fleece-config.h ├── fleece-copy.c ├── fleece-ctrl.c ├── fleece-ctrl.h ├── fleece-intern.h ├── fleece-lua.c ├── fleece-numbers.c ├── fleece-size.c ├── fleece-stringify.c ├── fleece.c └── fleece.h └── test ├── README.html ├── README.md ├── SAMPLE.html ├── SAMPLE.md ├── bench-fleece-10k.lua ├── bench-fleece.lua ├── bench-json4.lua ├── bench-luajson.lua ├── bench-yajl.lua ├── bench.lua ├── bench2-1k.lua ├── bench2.lua ├── bench3-1k.lua ├── bench3.lua ├── bench3a.lua ├── bench3b.lua ├── bench4.lua ├── bench4d.lua ├── bench4e.lua ├── bench5.lua ├── bench5a.lua ├── bench5b.lua ├── bench5c.lua ├── bench6a.lua ├── bench7-1k.lua ├── bench7.lua ├── bench74.lua ├── bench7a.lua ├── bench7c.lua ├── test-fleece-5m.lua ├── test-fleece-hello.lua ├── test-fleece-json.lua ├── test-fleece-nonstop.lua ├── test-fleece-rand.lua ├── test-fleece-size.lua ├── test-json4.lua ├── test-luajson.lua └── unit ├── basic.lua ├── complex.lua ├── escapes.lua ├── parameters.lua └── test.lua /.gitignore: -------------------------------------------------------------------------------- 1 | # Eonblast Fleece 0.2.2 2 | 3 | *.a 4 | *.o 5 | *.m 6 | *.so 7 | 8 | lua 9 | luac 10 | lua-yajl 11 | luajson 12 | fleece 13 | 14 | .DS_Store 15 | ._* 16 | *.bak 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | ## License 2 | 3 | **Copyright (c) 2011 Eonblast Corporation** 4 | 5 | Permission is hereby granted, free of charge, to any person 6 | obtaining a copy of this software and associated documentation 7 | files (the "Software"), to evaluate the Software or use it 8 | in education, science, not-for-profit work, personal, or Open 9 | Source projects including without limitation the rights to copy, 10 | modify, merge, publish, distribute, but not sublicense, and not 11 | sell copies of the Software, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 18 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 20 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 21 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 23 | OTHER DEALINGS IN THE SOFTWARE. 24 | 25 | ________________________________________________________________ 26 | LICENSE fleece - hd Feb 2011 - powered by Eonblast Corporation 27 | -------------------------------------------------------------------------------- /LICENSE.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | License 6 | 83 | 84 | 85 |
86 | 87 |
88 |
89 | 90 |

License

91 | 92 |

Copyright (c) 2011 Eonblast Corporation

93 | 94 |

Permission is hereby granted, free of charge, to any person 95 | obtaining a copy of this software and associated documentation 96 | files (the "Software"), to evaluate the Software or use it 97 | in education, science, not-for-profit work, personal, or Open 98 | Source projects including without limitation the rights to copy, 99 | modify, merge, publish, distribute, but not sublicense, and not 100 | sell copies of the Software, subject to the following conditions:

101 | 102 |

The above copyright notice and this permission notice shall be 103 | included in all copies or substantial portions of the Software.

104 | 105 |

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 106 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 107 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 108 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 109 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 110 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 111 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 112 | OTHER DEALINGS IN THE SOFTWARE.

113 | 114 |
115 |

LICENSE fleece - hd Feb 2011 - powered by Eonblast Corporation

116 | 117 |
118 |
119 | 120 | © 2011 121 | Eonblast Corporation 122 | 123 |
124 |
125 |
126 | 127 | 128 | -------------------------------------------------------------------------------- /etc/README.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Support Files 6 | 83 | 84 | 85 |
86 | 87 |
88 |
89 | 90 |

Eonblast Fleece 0.3.1

91 | 92 |

Support Files

93 | 94 | 100 | 101 |

LuaJSON and JSON4 are not part of Fleece but included here for comparison. They 102 | are governed by their respective licences.

103 | 104 | 109 | 110 |

See test/README.md

111 | 112 |
113 |

README fleece/etc hd Feb 2011 - powered by Eonblast http://www.eonblast.com

114 | 115 |
116 |
117 | 118 | © 2011 119 | Eonblast Corporation 120 | 121 |
122 |
123 |
124 | 125 | 126 | -------------------------------------------------------------------------------- /etc/README.md: -------------------------------------------------------------------------------- 1 | **Eonblast Fleece 0.3.1** 2 | 3 | Support Files 4 | ------------- 5 | 6 | * **json4:** Native Lua JSON implementation 7 | * **luajson:** C implementation of Lua JSON conversion. 8 | * **lunit:** Unit tests 9 | * **markdown.lua:** test md files (eg: lua etc/markdown.lua README.md) 10 | 11 | LuaJSON and JSON4 are not part of Fleece but included here for comparison. They 12 | are governed by their respective licences. 13 | 14 | * **JSON4 0.9.50**: (aka JSON4Lua, JSON for Lua) http://json.luaforge.net/ 15 | * **LuaJSON 1.1**: (aka luajsonlib, LuaJSON C Library) http://luaforge.net/projects/luajsonlib/ 16 | * **Lua-Yajl 1.0.9**: (with Oct '10 bindings) https://github.com/brimworks/lua-yajl/ 17 | 18 | See test/README.md 19 | 20 | ___________________________________________________________________________ 21 | README fleece/etc hd Feb 2011 - powered by Eonblast http://www.eonblast.com 22 | -------------------------------------------------------------------------------- /etc/css/style.css: -------------------------------------------------------------------------------- 1 | body { font-family: "Century Gothic", Verdana, Helvetica, Arial, sans-serif; 2 | margin: 0; 3 | background-color: #fffce0; } 4 | 5 | a { color: #989156; 6 | text-decoration: none; } 7 | 8 | a:visited { color: orange; } 9 | 10 | a:hover { color: gold; } 11 | 12 | 13 | img.head { margin: 10px 10px 10px 5em; } 14 | 15 | div.body { padding: 4em 5em 1em 5em; 16 | } 17 | 18 | div.panel { background-color: white; 19 | border-radius: 2em; 20 | -moz-border-radius: 2em; 21 | border: 1px solid brown; 22 | margin: 2em; 23 | padding: 2em; 24 | } 25 | 26 | pre.code-index, 27 | pre.code, 28 | pre 29 | { background-color: #f4eca8; /* #e2d090; */ /* #fff8d2; */ 30 | border: 2px solid #fcb000; /* #f4eac8; */ 31 | padding: 1em; 32 | border-bottom-left-radius: 1em; 33 | -moz-border-radius-bottomleft: 1em; 34 | border-top-right-radius: 1em; 35 | -moz-border-radius-topright: 1em; 36 | overflow: hidden; scrollbar: auto; 37 | } 38 | 39 | pre.codelast-index, 40 | pre.codelast 41 | { background-color: #f4eca8; /* #e2d090; */ /* #fff8d2; */ 42 | border: 2px solid #fcb000; /* #f4eac8; */ 43 | padding: 1em; 44 | border-bottom-left-radius: 1em; 45 | -moz-border-radius-bottomleft: 1em; 46 | border-bottom-right-radius: 1.2em; 47 | -moz-border-radius-bottomright: 1.2em; 48 | } 49 | 50 | pre.code, 51 | pre { background-color: white; 52 | border: 2px solid #f0d8c0; } 53 | 54 | pre.codelast 55 | { background-color: #f0e4c4; 56 | border: 2px solid white; } 57 | 58 | div.head { border: 0; 59 | border-top-left-radius: 20px 7px; 60 | -moz-border-radius-topleft: 20px 7px; 61 | border-bottom-right-radius: 35px 30px; 62 | -moz-border-radius-bottomright: 50px 15px; 63 | background: url(../../etc/images/fleece-head-1.2.2-background.jpg) no-repeat top right; 64 | height:81px; padding: 0; margin: 0; 65 | } 66 | 67 | div.foot { border: 0; 68 | border-top-left-radius: 15px; 69 | -moz-border-radius-topleft: 15px; 70 | border-bottom-right-radius: 15px; 71 | -moz-border-radius-bottomright: 15px; 72 | background: url(../../etc/images/fleece-head-1.2.2-background.jpg) no-repeat bottom left; 73 | height:31px; padding: 0; margin: 0; 74 | } 75 | -------------------------------------------------------------------------------- /etc/images/fleece-footer-1.1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Eonblast/fleece-lite/06103ea87601c74801ca5780bc9014011243bbec/etc/images/fleece-footer-1.1.png -------------------------------------------------------------------------------- /etc/images/fleece-footer-1.2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Eonblast/fleece-lite/06103ea87601c74801ca5780bc9014011243bbec/etc/images/fleece-footer-1.2.png -------------------------------------------------------------------------------- /etc/images/fleece-footer.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Eonblast/fleece-lite/06103ea87601c74801ca5780bc9014011243bbec/etc/images/fleece-footer.png -------------------------------------------------------------------------------- /etc/images/fleece-head-1.2.2-background.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Eonblast/fleece-lite/06103ea87601c74801ca5780bc9014011243bbec/etc/images/fleece-head-1.2.2-background.jpg -------------------------------------------------------------------------------- /etc/images/fleece-head-1.2.2-letters.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Eonblast/fleece-lite/06103ea87601c74801ca5780bc9014011243bbec/etc/images/fleece-head-1.2.2-letters.png -------------------------------------------------------------------------------- /etc/images/fleece-head-1.2.2.2-letters.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Eonblast/fleece-lite/06103ea87601c74801ca5780bc9014011243bbec/etc/images/fleece-head-1.2.2.2-letters.png -------------------------------------------------------------------------------- /etc/luajson/Makefile: -------------------------------------------------------------------------------- 1 | ###-------------------------------------------------------------------------### 2 | ### Package : Fleece - fast Lua to JSON module ### 3 | ### File : etc/luajson/Makefile ### 4 | ### Description : Building luajson as comparison package ### 5 | ### Author : H. Diedrich ### 6 | ### Created : Feb 2011 ### 7 | ### Changed : 06 Mar 2011 ### 8 | ###-------------------------------------------------------------------------### 9 | ### ### 10 | ### see src/Makefile, src/fleece-intern.h and etc/luajson/Makefile ### 11 | ### ### 12 | ### This is not actually part of Fleece. ### 13 | ### ### 14 | ### PLEASE CONTRIBUTE YOUR *NIX, WINDOWS OR OTHER PLATFORM'S INSTRUCTIONS ### 15 | ### for src/Makefile and etc/luajson/Makefile. Thank you. ### 16 | ### ### 17 | ###-------------------------------------------------------------------------### 18 | 19 | # for comparison: 20 | # makefile for building proton's luajson as dynamic C library 21 | # (this is not fleece, but a different package, for comparison) 22 | 23 | default: $(TPLAT) 24 | 25 | clean: 26 | rm -f *. *.o *.a *.so 27 | 28 | # convenience targets for popular platforms 29 | 30 | none: 31 | @echo "Please choose a platform:" 32 | @echo " $(TPLATS)" 33 | 34 | aix-test: 35 | @echo "Not tested, please contribute" 36 | 37 | ansi-test: 38 | @echo "Not tested, please contribute" 39 | 40 | bsd-test: 41 | @echo "Not tested, please contribute" 42 | 43 | freebsd-test: 44 | @echo "Not tested, please contribute" 45 | 46 | generic-test: 47 | @echo "Not tested, please contribute" 48 | 49 | linux-test: 50 | gcc -O2 -fomit-frame-pointer -c -o luajson.o luajson.c 51 | gcc -O -shared -fomit-frame-pointer -o luajson.so luajson.o 52 | 53 | linux-64-test: 54 | gcc -O2 -fpic -c -o luajson.o luajson.c 55 | gcc -O -shared -fpic -o luajson.so luajson.o 56 | 57 | macosx-test: 58 | gcc -bundle -undefined dynamic_lookup -o luajson.so luajson.c 59 | 60 | macosx-old-test: 61 | export MACOSX_DEPLOYMENT_TARGET="10.3" && gcc -bundle -undefined dynamic_lookup -o luajson.so luajson.c 62 | 63 | mingw-test: 64 | @echo "Not tested, please contribute" 65 | 66 | posix-test: 67 | @echo "Not tested, please contribute" 68 | 69 | solaris-test: 70 | @echo "Not tested, please contribute" 71 | 72 | # list targets that do not create files (but not all makes understand .PHONY) 73 | .PHONY: all $(PLATS) default o a clean depend echo none 74 | 75 | luajson.o: luajson.c luajson.h 76 | 77 | # ---------------------------------------------------------------------------------- 78 | # Makefile fleece/etc/luajson Feb 2011 - powered by Eonblast http://www.eonblast.com 79 | 80 | -------------------------------------------------------------------------------- /etc/luajson/luajson.lua: -------------------------------------------------------------------------------- 1 | require("LuaJSON_lib", package.seeall) 2 | 3 | local base = _G 4 | local json = json 5 | module("json") 6 | 7 | json.null = {_mt = {__tostring = function () return "null" end, __call = function () return "null" end}} 8 | base.setmetatable(json.null, json.null._mt) 9 | 10 | return json 11 | -------------------------------------------------------------------------------- /etc/luajson/readme.txt: -------------------------------------------------------------------------------- 1 | LuaJSON_lib 2 | 3 | Version 1.1, 14 November 2010 4 | by Nathaniel Musgrove (proton.zero@gmail.com) 5 | 6 | LuaJSON_lib provides two functions, json.parse() and 7 | json.stringify(), and one constant, json.null. 8 | 9 | json.parse() parses any single JSON value (object, array, string, 10 | number, true, false, null) into an appropriate Lua data structure. 11 | 12 | Objects are parsed as tables with string-keys. Arrays are parsed 13 | as tables with number-keys. Strings are parsed as strings, 14 | numbers as numbers, true as true, false as false, and null is 15 | parsed as json.null, a constant used solely for the purpose of 16 | representing JSON's null value. 17 | 18 | json.stringify() takes a single Lua value (which may be complex, such 19 | as nested tables) and outputs a JSON-formatted string representing 20 | that value. 21 | 22 | Tables are stringified as arrays if table[1] ~= nil, and 23 | stringified as objects otherwise (only table elements with keys 24 | that are strings will be put into the JSON object). Strings are 25 | stringified as strings, escaping backslashes, double-quotation 26 | marks, form feeds, line feeds, carriage returns, tabs, and 27 | backspace-characters, and unicode-escaping any other character 28 | less than 0x20. Numbers are stringified as numbers, booleans as 29 | booleans, and json.null as null. Anything other than tables, 30 | strings, numbers, booleans, and json.null will produce an invalid 31 | JSON string (except nil, which produces an empty string). 32 | 33 | 34 | Example 35 | 36 | -- import the LuaJSON module 37 | require("LuaJSON") 38 | local foo = json.parse([[ 39 | {"name" : "lol", "age" : -1.5e6, "foo" : ["bar", true, null]} 40 | ]]) 41 | print(foo.age) -- -1500000 42 | print(foo.name) -- lol 43 | print(foo.foo[1]) -- bar 44 | print(foo.foo[2]) -- true 45 | print(foo.foo[3]) -- null 46 | print(foo.foo[3] == json.null) -- true 47 | foo.foo = "omg :D" 48 | print(json.stringify(foo)) -- {"name":"lol",age:-1500000,"foo":"omg :D"} 49 | -------------------------------------------------------------------------------- /etc/luajson/test.lua: -------------------------------------------------------------------------------- 1 | -- import the LuaJSON module 2 | require("LuaJSON") 3 | local foo = json.parse([[ 4 | {"name" : "lol", "age" : -1.5e6, "foo" : ["bar", true, null]} 5 | ]]) 6 | print(foo.age) -- -1500000 7 | print(foo.name) -- lol 8 | print(foo.foo[1]) -- bar 9 | print(foo.foo[2]) -- true 10 | print(foo.foo[3]) -- null 11 | print(foo.foo[3] == json.null) -- true 12 | foo.foo = "omg :D" 13 | print(json.stringify(foo)) -- {"name":"lol",age:-1500000,"foo":"omg :D"} 14 | -------------------------------------------------------------------------------- /etc/lunit/ANNOUNCE: -------------------------------------------------------------------------------- 1 | 2 | Lunit Release 0.5 3 | =================== 4 | 5 | Lunit is a unit testing framework for lua, written in lua. 6 | 7 | Lunit provides 27 assert functions, and a few misc functions 8 | for usage in an easy unit testing framework. 9 | 10 | Lunit comes with a test suite to test itself. The testsuite 11 | consists of approximately 710 assertions. 12 | 13 | Lunit is licensed under the MIT license. 14 | 15 | Lunit can downloaded from http://www.mroth.net/lunit/ 16 | -------------------------------------------------------------------------------- /etc/lunit/CHANGES: -------------------------------------------------------------------------------- 1 | Release 0.5: 2 | 3 | New test function assert_error_match(). 4 | Lunit shell script returns an error code on failed tests. 5 | Be more compatible with Darwin / Mac OS X. 6 | Bug fixes. 7 | 8 | Release 0.4: 9 | 10 | Mostly complete rewrite. 11 | Lua-5.1 only. 12 | 13 | 14 | Release 0.3 (alpha): 15 | 16 | New function lunit.wrap() to wrap a single function in a 17 | complete test case. 18 | 19 | Now a backtrace is printed, if an error or failure occurs 20 | in a test case. 21 | 22 | 23 | Release 0.2 (alpha): 24 | 25 | The test suite consists now of approximately 630 assertions. 26 | 27 | Fixed an ugly bug in lunit.assert()! Testcases for lunit.assert() 28 | added. 29 | 30 | Now all assert functions return the actual value on success. 31 | -------------------------------------------------------------------------------- /etc/lunit/DOCUMENTATION: -------------------------------------------------------------------------------- 1 | 2 | To use the lunit unit testing framework copy these files to your 3 | lua search path or include it to your package: 4 | 5 | lunit 6 | lunit.lua 7 | lunit-console.lua 8 | 9 | 10 | To write a testcase, open the framework using require. The "lunit" shell script 11 | works hard to find the "lunit.lua" and "lunit-console.lua" in all cases. 12 | 13 | require "lunit" 14 | 15 | 16 | Lunit uses the lua-5.1 module system. A testcase is a arbitrarily named module 17 | marked with "lunit.testcase" as a testcase. An example: 18 | 19 | require "lunit" 20 | 21 | module( "my_testcase", lunit.testcase, package.seeall ) 22 | 23 | 24 | The tests itself in a testcase are functions whose names must begin 25 | or end with 'test'. The function names are case insensitive. Example: 26 | 27 | require "lunit" 28 | 29 | module( "my_testcase", lunit.testcase, package.seeall ) 30 | 31 | function FirstTest() 32 | -- Test code goes here 33 | end 34 | 35 | function test_something() 36 | -- Test code goes here 37 | end 38 | 39 | 40 | Inside the test functions you use asserts to test your code or package. 41 | Lunit defines 26 assert functions: 42 | 43 | fail( [msg] ) 44 | 45 | Always fails. 46 | 47 | assert( assertion, [msg] ) 48 | 49 | Fails, if 'assertion' is false or nil. 50 | 51 | assert_true( actual, [msg] ) 52 | 53 | Fails, if 'actual' isn't true. 54 | 55 | assert_false( actual, [msg] ) 56 | 57 | Fails, if 'actual' isn't false. (Even fails if 'actual' is 58 | a nil value!) 59 | 60 | assert_equal( expected, actual, [msg] ) 61 | 62 | Fails, if 'actual' is different from 'expected'. Make sure 63 | that you don't mix 'expected' and 'actual' because they are 64 | used to build a nice error message. 65 | 66 | assert_not_equal( unexpected, actual, [msg] ) 67 | 68 | Fails, if 'actual' and 'unexpected' are equal. 69 | 70 | assert_match( pattern, actual, [msg] ) 71 | 72 | Fails, if the string 'actual' doesn't match 'pattern'. 73 | 74 | assert_not_match( pattern, actual, [msg] ) 75 | 76 | Fails, if the string 'actual' match 'pattern'. 77 | 78 | assert_nil( actual, [msg] ) 79 | 80 | Fails, if 'actual' isn't a nil value. 81 | 82 | assert_not_nil( actual, [msg] ) 83 | 84 | Fails, if 'actual' is a nil value. 85 | 86 | assert_boolean( actual, [msg] ) 87 | 88 | Fails, if 'actual' isn't true or false. 89 | 90 | assert_not_boolean( actual, [msg] ) 91 | 92 | Fails, if 'actual' is true or false. 93 | 94 | assert_number( actual, [msg] ) 95 | 96 | Fails, if 'actual' isn't a number. 97 | 98 | assert_not_number( actual, [msg] ) 99 | 100 | Fails, if 'actual' is a number. 101 | 102 | assert_string( actual, [msg] ) 103 | 104 | Fails, if 'actual' isn't a string. 105 | 106 | assert_not_string( actual, [msg] ) 107 | 108 | Fails, if 'actual' is a string. 109 | 110 | assert_table( actual, [msg] ) 111 | 112 | Fails, if 'actual' isn't a table. 113 | 114 | assert_not_table( actual, [msg] ) 115 | 116 | Fails, if 'actual' is a table. 117 | 118 | assert_function( actual, [msg] ) 119 | 120 | Fails, if 'actual' isn't a function. 121 | 122 | assert_not_function( actual, [msg] ) 123 | 124 | Fails, if 'actual' is a function. 125 | 126 | assert_thread( actual, [msg] ) 127 | 128 | Fails, if 'actual' isn't a thread (created by 129 | coroutine.create or coroutine.wrap). 130 | 131 | assert_not_thread( actual, [msg] ) 132 | 133 | Fails, if 'actual' is a thread. 134 | 135 | assert_userdata( actual, [msg] ) 136 | 137 | Fails, if 'actual' isn't userdata. 138 | 139 | assert_not_userdata( actual, [msg] ) 140 | 141 | Fails, if 'actual' is userdata. 142 | 143 | assert_error( [msg], func ) 144 | 145 | Fails, if 'func' doesn't raises an error (using error()). 146 | 147 | assert_pass( [msg], func ) 148 | 149 | Fails, if 'func' raises an error. 150 | 151 | 152 | All assert functions take an optional message as the last argument. Only 153 | assert_pass() and assert_error() require the optional message as the first 154 | argument. The last argument of these two are functions. 155 | 156 | There are also useful functions to test for the type of a value: 157 | 158 | is_nil( actual ) 159 | is_boolean( actual ) 160 | is_number( actual ) 161 | is_string( actual ) 162 | is_table( actual ) 163 | is_function( actual ) 164 | is_thread( actual ) 165 | is_userdata( actual ) 166 | 167 | These all returns true if 'actual' is of correct type, otherwise false. 168 | 169 | You use the assert functions and the is_type functions in your tests to check 170 | your code or package. Example: 171 | 172 | require "lunit" 173 | 174 | module( "my_testcase", lunit.testcase, package.seeall ) 175 | 176 | function FirstTest() 177 | local result = compute_some_value() 178 | assert_string( result ) 179 | assert_equal("foobar", result) 180 | end 181 | 182 | function test_something() 183 | local result = flip_coin() -- flip_coin returns at random 0 or 1 184 | assert_number(result) 185 | if result == 0 then 186 | -- ok 187 | elseif result == 1 then 188 | -- ok 189 | else 190 | fail("flip_coin: invalid number: "..tostring(result)) 191 | end 192 | end 193 | 194 | 195 | You can define the functions setup() and teardown() if you have to allocate 196 | some resources or obtain some handles for your tests. 197 | The setup() function is called before every test and teardown() is 198 | called after every test. Example: 199 | 200 | require "lunit" 201 | 202 | module( "resource_testcase", lunit.testcase, package.seeall ) 203 | 204 | local orig_content, handle 205 | 206 | function setup() 207 | orig_content = { "row 1", "row 2", "row 3" } 208 | handle = database_open("test.db") 209 | database_create_table(handle, ...) 210 | database_fill_table(handle, orig_content, ...) 211 | end 212 | 213 | function teardown() 214 | database_drop_table(handle, ...) 215 | database_close(handle) 216 | handle = nil 217 | orig_content = nil 218 | delete_file("test.db") 219 | end 220 | 221 | function test_select() 222 | local content = database_select(handle, ...) 223 | assert_table( content ) 224 | assert_equal( orig_content, content ) 225 | end 226 | 227 | function test_insert() 228 | database_insert(handle, "row 4", ...) 229 | local content = database_select(handle, ...) 230 | assert_table( content ) 231 | assert_equal( { "row 1", "row 2", "row 3", "row 4" }, content ) 232 | end 233 | 234 | function test_delete() 235 | database_delete(handle, "row 2", ...) 236 | local content = database_select(handle, ...) 237 | assert_table( content ) 238 | assert_equal( { "row 1", "row 3" }, content ) 239 | end 240 | 241 | 242 | To run your testcases, simply use the shell script "lunit". Example: 243 | 244 | # ./lunit my_testcase.lua 245 | 246 | -------------------------------------------------------------------------------- /etc/lunit/LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Lunit License 3 | ------------- 4 | 5 | Lunit is written by Michael Roth and is licensed 6 | under the terms of the MIT license reproduced below. 7 | 8 | ======================================================================== 9 | 10 | Copyright (c) 2004-2009 Michael Roth 11 | 12 | Permission is hereby granted, free of charge, to any person 13 | obtaining a copy of this software and associated documentation 14 | files (the "Software"), to deal in the Software without restriction, 15 | including without limitation the rights to use, copy, modify, merge, 16 | publish, distribute, sublicense, and/or sell copies of the Software, 17 | and to permit persons to whom the Software is furnished to do so, 18 | subject to the following conditions: 19 | 20 | The above copyright notice and this permission notice shall be 21 | included in all copies or substantial portions of the Software. 22 | 23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 26 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 27 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 28 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 29 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 30 | 31 | ======================================================================== 32 | 33 | -------------------------------------------------------------------------------- /etc/lunit/PATCHES: -------------------------------------------------------------------------------- 1 | Lunit is available through a public GIT repository. 2 | To get the latest lunit source code, do: 3 | 4 | git clone git://repo.or.cz/lunit.git 5 | 6 | Currently there is no mailing list dedicated to lunit so if you have patches, 7 | please send them to: 8 | 9 | mroth@nessie.de 10 | 11 | Please don't forget to add a Signed-Off-By line (git option -s or --signoff) 12 | to state that your patches apply to the MIT license. 13 | -------------------------------------------------------------------------------- /etc/lunit/README: -------------------------------------------------------------------------------- 1 | This is lunit Version 0.5. 2 | 3 | Lunit is a unit testing framework for lua. 4 | 5 | To learn more about lunit take a look in ANNOUNCE 6 | and look in DOCUMENTATION and check example.lua. 7 | 8 | If you have suggestions, questions or feature request please 9 | feel free to contact me. 10 | 11 | 12 | Michael Roth 13 | -------------------------------------------------------------------------------- /etc/lunit/example.lua: -------------------------------------------------------------------------------- 1 | 2 | require "lunit" 3 | 4 | 5 | 6 | module( "simple", package.seeall, lunit.testcase ) 7 | 8 | function test_success() 9 | assert_true( true, "This test never fails.") 10 | end 11 | 12 | function test_failure() 13 | assert_true( false, "This test always fails!") 14 | end 15 | 16 | 17 | 18 | module( "enhanced", package.seeall, lunit.testcase ) 19 | 20 | local foobar = nil 21 | 22 | function setup() 23 | foobar = "Hello World" 24 | end 25 | 26 | function teardown() 27 | foobar = nil 28 | end 29 | 30 | function test1() 31 | assert_equal("Hello World", foobar) 32 | foobar = string.sub(foobar, 1, 5) 33 | assert_equal("Hello", foobar) 34 | end 35 | 36 | function test2() 37 | assert_equal("Hello World", foobar) 38 | foobar = string.sub(foobar, -5) 39 | assert_equal("World", foobar) 40 | end 41 | -------------------------------------------------------------------------------- /etc/lunit/lunit: -------------------------------------------------------------------------------- 1 | #! /bin/sh 2 | 3 | # This file is part of lunit 0.5. 4 | # 5 | # For Details about lunit look at: http://www.mroth.net/lunit/ 6 | # 7 | # Author: Michael Roth 8 | # 9 | # Copyright (c) 2004-2009 Michael Roth 10 | # 11 | # Permission is hereby granted, free of charge, to any person 12 | # obtaining a copy of this software and associated documentation 13 | # files (the "Software"), to deal in the Software without restriction, 14 | # including without limitation the rights to use, copy, modify, merge, 15 | # publish, distribute, sublicense, and/or sell copies of the Software, 16 | # and to permit persons to whom the Software is furnished to do so, 17 | # subject to the following conditions: 18 | # 19 | # The above copyright notice and this permission notice shall be 20 | # included in all copies or substantial portions of the Software. 21 | # 22 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 23 | # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 24 | # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 25 | # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 26 | # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 27 | # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 28 | # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 29 | 30 | 31 | if test $# = 0 ; then 32 | echo "$0: Usage Error. Try $0 --help" >&2 33 | exit 1 34 | fi 35 | 36 | if [ `uname` = "Darwin" ]; then 37 | scriptname="$(readlink -n "$0")" 38 | else 39 | scriptname="$(readlink -n -f "$0")" 40 | fi 41 | interpreter="lua" 42 | options="" 43 | 44 | while true ; do 45 | case "$1" in 46 | -h|--help) 47 | cat < 50 | This program comes WITHOUT WARRANTY OF ANY KIND. 51 | 52 | Usage: lunit [OPTIONS] [--] scripts 53 | 54 | Options: 55 | 56 | -i, --interpreter LUA Complete path of the lua binary to use. 57 | -p, --path PATH Sets the LUA_PATH environment for the tests. 58 | --cpath CPATH Sets the LUA_CPATH environment for the tests. 59 | -r, --runner RUNNER Testrunner to use, defaults to 'lunit-console'. 60 | -t, --test PATTERN Which tests to run, may contain * or ? wildcards. 61 | --loadonly Only load the tests. 62 | --dontforce Do not force to load $scriptname*.lua. 63 | -h, --help Print this help screen. 64 | --version Print lunit version. 65 | 66 | Please report bugs to . 67 | EOT 68 | exit ;; 69 | 70 | --version) 71 | echo "lunit 0.5 Copyright 2004-2009 Michael Roth " 72 | exit ;; 73 | 74 | -i|--interpreter) 75 | interpreter="$2" 76 | shift 2 ;; 77 | 78 | -p|--path) 79 | LUA_PATH="$2" 80 | export LUA_PATH 81 | shift 2 ;; 82 | 83 | --cpath) 84 | LUA_CPATH="$2" 85 | export LUA_CPATH 86 | shift 2 ;; 87 | 88 | --loadonly) 89 | options="$options $1" 90 | shift 1 ;; 91 | 92 | --dontforce) 93 | scriptname="" 94 | shift 1 ;; 95 | 96 | -r|--runner|-t|--test) 97 | options="$options $1 $2" 98 | shift 2 ;; 99 | 100 | --) 101 | break ;; 102 | 103 | -*) 104 | echo "$0: Invalid option: $1" >&2 105 | exit 1 ;; 106 | 107 | *) 108 | break ;; 109 | esac 110 | done 111 | 112 | 113 | exec "$interpreter" - "$scriptname" $options "$@" < 0 or stats.failed > 0 then 126 | os.exit(1) 127 | end 128 | EOT 129 | -------------------------------------------------------------------------------- /etc/lunit/lunit-console.lua: -------------------------------------------------------------------------------- 1 | 2 | --[[-------------------------------------------------------------------------- 3 | 4 | This file is part of lunit 0.5. 5 | 6 | For Details about lunit look at: http://www.mroth.net/lunit/ 7 | 8 | Author: Michael Roth 9 | 10 | Copyright (c) 2006-2008 Michael Roth 11 | 12 | Permission is hereby granted, free of charge, to any person 13 | obtaining a copy of this software and associated documentation 14 | files (the "Software"), to deal in the Software without restriction, 15 | including without limitation the rights to use, copy, modify, merge, 16 | publish, distribute, sublicense, and/or sell copies of the Software, 17 | and to permit persons to whom the Software is furnished to do so, 18 | subject to the following conditions: 19 | 20 | The above copyright notice and this permission notice shall be 21 | included in all copies or substantial portions of the Software. 22 | 23 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 26 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 27 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 28 | TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 29 | SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 30 | 31 | --]]-------------------------------------------------------------------------- 32 | 33 | 34 | 35 | --[[ 36 | 37 | begin() 38 | run(testcasename, testname) 39 | err(fullname, message, traceback) 40 | fail(fullname, where, message, usermessage) 41 | pass(testcasename, testname) 42 | done() 43 | 44 | Fullname: 45 | testcase.testname 46 | testcase.testname:setupname 47 | testcase.testname:teardownname 48 | 49 | --]] 50 | 51 | 52 | require "lunit" 53 | 54 | module( "lunit-console", package.seeall ) 55 | 56 | 57 | local function printformat(format, ...) 58 | io.write( string.format(format, ...) ) 59 | end 60 | 61 | 62 | local columns_printed = 0 63 | 64 | local function writestatus(char) 65 | if columns_printed == 0 then 66 | io.write(" ") 67 | end 68 | if columns_printed == 60 then 69 | io.write("\n ") 70 | columns_printed = 0 71 | end 72 | io.write(char) 73 | io.flush() 74 | columns_printed = columns_printed + 1 75 | end 76 | 77 | 78 | local msgs = {} 79 | 80 | 81 | function begin() 82 | local total_tc = 0 83 | local total_tests = 0 84 | 85 | for tcname in lunit.testcases() do 86 | total_tc = total_tc + 1 87 | for testname, test in lunit.tests(tcname) do 88 | total_tests = total_tests + 1 89 | end 90 | end 91 | 92 | printformat("Loaded testsuite with %d tests in %d testcases.\n\n", total_tests, total_tc) 93 | end 94 | 95 | 96 | function run(testcasename, testname) 97 | -- NOP 98 | end 99 | 100 | 101 | function err(fullname, message, traceback) 102 | writestatus("E") 103 | msgs[#msgs+1] = "Error! ("..fullname.."):\n"..message.."\n\t"..table.concat(traceback, "\n\t") .. "\n" 104 | end 105 | 106 | 107 | function fail(fullname, where, message, usermessage) 108 | writestatus("F") 109 | local text = "Failure ("..fullname.."):\n".. 110 | where..": "..message.."\n" 111 | 112 | if usermessage then 113 | text = text .. where..": "..usermessage.."\n" 114 | end 115 | 116 | msgs[#msgs+1] = text 117 | end 118 | 119 | 120 | function pass(testcasename, testname) 121 | writestatus(".") 122 | end 123 | 124 | 125 | 126 | function done() 127 | printformat("\n\n%d Assertions checked.\n", lunit.stats.assertions ) 128 | print() 129 | 130 | for i, msg in ipairs(msgs) do 131 | printformat( "%3d) %s\n", i, msg ) 132 | end 133 | 134 | printformat("Testsuite finished (%d passed, %d failed, %d errors).\n", 135 | lunit.stats.passed, lunit.stats.failed, lunit.stats.errors ) 136 | end 137 | 138 | 139 | 140 | 141 | 142 | -------------------------------------------------------------------------------- /hello.lua: -------------------------------------------------------------------------------- 1 | # simplest sample 2 | package.cpath="src/?.so" 3 | require("fleece") 4 | j = fleece.json({"hello","json"}) 5 | print(j) 6 | -------------------------------------------------------------------------------- /include/README.md: -------------------------------------------------------------------------------- 1 | **Eonblast Fleece 0.3.1** 2 | 3 | Lua Header Files 4 | ---------------- 5 | 6 | These l*.h C headers are original Lua 5.1.4 headers. 7 | 8 | Fleece headers are in src/, luajson headers (not fleece either) are in etc/luajson/. 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 | README fleece/include hd Mar 2011 - powered by Eonblast http://www.eonblast.com 44 | -------------------------------------------------------------------------------- /include/ldebug.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldebug.h,v 2.3.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Auxiliary functions from Debug Interface module 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldebug_h 8 | #define ldebug_h 9 | 10 | 11 | #include "lstate.h" 12 | 13 | 14 | #define pcRel(pc, p) (cast(int, (pc) - (p)->code) - 1) 15 | 16 | #define getline(f,pc) (((f)->lineinfo) ? (f)->lineinfo[pc] : 0) 17 | 18 | #define resethookcount(L) (L->hookcount = L->basehookcount) 19 | 20 | 21 | LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o, 22 | const char *opname); 23 | LUAI_FUNC void luaG_concaterror (lua_State *L, StkId p1, StkId p2); 24 | LUAI_FUNC void luaG_aritherror (lua_State *L, const TValue *p1, 25 | const TValue *p2); 26 | LUAI_FUNC int luaG_ordererror (lua_State *L, const TValue *p1, 27 | const TValue *p2); 28 | LUAI_FUNC void luaG_runerror (lua_State *L, const char *fmt, ...); 29 | LUAI_FUNC void luaG_errormsg (lua_State *L); 30 | LUAI_FUNC int luaG_checkcode (const Proto *pt); 31 | LUAI_FUNC int luaG_checkopenop (Instruction i); 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /include/ldo.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ldo.h,v 2.7.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Stack and Call structure of Lua 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ldo_h 8 | #define ldo_h 9 | 10 | 11 | #include "lobject.h" 12 | #include "lstate.h" 13 | #include "lzio.h" 14 | 15 | 16 | #define luaD_checkstack(L,n) \ 17 | if ((char *)L->stack_last - (char *)L->top <= (n)*(int)sizeof(TValue)) \ 18 | luaD_growstack(L, n); \ 19 | else condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); 20 | 21 | 22 | #define incr_top(L) {luaD_checkstack(L,1); L->top++;} 23 | 24 | #define savestack(L,p) ((char *)(p) - (char *)L->stack) 25 | #define restorestack(L,n) ((TValue *)((char *)L->stack + (n))) 26 | 27 | #define saveci(L,p) ((char *)(p) - (char *)L->base_ci) 28 | #define restoreci(L,n) ((CallInfo *)((char *)L->base_ci + (n))) 29 | 30 | 31 | /* results from luaD_precall */ 32 | #define PCRLUA 0 /* initiated a call to a Lua function */ 33 | #define PCRC 1 /* did a call to a C function */ 34 | #define PCRYIELD 2 /* C funtion yielded */ 35 | 36 | 37 | /* type of protected functions, to be ran by `runprotected' */ 38 | typedef void (*Pfunc) (lua_State *L, void *ud); 39 | 40 | LUAI_FUNC int luaD_protectedparser (lua_State *L, ZIO *z, const char *name); 41 | LUAI_FUNC void luaD_callhook (lua_State *L, int event, int line); 42 | LUAI_FUNC int luaD_precall (lua_State *L, StkId func, int nresults); 43 | LUAI_FUNC void luaD_call (lua_State *L, StkId func, int nResults); 44 | LUAI_FUNC int luaD_pcall (lua_State *L, Pfunc func, void *u, 45 | ptrdiff_t oldtop, ptrdiff_t ef); 46 | LUAI_FUNC int luaD_poscall (lua_State *L, StkId firstResult); 47 | LUAI_FUNC void luaD_reallocCI (lua_State *L, int newsize); 48 | LUAI_FUNC void luaD_reallocstack (lua_State *L, int newsize); 49 | LUAI_FUNC void luaD_growstack (lua_State *L, int n); 50 | 51 | LUAI_FUNC void luaD_throw (lua_State *L, int errcode); 52 | LUAI_FUNC int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud); 53 | 54 | LUAI_FUNC void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop); 55 | 56 | #endif 57 | 58 | -------------------------------------------------------------------------------- /include/lgc.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lgc.h,v 2.15.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Garbage Collector 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lgc_h 8 | #define lgc_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | ** Possible states of the Garbage Collector 16 | */ 17 | #define GCSpause 0 18 | #define GCSpropagate 1 19 | #define GCSsweepstring 2 20 | #define GCSsweep 3 21 | #define GCSfinalize 4 22 | 23 | 24 | /* 25 | ** some userful bit tricks 26 | */ 27 | #define resetbits(x,m) ((x) &= cast(lu_byte, ~(m))) 28 | #define setbits(x,m) ((x) |= (m)) 29 | #define testbits(x,m) ((x) & (m)) 30 | #define bitmask(b) (1<<(b)) 31 | #define bit2mask(b1,b2) (bitmask(b1) | bitmask(b2)) 32 | #define l_setbit(x,b) setbits(x, bitmask(b)) 33 | #define resetbit(x,b) resetbits(x, bitmask(b)) 34 | #define testbit(x,b) testbits(x, bitmask(b)) 35 | #define set2bits(x,b1,b2) setbits(x, (bit2mask(b1, b2))) 36 | #define reset2bits(x,b1,b2) resetbits(x, (bit2mask(b1, b2))) 37 | #define test2bits(x,b1,b2) testbits(x, (bit2mask(b1, b2))) 38 | 39 | 40 | 41 | /* 42 | ** Layout for bit use in `marked' field: 43 | ** bit 0 - object is white (type 0) 44 | ** bit 1 - object is white (type 1) 45 | ** bit 2 - object is black 46 | ** bit 3 - for userdata: has been finalized 47 | ** bit 3 - for tables: has weak keys 48 | ** bit 4 - for tables: has weak values 49 | ** bit 5 - object is fixed (should not be collected) 50 | ** bit 6 - object is "super" fixed (only the main thread) 51 | */ 52 | 53 | 54 | #define WHITE0BIT 0 55 | #define WHITE1BIT 1 56 | #define BLACKBIT 2 57 | #define FINALIZEDBIT 3 58 | #define KEYWEAKBIT 3 59 | #define VALUEWEAKBIT 4 60 | #define FIXEDBIT 5 61 | #define SFIXEDBIT 6 62 | #define WHITEBITS bit2mask(WHITE0BIT, WHITE1BIT) 63 | 64 | 65 | #define iswhite(x) test2bits((x)->gch.marked, WHITE0BIT, WHITE1BIT) 66 | #define isblack(x) testbit((x)->gch.marked, BLACKBIT) 67 | #define isgray(x) (!isblack(x) && !iswhite(x)) 68 | 69 | #define otherwhite(g) (g->currentwhite ^ WHITEBITS) 70 | #define isdead(g,v) ((v)->gch.marked & otherwhite(g) & WHITEBITS) 71 | 72 | #define changewhite(x) ((x)->gch.marked ^= WHITEBITS) 73 | #define gray2black(x) l_setbit((x)->gch.marked, BLACKBIT) 74 | 75 | #define valiswhite(x) (iscollectable(x) && iswhite(gcvalue(x))) 76 | 77 | #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS) 78 | 79 | 80 | #define luaC_checkGC(L) { \ 81 | condhardstacktests(luaD_reallocstack(L, L->stacksize - EXTRA_STACK - 1)); \ 82 | if (G(L)->totalbytes >= G(L)->GCthreshold) \ 83 | luaC_step(L); } 84 | 85 | 86 | #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \ 87 | luaC_barrierf(L,obj2gco(p),gcvalue(v)); } 88 | 89 | #define luaC_barriert(L,t,v) { if (valiswhite(v) && isblack(obj2gco(t))) \ 90 | luaC_barrierback(L,t); } 91 | 92 | #define luaC_objbarrier(L,p,o) \ 93 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(p))) \ 94 | luaC_barrierf(L,obj2gco(p),obj2gco(o)); } 95 | 96 | #define luaC_objbarriert(L,t,o) \ 97 | { if (iswhite(obj2gco(o)) && isblack(obj2gco(t))) luaC_barrierback(L,t); } 98 | 99 | LUAI_FUNC size_t luaC_separateudata (lua_State *L, int all); 100 | LUAI_FUNC void luaC_callGCTM (lua_State *L); 101 | LUAI_FUNC void luaC_freeall (lua_State *L); 102 | LUAI_FUNC void luaC_step (lua_State *L); 103 | LUAI_FUNC void luaC_fullgc (lua_State *L); 104 | LUAI_FUNC void luaC_link (lua_State *L, GCObject *o, lu_byte tt); 105 | LUAI_FUNC void luaC_linkupval (lua_State *L, UpVal *uv); 106 | LUAI_FUNC void luaC_barrierf (lua_State *L, GCObject *o, GCObject *v); 107 | LUAI_FUNC void luaC_barrierback (lua_State *L, Table *t); 108 | 109 | 110 | #endif 111 | -------------------------------------------------------------------------------- /include/llimits.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: llimits.h,v 1.69.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Limits, basic types, and some other `installation-dependent' definitions 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef llimits_h 8 | #define llimits_h 9 | 10 | 11 | #include 12 | #include 13 | 14 | 15 | #include "lua.h" 16 | 17 | 18 | typedef LUAI_UINT32 lu_int32; 19 | 20 | typedef LUAI_UMEM lu_mem; 21 | 22 | typedef LUAI_MEM l_mem; 23 | 24 | 25 | 26 | /* chars used as small naturals (so that `char' is reserved for characters) */ 27 | typedef unsigned char lu_byte; 28 | 29 | 30 | #define MAX_SIZET ((size_t)(~(size_t)0)-2) 31 | 32 | #define MAX_LUMEM ((lu_mem)(~(lu_mem)0)-2) 33 | 34 | 35 | #define MAX_INT (INT_MAX-2) /* maximum value of an int (-2 for safety) */ 36 | 37 | /* 38 | ** conversion of pointer to integer 39 | ** this is for hashing only; there is no problem if the integer 40 | ** cannot hold the whole pointer value 41 | */ 42 | #define IntPoint(p) ((unsigned int)(lu_mem)(p)) 43 | 44 | 45 | 46 | /* type to ensure maximum alignment */ 47 | typedef LUAI_USER_ALIGNMENT_T L_Umaxalign; 48 | 49 | 50 | /* result of a `usual argument conversion' over lua_Number */ 51 | typedef LUAI_UACNUMBER l_uacNumber; 52 | 53 | 54 | /* internal assertions for in-house debugging */ 55 | #ifdef lua_assert 56 | 57 | #define check_exp(c,e) (lua_assert(c), (e)) 58 | #define api_check(l,e) lua_assert(e) 59 | 60 | #else 61 | 62 | #define lua_assert(c) ((void)0) 63 | #define check_exp(c,e) (e) 64 | #define api_check luai_apicheck 65 | 66 | #endif 67 | 68 | 69 | #ifndef UNUSED 70 | #define UNUSED(x) ((void)(x)) /* to avoid warnings */ 71 | #endif 72 | 73 | 74 | #ifndef cast 75 | #define cast(t, exp) ((t)(exp)) 76 | #endif 77 | 78 | #define cast_byte(i) cast(lu_byte, (i)) 79 | #define cast_num(i) cast(lua_Number, (i)) 80 | #define cast_int(i) cast(int, (i)) 81 | 82 | 83 | 84 | /* 85 | ** type for virtual-machine instructions 86 | ** must be an unsigned with (at least) 4 bytes (see details in lopcodes.h) 87 | */ 88 | typedef lu_int32 Instruction; 89 | 90 | 91 | 92 | /* maximum stack for a Lua function */ 93 | #define MAXSTACK 250 94 | 95 | 96 | 97 | /* minimum size for the string table (must be power of 2) */ 98 | #ifndef MINSTRTABSIZE 99 | #define MINSTRTABSIZE 32 100 | #endif 101 | 102 | 103 | /* minimum size for string buffer */ 104 | #ifndef LUA_MINBUFFER 105 | #define LUA_MINBUFFER 32 106 | #endif 107 | 108 | 109 | #ifndef lua_lock 110 | #define lua_lock(L) ((void) 0) 111 | #define lua_unlock(L) ((void) 0) 112 | #endif 113 | 114 | #ifndef luai_threadyield 115 | #define luai_threadyield(L) {lua_unlock(L); lua_lock(L);} 116 | #endif 117 | 118 | 119 | /* 120 | ** macro to control inclusion of some hard tests on stack reallocation 121 | */ 122 | #ifndef HARDSTACKTESTS 123 | #define condhardstacktests(x) ((void)0) 124 | #else 125 | #define condhardstacktests(x) x 126 | #endif 127 | 128 | #endif 129 | -------------------------------------------------------------------------------- /include/lmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lmem.h,v 1.31.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Interface to Memory Manager 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lmem_h 8 | #define lmem_h 9 | 10 | 11 | #include 12 | 13 | #include "llimits.h" 14 | #include "lua.h" 15 | 16 | #define MEMERRMSG "not enough memory" 17 | 18 | 19 | #define luaM_reallocv(L,b,on,n,e) \ 20 | ((cast(size_t, (n)+1) <= MAX_SIZET/(e)) ? /* +1 to avoid warnings */ \ 21 | luaM_realloc_(L, (b), (on)*(e), (n)*(e)) : \ 22 | luaM_toobig(L)) 23 | 24 | #define luaM_freemem(L, b, s) luaM_realloc_(L, (b), (s), 0) 25 | #define luaM_free(L, b) luaM_realloc_(L, (b), sizeof(*(b)), 0) 26 | #define luaM_freearray(L, b, n, t) luaM_reallocv(L, (b), n, 0, sizeof(t)) 27 | 28 | #define luaM_malloc(L,t) luaM_realloc_(L, NULL, 0, (t)) 29 | #define luaM_new(L,t) cast(t *, luaM_malloc(L, sizeof(t))) 30 | #define luaM_newvector(L,n,t) \ 31 | cast(t *, luaM_reallocv(L, NULL, 0, n, sizeof(t))) 32 | 33 | #define luaM_growvector(L,v,nelems,size,t,limit,e) \ 34 | if ((nelems)+1 > (size)) \ 35 | ((v)=cast(t *, luaM_growaux_(L,v,&(size),sizeof(t),limit,e))) 36 | 37 | #define luaM_reallocvector(L, v,oldn,n,t) \ 38 | ((v)=cast(t *, luaM_reallocv(L, v, oldn, n, sizeof(t)))) 39 | 40 | 41 | LUAI_FUNC void *luaM_realloc_ (lua_State *L, void *block, size_t oldsize, 42 | size_t size); 43 | LUAI_FUNC void *luaM_toobig (lua_State *L); 44 | LUAI_FUNC void *luaM_growaux_ (lua_State *L, void *block, int *size, 45 | size_t size_elem, int limit, 46 | const char *errormsg); 47 | 48 | #endif 49 | 50 | -------------------------------------------------------------------------------- /include/lstate.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lstate.h,v 2.24.1.2 2008/01/03 15:20:39 roberto Exp $ 3 | ** Global State 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef lstate_h 8 | #define lstate_h 9 | 10 | #include "lua.h" 11 | 12 | #include "lobject.h" 13 | #include "ltm.h" 14 | #include "lzio.h" 15 | 16 | 17 | 18 | struct lua_longjmp; /* defined in ldo.c */ 19 | 20 | 21 | /* table of globals */ 22 | #define gt(L) (&L->l_gt) 23 | 24 | /* registry */ 25 | #define registry(L) (&G(L)->l_registry) 26 | 27 | 28 | /* extra stack space to handle TM calls and some other extras */ 29 | #define EXTRA_STACK 5 30 | 31 | 32 | #define BASIC_CI_SIZE 8 33 | 34 | #define BASIC_STACK_SIZE (2*LUA_MINSTACK) 35 | 36 | 37 | 38 | typedef struct stringtable { 39 | GCObject **hash; 40 | lu_int32 nuse; /* number of elements */ 41 | int size; 42 | } stringtable; 43 | 44 | 45 | /* 46 | ** informations about a call 47 | */ 48 | typedef struct CallInfo { 49 | StkId base; /* base for this function */ 50 | StkId func; /* function index in the stack */ 51 | StkId top; /* top for this function */ 52 | const Instruction *savedpc; 53 | int nresults; /* expected number of results from this function */ 54 | int tailcalls; /* number of tail calls lost under this entry */ 55 | } CallInfo; 56 | 57 | 58 | 59 | #define curr_func(L) (clvalue(L->ci->func)) 60 | #define ci_func(ci) (clvalue((ci)->func)) 61 | #define f_isLua(ci) (!ci_func(ci)->c.isC) 62 | #define isLua(ci) (ttisfunction((ci)->func) && f_isLua(ci)) 63 | 64 | 65 | /* 66 | ** `global state', shared by all threads of this state 67 | */ 68 | typedef struct global_State { 69 | stringtable strt; /* hash table for strings */ 70 | lua_Alloc frealloc; /* function to reallocate memory */ 71 | void *ud; /* auxiliary data to `frealloc' */ 72 | lu_byte currentwhite; 73 | lu_byte gcstate; /* state of garbage collector */ 74 | int sweepstrgc; /* position of sweep in `strt' */ 75 | GCObject *rootgc; /* list of all collectable objects */ 76 | GCObject **sweepgc; /* position of sweep in `rootgc' */ 77 | GCObject *gray; /* list of gray objects */ 78 | GCObject *grayagain; /* list of objects to be traversed atomically */ 79 | GCObject *weak; /* list of weak tables (to be cleared) */ 80 | GCObject *tmudata; /* last element of list of userdata to be GC */ 81 | Mbuffer buff; /* temporary buffer for string concatentation */ 82 | lu_mem GCthreshold; 83 | lu_mem totalbytes; /* number of bytes currently allocated */ 84 | lu_mem estimate; /* an estimate of number of bytes actually in use */ 85 | lu_mem gcdept; /* how much GC is `behind schedule' */ 86 | int gcpause; /* size of pause between successive GCs */ 87 | int gcstepmul; /* GC `granularity' */ 88 | lua_CFunction panic; /* to be called in unprotected errors */ 89 | TValue l_registry; 90 | struct lua_State *mainthread; 91 | UpVal uvhead; /* head of double-linked list of all open upvalues */ 92 | struct Table *mt[NUM_TAGS]; /* metatables for basic types */ 93 | TString *tmname[TM_N]; /* array with tag-method names */ 94 | } global_State; 95 | 96 | 97 | /* 98 | ** `per thread' state 99 | */ 100 | struct lua_State { 101 | CommonHeader; 102 | lu_byte status; 103 | StkId top; /* first free slot in the stack */ 104 | StkId base; /* base of current function */ 105 | global_State *l_G; 106 | CallInfo *ci; /* call info for current function */ 107 | const Instruction *savedpc; /* `savedpc' of current function */ 108 | StkId stack_last; /* last free slot in the stack */ 109 | StkId stack; /* stack base */ 110 | CallInfo *end_ci; /* points after end of ci array*/ 111 | CallInfo *base_ci; /* array of CallInfo's */ 112 | int stacksize; 113 | int size_ci; /* size of array `base_ci' */ 114 | unsigned short nCcalls; /* number of nested C calls */ 115 | unsigned short baseCcalls; /* nested C calls when resuming coroutine */ 116 | lu_byte hookmask; 117 | lu_byte allowhook; 118 | int basehookcount; 119 | int hookcount; 120 | lua_Hook hook; 121 | TValue l_gt; /* table of globals */ 122 | TValue env; /* temporary place for environments */ 123 | GCObject *openupval; /* list of open upvalues in this stack */ 124 | GCObject *gclist; 125 | struct lua_longjmp *errorJmp; /* current error recover point */ 126 | ptrdiff_t errfunc; /* current error handling function (stack index) */ 127 | }; 128 | 129 | 130 | #define G(L) (L->l_G) 131 | 132 | 133 | /* 134 | ** Union of all collectable objects 135 | */ 136 | union GCObject { 137 | GCheader gch; 138 | union TString ts; 139 | union Udata u; 140 | union Closure cl; 141 | struct Table h; 142 | struct Proto p; 143 | struct UpVal uv; 144 | struct lua_State th; /* thread */ 145 | }; 146 | 147 | 148 | /* macros to convert a GCObject into a specific value */ 149 | #define rawgco2ts(o) check_exp((o)->gch.tt == LUA_TSTRING, &((o)->ts)) 150 | #define gco2ts(o) (&rawgco2ts(o)->tsv) 151 | #define rawgco2u(o) check_exp((o)->gch.tt == LUA_TUSERDATA, &((o)->u)) 152 | #define gco2u(o) (&rawgco2u(o)->uv) 153 | #define gco2cl(o) check_exp((o)->gch.tt == LUA_TFUNCTION, &((o)->cl)) 154 | #define gco2h(o) check_exp((o)->gch.tt == LUA_TTABLE, &((o)->h)) 155 | #define gco2p(o) check_exp((o)->gch.tt == LUA_TPROTO, &((o)->p)) 156 | #define gco2uv(o) check_exp((o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 157 | #define ngcotouv(o) \ 158 | check_exp((o) == NULL || (o)->gch.tt == LUA_TUPVAL, &((o)->uv)) 159 | #define gco2th(o) check_exp((o)->gch.tt == LUA_TTHREAD, &((o)->th)) 160 | 161 | /* macro to convert any Lua object into a GCObject */ 162 | #define obj2gco(v) (cast(GCObject *, (v))) 163 | 164 | 165 | LUAI_FUNC lua_State *luaE_newthread (lua_State *L); 166 | LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1); 167 | 168 | #endif 169 | 170 | -------------------------------------------------------------------------------- /include/ltable.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltable.h,v 2.10.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Lua tables (hash) 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltable_h 8 | #define ltable_h 9 | 10 | #include "lobject.h" 11 | 12 | 13 | #define gnode(t,i) (&(t)->node[i]) 14 | #define gkey(n) (&(n)->i_key.nk) 15 | #define gval(n) (&(n)->i_val) 16 | #define gnext(n) ((n)->i_key.nk.next) 17 | 18 | #define key2tval(n) (&(n)->i_key.tvk) 19 | 20 | 21 | LUAI_FUNC const TValue *luaH_getnum (Table *t, int key); 22 | LUAI_FUNC TValue *luaH_setnum (lua_State *L, Table *t, int key); 23 | LUAI_FUNC const TValue *luaH_getstr (Table *t, TString *key); 24 | LUAI_FUNC TValue *luaH_setstr (lua_State *L, Table *t, TString *key); 25 | LUAI_FUNC const TValue *luaH_get (Table *t, const TValue *key); 26 | LUAI_FUNC TValue *luaH_set (lua_State *L, Table *t, const TValue *key); 27 | LUAI_FUNC Table *luaH_new (lua_State *L, int narray, int lnhash); 28 | LUAI_FUNC void luaH_resizearray (lua_State *L, Table *t, int nasize); 29 | LUAI_FUNC void luaH_free (lua_State *L, Table *t); 30 | LUAI_FUNC int luaH_next (lua_State *L, Table *t, StkId key); 31 | LUAI_FUNC int luaH_getn (Table *t); 32 | 33 | 34 | #if defined(LUA_DEBUG) 35 | LUAI_FUNC Node *luaH_mainposition (const Table *t, const TValue *key); 36 | LUAI_FUNC int luaH_isdummy (Node *n); 37 | #endif 38 | 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /include/ltm.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: ltm.h,v 2.6.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Tag methods 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | #ifndef ltm_h 8 | #define ltm_h 9 | 10 | 11 | #include "lobject.h" 12 | 13 | 14 | /* 15 | * WARNING: if you change the order of this enumeration, 16 | * grep "ORDER TM" 17 | */ 18 | typedef enum { 19 | TM_INDEX, 20 | TM_NEWINDEX, 21 | TM_GC, 22 | TM_MODE, 23 | TM_EQ, /* last tag method with `fast' access */ 24 | TM_ADD, 25 | TM_SUB, 26 | TM_MUL, 27 | TM_DIV, 28 | TM_MOD, 29 | TM_POW, 30 | TM_UNM, 31 | TM_LEN, 32 | TM_LT, 33 | TM_LE, 34 | TM_CONCAT, 35 | TM_CALL, 36 | TM_N /* number of elements in the enum */ 37 | } TMS; 38 | 39 | 40 | 41 | #define gfasttm(g,et,e) ((et) == NULL ? NULL : \ 42 | ((et)->flags & (1u<<(e))) ? NULL : luaT_gettm(et, e, (g)->tmname[e])) 43 | 44 | #define fasttm(l,et,e) gfasttm(G(l), et, e) 45 | 46 | LUAI_DATA const char *const luaT_typenames[]; 47 | 48 | 49 | LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename); 50 | LUAI_FUNC const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, 51 | TMS event); 52 | LUAI_FUNC void luaT_init (lua_State *L); 53 | 54 | #endif 55 | -------------------------------------------------------------------------------- /include/lzio.h: -------------------------------------------------------------------------------- 1 | /* 2 | ** $Id: lzio.h,v 1.21.1.1 2007/12/27 13:02:25 roberto Exp $ 3 | ** Buffered streams 4 | ** See Copyright Notice in lua.h 5 | */ 6 | 7 | 8 | #ifndef lzio_h 9 | #define lzio_h 10 | 11 | #include "lua.h" 12 | 13 | #include "lmem.h" 14 | 15 | 16 | #define EOZ (-1) /* end of stream */ 17 | 18 | typedef struct Zio ZIO; 19 | 20 | #define char2int(c) cast(int, cast(unsigned char, (c))) 21 | 22 | #define zgetc(z) (((z)->n--)>0 ? char2int(*(z)->p++) : luaZ_fill(z)) 23 | 24 | typedef struct Mbuffer { 25 | char *buffer; 26 | size_t n; 27 | size_t buffsize; 28 | } Mbuffer; 29 | 30 | #define luaZ_initbuffer(L, buff) ((buff)->buffer = NULL, (buff)->buffsize = 0) 31 | 32 | #define luaZ_buffer(buff) ((buff)->buffer) 33 | #define luaZ_sizebuffer(buff) ((buff)->buffsize) 34 | #define luaZ_bufflen(buff) ((buff)->n) 35 | 36 | #define luaZ_resetbuffer(buff) ((buff)->n = 0) 37 | 38 | 39 | #define luaZ_resizebuffer(L, buff, size) \ 40 | (luaM_reallocvector(L, (buff)->buffer, (buff)->buffsize, size, char), \ 41 | (buff)->buffsize = size) 42 | 43 | #define luaZ_freebuffer(L, buff) luaZ_resizebuffer(L, buff, 0) 44 | 45 | 46 | LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); 47 | LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, 48 | void *data); 49 | LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ 50 | LUAI_FUNC int luaZ_lookahead (ZIO *z); 51 | 52 | 53 | 54 | /* --------- Private Part ------------------ */ 55 | 56 | struct Zio { 57 | size_t n; /* bytes still unread */ 58 | const char *p; /* current position in buffer */ 59 | lua_Reader reader; 60 | void* data; /* additional data */ 61 | lua_State *L; /* Lua state (for reader) */ 62 | }; 63 | 64 | 65 | LUAI_FUNC int luaZ_fill (ZIO *z); 66 | 67 | #endif 68 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Eonblast Fleece 0.3.1 Lite 6 | 7 | 8 | 9 | 10 |
11 |
12 | 13 |

Eonblast Fleece 0.3.1 Lite

14 | 15 |

Fast Lua to JSON conversion.

16 | 17 |
18 | 19 |
20 | 21 | 22 | 23 | 24 | 39 | 62 | 63 |
25 | 26 | More
27 | Tests
28 | Source
29 | Misc
30 | Sample
31 |
32 | Online Home
33 | License
34 | Repository
35 | Eonblast
36 |
37 |
38 |
40 | 41 |

Download

42 | 43 |
git clone https://Eonblast@github.com/Eonblast/fleece-lite.git fleece
44 | 45 |

Build

46 | 47 |
cd fleece
48 | make linux # or make macosx, or make macosx-old for <= 10.3
49 | 
50 | 51 |

Testing

52 | 53 |
make test
54 | make bench
55 | 56 |

Sample

57 | 58 |
t = {1,2,3};
59 | json_str = fleece.json(t)
60 | 61 |
64 | 65 |
66 | 67 | 68 |
69 | 70 | © 2011 Eonblast Corporation 71 | 72 |
73 | 74 |
75 | 76 | 77 | -------------------------------------------------------------------------------- /src/Makefile: -------------------------------------------------------------------------------- 1 | ###-------------------------------------------------------------------------### 2 | ### Package : Fleece - fast Lua to JSON module ### 3 | ### File : Makefile ### 4 | ### Description : makefile for building Fleece as dynamic library ### 5 | ### Version : 0.3.0 / alpha ### 6 | ### Author : H. Diedrich ### 7 | ### Created : Feb 2011 ### 8 | ### Changed : 12 Mar 2011 ### 9 | ###-------------------------------------------------------------------------### 10 | ### ### 11 | ### Don't use directly but use ../Makefile, see there or ../README.md ### 12 | ### ### 13 | ### Also see src/fleece-intern.h for compile time configurations. ### 14 | ### ### 15 | ### PLEASE CONTRIBUTE YOUR WINDOWS OR OTHER PLATFORM'S MAKE INSTRUCTIONS ### 16 | ### for this file src/Makefile and etc/luajson/Makefile. Thank you! ### 17 | ### ### 18 | ###-------------------------------------------------------------------------### 19 | 20 | default: $(PLAT) 21 | 22 | clean: 23 | rm -f *. *.o *.a *.so 24 | 25 | # convenience targets for popular platforms 26 | 27 | none: 28 | @echo "Please choose a platform:" 29 | @echo " $(PLATS)" 30 | 31 | aix: 32 | @echo "Not tested, please contribute" 33 | 34 | ansi: 35 | @echo "Not tested, please contribute" 36 | 37 | bsd: 38 | @echo "Not tested, please contribute" 39 | 40 | freebsd: 41 | @echo "Not tested, please contribute" 42 | 43 | generic: 44 | @echo "Not tested, please contribute" 45 | 46 | linux: 47 | gcc -O2 -fomit-frame-pointer -Wall -I../include -c -o fleece.o fleece.c 48 | gcc -O -shared -fomit-frame-pointer -o fleece.so fleece.o 49 | 50 | linux-asm: 51 | gcc -O2 -fomit-frame-pointer -Wall -DFLEECE_ASM -I../include -c -o fleece.o fleece.c 52 | gcc -O -shared -fomit-frame-pointer -o fleece.so fleece.o 53 | 54 | linux-64: 55 | gcc -O2 -fpic -Wall -I../include -c -o fleece.o fleece.c 56 | gcc -O -shared -fpic -o fleece.so fleece.o 57 | 58 | linux-64-asm: 59 | gcc -O2 -fpic -Wall -DFLEECE_ASM -I../include -c -o fleece.o fleece.c 60 | gcc -O -shared -fpic -o fleece.so fleece.o 61 | 62 | macosx: 63 | gcc -Wall -bundle -undefined dynamic_lookup -I../include -o fleece.so fleece.c 64 | 65 | macosx-asm: 66 | gcc -Wall -bundle -undefined dynamic_lookup -DFLEECE_ASM -I../include -o fleece.so fleece.c 67 | 68 | macosx-old: 69 | export MACOSX_DEPLOYMENT_TARGET="10.3" && gcc -bundle -undefined dynamic_lookup -I../include -o fleece.so fleece.c 70 | 71 | mingw: 72 | @echo "Not tested, please contribute" 73 | 74 | posix: 75 | @echo "Not tested, please contribute" 76 | 77 | solaris: 78 | @echo "Not tested, please contribute" 79 | 80 | # list targets that do not create files (but not all makes understand .PHONY) 81 | .PHONY: all $(PLATS) default o a clean depend echo none 82 | 83 | luajson.o: luajson.c luajson.h 84 | fleece.o: fleece.h fleece-config.h fleece-intern.h fleece.c fleece-buffers.c fleece-numbers.c \ 85 | fleece-ctrl.c fleece-copy.c fleece-size.c fleece-stringify.c fleece-lua.c 86 | 87 | # -------------------------------------------------------------------------- 88 | # Makefile fleece/src Feb 2011 - powered by Eonblast http://www.eonblast.com 89 | -------------------------------------------------------------------------------- /src/README.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Source Folder 6 | 83 | 84 | 85 |
86 | 87 |
88 |
89 | 90 |

Eonblast Fleece 0.3.1

91 | 92 |

Source Folder

93 | 94 |

Lua-Facing Files

95 | 96 |
fleece-config.h       compile-time configurations
 97 | 
 98 | fleece.c              main module file, includes the other c files  
 99 | fleece.h              main header file, not relevant  
100 | 
101 | fleece-lua.c          lua-facing Fleece functions (hello & json so far)  
102 | 
103 | 104 | 105 |

Main Module Files

106 | 107 |
fleece-intern.h       internal header file
108 | 
109 | fleece-buffers.c      custom string buffer source  
110 | fleece-buffers.h
111 | 
112 | fleece-insp_ctrl.c    traversal control structure  
113 | fleece-insp_ctrl.h      
114 | 
115 | fleece-numbers.c      number to ascii conversions  
116 | fleece-size.c         JSON size pre-calculation  
117 | fleece-stringify.c    Lua data traversal and JSON string production  
118 | fleece-copy.c         JSON-optimized copying sources
119 | 
120 | 121 | 122 |

Other Files

123 | 124 |
Makefile             sub make file, used by the Makefile in Fleece root
125 | 
126 | README.md            this file
127 | README.html          same as README.md in HTML format
128 | 
129 | fleece.o             Fleece C compilation file, present after build
130 | fleece.so            Fleece C library, present after build
131 | 
132 | 133 |

Lua headers are included from ../include/

134 | 135 |

For tests see the ../test/ folder.

136 | 137 |

Escaping

138 | 139 |

If you use escaping, assembler can make a difference. Fleece does well for non-escaped strings with or without assembler. But consider guarding your string contents against special characters yourself in any case, and do any necessary escaping yourself. In most cases this will prevent much double checking. The setting E0 then ignores escaping issues entirely and is the fastest option for JSON conversion. It allows for strings being copied as they are, without looking at the individual characters.

140 | 141 |

Assembler

142 | 143 |

Assembler helps most with strings that are to get their control characters escaped, as prescribed by the JSON standard. Assembler is implemented only for x86, using gcc inline/AT&T format. Switch assembler on by using linux-asm, linux-64-asm, or macosx-asm. The options E1 - E4 are the most affected.

144 | 145 |

The non-assembler parts are sufficiently fast and optimized for their task. Depending on architecture, they are only around 10 to 50% slower than assembler, mostly due to highly optimized C library memcpy functions.

146 | 147 |

Obviously, when escaping characters, the built-in memcpy can not be used alone but string traversing has to take place. strcspn() turned out to be no help performance-wise.

148 | 149 |

Copy Strategies

150 | 151 |

There are multiple strategies visible in the fleece-copy.c source, some will be abandoned and cleaned up. Basically:

152 | 153 |

Buffer Space

154 |

A) count first, check remaining buffer, enlarge if nec., then copy
155 | B) check if remaining buffer < 'max escape case' (6x), enlarge, copy
156 | C) without any checks rely on assured reserve buffer for single chars

157 | 158 |

Size Prediction

159 |

I) plain count, for finding the destination size, then copy
160 | II) count and copy, cancel copying when exceeding assured reserve
161 | III) use built in C lib strcspn to count escape characters (--)

162 | 163 |

Copy Control

164 |

i) memory-copy using given source length
165 | ii) string-copy relying on final 0 (faster for ASM, not tru to Lua)

166 | 167 |

Copying

168 |

1) use built in C lib memcpy
169 | 2) use native C programmed memcpy (faster for old PPC) (--)
170 | 3) use native C programmed, block pointer casting-memcpy
171 | 4) use assembler (x86 only, 10 - 20% faster, more on older CPUs)

172 | 173 |
174 |

README fleece/src hd Feb 2011 - powered by Eonblast http://www.eonblast.com

175 | 176 |
177 |
178 | 179 | © 2011 180 | Eonblast Corporation 181 | 182 |
183 |
184 |
185 | 186 | 187 | -------------------------------------------------------------------------------- /src/README.md: -------------------------------------------------------------------------------- 1 | **Eonblast Fleece 0.3.1** 2 | 3 | Source Folder 4 | ------------- 5 | 6 | #### Lua-Facing Files 7 | 8 | fleece-config.h compile-time configurations 9 | 10 | fleece.c main module file, includes the other c files 11 | fleece.h main header file, not relevant 12 | 13 | fleece-lua.c lua-facing Fleece functions (hello & json so far) 14 | 15 | 16 | #### Main Module Files 17 | 18 | fleece-intern.h internal header file 19 | 20 | fleece-buffers.c custom string buffer source 21 | fleece-buffers.h 22 | 23 | fleece-insp_ctrl.c traversal control structure 24 | fleece-insp_ctrl.h 25 | 26 | fleece-numbers.c number to ascii conversions 27 | fleece-size.c JSON size pre-calculation 28 | fleece-stringify.c Lua data traversal and JSON string production 29 | fleece-copy.c JSON-optimized copying sources 30 | 31 | 32 | #### Other Files 33 | 34 | Makefile sub make file, used by the Makefile in Fleece root 35 | 36 | README.md this file 37 | README.html same as README.md in HTML format 38 | 39 | fleece.o Fleece C compilation file, present after build 40 | fleece.so Fleece C library, present after build 41 | 42 | *Lua* headers are included from ../include/ 43 | 44 | For tests see the ../test/ folder. 45 | 46 | ## Escaping 47 | 48 | If you use escaping, assembler *can* make a difference. Fleece does well for non-escaped strings with or without assembler. But consider guarding your string contents against special characters yourself in any case, and do any necessary escaping yourself. In most cases this will prevent much double checking. The setting **E0** then ignores escaping issues entirely and is the fastest option for JSON conversion. It allows for strings being copied as they are, without looking at the individual characters. 49 | 50 | ## Assembler 51 | 52 | Assembler helps most with strings that *are* to get their control characters escaped, as prescribed by the JSON standard. Assembler is implemented only for x86, using gcc inline/AT&T format. Switch assembler on by using linux-asm, linux-64-asm, or macosx-asm. The options *E1* - *E4* are the most affected. 53 | 54 | The non-assembler parts are sufficiently fast and optimized for their task. Depending on architecture, they are only around 10 to 50% slower than assembler, mostly due to highly optimized C library memcpy functions. 55 | 56 | Obviously, when escaping characters, the built-in memcpy can not be used alone but string traversing has to take place. strcspn() turned out to be no help performance-wise. 57 | 58 | ## Copy Strategies 59 | 60 | There are multiple strategies visible in the fleece-copy.c source, some will be abandoned and cleaned up. Basically: 61 | 62 | #### Buffer Space 63 | A) count first, check remaining buffer, enlarge if nec., then copy 64 | B) check if remaining buffer < 'max escape case' (6x), enlarge, copy 65 | C) without any checks rely on assured reserve buffer for single chars 66 | 67 | #### Size Prediction 68 | I) plain count, for finding the destination size, then copy 69 | II) count and copy, cancel copying when exceeding assured reserve 70 | III) use built in C lib strcspn to count escape characters (--) 71 | 72 | #### Copy Control 73 | i) memory-copy using given source length 74 | ii) string-copy relying on final 0 (faster for ASM, not tru to Lua) 75 | 76 | #### Copying 77 | 1) use built in C lib memcpy 78 | 2) use native C programmed memcpy (faster for old PPC) (--) 79 | 3) use native C programmed, block pointer casting-memcpy 80 | 4) use assembler (x86 only, 10 - 20% faster, more on older CPUs) 81 | 82 | ___________________________________________________________________________ 83 | README fleece/src hd Feb 2011 - powered by Eonblast http://www.eonblast.com 84 | -------------------------------------------------------------------------------- /src/fleece-config.h: -------------------------------------------------------------------------------- 1 | /**-------------------------------------------------------------------------*** 2 | *** Packages : Fleece - fast Lua to JSON module *** 3 | *** File : fleece-config.h *** 4 | *** Description : header with compile switches and default definitions *** 5 | *** Version : 0.3.1 / alpha *** 6 | *** Copyright : 2011 Henning Diedrich, Eonblast Corporation *** 7 | *** Author : H. Diedrich *** 8 | *** License : see file LICENSE *** 9 | *** Created : 5 Mar 2011 *** 10 | *** Changed : 19 Mar 2011 *** 11 | ***-------------------------------------------------------------------------**/ 12 | 13 | #ifndef FLEECE_CONFIG_H 14 | #define FLEECE_CONFIG_H 15 | 16 | /*****************************************************************************\ 17 | *** *** 18 | *** DEBUG SETTINGS *** 19 | *** *** 20 | \*****************************************************************************/ 21 | 22 | /* For debugging. Levels 0 to 10. 23 | * 0 = none = fastest. 10 = all = slowest, very verbose. 24 | * TODO: currently no good - won't compile */ 25 | #define SELFCHECK 0 26 | #define VERBOSITY 0 27 | 28 | /* Safer mode where 0 is placed after each string as soon as it is written 29 | * into the buffer. Obviously, it's constantly overwritten right away. 30 | * Uncomment to activate. */ 31 | 32 | // #define FLEECE_ALWAYS_DELIMIT 33 | 34 | /*****************************************************************************\ 35 | *** *** 36 | *** PERFORMANCE *** 37 | *** *** 38 | \*****************************************************************************/ 39 | 40 | /** use assembler copy routines (x86 only) 41 | * Use this with make RATHER than uncommenting below: 42 | * linux-asm, or linux-64-asm, or macosx-asm 43 | * which trigger this compile flag implicitly */ 44 | 45 | // #define FLEECE_ASM 46 | 47 | /* Allowed buffer byte size for first buffer. Affects the performance a lot. */ 48 | 49 | #define FLEECE_DEFAULT_LUCKY_BUF 100000 50 | 51 | /* Allowed buffer size for following parts. 52 | * Usually smaller, to reduce footprint. */ 53 | 54 | #define FLEECE_DEFAULT_PARTS_BUF 10000 55 | 56 | /* shaky, better don't touch. The higher the faster with escapes. Must be > 2 57 | !!! RSRV_BUF MUST BE SAME AS RESERVE_BUFFER, AS STRING !!! */ 58 | 59 | #define RESERVE_BUFFER 12 60 | #define RSRV_BUF "12" 61 | 62 | /** Relevant for FLEECE_ASM: use string copy rather than memcpy, which is faster. 63 | * The trade off is that Lua can have zeroes in strings, and this forfeits 64 | * them, stopping traversion as soon as a 0 is met, and taking that as the end. 65 | * This is faster than a memcpy, because the counter can be dropped. */ 66 | 67 | #define FLEECE_ZERO_AS_END 68 | 69 | /*****************************************************************************\ 70 | *** *** 71 | *** FEATURES *** 72 | *** *** 73 | \*****************************************************************************/ 74 | 75 | /* compile with escape capability, else E, E1-4 are disabled. 76 | * not tested with 0 */ 77 | 78 | #define FLEECE_WITH_ESCAPE 1 79 | 80 | /*****************************************************************************\ 81 | *** *** 82 | *** DEFAULTS *** 83 | *** *** 84 | \*****************************************************************************/ 85 | 86 | /* which special characters to escape: 87 | * E = E4; E0 none; E1 purge, E2 ",\; E3 ",\,/; E4 0-31,127,",\,/ */ 88 | 89 | #define FLEECE_DEFAULT_ESCAPE_FLAG 0 90 | 91 | /* TODO:Not currently implemented correctly. 92 | * character to be used under E1 93 | * to replace " and / */ 94 | #define FLEECE_PURGE_CHAR '*' 95 | 96 | #endif 97 | 98 | -------------------------------------------------------------------------------- /src/fleece-ctrl.h: -------------------------------------------------------------------------------- 1 | /**-------------------------------------------------------------------------*** 2 | *** Packages : Fleece - fast Lua to JSON module *** 3 | *** File : fleece-insp_ctrl.h *** 4 | *** Description : Lua data traversal and JSON str build control structure *** 5 | *** Version : 0.3.1 / alpha *** 6 | *** Requirement : self sufficient ANSI C *** 7 | *** Copyright : 2011 Henning Diedrich, Eonblast Corporation *** 8 | *** Author : H. Diedrich *** 9 | *** License : see file LICENSE *** 10 | *** Created : Feb 2011 *** 11 | *** Changed : 08 Mar 2011 *** 12 | ***-------------------------------------------------------------------------*** 13 | *** *** 14 | *** Most of the parameters are not used yet. *** 15 | *** *** 16 | ***-------------------------------------------------------------------------**/ 17 | 18 | #ifndef INSP_CTRL_H 19 | #define INSP_CTRL_H 1 20 | 21 | #include "fleece-config.h" 22 | #include "fleece-intern.h" 23 | 24 | /*****************************************************************************\ 25 | *** *** 26 | *** TRAVERSION CONTROL STRUCTURE *** 27 | *** *** 28 | ***************************************************************************** 29 | * Control structure for traversal of data, both stringification & pre-count.* 30 | * Most fields are not used yet. */ 31 | 32 | struct insp_ctrl_struct { 33 | 34 | /* parameters */ 35 | 36 | const TValue *subject; /* */ 37 | char *flags; /* (+ctrl-flags+) [f|g|e|x|r] + [b|B] + [t|T] + [k|K] + [s|S] + [h|H] + [i|I] + [j|J] + [q|Q] + [v|V] + [d|D] */ 38 | char **repr; /* ... */ 39 | int precision; /* 0 = 7, > 22 = 22 */ 40 | int primary; /* 0: default, 1: buffered, 2: pre-count, 3: precise parts */ 41 | int secondary; /* 0: default, 1: buffered, 2: pre-count, 3: precise parts */ 42 | size_t max_depth; /* allowed ... */ 43 | size_t max_waste; /* allowed wasted space before switching to stragegy #3 */ 44 | size_t lucky_buf; /* allowed buffer for trying strategy #1, the lucky shot */ 45 | size_t parts_buf; /* allowed buffer for strategy #2, pre-allocated parts */ 46 | size_t max_buf; /* allowed ... */ 47 | size_t max_holes; /* allowed ... */ 48 | char *func_str; /* string to use to represent function pointers */ 49 | 50 | /* (+ctrl-flags+) */ 51 | char floatflag; /* f: fixed prec, g: brief, e: scient, x: hex bytes, r: raw bytes */ 52 | int escape_flag; /* E: E = E3; E0 none; E1 ",\; E2 ",\,/; E3 0-31,127,",\,/ */ 53 | int write_bool; /* b: write 'true' and 'false', else 1 and 0 */ 54 | int use_sprintf; /* s: use sprintf() to make floats */ 55 | int hasty_float; /* h: hasty float count as 32 chars in pre-count */ 56 | int hasty_int; /* i: hasty int count as 11 or 20 chars in pre-count */ 57 | int verbose; /* v: verbose */ 58 | int debug; /* d: debug verbose */ 59 | int allow_arrays; 60 | int self_checks; 61 | int wrong_key_handling; 62 | int allow_holes; 63 | int multi_structure; 64 | int pointer_notation; 65 | int quit_rule; 66 | int array_unsorted; 67 | int integer_conversion; 68 | int array_decision; 69 | int use_null; 70 | int wrong_types; 71 | int array_zip; 72 | int recurse; 73 | 74 | /* traversion control */ 75 | size_t total_len; /* running counter of needed byte size */ 76 | size_t total_esc_bytes; /* running counter of bytes needed for escape sequences */ 77 | size_t total_esc_chars; /* running counter of characters that need escape sequences */ 78 | size_t total_parts; /* running counter of use parts buffers */ 79 | size_t had_parts; /* saved counter of use parts buffers */ 80 | size_t depth; /* running counter of table recursion depth vs dead locks */ 81 | size_t total_waste; /* running counter of unused allocated parts' buffers */ 82 | size_t max_size; /* running counter of biggest met value buffer size */ 83 | size_t lucky_time; /* running counter of processor time used for first part */ 84 | size_t count_time; /* running counter of processor time used for pre-count */ 85 | size_t coll_time; /* running counter of processor time used for collapse */ 86 | size_t total_time; /* running counter of processor time used in total */ 87 | 88 | /* strategy control */ 89 | int strategy; /* current strategy - 1: buffer, 2: pre-count, 3: precise */ 90 | int tried_sg1; /* strategy #1 has been tried and abandoned */ 91 | char *error; /* user error message */ 92 | char *severe; /* dev error message */ 93 | int final_copy; /* there was a final copy for collapsing */ 94 | 95 | /* parts_list */ 96 | // string_parts 97 | string_part *parts_list_first; 98 | string_part *parts_list_last; 99 | 100 | /* helper, since at least hasty_int_size is a formula */ 101 | int hasty_int_size; 102 | int hasty_float_size; 103 | }; 104 | 105 | typedef struct insp_ctrl_struct insp_ctrl; 106 | 107 | #endif 108 | 109 | -------------------------------------------------------------------------------- /src/fleece.c: -------------------------------------------------------------------------------- 1 | /**-------------------------------------------------------------------------*** 2 | *** Package : Fleece - fast Lua to JSON conversion *** 3 | *** File : fleece.c *** 4 | *** Description : main source file: includes all others *** 5 | *** Version : 0.3.1 / alpha *** 6 | *** Requirement : Lua 5.1.4 - 5.1.4-2 *** 7 | *** Copyright : 2011 Henning Diedrich, Eonblast Corporation *** 8 | *** Author : H. Diedrich *** 9 | *** License : see file LICENSE *** 10 | *** Created : Feb 2011 *** 11 | *** Changed : 18 Mar 2011 *** 12 | ***-------------------------------------------------------------------------*** 13 | *** *** 14 | *** Fleece is optimized for the fastest Lua to JSON conversion and beats *** 15 | *** other JSON implementations by around 10 times, native Lua up to 100x. *** 16 | *** *** 17 | ***-------------------------------------------------------------------------*** 18 | *** *** 19 | *** This file includes all other src/fleece-*.c files. *** 20 | *** *** 21 | *** Header files are not cleaned up. *** 22 | *** *** 23 | ***-------------------------------------------------------------------------**/ 24 | 25 | /* C headers --------------------------------------------------------------- */ 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | /* Lua headers (in include/) ----------------------------------------------- */ 33 | #include "lobject.h" 34 | #include "ltable.h" 35 | #include "lua.h" 36 | #include "lauxlib.h" 37 | #include "lualib.h" 38 | #include "ldebug.h" 39 | #include "ldo.h" 40 | #include "lgc.h" 41 | #include "lmem.h" 42 | #include "lstate.h" 43 | 44 | /* Fleece headers (in src/) ------------------------------------------------ */ 45 | #include "fleece-intern.h" 46 | #include "fleece.h" 47 | 48 | /* Fleece sources (in src/) ------------------------------------------------ */ 49 | #include "fleece-buffers.c" 50 | #include "fleece-copy.c" 51 | #include "fleece-ctrl.c" 52 | #include "fleece-numbers.c" 53 | #include "fleece-size.c" 54 | #include "fleece-stringify.c" 55 | #include "fleece-lua.c" 56 | 57 | -------------------------------------------------------------------------------- /src/fleece.h: -------------------------------------------------------------------------------- 1 | /**-------------------------------------------------------------------------*** 2 | *** Packages : Fleece - fast Lua to JSON module *** 3 | *** File : fleece.h *** 4 | *** Description : main outward header file - of not much use really as is *** 5 | *** Version : 0.3.1 / alpha *** 6 | *** Requirement : Lua 5.1.4 - 5.1.4-2 *** 7 | *** Copyright : 2011 Henning Diedrich, Eonblast Corporation *** 8 | *** Author : H. Diedrich *** 9 | *** License : see file LICENSE *** 10 | *** Created : Feb 2011 *** 11 | *** Changed : Feb 2011 *** 12 | ***-------------------------------------------------------------------------*** 13 | *** *** 14 | *** Fleece is optimized for the fastest Lua to JSON conversion and beats *** 15 | *** other JSON implementations by around 10 times, native Lua up to 100x. *** 16 | *** *** 17 | ***-------------------------------------------------------------------------*** 18 | *** *** 19 | *** Header files not cleaned up. *** 20 | *** *** 21 | ***-------------------------------------------------------------------------**/ 22 | 23 | #include "lua.h" 24 | 25 | #define LUA_FLEECE "fleece" 26 | 27 | LUALIB_API int (luaopen_fleece) (lua_State *L); 28 | 29 | -------------------------------------------------------------------------------- /test/bench-fleece-10k.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench-fleece.lua --- 4 | --- Description : Fleece benchmarks: random tables, speed clocked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 18 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- This runs only Fleece. Other examples make comparisons to: --- 14 | --- --- 15 | --- JSON4 0.9.50: (aka JSON4Lua, JSON for Lua) http://json.luaforge.net/ --- 16 | --- LuaJSON 1.1: (aka luajsonlib) http://luaforge.net/projects/luajsonlib/ --- 17 | --- --- 18 | --- Use: lua test/bench-fleece.lua --- 19 | --- Or: make bench --- 20 | --- --- 21 | ------------------------------------------------------------------------------- 22 | 23 | print() 24 | print("Fleece Benchmarks (10,000 iterations)") 25 | print("=====================================") 26 | print("A couple of random tables are created and speed is clocked.") 27 | print("You should have built fleece first with 'make ', ") 28 | print("and now be in the fleece root directory.") 29 | 30 | package.cpath="src/?.so" 31 | fleece = require("fleece") 32 | 33 | function printf(...) 34 | io.write(string.format(...)) 35 | end 36 | 37 | function nanosecs_per(time, cycles) 38 | return time * 1000000000 / cycles 39 | end 40 | 41 | function microsecs_per(time, cycles) 42 | return time * 1000000 / cycles 43 | end 44 | 45 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 46 | function randstr(i) 47 | local r = {} 48 | local length = math.random(1,20) 49 | local i 50 | for i = 1, length do 51 | r[i] = abc[math.random(1, 26)] 52 | end 53 | 54 | return table.concat(r) 55 | end 56 | 57 | local t = {} 58 | local function measure(prepP, prepare, actionP, action) 59 | 60 | local items = 1000 61 | local cycles = 10000 62 | local clock = os.clock 63 | 64 | print("-------------------------------------------------------------------------") 65 | printf("i=[1..%d] in %-12s for %d cycles of %-12s\n", items, prepP, cycles, actionP) 66 | print(". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .") 67 | io.write("prep table ... ") 68 | 69 | t = {} 70 | local i = 1 71 | local size = 0 72 | while(i <= items) do prepare(i); i = i + 1 end 73 | 74 | i = 1 75 | local last = nil 76 | io.write("action ... ") 77 | local tm = clock() 78 | while(i <= cycles) do last = action(i); i = i + 1 end 79 | tm = clock() - tm 80 | if tm ~= 0 then 81 | mark = nanosecs_per(tm, cycles * items) 82 | printf("%10.0f nanosec/element => %3.0fM elements/sec \n", mark, 83 | math.floor(1000 / mark)) 84 | else 85 | printf("%dx %-12s ** sample too small, could not measure, increase 'cycles' in this script \n", cycles, actionP) 86 | end 87 | end 88 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 89 | print(" - Fleece 0.3.1") 90 | 91 | 92 | measure("t[i]=i", 93 | function(i) t[i] = i end, 94 | "fleece.json(t)", 95 | function(i) fleece.json(t) end 96 | ) 97 | 98 | measure("t['x'..i]=i", 99 | function(i) t['x'..i] = i end, 100 | "fleece.json(t)", 101 | function(i) fleece.json(t) end 102 | ) 103 | 104 | measure("t[i]=randstr(i)", 105 | function(i) t[i] = randstr(i) end, 106 | "fleece.json(t)", 107 | function(i) fleece.json(t) end 108 | ) 109 | 110 | measure("t[randstr(i)]=randstr(i)", 111 | function(i) t[randstr(i)] = randstr(i) end, 112 | "fleece.json(t)", 113 | function(i) fleece.json(t) end 114 | ) 115 | print("-------------------------------------------------------------------------") 116 | print("There are more benchmarks. Try bench2, bench3a, bench7a.") 117 | print("Run 'make' w/o parameters for details, or see test/README.md.") 118 | print("Repeat runs will differ.") 119 | -------------------------------------------------------------------------------- /test/bench-fleece.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench-fleece.lua --- 4 | --- Description : Fleece benchmarks: random tables, speed clocked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 18 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- This runs only Fleece. Other examples make comparisons to: --- 14 | --- --- 15 | --- JSON4 0.9.50: (aka JSON4Lua, JSON for Lua) http://json.luaforge.net/ --- 16 | --- LuaJSON 1.1: (aka luajsonlib) http://luaforge.net/projects/luajsonlib/ --- 17 | --- --- 18 | --- Use: lua test/bench-fleece.lua --- 19 | --- Or: make bench --- 20 | --- --- 21 | ------------------------------------------------------------------------------- 22 | 23 | print() 24 | print("Fleece Benchmarks") 25 | print("=================") 26 | print("A couple of random tables are created and speed is clocked.") 27 | print("You should have built fleece first with 'make ', ") 28 | print("and now be in the fleece root directory.") 29 | 30 | package.cpath="src/?.so" 31 | fleece = require("fleece") 32 | 33 | function printf(...) 34 | io.write(string.format(...)) 35 | end 36 | 37 | function nanosecs_per(time, cycles) 38 | return time * 1000000000 / cycles 39 | end 40 | 41 | function microsecs_per(time, cycles) 42 | return time * 1000000 / cycles 43 | end 44 | 45 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 46 | function randstr(i) 47 | local r = {} 48 | local length = math.random(1,20) 49 | local i 50 | for i = 1, length do 51 | r[i] = abc[math.random(1, 26)] 52 | end 53 | 54 | return table.concat(r) 55 | end 56 | 57 | local t = {} 58 | local function measure(prepP, prepare, actionP, action) 59 | 60 | local items = 1000 61 | local cycles = 1000 62 | local clock = os.clock 63 | 64 | print("-------------------------------------------------------------------------") 65 | printf("i=[1..%d] in %-12s for %d cycles of %-12s\n", items, prepP, cycles, actionP) 66 | print(". . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .") 67 | io.write("prep table ... ") 68 | 69 | t = {} 70 | local i = 1 71 | local size = 0 72 | while(i <= items) do prepare(i); i = i + 1 end 73 | 74 | i = 1 75 | io.write("action ... ") 76 | local tm = clock() 77 | while(i <= cycles) do action(i); i = i + 1 end 78 | tm = clock() - tm 79 | if tm ~= 0 then 80 | mark = nanosecs_per(tm, cycles * items) 81 | printf("%10.0f nanosec/element => %3.0fM elements/sec \n", mark, 82 | math.floor(1000 / mark)) 83 | else 84 | printf("%dx %-12s ** sample too small, could not measure, use bench-10k \n", cycles, actionP) 85 | end 86 | end 87 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 88 | print(" - Fleece 0.3.1") 89 | 90 | 91 | measure("t[i]=i", 92 | function(i) t[i] = i end, 93 | "fleece.json(t)", 94 | function(i) fleece.json(t) end 95 | ) 96 | 97 | measure("t['x'..i]=i", 98 | function(i) t['x'..i] = i end, 99 | "fleece.json(t)", 100 | function(i) fleece.json(t) end 101 | ) 102 | 103 | measure("t[i]=randstr(i)", 104 | function(i) t[i] = randstr(i) end, 105 | "fleece.json(t)", 106 | function(i) fleece.json(t) end 107 | ) 108 | 109 | measure("t[randstr(i)]=randstr(i)", 110 | function(i) t[randstr(i)] = randstr(i) end, 111 | "fleece.json(t)", 112 | function(i) fleece.json(t) end 113 | ) 114 | print("-------------------------------------------------------------------------") 115 | print("There are more benchmarks. Try bench2, bench3a, bench7a.") 116 | print("Run 'make' w/o parameters for details, or see test/README.md.") 117 | print("Repeat runs will differ. For finer grained figures run bench-10k. ") 118 | -------------------------------------------------------------------------------- /test/bench-json4.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench-json4.lua --- 4 | --- Description : JSON4 test: random tables, clocked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : 02 Mar 2011 --- 10 | --- Changed : 02 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- This tests JSON4 only --- 14 | --- --- 15 | --- JSON4 0.9.50: (aka JSON4Lua, JSON for Lua) http://json.luaforge.net/ --- 16 | --- --- 17 | --- Use: lua test/bench-json4.lua --- 18 | --- --- 19 | ------------------------------------------------------------------------------- 20 | 21 | print("Fleece Benchmarks: JSON4 only (native Lua JSON conversion)") 22 | print("============================================================") 23 | print("A couple of random tables are created and speed is clocked.") 24 | print("You should start this from the fleece root directory.") 25 | print("JSON4 is in etc/json4.") 26 | 27 | ELEMENTS = 1000 28 | CYCLES = 100 29 | 30 | package.path="etc/json4/?.lua" 31 | json4 = require("json") 32 | 33 | sep = "------------------------------------------------------------------------------" 34 | 35 | printcol = 0 36 | function printf(...) 37 | s = string.format(...) 38 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 39 | io.write(s) 40 | end 41 | 42 | function tab(x) 43 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 44 | end 45 | 46 | function nanosecs_per(time, per) 47 | return time * 1000000000 / per 48 | end 49 | 50 | function microsecs_per(time, per) 51 | return time * 1000000 / per 52 | end 53 | 54 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 55 | function randstr(i) 56 | local r = {} 57 | local length = math.random(1,20) 58 | local i 59 | for i = 1, length do 60 | r[i] = abc[math.random(1, 26)] 61 | end 62 | 63 | return table.concat(r) 64 | end 65 | 66 | local t = {} 67 | local function measure(prepP, prepare, actionP, action, printPrepP) 68 | 69 | local items = ELEMENTS 70 | local cycles = CYCLES 71 | local clock = os.clock 72 | 73 | if(printPrepP) then 74 | printf("%d elements in %-25s\n", items, prepP) 75 | end 76 | printf("%dx %-12s ", cycles, actionP) 77 | 78 | t = {} 79 | local i = 1 80 | local size = 0 81 | while(i <= items) do prepare(i); i = i + 1 end 82 | 83 | i = 1 84 | local last = nil 85 | local tm = clock() 86 | while(i <= cycles) do last = action(i); i = i + 1 end 87 | tm = clock() - tm 88 | if tm ~= 0 then 89 | mspc= nanosecs_per(tm, cycles * items) 90 | tab(27) 91 | printf("%10.0fns/element ", mspc) 92 | else 93 | mspc = nil 94 | printf("%dx %-12s sample too small, could not measure ", cycles, actionP) 95 | end 96 | 97 | return mspc, last 98 | end 99 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 100 | print(" - Fleece 0.3.1") 101 | 102 | 103 | local function measure1(prepP, prepare, p1, a1) 104 | 105 | print(sep) 106 | 107 | first, r1 = measure(prepP, prepare, p1, a1, true) 108 | printf(" %.20s.. \n", r1) 109 | 110 | end 111 | 112 | measure1("t[i]=i", 113 | function(i) t[i] = i end, 114 | "json4.encode(t)", 115 | function(i) return json4.encode(t) end 116 | ) 117 | 118 | measure1("t['x'..i]=i", 119 | function(i) t['x'..i] = i end, 120 | "json4.encode(t)", 121 | function(i) return json4.encode(t) end 122 | ) 123 | 124 | measure1("t[i]=randstr(i)", 125 | function(i) t[i] = randstr(i) end, 126 | "json4.encode(t)", 127 | function(i) return json4.encode(t) end 128 | ) 129 | 130 | measure1("t[randstr(i)]=randstr(i)", 131 | function(i) t[randstr(i)] = randstr(i) end, 132 | "json4.encode(t)", 133 | function(i) return json4.encode(t) end 134 | ) 135 | 136 | print(sep) 137 | 138 | -------------------------------------------------------------------------------- /test/bench-luajson.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench-luajson.lua --- 4 | --- Description : luajson test: random tables, speed cklocked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : 02 Mar 2011 --- 10 | --- Changed : 02 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- This tests only the luajson C library (not Fleece): --- 14 | --- --- 15 | --- LuaJSON 1.1: (aka luajsonlib) http://luaforge.net/projects/luajsonlib/ --- 16 | --- --- 17 | --- Use: lua test/bench-luajson.lua --- 18 | --- --- 19 | ------------------------------------------------------------------------------- 20 | 21 | print("Fleece Benchmarks: LuaJSON C lib") 22 | print("=========================================================") 23 | print("A couple of random tables are created and speed is clocked.") 24 | print("You should have built luajson.so with 'make -test,") 25 | print("and now be in the fleece root directory.") 26 | print("LUAJSON CAN CRASH THIS TEST. SEE BENCH3a & BENCH3b.") 27 | 28 | ELEMENTS = 1000 29 | CYCLES = 100 30 | 31 | package.cpath="etc/luajson/?.so" 32 | luajson = require("luajson") 33 | 34 | -- luajson stuff 35 | local base = _G 36 | local json = luajson 37 | json.null = {_mt = {__tostring = function () return "null" end, __call = function () return "null" end}} 38 | base.setmetatable(json.null, json.null._mt) 39 | 40 | 41 | sep = "---------------------------------------------------------------------------------" 42 | 43 | printcol = 0 44 | function printf(...) 45 | s = string.format(...) 46 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 47 | io.write(s) 48 | end 49 | 50 | function tab(x) 51 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 52 | end 53 | 54 | function nanosecs_per(time, per) 55 | return time * 1000000000 / per 56 | end 57 | 58 | function microsecs_per(time, per) 59 | return time * 1000000 / per 60 | end 61 | 62 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 63 | function randstr(i) 64 | local r = {} 65 | local length = math.random(1,20) 66 | local i 67 | for i = 1, length do 68 | r[i] = abc[math.random(1, 26)] 69 | end 70 | 71 | return table.concat(r) 72 | end 73 | 74 | local t = {} 75 | local function measure(prepP, prepare, actionP, action, printPrepP) 76 | 77 | local items = ELEMENTS 78 | local cycles = CYCLES 79 | local clock = os.clock 80 | 81 | if(printPrepP) then 82 | printf("%d elements in %-25s\n", items, prepP) 83 | end 84 | printf("%dx %-12s ", cycles, actionP) 85 | 86 | t = {} 87 | local i = 1 88 | local size = 0 89 | while(i <= items) do prepare(i); i = i + 1 end 90 | 91 | i = 1 92 | local last = nil 93 | local tm = clock() 94 | while(i <= cycles) do last = action(i); i = i + 1 end 95 | tm = clock() - tm 96 | if tm ~= 0 then 97 | mspc= nanosecs_per(tm, cycles * items) 98 | tab(27) 99 | printf("%10.0fns/element ", mspc) 100 | else 101 | mspc = nil 102 | printf("%dx %-12s sample too small, could not measure ", cycles, actionP) 103 | end 104 | 105 | return mspc, last 106 | end 107 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 108 | print(" - Fleece 0.3.1") 109 | 110 | 111 | local function measure1(prepP, prepare, prompt1, action1) 112 | 113 | print(sep) 114 | 115 | first, r1 = measure(prepP, prepare, prompt1, action1, true) 116 | printf(" %.20s.. \n", r1) 117 | 118 | end 119 | 120 | 121 | measure1("t[i]=i", 122 | function(i) t[i] = i end, 123 | "luajson.stringify(t)", 124 | function(i) return luajson.stringify(t) end 125 | ) 126 | 127 | measure1("t['x'..i]=i", 128 | function(i) t['x'..i] = i end, 129 | "luajson.stringify(t)", 130 | function(i) return luajson.stringify(t) end 131 | ) 132 | 133 | measure1("t[i]=randstr(i)", 134 | function(i) t[i] = randstr(i) end, 135 | "luajson.stringify(t)", 136 | function(i) return luajson.stringify(t) end 137 | ) 138 | 139 | measure1("t[randstr(i)]=randstr(i)", 140 | function(i) t[randstr(i)] = randstr(i) end, 141 | "luajson.stringify(t)", 142 | function(i) return luajson.stringify(t) end 143 | ) 144 | print(sep) 145 | 146 | -------------------------------------------------------------------------------- /test/bench-yajl.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench-yajl.lua --- 4 | --- Description : Lua-Yajl benchmarks: random tables, speed clocked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 02 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- This runs Lua-Yajl, a streaming C implementation of JSON. --- 14 | --- --- 15 | --- Yajl 1.0.9: http://lloyd.github.com/yajl/ --- 16 | --- Lua-yajl Oct '10: http://github.com/brimworks/lua-yajl/ --- 17 | --- --- 18 | --- Building with Yajl is not always fun, you can safely skip this. --- 19 | --- --- 20 | --- Use: lua test/bench1-yajl.lua --- 21 | --- --- 22 | ------------------------------------------------------------------------------- 23 | 24 | print("Fleece Benchmarks: Lua-Yajl only") 25 | print("=========================================================") 26 | print("A couple of random tables are created and speed is clocked.") 27 | print("Run this in the main fleece dir and have yajl built into etc/yajl.") 28 | print("There is no support in fleece to build yajl or lua-yajl,") 29 | print("you need to get and build them manually.") 30 | print("Yajl 1.0.9: http://lloyd.github.com/yajl/") 31 | print("Lua-yajl Oct '10: http://github.com/brimworks/lua-yajl/") 32 | 33 | ELEMENTS = 1000 34 | CYCLES = 100 35 | 36 | -- luayajl stuff 37 | package.cpath = "etc/yajl/?.so" 38 | local yajl = require("yajl") 39 | 40 | sep = "---------------------------------------------------------------------------------" 41 | 42 | printcol = 0 43 | function printf(...) 44 | s = string.format(...) 45 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 46 | io.write(s) 47 | end 48 | 49 | function tab(x) 50 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 51 | end 52 | 53 | function nanosecs_per(time, per) 54 | return time * 1000000000 / per 55 | end 56 | 57 | function microsecs_per(time, per) 58 | return time * 1000000 / per 59 | end 60 | 61 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 62 | function randstr(i) 63 | local r = {} 64 | local length = math.random(1,20) 65 | local i 66 | for i = 1, length do 67 | r[i] = abc[math.random(1, 26)] 68 | end 69 | 70 | return table.concat(r) 71 | end 72 | 73 | local t = {} 74 | local function measure(prepP, prepare, actionP, action, printPrepP) 75 | 76 | local items = ELEMENTS 77 | local cycles = CYCLES 78 | local clock = os.clock 79 | 80 | if(printPrepP) then 81 | printf("%d elements in %-25s\n", items, prepP) 82 | end 83 | printf("%dx %-12s ", cycles, actionP) 84 | 85 | t = {} 86 | local i = 1 87 | local size = 0 88 | while(i <= items) do prepare(i); i = i + 1 end 89 | 90 | i = 1 91 | local last = nil 92 | local tm = clock() 93 | while(i <= cycles) do last = action(i); i = i + 1 end 94 | tm = clock() - tm 95 | if tm ~= 0 then 96 | mspc= nanosecs_per(tm, cycles * items) 97 | tab(27) 98 | printf("%10.0fns/element ", mspc) 99 | else 100 | mspc = nil 101 | printf("%dx %-12s sample too small, could not measure ", cycles, actionP) 102 | end 103 | 104 | return mspc, last 105 | end 106 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 107 | print(" - Fleece 0.3.1") 108 | 109 | 110 | local function measure1(prepP, prepare, p1, a1) 111 | 112 | print(sep) 113 | 114 | first, r1 = measure(prepP, prepare, p1, a1, true) 115 | printf(" %.20s.. \n", r1) 116 | 117 | end 118 | 119 | measure1("t[i]=i", 120 | function(i) t[i] = i end, 121 | "yajl.to_string(t)", 122 | function(i) return yajl.to_string(t) end 123 | ) 124 | 125 | -------------------------------------------------------------------------------- /test/bench.lua: -------------------------------------------------------------------------------- 1 | print("Use test/bench-fleece.lua. See test/README.md for a list of scripts.") 2 | -------------------------------------------------------------------------------- /test/bench2-1k.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench2.lua --- 4 | --- Description : Fleece vs JSON4 benchmarks: random tables, speed clocked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 18 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- This runs Fleece vs JSON4, a native Lua implementation of JSON. --- 14 | --- --- 15 | --- JSON4 0.9.50: (aka JSON4Lua, JSON for Lua) http://json.luaforge.net/ --- 16 | --- --- 17 | --- Use: lua test/bench2.lua --- 18 | --- Or: make bench2 --- 19 | --- --- 20 | ------------------------------------------------------------------------------- 21 | 22 | print("Fleece Benchmarks vs Json4, a native Lua JSON implementation") 23 | print("============================================================") 24 | print("A couple of random tables are created and speed is clocked.") 25 | print("You should have built fleece first with 'make ' ") 26 | print("and should now be in the fleece root directory.") 27 | 28 | ELEMENTS = 1000 29 | CYCLES = 1000 30 | 31 | package.cpath="src/?.so" 32 | fleece = require("fleece") 33 | package.path="etc/json4/?.lua" 34 | json4 = require("json") 35 | 36 | sep = "---------------------------------------------------------------------------------" 37 | 38 | printcol = 0 39 | function printf(...) 40 | s = string.format(...) 41 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 42 | io.write(s) 43 | end 44 | 45 | function tab(x) 46 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 47 | end 48 | 49 | function nanosecs_per(time, per) 50 | return time * 1000000000 / per 51 | end 52 | 53 | function microsecs_per(time, per) 54 | return time * 1000000 / per 55 | end 56 | 57 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 58 | function randstr(i) 59 | local r = {} 60 | local length = math.random(1,20) 61 | local i 62 | for i = 1, length do 63 | r[i] = abc[math.random(1, 26)] 64 | end 65 | 66 | return table.concat(r) 67 | end 68 | 69 | function prcstr(part, base) 70 | if secnd == 0 then return 0 end 71 | x = math.floor(part / base * 100) 72 | if(x <= 2) then 73 | x = math.floor(part / base * 1000) / 10 74 | end 75 | return x 76 | end 77 | 78 | local t = {} 79 | local function measure(prepP, prepare, actionP, action, printPrepP) 80 | 81 | local items = ELEMENTS 82 | local cycles = CYCLES 83 | local clock = os.clock 84 | 85 | if(printPrepP) then 86 | printf("%d elements in %-25s\n", items, prepP) 87 | end 88 | printf("%dx %-12s ", cycles, actionP) 89 | 90 | t = {} 91 | local i = 1 92 | local size = 0 93 | while(i <= items) do prepare(i); i = i + 1 end 94 | 95 | i = 1 96 | local last = nil 97 | local tm = clock() 98 | while(i <= cycles) do last = action(i); i = i + 1 end 99 | tm = clock() - tm 100 | if tm ~= 0 then 101 | mspc= nanosecs_per(tm, cycles * items) 102 | tab(27) 103 | printf("%10.0fns/element ", mspc) 104 | else 105 | mspc = nil 106 | printf("%dx %-12s sample too small, could not measure ", cycles, actionP) 107 | end 108 | 109 | return mspc, last 110 | end 111 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 112 | print(" - Fleece 0.3.1") 113 | 114 | local function measure3(prepP, prepare, prompt1, action1, prompt2, action2, prompt3, action3) 115 | 116 | print(sep) 117 | 118 | secnd, r2 = measure(prepP, prepare, prompt2, action2, true) 119 | printf(" %.20s.. \n", r2) 120 | third, r3 = measure(prepP, prepare, prompt3, action3) 121 | 122 | if(secnd and third) then prc = prcstr(third, secnd) 123 | printf("%3g%% %.20s.. \n", prc, r3) 124 | else 125 | prc = "-" 126 | printf(" %.20s.. \n", r3) 127 | end 128 | end 129 | 130 | 131 | measure3("t[i]=i", 132 | function(i) t[i] = i end, 133 | "luajson.stringify(t)", 134 | function(i) return luajson.stringify(t) end, 135 | "json4.encode(t)", 136 | function(i) return json4.encode(t) end, 137 | "fleece.json(t)", 138 | function(i) return fleece.json(t) end 139 | ) 140 | 141 | measure3("t['x'..i]=i", 142 | function(i) t['x'..i] = i end, 143 | "luajson.stringify(t)", 144 | function(i) return luajson.stringify(t) end, 145 | "json4.encode(t)", 146 | function(i) return json4.encode(t) end, 147 | "fleece.json(t)", 148 | function(i) return fleece.json(t) end 149 | ) 150 | 151 | measure3("t[i]=randstr(i)", 152 | function(i) t[i] = randstr(i) end, 153 | "luajson.stringify(t)", 154 | function(i) return luajson.stringify(t) end, 155 | "json4.encode(t)", 156 | function(i) return json4.encode(t) end, 157 | "fleece.json(t)", 158 | function(i) return fleece.json(t) end 159 | ) 160 | 161 | measure3("t[randstr(i)]=randstr(i)", 162 | function(i) t[randstr(i)] = randstr(i) end, 163 | "luajson.stringify(t)", 164 | function(i) return luajson.stringify(t) end, 165 | "json4.encode(t)", 166 | function(i) return json4.encode(t) end, 167 | "fleece.json(t)", 168 | function(i) return fleece.json(t) end 169 | ) 170 | print(sep) 171 | 172 | print("Note that fleece may list associative arrays in different order.") 173 | -------------------------------------------------------------------------------- /test/bench2.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench2.lua --- 4 | --- Description : Fleece vs JSON4 benchmarks: random tables, speed clocked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 18 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- This runs Fleece vs JSON4, a native Lua implementation of JSON. --- 14 | --- --- 15 | --- JSON4 0.9.50: (aka JSON4Lua, JSON for Lua) http://json.luaforge.net/ --- 16 | --- --- 17 | --- Use: lua test/bench2.lua --- 18 | --- Or: make bench2 --- 19 | --- --- 20 | ------------------------------------------------------------------------------- 21 | 22 | print("Fleece Benchmarks vs Json4, a native Lua JSON implementation") 23 | print("============================================================") 24 | print("A couple of random tables are created and speed is clocked.") 25 | print("You should have built fleece first with 'make ' ") 26 | print("and should now be in the fleece root directory.") 27 | 28 | ELEMENTS = 1000 29 | CYCLES = 100 30 | 31 | package.cpath="src/?.so" 32 | fleece = require("fleece") 33 | package.path="etc/json4/?.lua" 34 | json4 = require("json") 35 | 36 | sep = "---------------------------------------------------------------------------------" 37 | 38 | printcol = 0 39 | function printf(...) 40 | s = string.format(...) 41 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 42 | io.write(s) 43 | end 44 | 45 | function tab(x) 46 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 47 | end 48 | 49 | function nanosecs_per(time, per) 50 | return time * 1000000000 / per 51 | end 52 | 53 | function microsecs_per(time, per) 54 | return time * 1000000 / per 55 | end 56 | 57 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 58 | function randstr(i) 59 | local r = {} 60 | local length = math.random(1,20) 61 | local i 62 | for i = 1, length do 63 | r[i] = abc[math.random(1, 26)] 64 | end 65 | 66 | return table.concat(r) 67 | end 68 | 69 | function prcstr(part, base) 70 | if secnd == 0 then return 0 end 71 | x = math.floor(part / base * 100) 72 | if(x <= 2) then 73 | x = math.floor(part / base * 1000) / 10 74 | end 75 | return x 76 | end 77 | 78 | local t = {} 79 | local function measure(prepP, prepare, actionP, action, printPrepP) 80 | 81 | local items = ELEMENTS 82 | local cycles = CYCLES 83 | local clock = os.clock 84 | 85 | if(printPrepP) then 86 | printf("%d elements in %-25s\n", items, prepP) 87 | end 88 | printf("%dx %-12s ", cycles, actionP) 89 | 90 | t = {} 91 | local i = 1 92 | local size = 0 93 | while(i <= items) do prepare(i); i = i + 1 end 94 | 95 | i = 1 96 | local last = nil 97 | local tm = clock() 98 | while(i <= cycles) do last = action(i); i = i + 1 end 99 | tm = clock() - tm 100 | if tm ~= 0 then 101 | mspc= nanosecs_per(tm, cycles * items) 102 | tab(27) 103 | printf("%10.0fns/element ", mspc) 104 | else 105 | mspc = nil 106 | printf("%dx %-12s ** sample too small, could not measure, use bench2-1k ** ", cycles, actionP) 107 | end 108 | 109 | return mspc, last 110 | end 111 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 112 | print(" - Fleece 0.3.1") 113 | 114 | local function measure3(prepP, prepare, prompt1, action1, prompt2, action2, prompt3, action3) 115 | 116 | print(sep) 117 | 118 | secnd, r2 = measure(prepP, prepare, prompt2, action2, true) 119 | printf(" %.20s.. \n", r2) 120 | third, r3 = measure(prepP, prepare, prompt3, action3) 121 | 122 | if(secnd and third) then prc = prcstr(third, secnd) 123 | printf("%3g%% %.20s.. \n", prc, r3) 124 | else 125 | prc = "-" 126 | printf(" %.20s.. \n", r3) 127 | end 128 | end 129 | 130 | 131 | measure3("t[i]=i", 132 | function(i) t[i] = i end, 133 | "luajson.stringify(t)", 134 | function(i) return luajson.stringify(t) end, 135 | "json4.encode(t)", 136 | function(i) return json4.encode(t) end, 137 | "fleece.json(t)", 138 | function(i) return fleece.json(t) end 139 | ) 140 | 141 | measure3("t['x'..i]=i", 142 | function(i) t['x'..i] = i end, 143 | "luajson.stringify(t)", 144 | function(i) return luajson.stringify(t) end, 145 | "json4.encode(t)", 146 | function(i) return json4.encode(t) end, 147 | "fleece.json(t)", 148 | function(i) return fleece.json(t) end 149 | ) 150 | 151 | measure3("t[i]=randstr(i)", 152 | function(i) t[i] = randstr(i) end, 153 | "luajson.stringify(t)", 154 | function(i) return luajson.stringify(t) end, 155 | "json4.encode(t)", 156 | function(i) return json4.encode(t) end, 157 | "fleece.json(t)", 158 | function(i) return fleece.json(t) end 159 | ) 160 | 161 | measure3("t[randstr(i)]=randstr(i)", 162 | function(i) t[randstr(i)] = randstr(i) end, 163 | "luajson.stringify(t)", 164 | function(i) return luajson.stringify(t) end, 165 | "json4.encode(t)", 166 | function(i) return json4.encode(t) end, 167 | "fleece.json(t)", 168 | function(i) return fleece.json(t) end 169 | ) 170 | print(sep) 171 | 172 | print("Note that fleece may list associative arrays in different order.") 173 | -------------------------------------------------------------------------------- /test/bench3-1k.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench3.lua --- 4 | --- Description : Fleece vs JSON4 and luajson: random tables, speedcked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 02 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- Fleece is optimized for the fastest Lua to JSON conversion and beats --- 14 | --- other JSON implementations by around 10 times, native Lua up to 100x. --- 15 | --- Please let me know about the speed you are observing. --- 16 | --- hd2010@eonblast.com --- 17 | --- --- 18 | --- This compares Fleece to a native Lua implementation and a C library: --- 19 | --- --- 20 | --- JSON4 0.9.50: (aka JSON4Lua, JSON for Lua) http://json.luaforge.net/ --- 21 | --- LuaJSON 1.1: (aka luajsonlib) http://luaforge.net/projects/luajsonlib/ --- 22 | --- --- 23 | --- Use: lua test/bench3.lua --- 24 | --- --- 25 | ------------------------------------------------------------------------------- 26 | 27 | print("Fleece Benchmarks vs Json4 / native Lua and LuaJSON C lib") 28 | print("=========================================================") 29 | print("A couple of random tables are created and speed is clocked.") 30 | print("You should have built fleece first with 'make ', ") 31 | print("and also built luajson.so with 'make -test,") 32 | print("and now be in the fleece root directory.") 33 | print("IF THIS TEST CRASHES, TRY BENCH3a & BENCH3b TO SEE WHO DOES.") 34 | 35 | ELEMENTS = 1000 36 | CYCLES = 1000 37 | 38 | package.cpath="src/?.so" 39 | fleece = require("fleece") 40 | package.path="etc/json4/?.lua" 41 | json4 = require("json") 42 | package.cpath="etc/luajson/?.so" 43 | luajson = require("luajson") 44 | 45 | -- luajson stuff 46 | local base = _G 47 | local json = luajson 48 | json.null = {_mt = {__tostring = function () return "null" end, __call = function () return "null" end}} 49 | base.setmetatable(json.null, json.null._mt) 50 | 51 | 52 | sep = "---------------------------------------------------------------------------------" 53 | 54 | printcol = 0 55 | function printf(...) 56 | s = string.format(...) 57 | if(s:find("\n") or s:len() == 0) then printcol = 0 else printcol = printcol + s:len() end 58 | io.write(s) 59 | end 60 | 61 | function tab(x) 62 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 63 | end 64 | 65 | function nanosecs_per(time, per) 66 | return time * 1000000000 / per 67 | end 68 | 69 | function microsecs_per(time, per) 70 | return time * 1000000 / per 71 | end 72 | 73 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 74 | function randstr(i) 75 | local r = {} 76 | local length = math.random(1,20) 77 | local i 78 | for i = 1, length do 79 | r[i] = abc[math.random(1, 26)] 80 | end 81 | 82 | return table.concat(r) 83 | end 84 | 85 | 86 | function prcstr(part, base) 87 | if base == 0 or base == nil or part == nil then return 0 end 88 | x = math.floor(part / base * 100) 89 | if(x <= 2) then 90 | x = math.floor(part / base * 1000) / 10 91 | end 92 | return x 93 | end 94 | 95 | local t = {} 96 | local function measure(prepP, prepare, actionP, action, printPrepP) 97 | 98 | local items = ELEMENTS 99 | local cycles = CYCLES 100 | local clock = os.clock 101 | 102 | if(printPrepP) then 103 | printf("%d elements in %-25s\n", items, prepP) 104 | end 105 | printf("%dx %-12s ", cycles, actionP) 106 | 107 | t = {} 108 | local i = 1 109 | local size = 0 110 | while(i <= items) do prepare(i); i = i + 1 end 111 | 112 | i = 1 113 | local last = nil 114 | local tm = clock() 115 | while(i <= cycles) do last = action(i); i = i + 1 end 116 | tm = clock() - tm 117 | if tm ~= 0 then 118 | mspc= nanosecs_per(tm, cycles * items) 119 | tab(27) 120 | printf("%10.0fns/element ", mspc) 121 | else 122 | mspc = nil 123 | printf("%dx %-12s ** sample too small, could not measure, increase CYCLES in the source of this test ** ", cycles, actionP) 124 | end 125 | 126 | return mspc, last 127 | end 128 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 129 | print(" - Fleece 0.3.1") 130 | 131 | 132 | local function measure3(prepP, prepare, prompt1, action1, prompt2, action2, prompt3, action3) 133 | 134 | print(sep) 135 | 136 | first, r1 = measure(prepP, prepare, prompt1, action1, true) 137 | printf(" %.20s.. \n", r1) 138 | secnd, r2 = measure(prepP, prepare, prompt2, action2) 139 | printf(" %.20s.. \n", r2) 140 | third, r3 = measure(prepP, prepare, prompt3, action3) 141 | 142 | prc = prcstr(third, first) 143 | printf("%3g%%, ", prc) 144 | prc = prcstr(third, secnd) 145 | printf("%3g%% %.20s.. \n", prc, r3) 146 | 147 | end 148 | 149 | 150 | measure3("t[i]=i", 151 | function(i) t[i] = i end, 152 | "luajson.stringify(t)", 153 | function(i) return luajson.stringify(t) end, 154 | "json4.encode(t)", 155 | function(i) return json4.encode(t) end, 156 | "fleece.json(t)", 157 | function(i) return fleece.json(t) end 158 | ) 159 | 160 | measure3("t['x'..i]=i", 161 | function(i) t['x'..i] = i end, 162 | "luajson.stringify(t)", 163 | function(i) return luajson.stringify(t) end, 164 | "json4.encode(t)", 165 | function(i) return json4.encode(t) end, 166 | "fleece.json(t)", 167 | function(i) return fleece.json(t) end 168 | ) 169 | 170 | measure3("t[i]=randstr(i)", 171 | function(i) t[i] = randstr(i) end, 172 | "luajson.stringify(t)", 173 | function(i) return luajson.stringify(t) end, 174 | "json4.encode(t)", 175 | function(i) return json4.encode(t) end, 176 | "fleece.json(t)", 177 | function(i) return fleece.json(t) end 178 | ) 179 | 180 | measure3("t[randstr(i)]=randstr(i)", 181 | function(i) t[randstr(i)] = randstr(i) end, 182 | "luajson.stringify(t)", 183 | function(i) return luajson.stringify(t) end, 184 | "json4.encode(t)", 185 | function(i) return json4.encode(t) end, 186 | "fleece.json(t)", 187 | function(i) return fleece.json(t) end 188 | ) 189 | print(sep) 190 | 191 | print("Note that fleece may list associative arrays in different order.") 192 | 193 | -------------------------------------------------------------------------------- /test/bench3.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench3.lua --- 4 | --- Description : Fleece vs JSON4 and luajson: random tables, speedcked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 02 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- Fleece is optimized for the fastest Lua to JSON conversion and beats --- 14 | --- other JSON implementations by around 10 times, native Lua up to 100x. --- 15 | --- Please let me know about the speed you are observing. --- 16 | --- hd2010@eonblast.com --- 17 | --- --- 18 | --- This compares Fleece to a native Lua implementation and a C library: --- 19 | --- --- 20 | --- JSON4 0.9.50: (aka JSON4Lua, JSON for Lua) http://json.luaforge.net/ --- 21 | --- LuaJSON 1.1: (aka luajsonlib) http://luaforge.net/projects/luajsonlib/ --- 22 | --- --- 23 | --- Use: lua test/bench3.lua --- 24 | --- --- 25 | ------------------------------------------------------------------------------- 26 | 27 | print("Fleece Benchmarks vs Json4 / native Lua and LuaJSON C lib") 28 | print("=========================================================") 29 | print("A couple of random tables are created and speed is clocked.") 30 | print("You should have built fleece first with 'make ', ") 31 | print("and also built luajson.so with 'make -test,") 32 | print("and now be in the fleece root directory.") 33 | print("IF THIS TEST CRASHES, TRY BENCH3a & BENCH3b TO SEE WHO DOES.") 34 | 35 | ELEMENTS = 1000 36 | CYCLES = 100 37 | 38 | package.cpath="src/?.so" 39 | fleece = require("fleece") 40 | package.path="etc/json4/?.lua" 41 | json4 = require("json") 42 | package.cpath="etc/luajson/?.so" 43 | luajson = require("luajson") 44 | 45 | -- luajson stuff 46 | local base = _G 47 | local json = luajson 48 | json.null = {_mt = {__tostring = function () return "null" end, __call = function () return "null" end}} 49 | base.setmetatable(json.null, json.null._mt) 50 | 51 | 52 | sep = "---------------------------------------------------------------------------------" 53 | 54 | printcol = 0 55 | function printf(...) 56 | s = string.format(...) 57 | if(s:find("\n") or s:len() == 0) then printcol = 0 else printcol = printcol + s:len() end 58 | io.write(s) 59 | end 60 | 61 | function tab(x) 62 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 63 | end 64 | 65 | function nanosecs_per(time, per) 66 | return time * 1000000000 / per 67 | end 68 | 69 | function microsecs_per(time, per) 70 | return time * 1000000 / per 71 | end 72 | 73 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 74 | function randstr(i) 75 | local r = {} 76 | local length = math.random(1,20) 77 | local i 78 | for i = 1, length do 79 | r[i] = abc[math.random(1, 26)] 80 | end 81 | 82 | return table.concat(r) 83 | end 84 | 85 | 86 | function prcstr(part, base) 87 | if base == 0 or base == nil or part == nil then return 0 end 88 | x = math.floor(part / base * 100) 89 | if(x <= 2) then 90 | x = math.floor(part / base * 1000) / 10 91 | end 92 | return x 93 | end 94 | 95 | local t = {} 96 | local function measure(prepP, prepare, actionP, action, printPrepP) 97 | 98 | local items = ELEMENTS 99 | local cycles = CYCLES 100 | local clock = os.clock 101 | 102 | if(printPrepP) then 103 | printf("%d elements in %-25s\n", items, prepP) 104 | end 105 | printf("%dx %-12s ", cycles, actionP) 106 | 107 | t = {} 108 | local i = 1 109 | local size = 0 110 | while(i <= items) do prepare(i); i = i + 1 end 111 | 112 | i = 1 113 | local last = nil 114 | local tm = clock() 115 | while(i <= cycles) do last = action(i); i = i + 1 end 116 | tm = clock() - tm 117 | if tm ~= 0 then 118 | mspc= nanosecs_per(tm, cycles * items) 119 | tab(27) 120 | printf("%10.0fns/element ", mspc) 121 | else 122 | mspc = nil 123 | printf("%dx %-12s ** sample too small, could not measure, use bench3-1k ** ", cycles, actionP) 124 | end 125 | 126 | return mspc, last 127 | end 128 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 129 | print(" - Fleece 0.3.1") 130 | 131 | 132 | local function measure3(prepP, prepare, prompt1, action1, prompt2, action2, prompt3, action3) 133 | 134 | print(sep) 135 | 136 | first, r1 = measure(prepP, prepare, prompt1, action1, true) 137 | printf(" %.20s.. \n", r1) 138 | secnd, r2 = measure(prepP, prepare, prompt2, action2) 139 | printf(" %.20s.. \n", r2) 140 | third, r3 = measure(prepP, prepare, prompt3, action3) 141 | 142 | prc = prcstr(third, first) 143 | printf("%3g%%, ", prc) 144 | prc = prcstr(third, secnd) 145 | printf("%3g%% %.20s.. \n", prc, r3) 146 | 147 | end 148 | 149 | 150 | measure3("t[i]=i", 151 | function(i) t[i] = i end, 152 | "luajson.stringify(t)", 153 | function(i) return luajson.stringify(t) end, 154 | "json4.encode(t)", 155 | function(i) return json4.encode(t) end, 156 | "fleece.json(t)", 157 | function(i) return fleece.json(t) end 158 | ) 159 | 160 | measure3("t['x'..i]=i", 161 | function(i) t['x'..i] = i end, 162 | "luajson.stringify(t)", 163 | function(i) return luajson.stringify(t) end, 164 | "json4.encode(t)", 165 | function(i) return json4.encode(t) end, 166 | "fleece.json(t)", 167 | function(i) return fleece.json(t) end 168 | ) 169 | 170 | measure3("t[i]=randstr(i)", 171 | function(i) t[i] = randstr(i) end, 172 | "luajson.stringify(t)", 173 | function(i) return luajson.stringify(t) end, 174 | "json4.encode(t)", 175 | function(i) return json4.encode(t) end, 176 | "fleece.json(t)", 177 | function(i) return fleece.json(t) end 178 | ) 179 | 180 | measure3("t[randstr(i)]=randstr(i)", 181 | function(i) t[randstr(i)] = randstr(i) end, 182 | "luajson.stringify(t)", 183 | function(i) return luajson.stringify(t) end, 184 | "json4.encode(t)", 185 | function(i) return json4.encode(t) end, 186 | "fleece.json(t)", 187 | function(i) return fleece.json(t) end 188 | ) 189 | print(sep) 190 | 191 | print("Note that fleece may list associative arrays in different order.") 192 | 193 | -------------------------------------------------------------------------------- /test/bench3a.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench3a.lua --- 4 | --- Description : Fleece: random tables, speed clocked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 02 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- This runs ONLY Fleece, with the same volume as bench3.lua --- 14 | --- --- 15 | --- Run this file in case bench3.lua crashes to see if this script --- 16 | --- -- only fleece -- also crashes; should proof that Fleece to be clean. --- 17 | --- --- 18 | --- Use: lua test/bench3a.lua --- 19 | --- --- 20 | ------------------------------------------------------------------------------- 21 | 22 | print("Fleece Benchmarks 3 / Fleece ONLY") 23 | print("=========================================================") 24 | print("A couple of random tables are created and speed is clocked.") 25 | print("You should have built fleece first with 'make ', ") 26 | print("and now be in the fleece root directory. SEE BENCH3 & BENCH3b.") 27 | 28 | ELEMENTS = 1000 29 | CYCLES = 100 30 | 31 | package.cpath="src/?.so" 32 | fleece = require("fleece") 33 | -- package.path="etc/?.lua" 34 | -- json4 = require("json") 35 | -- package.cpath="etc/?.so" 36 | -- luajson = require("luajson") 37 | 38 | -- luajson stuff 39 | -- local base = _G 40 | -- json = luajson 41 | -- json.null = {_mt = {__tostring = function () return "null" end, __call = function () return "null" end}} 42 | -- base.setmetatable(json.null, json.null._mt) 43 | 44 | sep = "---------------------------------------------------------------------------------" 45 | 46 | printcol = 0 47 | function printf(...) 48 | s = string.format(...) 49 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 50 | io.write(s) 51 | end 52 | 53 | function tab(x) 54 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 55 | end 56 | 57 | function nanosecs_per(time, per) 58 | return time * 1000000000 / per 59 | end 60 | 61 | function microsecs_per(time, per) 62 | return time * 1000000 / per 63 | end 64 | 65 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 66 | function randstr(i) 67 | local r = {} 68 | local length = math.random(1,20) 69 | local i 70 | for i = 1, length do 71 | r[i] = abc[math.random(1, 26)] 72 | end 73 | 74 | return table.concat(r) 75 | end 76 | 77 | local t = {} 78 | local function measure(prepP, prepare, actionP, action, printPrepP) 79 | 80 | local items = ELEMENTS 81 | local cycles = CYCLES 82 | local clock = os.clock 83 | 84 | if(printPrepP) then 85 | printf("%d elements in %-25s\n", items, prepP) 86 | end 87 | printf("%dx %-12s ", cycles, actionP) 88 | 89 | t = {} 90 | local i = 1 91 | local size = 0 92 | while(i <= items) do prepare(i); i = i + 1 end 93 | 94 | i = 1 95 | local last = nil 96 | local tm = clock() 97 | while(i <= cycles) do last = action(i); i = i + 1 end 98 | tm = clock() - tm 99 | if tm ~= 0 then 100 | mspc= nanosecs_per(tm, cycles * items) 101 | tab(27) 102 | printf("%10.0fns/element ", mspc) 103 | else 104 | mspc = nil 105 | printf("%dx %-12s sample too small, could not measure ", cycles, actionP) 106 | end 107 | 108 | return mspc, last 109 | end 110 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 111 | print(" - Fleece 0.3.1") 112 | 113 | 114 | local function measure3(prepP, prepare, prompt1, action1, prompt2, action2, prompt3, action3) 115 | 116 | print(sep) 117 | 118 | third, r3 = measure(prepP, prepare, prompt3, action3, true) 119 | printf(" %.20s.. \n", r3) 120 | end 121 | 122 | 123 | measure3("t[i]=i", 124 | function(i) t[i] = i end, 125 | "luajson.stringify(t)", 126 | function(i) return luajson.stringify(t) end, 127 | "json4.encode(t)", 128 | function(i) return json4.encode(t) end, 129 | "fleece.json(t)", 130 | function(i) return fleece.json(t) end 131 | ) 132 | 133 | measure3("t['x'..i]=i", 134 | function(i) t['x'..i] = i end, 135 | "luajson.stringify(t)", 136 | function(i) return luajson.stringify(t) end, 137 | "json4.encode(t)", 138 | function(i) return json4.encode(t) end, 139 | "fleece.json(t)", 140 | function(i) return fleece.json(t) end 141 | ) 142 | 143 | measure3("t[i]=randstr(i)", 144 | function(i) t[i] = randstr(i) end, 145 | "luajson.stringify(t)", 146 | function(i) return luajson.stringify(t) end, 147 | "json4.encode(t)", 148 | function(i) return json4.encode(t) end, 149 | "fleece.json(t)", 150 | function(i) return fleece.json(t) end 151 | ) 152 | 153 | measure3("t[randstr(i)]=randstr(i)", 154 | function(i) t[randstr(i)] = randstr(i) end, 155 | "luajson.stringify(t)", 156 | function(i) return luajson.stringify(t) end, 157 | "json4.encode(t)", 158 | function(i) return json4.encode(t) end, 159 | "fleece.json(t)", 160 | function(i) return fleece.json(t) end 161 | ) 162 | print(sep) 163 | print("Note that fleece may list associative arrays in different order.") 164 | 165 | -------------------------------------------------------------------------------- /test/bench3b.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench3b.lua --- 4 | --- Description : JSON4 and luajson: random tables, speed clocked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 02 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- This runs ONLY: --- 14 | --- --- 15 | --- JSON4 0.9.50: (aka JSON4Lua, JSON for Lua) http://json.luaforge.net/ --- 16 | --- LuaJSON 1.1: (aka luajsonlib) http://luaforge.net/projects/luajsonlib/ --- 17 | --- --- 18 | --- Run this file in case bench3.lua crashes to see if this script --- 19 | --- -- without Fleece -- also crashes; as proof that it may not be --- 20 | --- Fleece. --- 21 | --- --- 22 | --- Use: lua test/bench3b.lua --- 23 | --- --- 24 | ------------------------------------------------------------------------------- 25 | 26 | print("Fleece Benchmarks vs Json4 / native Lua and LuaJSON C lib") 27 | print("=========================================================") 28 | print("A couple of random tables are created and speed is clocked.") 29 | print("You should have built luajson.so with 'make -test,") 30 | print("and now be in the fleece root directory.") 31 | print("IF BENCH3 CRASHES, CHECK OUT IF THIS SCRIPT CRASHES, TOO.") 32 | 33 | 34 | ELEMENTS = 1000 35 | CYCLES = 100 36 | 37 | package.path="etc/json4/?.lua" 38 | json4 = require("json") 39 | package.cpath="etc/luajson/?.so" 40 | luajson = require("luajson") 41 | 42 | -- luajson stuff 43 | local base = _G 44 | json = luajson 45 | json.null = {_mt = {__tostring = function () return "null" end, __call = function () return "null" end}} 46 | base.setmetatable(json.null, json.null._mt) 47 | 48 | sep = "---------------------------------------------------------------------------------" 49 | 50 | printcol = 0 51 | function printf(...) 52 | s = string.format(...) 53 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 54 | io.write(s) 55 | end 56 | 57 | function tab(x) 58 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 59 | end 60 | 61 | function nanosecs_per(time, per) 62 | return time * 1000000000 / per 63 | end 64 | 65 | function microsecs_per(time, per) 66 | return time * 1000000 / per 67 | end 68 | 69 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 70 | function randstr(i) 71 | local r = {} 72 | local length = math.random(1,20) 73 | local i 74 | for i = 1, length do 75 | r[i] = abc[math.random(1, 26)] 76 | end 77 | 78 | return table.concat(r) 79 | end 80 | 81 | local t = {} 82 | local function measure(prepP, prepare, actionP, action, printPrepP) 83 | 84 | local items = ELEMENTS 85 | local cycles = CYCLES 86 | local clock = os.clock 87 | 88 | if(printPrepP) then 89 | printf("%d elements in %-25s\n", items, prepP) 90 | end 91 | printf("%dx %-12s ", cycles, actionP) 92 | 93 | t = {} 94 | local i = 1 95 | local size = 0 96 | while(i <= items) do prepare(i); i = i + 1 end 97 | 98 | i = 1 99 | local last = nil 100 | local tm = clock() 101 | while(i <= cycles) do last = action(i); i = i + 1 end 102 | tm = clock() - tm 103 | if tm ~= 0 then 104 | mspc= nanosecs_per(tm, cycles * items) 105 | tab(27) 106 | printf("%10.0fns/element ", mspc) 107 | else 108 | mspc = nil 109 | printf("%dx %-12s sample too small, could not measure ", cycles, actionP) 110 | end 111 | 112 | return mspc, last 113 | end 114 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 115 | print(" - Fleece 0.3.1") 116 | 117 | 118 | local function measure3(prepP, prepare, prompt1, action1, prompt2, action2, prompt3, action3) 119 | 120 | print(sep) 121 | 122 | first, r1 = measure(prepP, prepare, prompt1, action1, true) 123 | printf(" %.20s.. \n", r1) 124 | secnd, r2 = measure(prepP, prepare, prompt2, action2) 125 | printf(" %.20s.. \n", r2) 126 | 127 | 128 | end 129 | 130 | 131 | measure3("t[i]=i", 132 | function(i) t[i] = i end, 133 | "luajson.stringify(t)", 134 | function(i) return luajson.stringify(t) end, 135 | "json4.encode(t)", 136 | function(i) return json4.encode(t) end, 137 | "fleece.json(t)", 138 | function(i) return fleece.json(t) end 139 | ) 140 | 141 | measure3("t['x'..i]=i", 142 | function(i) t['x'..i] = i end, 143 | "luajson.stringify(t)", 144 | function(i) return luajson.stringify(t) end, 145 | "json4.encode(t)", 146 | function(i) return json4.encode(t) end, 147 | "fleece.json(t)", 148 | function(i) return fleece.json(t) end 149 | ) 150 | 151 | measure3("t[i]=randstr(i)", 152 | function(i) t[i] = randstr(i) end, 153 | "luajson.stringify(t)", 154 | function(i) return luajson.stringify(t) end, 155 | "json4.encode(t)", 156 | function(i) return json4.encode(t) end, 157 | "fleece.json(t)", 158 | function(i) return fleece.json(t) end 159 | ) 160 | 161 | measure3("t[randstr(i)]=randstr(i)", 162 | function(i) t[randstr(i)] = randstr(i) end, 163 | "luajson.stringify(t)", 164 | function(i) return luajson.stringify(t) end, 165 | "json4.encode(t)", 166 | function(i) return json4.encode(t) end, 167 | "fleece.json(t)", 168 | function(i) return fleece.json(t) end 169 | ) 170 | print(sep) 171 | 172 | -------------------------------------------------------------------------------- /test/bench4d.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench4.lua --- 4 | --- Description : Fleece vs JSON4, luajson and Lua-Yajl --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 02 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- Fleece is optimized for the fastest Lua to JSON conversion and beats --- 14 | --- other JSON implementations by around 10 times, native Lua around 100. --- 15 | --- Please let me know about the speed you are observing. --- 16 | --- hd2010@eonblast.com --- 17 | --- --- 18 | --- This compares Fleece to JSON4, LuaJSON and Lua-Yajl: --- 19 | --- --- 20 | --- JSON4 0.9.50: (aka JSON4Lua, JSON for Lua) http://json.luaforge.net/ --- 21 | --- LuaJSON 1.1: (aka luajsonlib) http://luaforge.net/projects/luajsonlib/ --- 22 | --- Yajl 1.0.9: http://lloyd.github.com/yajl/ --- 23 | --- Lua-yajl Oct '10: http://github.com/brimworks/lua-yajl/ --- 24 | --- --- 25 | --- Building with Yajl is not always fun, you can safely skip this. --- 26 | --- --- 27 | --- Use: lua test/bench4.lua --- 28 | --- --- 29 | ------------------------------------------------------------------------------- 30 | 31 | print("Fleece Benchmarks Lua-Yajl C lib only") 32 | print("============================================================") 33 | print("A couple of random tables are created and speed is clocked.") 34 | print("Building with Yajl is not always fun, you can safely skip this.") 35 | print("You should have built fleece first with 'make ', and also") 36 | print("built luajson.so with 'make -test, and now be in the") 37 | print("fleece root directory. You should have built yajl into etc/yajl.") 38 | print("There is no support in fleece to build yajl or lua-yajl.") 39 | print("You need to get and build yajl and lua-yajl manually.") 40 | print("Yajl 1.0.9: http://lloyd.github.com/yajl/") 41 | print("Lua-yajl Oct '10: http://github.com/brimworks/lua-yajl/") 42 | print("LUAJSON (NOT FLEECE) CAN CRASH THIS TEST. SEE BENCH3a & BENCH3b.") 43 | 44 | ELEMENTS = 1000 45 | CYCLES = 10 46 | 47 | package.cpath="src/?.so" 48 | fleece = require("fleece") 49 | package.path="etc/json4/?.lua" 50 | json4 = require("json") 51 | package.cpath="etc/luajson/?.so" 52 | luajson = require("luajson") 53 | 54 | -- luajson stuff 55 | local base = _G 56 | local json = luajson 57 | json.null = {_mt = {__tostring = function () return "null" end, __call = function () return "null" end}} 58 | base.setmetatable(json.null, json.null._mt) 59 | 60 | -- luayajl stuff 61 | package.cpath = "etc/yajl/?.so" 62 | local yajl = require("yajl") 63 | 64 | sep = "---------------------------------------------------------------------------------" 65 | 66 | printcol = 0 67 | function printf(...) 68 | s = string.format(...) 69 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 70 | io.write(s) 71 | end 72 | 73 | function tab(x) 74 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 75 | end 76 | 77 | function nanosecs_per(time, per) 78 | return time * 1000000000 / per 79 | end 80 | 81 | function microsecs_per(time, per) 82 | return time * 1000000 / per 83 | end 84 | 85 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 86 | function randstr(i) 87 | local r = {} 88 | local length = math.random(1,20) 89 | local i 90 | for i = 1, length do 91 | r[i] = abc[math.random(1, 26)] 92 | end 93 | 94 | return table.concat(r) 95 | end 96 | 97 | local t = {} 98 | local function measure(prepP, prepare, actionP, action, printPrepP) 99 | 100 | local items = ELEMENTS 101 | local cycles = CYCLES 102 | local clock = os.clock 103 | 104 | if(printPrepP) then 105 | printf("%d elements in %-25s\n", items, prepP) 106 | end 107 | printf("%dx %-12s ", cycles, actionP) 108 | 109 | t = {} 110 | local i = 1 111 | local size = 0 112 | while(i <= items) do prepare(i); i = i + 1 end 113 | 114 | i = 1 115 | local last = nil 116 | local tm = clock() 117 | while(i <= cycles) do last = action(i); i = i + 1 end 118 | tm = clock() - tm 119 | if tm ~= 0 then 120 | mspc= nanosecs_per(tm, cycles * items) 121 | tab(27) 122 | printf("%10.0fns/element ", mspc) 123 | else 124 | mspc = nil 125 | printf("%dx %-12s sample too small, could not measure ", cycles, actionP) 126 | end 127 | 128 | return mspc, last 129 | end 130 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 131 | print(" - Fleece 0.3.1") 132 | 133 | 134 | local function measure4(prepP, prepare, p1, a1, p2, a2, p3, a3, p4, a4) 135 | 136 | print(sep) 137 | 138 | third, r3 = measure(prepP, prepare, p3, a3) 139 | printf(" %.20s.. \n", r3) 140 | 141 | 142 | end 143 | 144 | measure4("t[i]=i", 145 | function(i) t[i] = i end, 146 | "luajson.stringify(t)", 147 | function(i) return luajson.stringify(t) end, 148 | "json4.encode(t)", 149 | function(i) return json4.encode(t) end, 150 | "yajl.to_string(t)", 151 | function(i) return yajl.to_string(t) end, 152 | "fleece.json(t)", 153 | function(i) return fleece.json(t) end 154 | ) 155 | 156 | measure4("t['x'..i]=i", 157 | function(i) t['x'..i] = i end, 158 | "luajson.stringify(t)", 159 | function(i) return luajson.stringify(t) end, 160 | "json4.encode(t)", 161 | function(i) return json4.encode(t) end, 162 | "yajl.to_string(t)", 163 | function(i) return yajl.to_string(t) end, 164 | "fleece.json(t)", 165 | function(i) return fleece.json(t) end 166 | ) 167 | 168 | measure4("t[i]=randstr(i)", 169 | function(i) t[i] = randstr(i) end, 170 | "luajson.stringify(t)", 171 | function(i) return luajson.stringify(t) end, 172 | "json4.encode(t)", 173 | function(i) return json4.encode(t) end, 174 | "yajl.to_string(t)", 175 | function(i) return yajl.to_string(t) end, 176 | "fleece.json(t)", 177 | function(i) return fleece.json(t) end 178 | ) 179 | 180 | measure4("t[randstr(i)]=randstr(i)", 181 | function(i) t[randstr(i)] = randstr(i) end, 182 | "luajson.stringify(t)", 183 | function(i) return luajson.stringify(t) end, 184 | "json4.encode(t)", 185 | function(i) return json4.encode(t) end, 186 | "yajl.to_string(t)", 187 | function(i) return yajl.to_string(t) end, 188 | "fleece.json(t)", 189 | function(i) return fleece.json(t) end 190 | ) 191 | print(sep) 192 | print("Note that fleece may list associative arrays in different order.") 193 | 194 | -------------------------------------------------------------------------------- /test/bench4e.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench4e.lua --- 4 | --- Description : Fleece vs Lua-Yajl --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : 20 Mar 2011 --- 10 | --- Changed : 20 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- Fleece is optimized for the fastest Lua to JSON conversion and beats --- 14 | --- other JSON implementations by around 10 times, native Lua around 100. --- 15 | --- Please let me know about the speed you are observing. --- 16 | --- hd2010@eonblast.com --- 17 | --- --- 18 | --- This compares Fleece to Lua-Yajl: --- 19 | --- --- 20 | --- Yajl 1.0.9: http://lloyd.github.com/yajl/ --- 21 | --- Lua-yajl Oct '10: http://github.com/brimworks/lua-yajl/ --- 22 | --- --- 23 | --- Building with Yajl is not always fun, you can safely skip this. --- 24 | --- --- 25 | --- Use: lua test/bench4e.lua --- 26 | --- --- 27 | ------------------------------------------------------------------------------- 28 | 29 | print("Fleece Benchmarks vs Lua-Yajl C lib") 30 | print("===================================") 31 | print("A couple of random tables are created and speed is clocked.") 32 | print("Building with Yajl is not always fun, you can safely skip this.") 33 | print("You should have built fleece first with 'make '") 34 | print("And now be in the fleece root directory. You should have ") 35 | print("built yajl into etc/yajl.") 36 | print("There is no support in fleece to build yajl or lua-yajl.") 37 | print("You need to get and build yajl and lua-yajl manually.") 38 | print("Yajl 1.0.9: http://lloyd.github.com/yajl/") 39 | print("Lua-yajl Oct '10: http://github.com/brimworks/lua-yajl/") 40 | 41 | ELEMENTS = 1000 42 | CYCLES = 1000 43 | 44 | package.cpath="src/?.so" 45 | fleece = require("fleece") 46 | 47 | -- luayajl stuff 48 | package.cpath = "etc/yajl/?.so" 49 | local yajl = require("yajl") 50 | 51 | sep = "---------------------------------------------------------------------------------" 52 | 53 | printcol = 0 54 | function printf(...) 55 | s = string.format(...) 56 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 57 | io.write(s) 58 | end 59 | 60 | function tab(x) 61 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 62 | end 63 | 64 | function nanosecs_per(time, per) 65 | return time * 1000000000 / per 66 | end 67 | 68 | function microsecs_per(time, per) 69 | return time * 1000000 / per 70 | end 71 | 72 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 73 | function randstr(i) 74 | local r = {} 75 | local length = math.random(1,20) 76 | local i 77 | for i = 1, length do 78 | r[i] = abc[math.random(1, 26)] 79 | end 80 | 81 | return table.concat(r) 82 | end 83 | 84 | local t = {} 85 | local function measure(prepP, prepare, actionP, action, printPrepP) 86 | 87 | local items = ELEMENTS 88 | local cycles = CYCLES 89 | local clock = os.clock 90 | 91 | if(printPrepP) then 92 | printf("%d elements in %-25s\n", items, prepP) 93 | end 94 | printf("%dx %-12s ", cycles, actionP) 95 | 96 | t = {} 97 | local i = 1 98 | local size = 0 99 | while(i <= items) do prepare(i); i = i + 1 end 100 | 101 | i = 1 102 | local last = nil 103 | local tm = clock() 104 | while(i <= cycles) do last = action(i); i = i + 1 end 105 | tm = clock() - tm 106 | if tm ~= 0 then 107 | mspc= nanosecs_per(tm, cycles * items) 108 | tab(27) 109 | printf("%10.0fns/element ", mspc) 110 | else 111 | mspc = nil 112 | printf("%dx %-12s sample too small, could not measure ", cycles, actionP) 113 | end 114 | 115 | return mspc, last 116 | end 117 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 118 | print(" - Fleece 0.3.1") 119 | 120 | 121 | local function measure4(prepP, prepare, p1, a1, p2, a2) 122 | 123 | print(sep) 124 | 125 | first, r1 = measure(prepP, prepare, p1, a1, true) 126 | printf(" %.20s.. \n", r1) 127 | secnd, r2 = measure(prepP, prepare, p2, a2) 128 | 129 | if(first and secnd) then prc = math.floor(forth / first * 100) else prc = 0 end 130 | printf("%2d%% ", prc) 131 | printf(" %.20s.. \n", r4) 132 | 133 | end 134 | 135 | measure4("t[i]=i", 136 | function(i) t[i] = i end, 137 | "yajl.to_string(t)", 138 | function(i) return yajl.to_string(t) end, 139 | "fleece.json(t)", 140 | function(i) return fleece.json(t) end 141 | ) 142 | 143 | measure4("t['x'..i]=i", 144 | function(i) t['x'..i] = i end, 145 | "yajl.to_string(t)", 146 | function(i) return yajl.to_string(t) end, 147 | "fleece.json(t)", 148 | function(i) return fleece.json(t) end 149 | ) 150 | 151 | measure4("t[i]=randstr(i)", 152 | function(i) t[i] = randstr(i) end, 153 | "yajl.to_string(t)", 154 | function(i) return yajl.to_string(t) end, 155 | "fleece.json(t)", 156 | function(i) return fleece.json(t) end 157 | ) 158 | 159 | measure4("t[randstr(i)]=randstr(i)", 160 | function(i) t[randstr(i)] = randstr(i) end, 161 | "yajl.to_string(t)", 162 | function(i) return yajl.to_string(t) end, 163 | "fleece.json(t)", 164 | function(i) return fleece.json(t) end 165 | ) 166 | print(sep) 167 | print("Note that fleece may list associative arrays in different order.") 168 | 169 | -------------------------------------------------------------------------------- /test/bench5a.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench5a.lua --- 4 | --- Description : Fleece only: random tables, speed cklocked, very long --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 02 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- Fleece is optimized for the fastest Lua to JSON conversion and beats --- 14 | --- other JSON implementations by around 10 times, native Lua around 100. --- 15 | --- Please let me know about the speed you are observing. --- 16 | --- hd2010@eonblast.com --- 17 | --- --- 18 | --- This is a 'crash test' running long and slow. It will likely end with --- 19 | --- with 'out of memory', which is ok for now. --- 20 | --- --- 21 | --- This file tests Fleece --- 22 | --- --- 23 | --- Use: lua test/bench5a.lua --- 24 | --- --- 25 | ------------------------------------------------------------------------------- 26 | 27 | print("Fleece Benchmarks - Fleece Long Running Crash Test") 28 | print("=========================================================") 29 | print("A couple of random tables are created and speed is clocked.") 30 | print("You should now be in the main fleece dir, have built fleece first,") 31 | print("doing 'make '.") 32 | print("THESE TESTS BRING NOTHING NEW, THEY ARE MERELY VERY LONG RUNNING."); 33 | 34 | package.cpath="src/?.so" 35 | fleece = require("fleece") 36 | 37 | 38 | sep = "---------------------------------------------------------------------------------" 39 | 40 | all = function() 41 | 42 | printcol = 0 43 | function printf(...) 44 | s = string.format(...) 45 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 46 | io.write(s) 47 | end 48 | 49 | function tab(x) 50 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 51 | end 52 | 53 | function nanosecs_per(time, per) 54 | return time * 1000000000 / per 55 | end 56 | 57 | function microsecs_per(time, per) 58 | return time * 1000000 / per 59 | end 60 | 61 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 62 | function randstr(i) 63 | local r = {} 64 | local length = math.random(1,20) 65 | local i 66 | for i = 1, length do 67 | r[i] = abc[math.random(1, 26)] 68 | end 69 | 70 | return table.concat(r) 71 | end 72 | 73 | local t = {} 74 | local function measure(prepP, prepare, actionP, action, printPrepP) 75 | 76 | local items = ELEMENTS 77 | local cycles = CYCLES 78 | local clock = os.clock 79 | 80 | if(printPrepP) then 81 | printf("%d elements in %-25s\n", items, prepP) 82 | end 83 | printf("%dx %-12s ", cycles, actionP) 84 | 85 | t = {} 86 | local i = 1 87 | local size = 0 88 | while(i <= items) do prepare(i); i = i + 1 end 89 | 90 | i = 1 91 | local last = nil 92 | local tm = clock() 93 | while(i <= cycles) do last = action(i); i = i + 1 end 94 | tm = clock() - tm 95 | if tm ~= 0 then 96 | mspc= nanosecs_per(tm, cycles * items) 97 | tab(27) 98 | printf("%10.0fns/element ", mspc) 99 | else 100 | mspc = nil 101 | printf("%dx %-12s sample too small, could not measure ", cycles, actionP) 102 | end 103 | 104 | return mspc, last 105 | end 106 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 107 | print(" - Fleece 0.3.1") 108 | 109 | 110 | local function measure3(prepP, prepare, prompt1, action1, prompt2, action2, prompt3, action3) 111 | 112 | print(sep) 113 | 114 | third, r3 = measure(prepP, prepare, prompt3, action3, true) 115 | printf(" %.20s.. \n", r3) 116 | end 117 | 118 | 119 | measure3("t[i]=i", 120 | function(i) t[i] = i end, 121 | "luajson.stringify(t)", 122 | function(i) return luajson.stringify(t) end, 123 | "json4.encode(t)", 124 | function(i) return json4.encode(t) end, 125 | "fleece.json(t)", 126 | function(i) return fleece.json(t) end 127 | ) 128 | 129 | measure3("t['x'..i]=i", 130 | function(i) t['x'..i] = i end, 131 | "luajson.stringify(t)", 132 | function(i) return luajson.stringify(t) end, 133 | "json4.encode(t)", 134 | function(i) return json4.encode(t) end, 135 | "fleece.json(t)", 136 | function(i) return fleece.json(t) end 137 | ) 138 | 139 | measure3("t[i]=randstr(i)", 140 | function(i) t[i] = randstr(i) end, 141 | "luajson.stringify(t)", 142 | function(i) return luajson.stringify(t) end, 143 | "json4.encode(t)", 144 | function(i) return json4.encode(t) end, 145 | "fleece.json(t)", 146 | function(i) return fleece.json(t) end 147 | ) 148 | 149 | measure3("t[randstr(i)]=randstr(i)", 150 | function(i) t[randstr(i)] = randstr(i) end, 151 | "luajson.stringify(t)", 152 | function(i) return luajson.stringify(t) end, 153 | "json4.encode(t)", 154 | function(i) return json4.encode(t) end, 155 | "fleece.json(t)", 156 | function(i) return fleece.json(t) end 157 | ) 158 | print(sep) 159 | 160 | end 161 | 162 | ELEMENTS = 1 163 | CYCLES = 1000000 164 | all() 165 | 166 | ELEMENTS = 10 167 | CYCLES = 100000 168 | all() 169 | 170 | ELEMENTS = 100 171 | CYCLES = 10000 172 | all() 173 | 174 | ELEMENTS = 1000 175 | CYCLES = 10000 176 | all() 177 | 178 | ELEMENTS = 1000 179 | CYCLES = 1000 180 | all() 181 | 182 | ELEMENTS = 10000 183 | CYCLES = 100 184 | all() 185 | 186 | ELEMENTS = 100000 187 | CYCLES = 10 188 | all() 189 | 190 | ELEMENTS = 1000000 191 | CYCLES = 1 192 | all() 193 | 194 | ELEMENTS = 1 195 | CYCLES = 10000000 196 | all() 197 | 198 | ELEMENTS = 10 199 | CYCLES = 1000000 200 | all() 201 | 202 | ELEMENTS = 100 203 | CYCLES = 100000 204 | all() 205 | 206 | ELEMENTS = 1000 207 | CYCLES = 100000 208 | all() 209 | 210 | ELEMENTS = 10000 211 | CYCLES = 10000 212 | all() 213 | 214 | ELEMENTS = 10000 215 | CYCLES = 1000 216 | all() 217 | 218 | ELEMENTS = 100000 219 | CYCLES = 100 220 | all() 221 | 222 | ELEMENTS = 1000000 223 | CYCLES = 10 224 | all() 225 | 226 | ELEMENTS = 10000000 227 | CYCLES = 1 228 | all() 229 | 230 | ELEMENTS = 100000 231 | CYCLES = 10000 232 | all() 233 | 234 | ELEMENTS = 10000 235 | CYCLES = 100000 236 | all() 237 | 238 | ELEMENTS = 100000000 239 | CYCLES = 1 240 | all() 241 | 242 | ELEMENTS = 1 243 | CYCLES = 100000000 244 | all() 245 | 246 | print("Note that fleece may list associative arrays in different order.") 247 | -------------------------------------------------------------------------------- /test/bench5b.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench5b.lua --- 4 | --- Description : test JSON4: random tables are converted, time clocked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 02 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- Fleece is optimized for the fastest Lua to JSON conversion and beats --- 14 | --- other JSON implementations by around 10 times, native Lua up to 100x. --- 15 | --- Please let me know about the speed you are observing. --- 16 | --- hd2010@eonblast.com --- 17 | --- --- 18 | --- This is a 'crash test' running long and slow. It will likely end with --- 19 | --- with 'out of memory', which is ok for now. --- 20 | --- --- 21 | --- This file tests JSON4 (not Fleece) http://json.luaforge.net/ --- 22 | --- LuaJSON 1.1: (aka luajsonlib) http://luaforge.net/projects/luajsonlib/ --- 23 | --- --- 24 | --- Use: lua test/bench5b.lua --- 25 | --- Or: make bench5b --- 26 | --- --- 27 | ------------------------------------------------------------------------------- 28 | 29 | print("Fleece Benchmarks - JSON4 Long Running Crash Test") 30 | print("=========================================================") 31 | print("A couple of random tables are created and speed is clocked.") 32 | print("THIS TEST TAKES VERY LONG. ONLY JSON4 AND LUAJSON, NOT FLEECE."); 33 | 34 | package.path="etc/json4/?.lua" 35 | local luajson = require("json") 36 | package.cpath="etc/luajson/?.so" 37 | luajson = require("luajson") 38 | 39 | -- luajson stuff 40 | local base = _G 41 | local json = luajson 42 | json.null = {_mt = {__tostring = function () return "null" end, __call = function () return "null" end}} 43 | base.setmetatable(json.null, json.null._mt) 44 | 45 | sep = "---------------------------------------------------------------------------------" 46 | 47 | all = function() 48 | 49 | printcol = 0 50 | function printf(...) 51 | s = string.format(...) 52 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 53 | io.write(s) 54 | io.flush() 55 | end 56 | 57 | function tab(x) 58 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 59 | end 60 | 61 | function nanosecs_per(time, per) 62 | return time * 1000000000 / per 63 | end 64 | 65 | function microsecs_per(time, per) 66 | return time * 1000000 / per 67 | end 68 | 69 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 70 | function randstr(i) 71 | local r = {} 72 | local length = math.random(1,20) 73 | local i 74 | for i = 1, length do 75 | r[i] = abc[math.random(1, 26)] 76 | end 77 | 78 | return table.concat(r) 79 | end 80 | 81 | local t = {} 82 | local function measure(prepP, prepare, actionP, action, printPrepP) 83 | 84 | local items = ELEMENTS 85 | local cycles = CYCLES 86 | local clock = os.clock 87 | 88 | if(printPrepP) then 89 | printf("%d elements in %-25s\n", items, prepP) 90 | end 91 | printf("%dx %-12s: ", cycles, actionP) 92 | 93 | printf("prepare ... ", cycles, actionP) 94 | 95 | t = {} 96 | local i = 1 97 | local size = 0 98 | while(i <= items) do prepare(i); i = i + 1 end 99 | 100 | printf("convert ... ", cycles, actionP) 101 | 102 | i = 1 103 | local last = nil 104 | local tm = clock() 105 | while(i <= cycles) do last = action(i); i = i + 1 end 106 | tm = clock() - tm 107 | if tm ~= 0 then 108 | mspc= nanosecs_per(tm, cycles * items) 109 | tab(27) 110 | printf("%10.0fns/element ", mspc) 111 | else 112 | mspc = nil 113 | printf("%dx %-12s sample too small, could not measure ", cycles, actionP) 114 | end 115 | 116 | return mspc, last 117 | end 118 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 119 | print(" - Fleece 0.3.1") 120 | 121 | 122 | local function measure3(prepP, prepare, prompt1, action1, prompt2, action2, prompt3, action3) 123 | 124 | print(sep) 125 | 126 | time, result = measure(prepP, prepare, prompt1, action1, true) 127 | printf(" %.20s.. \n", result) 128 | 129 | 130 | end 131 | 132 | 133 | measure3("t[i]=i", 134 | function(i) t[i] = i end, 135 | "luajson.stringify(t)", 136 | function(i) return luajson.stringify(t) end, 137 | "json4.encode(t)", 138 | function(i) return json4.encode(t) end, 139 | "fleece.json(t)", 140 | function(i) return fleece.json(t) end 141 | ) 142 | 143 | measure3("t['x'..i]=i", 144 | function(i) t['x'..i] = i end, 145 | "luajson.stringify(t)", 146 | function(i) return luajson.stringify(t) end, 147 | "json4.encode(t)", 148 | function(i) return json4.encode(t) end, 149 | "fleece.json(t)", 150 | function(i) return fleece.json(t) end 151 | ) 152 | 153 | measure3("t[i]=randstr(i)", 154 | function(i) t[i] = randstr(i) end, 155 | "luajson.stringify(t)", 156 | function(i) return luajson.stringify(t) end, 157 | "json4.encode(t)", 158 | function(i) return json4.encode(t) end, 159 | "fleece.json(t)", 160 | function(i) return fleece.json(t) end 161 | ) 162 | 163 | measure3("t[randstr(i)]=randstr(i)", 164 | function(i) t[randstr(i)] = randstr(i) end, 165 | "luajson.stringify(t)", 166 | function(i) return luajson.stringify(t) end, 167 | "json4.encode(t)", 168 | function(i) return json4.encode(t) end, 169 | "fleece.json(t)", 170 | function(i) return fleece.json(t) end 171 | ) 172 | print(sep) 173 | 174 | end 175 | 176 | ELEMENTS = 1 177 | CYCLES = 1000000 178 | all() 179 | 180 | ELEMENTS = 10 181 | CYCLES = 100000 182 | all() 183 | 184 | ELEMENTS = 100 185 | CYCLES = 10000 186 | all() 187 | 188 | ELEMENTS = 1000 189 | CYCLES = 10000 190 | all() 191 | 192 | ELEMENTS = 1000 193 | CYCLES = 1000 194 | all() 195 | 196 | ELEMENTS = 10000 197 | CYCLES = 100 198 | all() 199 | 200 | ELEMENTS = 100000 201 | CYCLES = 10 202 | all() 203 | 204 | ELEMENTS = 1000000 205 | CYCLES = 1 206 | all() 207 | 208 | ELEMENTS = 1 209 | CYCLES = 10000000 210 | all() 211 | 212 | ELEMENTS = 10 213 | CYCLES = 1000000 214 | all() 215 | 216 | ELEMENTS = 100 217 | CYCLES = 100000 218 | all() 219 | 220 | ELEMENTS = 1000 221 | CYCLES = 100000 222 | all() 223 | 224 | ELEMENTS = 10000 225 | CYCLES = 10000 226 | all() 227 | 228 | ELEMENTS = 10000 229 | CYCLES = 1000 230 | all() 231 | 232 | ELEMENTS = 100000 233 | CYCLES = 100 234 | all() 235 | 236 | ELEMENTS = 1000000 237 | CYCLES = 10 238 | all() 239 | 240 | ELEMENTS = 10000000 241 | CYCLES = 1 242 | all() 243 | 244 | ELEMENTS = 100000 245 | CYCLES = 10000 246 | all() 247 | 248 | ELEMENTS = 10000 249 | CYCLES = 100000 250 | all() 251 | 252 | ELEMENTS = 100000000 253 | CYCLES = 1 254 | all() 255 | 256 | ELEMENTS = 1 257 | CYCLES = 100000000 258 | all() 259 | 260 | 261 | print("Note that fleece may list associative arrays in different order.") 262 | -------------------------------------------------------------------------------- /test/bench5c.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : bench5c.lua --- 4 | --- Description : luajson test: random tables are converted, time clocked --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : Feb 2011 --- 10 | --- Changed : 02 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- Fleece is optimized for the fastest Lua to JSON conversion and beats --- 14 | --- other JSON implementations by around 10 times, native Lua around 100. --- 15 | --- Please let me know about the speed you are observing. --- 16 | --- hd2010@eonblast.com --- 17 | --- --- 18 | --- This is a 'crash test' running long and slow. It will likely end with --- 19 | --- with 'out of memory', which is ok for now. --- 20 | --- --- 21 | --- This script tests luajson C library (not Fleece) --- 22 | --- http://luaforge.net/projects/luajsonlib/ --- 23 | --- --- 24 | --- Use: lua test/bench5c.lua --- 25 | --- --- 26 | ------------------------------------------------------------------------------- 27 | 28 | print("Fleece Benchmarks - LuaJSON Long Running Crash Test") 29 | print("=========================================================") 30 | print("A couple of random tables are created and speed is clocked.") 31 | print("You should have built luajson first, by make -test.") 32 | print("If this does not run you most likely haven't yet."); 33 | print("THIS TEST TAKES VERY LONG. THIS TESTS ONLY LUAJSON, NOT FLEECE."); 34 | 35 | package.cpath="etc/luajson/?.so" 36 | luajson = require("luajson") 37 | 38 | -- luajson stuff 39 | local base = _G 40 | json = luajson 41 | json.null = {_mt = {__tostring = function () return "null" end, __call = function () return "null" end}} 42 | base.setmetatable(json.null, json.null._mt) 43 | 44 | sep = "---------------------------------------------------------------------------------" 45 | 46 | all = function() 47 | 48 | printcol = 0 49 | function printf(...) 50 | s = string.format(...) 51 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 52 | io.write(s) 53 | io.flush() 54 | end 55 | 56 | function tab(x) 57 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 58 | end 59 | 60 | function nanosecs_per(time, per) 61 | return time * 1000000000 / per 62 | end 63 | 64 | function microsecs_per(time, per) 65 | return time * 1000000 / per 66 | end 67 | 68 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 69 | function randstr(i) 70 | local r = {} 71 | local length = math.random(1,20) 72 | local i 73 | for i = 1, length do 74 | r[i] = abc[math.random(1, 26)] 75 | end 76 | 77 | return table.concat(r) 78 | end 79 | 80 | local t = {} 81 | local function measure(prepP, prepare, actionP, action, printPrepP) 82 | 83 | local items = ELEMENTS 84 | local cycles = CYCLES 85 | local clock = os.clock 86 | 87 | if(printPrepP) then 88 | printf("%d elements in %-25s\n", items, prepP) 89 | end 90 | printf("%dx %-12s: ", cycles, actionP) 91 | 92 | printf("prepare ... ", cycles, actionP) 93 | 94 | t = {} 95 | local i = 1 96 | local size = 0 97 | while(i <= items) do prepare(i); i = i + 1 end 98 | 99 | printf("convert ... ", cycles, actionP) 100 | 101 | i = 1 102 | local last = nil 103 | local tm = clock() 104 | while(i <= cycles) do last = action(i); i = i + 1 end 105 | tm = clock() - tm 106 | if tm ~= 0 then 107 | mspc= nanosecs_per(tm, cycles * items) 108 | tab(27) 109 | printf("%10.0fns/element ", mspc) 110 | else 111 | mspc = nil 112 | printf("%dx %-12s sample too small, could not measure ", cycles, actionP) 113 | end 114 | 115 | return mspc, last 116 | end 117 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 118 | print(" - Fleece 0.3.1") 119 | 120 | 121 | local function measure3(prepP, prepare, prompt1, action1, prompt2, action2, prompt3, action3) 122 | 123 | print(sep) 124 | 125 | time, result = measure(prepP, prepare, prompt2, action2, true) 126 | printf(" %.20s.. \n", result) 127 | 128 | 129 | end 130 | 131 | 132 | measure3("t[i]=i", 133 | function(i) t[i] = i end, 134 | "luajson.stringify(t)", 135 | function(i) return luajson.stringify(t) end, 136 | "json4.encode(t)", 137 | function(i) return json4.encode(t) end, 138 | "fleece.json(t)", 139 | function(i) return fleece.json(t) end 140 | ) 141 | 142 | measure3("t['x'..i]=i", 143 | function(i) t['x'..i] = i end, 144 | "luajson.stringify(t)", 145 | function(i) return luajson.stringify(t) end, 146 | "json4.encode(t)", 147 | function(i) return json4.encode(t) end, 148 | "fleece.json(t)", 149 | function(i) return fleece.json(t) end 150 | ) 151 | 152 | measure3("t[i]=randstr(i)", 153 | function(i) t[i] = randstr(i) end, 154 | "luajson.stringify(t)", 155 | function(i) return luajson.stringify(t) end, 156 | "json4.encode(t)", 157 | function(i) return json4.encode(t) end, 158 | "fleece.json(t)", 159 | function(i) return fleece.json(t) end 160 | ) 161 | 162 | measure3("t[randstr(i)]=randstr(i)", 163 | function(i) t[randstr(i)] = randstr(i) end, 164 | "luajson.stringify(t)", 165 | function(i) return luajson.stringify(t) end, 166 | "json4.encode(t)", 167 | function(i) return json4.encode(t) end, 168 | "fleece.json(t)", 169 | function(i) return fleece.json(t) end 170 | ) 171 | print(sep) 172 | 173 | end 174 | 175 | ELEMENTS = 1 176 | CYCLES = 1000000 177 | all() 178 | 179 | ELEMENTS = 10 180 | CYCLES = 100000 181 | all() 182 | 183 | ELEMENTS = 100 184 | CYCLES = 10000 185 | all() 186 | 187 | ELEMENTS = 1000 188 | CYCLES = 10000 189 | all() 190 | 191 | ELEMENTS = 1000 192 | CYCLES = 1000 193 | all() 194 | 195 | ELEMENTS = 10000 196 | CYCLES = 100 197 | all() 198 | 199 | ELEMENTS = 100000 200 | CYCLES = 10 201 | all() 202 | 203 | ELEMENTS = 1000000 204 | CYCLES = 1 205 | all() 206 | 207 | ELEMENTS = 1 208 | CYCLES = 10000000 209 | all() 210 | 211 | ELEMENTS = 10 212 | CYCLES = 1000000 213 | all() 214 | 215 | ELEMENTS = 100 216 | CYCLES = 100000 217 | all() 218 | 219 | ELEMENTS = 1000 220 | CYCLES = 100000 221 | all() 222 | 223 | ELEMENTS = 10000 224 | CYCLES = 10000 225 | all() 226 | 227 | ELEMENTS = 10000 228 | CYCLES = 1000 229 | all() 230 | 231 | ELEMENTS = 100000 232 | CYCLES = 100 233 | all() 234 | 235 | ELEMENTS = 1000000 236 | CYCLES = 10 237 | all() 238 | 239 | ELEMENTS = 10000000 240 | CYCLES = 1 241 | all() 242 | 243 | ELEMENTS = 100000 244 | CYCLES = 10000 245 | all() 246 | 247 | ELEMENTS = 10000 248 | CYCLES = 100000 249 | all() 250 | 251 | ELEMENTS = 100000000 252 | CYCLES = 1 253 | all() 254 | 255 | ELEMENTS = 1 256 | CYCLES = 100000000 257 | all() 258 | -------------------------------------------------------------------------------- /test/bench6a.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/bench6a.lua --- 4 | --- Description : Fleece only: buf finder. 10 tables, 10000 elements --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : 03 Mar 2011 --- 10 | --- Changed : 03 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- Fleece is optimized for the fastest Lua to JSON conversion and beats --- 14 | --- other JSON implementations by around 10 times, native Lua up to 100x. --- 15 | --- Please let me know about the speed you are observing. --- 16 | --- hd2010@eonblast.com --- 17 | --- --- 18 | --- This is a 'crash test' running long and slow. It was introduced to --- 19 | --- find a specific bug in the buffers that could be replicated with --- 20 | --- the 10000/10 setting. --- 21 | --- --- 22 | --- This file tests Fleece --- 23 | --- --- 24 | --- Use: lua test/bench6a.lua --- 25 | --- --- 26 | ------------------------------------------------------------------------------- 27 | 28 | print("Fleece Benchmarks - Long Running Crash Test") 29 | print("=========================================================") 30 | print("A couple of random tables are created and speed is clocked.") 31 | print("This test was introduced to track a bug down."); 32 | 33 | package.cpath="src/?.so" 34 | fleece = require("fleece") 35 | 36 | sep = "---------------------------------------------------------------------------------" 37 | 38 | all = function() 39 | 40 | printcol = 0 41 | function printf(...) 42 | s = string.format(...) 43 | if(s:find("\n")) then printcol = 0 else printcol = printcol + s:len() end 44 | io.write(s) 45 | end 46 | 47 | function tab(x) 48 | while(printcol < x) do io.write(" "); printcol = printcol + 1 end 49 | end 50 | 51 | function nanosecs_per(time, per) 52 | return time * 1000000000 / per 53 | end 54 | 55 | function microsecs_per(time, per) 56 | return time * 1000000 / per 57 | end 58 | 59 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 60 | function randstr(i) 61 | local r = {} 62 | local length = math.random(1,20) 63 | local i 64 | for i = 1, length do 65 | r[i] = abc[math.random(1, 26)] 66 | end 67 | 68 | return table.concat(r) 69 | end 70 | 71 | local t = {} 72 | local function measure(prepP, prepare, actionP, action, printPrepP) 73 | 74 | local items = ELEMENTS 75 | local cycles = CYCLES 76 | local clock = os.clock 77 | 78 | if(printPrepP) then 79 | printf("%d elements in %-25s\n", items, prepP) 80 | end 81 | printf("%dx %-12s ", cycles, actionP) 82 | 83 | t = {} 84 | local i = 1 85 | local size = 0 86 | while(i <= items) do prepare(i); i = i + 1 end 87 | 88 | i = 1 89 | local last = nil 90 | local tm = clock() 91 | while(i <= cycles) do last = action(i); i = i + 1 end 92 | tm = clock() - tm 93 | if tm ~= 0 then 94 | mspc= nanosecs_per(tm, cycles * items) 95 | tab(27) 96 | printf("%10.0fns/element ", mspc) 97 | else 98 | mspc = nil 99 | printf("%dx %-12s sample too small, could not measure ", cycles, actionP) 100 | end 101 | 102 | return mspc, last 103 | end 104 | if(_PATCH) then io.write(_PATCH) else io.write(_VERSION .. ' official') end 105 | print(" - Fleece 0.3.1") 106 | 107 | 108 | local function measure3(prepP, prepare, prompt1, action1, prompt2, action2, prompt3, action3) 109 | 110 | print(sep) 111 | 112 | third, r3 = measure(prepP, prepare, prompt3, action3, true) 113 | printf(" %.20s.. \n", r3) 114 | end 115 | 116 | 117 | measure3("t[i]=i", 118 | function(i) t[i] = i end, 119 | "luajson.stringify(t)", 120 | function(i) return luajson.stringify(t) end, 121 | "json4.encode(t)", 122 | function(i) return json4.encode(t) end, 123 | "fleece.json(t)", 124 | function(i) return fleece.json(t) end 125 | ) 126 | 127 | measure3("t['x'..i]=i", 128 | function(i) t['x'..i] = i end, 129 | "luajson.stringify(t)", 130 | function(i) return luajson.stringify(t) end, 131 | "json4.encode(t)", 132 | function(i) return json4.encode(t) end, 133 | "fleece.json(t)", 134 | function(i) return fleece.json(t) end 135 | ) 136 | 137 | measure3("t[i]=randstr(i)", 138 | function(i) t[i] = randstr(i) end, 139 | "luajson.stringify(t)", 140 | function(i) return luajson.stringify(t) end, 141 | "json4.encode(t)", 142 | function(i) return json4.encode(t) end, 143 | "fleece.json(t)", 144 | function(i) return fleece.json(t) end 145 | ) 146 | 147 | measure3("t[randstr(i)]=randstr(i)", 148 | function(i) t[randstr(i)] = randstr(i) end, 149 | "luajson.stringify(t)", 150 | function(i) return luajson.stringify(t) end, 151 | "json4.encode(t)", 152 | function(i) return json4.encode(t) end, 153 | "fleece.json(t)", 154 | function(i) return fleece.json(t) end 155 | ) 156 | print(sep) 157 | 158 | end 159 | 160 | ELEMENTS = 10000 161 | CYCLES = 100 162 | all() 163 | 164 | print("Note that fleece may list associative arrays in different order.") 165 | -------------------------------------------------------------------------------- /test/test-fleece-hello.lua: -------------------------------------------------------------------------------- 1 | -- Fleece test file test-fleece-hello.lua (a dummy function) 2 | 3 | io.write("This tests, wether the Fleece library can be loaded at all:\n") 4 | io.write("Calling fleece.hello() -- should print: Hello LuaJSON World!: ") 5 | 6 | package.cpath="src/?.so" 7 | local fleece = require("fleece") 8 | 9 | fleece.hello() 10 | -------------------------------------------------------------------------------- /test/test-fleece-json.lua: -------------------------------------------------------------------------------- 1 | -- Fleece test file test-fleece-json.lua (the main thing in all this really) 2 | 3 | io.write("This tests, wether json() responds to a most simple call:\n") 4 | io.write("Calling fleece.json({'ok'}) -- should print [\"ok\"]: ") 5 | 6 | package.cpath="src/?.so" 7 | local fleece = require("fleece") 8 | 9 | print( fleece.json({"ok"}) ) 10 | -------------------------------------------------------------------------------- /test/test-fleece-size.lua: -------------------------------------------------------------------------------- 1 | -- Fleece test file test-fleece-size.lua 2 | 3 | print "Checking fleece.size() -- should print: 7" 4 | 5 | package.cpath="src/?.so" 6 | local fleece = require("fleece") 7 | 8 | print( fleece.size({1,2,3}) ) 9 | 10 | -------------------------------------------------------------------------------- /test/test-json4.lua: -------------------------------------------------------------------------------- 1 | -- Fleece test file hello-json4.lua (not fleece) 2 | 3 | print "Checking if JSON4 is working (for benchmarking against)" 4 | 5 | package.path="etc/json4/?.lua" 6 | local json4 = require("json") 7 | 8 | t = {'ok'} 9 | print(json4.encode(t)) 10 | 11 | -------------------------------------------------------------------------------- /test/test-luajson.lua: -------------------------------------------------------------------------------- 1 | -- Fleece test file hello-luajson.lua (not fleece) 2 | 3 | print "Checking if LuaJSON is working (for benchmarking against)" 4 | 5 | package.cpath="etc/luajson/?.so" 6 | local luajson = require("luajson") 7 | 8 | -- luajson.null = {_mt = {__tostring = function () return "null" end, __call = function () return "null" end}} 9 | -- base.setmetatable(luajson.null, luajson.null._mt) 10 | 11 | t = {'ok'} 12 | print(luajson.stringify(t)) 13 | 14 | -------------------------------------------------------------------------------- /test/unit/basic.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/unit/basics.lua --- 4 | --- Description : Fleece unit tests: basic set of tests --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : 05 Mar 2011 --- 10 | --- Changed : 05 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- Unit tests for Fleece. 14 | --- --- 15 | --- Use: make unit-tests --- 16 | --- --- 17 | ------------------------------------------------------------------------------- 18 | 19 | 20 | package.cpath="src/?.so" 21 | fleece = require("fleece") 22 | 23 | module( "basic", lunit.testcase, package.seeall ) 24 | 25 | ------------------------------------------------------------------------------- 26 | --- json --- 27 | ------------------------------------------------------------------------------- 28 | 29 | function test_string() 30 | assert_equal( '"hello"', fleece.json("hello")) 31 | assert_equal( '""', fleece.json("")) 32 | assert_equal( '"\""', fleece.json("\"")) 33 | end 34 | 35 | function test_integer() 36 | assert_equal( '0', fleece.json(0)) 37 | assert_equal( '1', fleece.json(1)) 38 | assert_equal( '-1', fleece.json(-1)) 39 | assert_equal( '1000', fleece.json(1000)) 40 | end 41 | 42 | function test_float() 43 | assert_equal( '0', fleece.json(0.0)) 44 | assert_equal( '1', fleece.json(1.0)) 45 | assert_equal( '-1', fleece.json(-1.0)) 46 | assert_equal( '1000', fleece.json(1000.0)) 47 | assert_equal( '0.1', fleece.json(0.1)) 48 | assert_equal( '1.1', fleece.json(1.1)) 49 | assert_equal( '-1.1', fleece.json(-1.1)) 50 | assert_equal( '1000.1', fleece.json(1000.1)) 51 | end 52 | 53 | function test_boolean() 54 | assert_equal( "true", fleece.json(true)) 55 | assert_equal( "false", fleece.json(false)) 56 | assert_not_equal( "true", fleece.json(True)) 57 | assert_not_equal( "false", fleece.json(False)) 58 | end 59 | 60 | function test_table() 61 | assert_equal( "[1]", fleece.json( {1} )) 62 | assert_equal( "[1,2,3]", fleece.json( {1,2,3} )) 63 | assert_equal( "[]", fleece.json( {} )) 64 | assert_equal( "[[]]", fleece.json( {{}} )) 65 | end 66 | 67 | ------------------------------------------------------------------------------- 68 | --- size --- 69 | ------------------------------------------------------------------------------- 70 | 71 | function test_size() 72 | 73 | assert_equal( 1, fleece.size( 1 )) 74 | assert_equal( 3, fleece.size( "1" )) 75 | assert_equal( 3, fleece.size( {1} )) 76 | 77 | assert_equal( string.len('0'), fleece.size(0.0)) 78 | assert_equal( string.len('1'), fleece.size(1.0)) 79 | assert_equal( string.len('-1'), fleece.size(-1.0)) 80 | assert_equal( string.len('1000'), fleece.size(1000.0)) 81 | -- assert_equal( string.len('0.1'), fleece.size(0.1)) -- TODO: WRONG! 82 | assert_equal( string.len('1.1'), fleece.size(1.1)) 83 | assert_equal( string.len('-1.1'), fleece.size(-1.1)) 84 | assert_equal( string.len('1000.1'), fleece.size(1000.1)) 85 | 86 | 87 | end 88 | -------------------------------------------------------------------------------- /test/unit/complex.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/unit/complex.lua --- 4 | --- Description : Fleece unit tests: more complex tests --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : 05 Mar 2011 --- 10 | --- Changed : 05 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- Complex unit tests for Fleece that take a little longer. These tests --- 14 | --- check fringe-conditions: overflows by one, or by very many consecutive --- 15 | --- brackets etc. Use it with smaller buffer sizes, around 100-1000, for --- 16 | --- best results. --- 17 | --- --- 18 | --- Use: in Fleece root directory 'make unit-tests' --- 19 | --- --- 20 | ------------------------------------------------------------------------------- 21 | 22 | 23 | package.cpath="src/?.so" 24 | fleece = require("fleece") 25 | 26 | module( "complex", lunit.testcase, package.seeall ) 27 | 28 | ------------------------------------------------------------------------------- 29 | -- utility 30 | ------------------------------------------------------------------------------- 31 | local abc = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 32 | 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} 33 | 34 | -- Creation of Random Strings 35 | function randstr(length) 36 | local r = {} 37 | local i 38 | for i = 1, length do 39 | r[i] = abc[math.random(1, 26)] 40 | end 41 | 42 | return table.concat(r) 43 | end 44 | 45 | -- Creation of Fixed Strings 46 | function repstr(length) 47 | 48 | if length == 0 then return "" end 49 | 50 | local r = {} 51 | local i 52 | for i = 1, length do 53 | r[i] = abc[(i-1) % 26 + 1] 54 | end 55 | 56 | return table.concat(r) 57 | end 58 | 59 | -- Creation of nested tables 60 | function nest(depth, inner) 61 | if depth == 0 then 62 | t = {}; t[1] = inner; return t, "[\""..inner.."\"]" end 63 | gett, gets = nest(depth-1,inner) 64 | ret = { gett } 65 | res = "[" .. gets .. "]" 66 | return ret, res 67 | end 68 | 69 | ------------------------------------------------------------------------------- 70 | -- tests 71 | ------------------------------------------------------------------------------- 72 | 73 | 74 | -- strings -------------------------------------------------------------------- 75 | 76 | function subtest_string(min, max) 77 | 78 | sx = complex.repstr(max) 79 | for i = min, max do 80 | s = sx:sub(min,i) 81 | exp = '"'..s..'"' 82 | io.write(string.format("test string of length %8d %.20s..", i, exp)) 83 | io.flush() 84 | assert_equal( exp, fleece.json(s)) 85 | print(" ok") 86 | end 87 | end 88 | 89 | function ptest_string() 90 | 91 | io.write(" strings ...") 92 | 93 | -- test all strings of length 1 to 1110 94 | subtest_string(1,1110) 95 | 96 | -- test all strings of length 9999 to 10099 97 | subtest_string(9999,10099) 98 | 99 | -- test all strings of length 10999 to 11099 100 | subtest_string(10999,11099) 101 | 102 | -- test all strings of length 999999 to 1000003 103 | subtest_string(999999,1000003) 104 | 105 | -- test 0 length 106 | subtest_string(0,0) 107 | 108 | end 109 | 110 | 111 | -- table ---------------------------------------------------------------------- 112 | 113 | function subtest_table(depth, innerlength) 114 | 115 | t,s = nest(depth, repstr(innerlength)) 116 | print("table nesting " .. 117 | string.format("%5d", depth).. 118 | " levels deep, with inside string of length " .. 119 | string.format("%5d", innerlength).. 120 | " " .. s:sub(1,20) .. "..") 121 | assert_equal( s, fleece.json( t )) 122 | 123 | end 124 | 125 | function test_table() 126 | 127 | io.write(" tables ...") 128 | 129 | -- these numbers are used both for nest depth and the 130 | -- inner string's length. See output when running 131 | interesting = { 0,1,2,3,4,5,6,7,8,9,10,11,12, 132 | 97,98,99,100,101,102, 133 | 997,998,999,1000,1001,1002, 134 | 9997,9998,9999,10000,10001,10002, 135 | 10097,10098,10099,10100,10101,10102 } 136 | 137 | for i = 1, #interesting do 138 | for j = 1, #interesting do 139 | depth = interesting[i] 140 | innerlen = interesting[j] 141 | 142 | subtest_table(depth, innerlen) 143 | end 144 | end 145 | 146 | print("'Complex' tests done." ) 147 | print("Note that the last test is most effective when setting, in fleece-config.h,") 148 | print("FLEECE_DEFAULT_LUCKY_BUF to 1000 and FLEECE_DEFAULT_PART_BUF to 100") 149 | print("and useless if FLEECE_DEFAULT_LUCKY_BUF is set > ~10,100.") 150 | 151 | 152 | end 153 | 154 | 155 | -------------------------------------------------------------------------------- /test/unit/parameters.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/unit/parameters.lua --- 4 | --- Description : Fleece unit tests: focus on parameters --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : 08 Mar 2011 --- 10 | --- Changed : 08 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- Unit tests for Fleece, with a focus on parameters to function calls. --- 14 | --- --- 15 | --- Use: make unit-tests --- 16 | --- --- 17 | ------------------------------------------------------------------------------- 18 | 19 | 20 | package.cpath="src/?.so" 21 | fleece = require("fleece") 22 | 23 | module( "parameters", lunit.testcase, package.seeall ) 24 | 25 | ------------------------------------------------------------------------------- 26 | 27 | -- used for finding bugs 28 | function disect(x) 29 | 30 | print("disecting " .. x) 31 | len = x:len() 32 | c = 1 33 | while(c <= len) do 34 | print(c .. ": " .. x:sub(c,c) .. " (" .. string.byte(x:sub(c,c)) .. ")") 35 | c = c + 1 36 | end 37 | 38 | end 39 | 40 | function test_single_and_none() 41 | 42 | --/* warm up: hello, empty string, empty table */ 43 | assert_equal( '"hello"', fleece.json("hello")) 44 | assert_equal( '""', fleece.json("")) 45 | assert_equal( '[]', fleece.json({})) 46 | 47 | --/* main escapers, " and \ */ 48 | assert_equal( '"""', fleece.json("\"")) 49 | assert_equal( '"\\"', fleece.json("\\")) 50 | 51 | assert_equal( '"""', fleece.json("\"", "E0")) 52 | assert_equal( '"\\"', fleece.json("\\", "E0")) 53 | 54 | assert_equal( '"*"', fleece.json("\"", "E1")) 55 | assert_equal( '"*"', fleece.json("\\", "E1")) 56 | 57 | assert_equal( '"\\\""', fleece.json("\"", "E2")) 58 | assert_equal( '"\\\\"', fleece.json("\\", "E2")) 59 | 60 | assert_equal( '"\\\""', fleece.json("\"", "E3")) 61 | assert_equal( '"\\\\"', fleece.json("\\", "E3")) 62 | 63 | assert_equal( '"\\\""', fleece.json("\"", "E")) 64 | assert_equal( '"\\\\"', fleece.json("\\", "E")) 65 | end 66 | 67 | function test_multiple_escape_strings() 68 | 69 | assert_equal( '"\\\\\\\""', fleece.json("\\\"", "E")) 70 | assert_equal( '"\'\\\\\'"', fleece.json("'\\'", "E")) 71 | 72 | assert_equal( '"\\b"', fleece.json("\b", "E")) 73 | assert_equal( '"\\t\\t\\t \\t"', fleece.json("\t\t\t \t", "E")) 74 | assert_equal( '"\\r\\n"', fleece.json("\r\n", "E")) 75 | assert_equal( '"Hello!\\r\\nWorld!"', fleece.json("Hello!\r\nWorld!", "E")) 76 | 77 | assert_equal( '"\\u0001\\u0001\\u0001"', 78 | fleece.json(string.char(1)..string.char(1)..string.char(1), "E")) 79 | -- depends on FLEECE_ZERO_AS_END 80 | -- assert_equal( '"\\u0000x\\u0000"', 81 | -- fleece.json(string.char(0).."x"..string.char(0), "E")) 82 | -- assert_equal( '"\\u0003\\u0000\\u0004"', 83 | -- fleece.json(string.char(3)..string.char(0)..string.char(4), "E")) 84 | end 85 | 86 | -------------------------------------------------------------------------------- /test/unit/test.lua: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------------------- 2 | --- Package : Fleece - fast Lua to JSON module --- 3 | --- File : test/unit/basics.lua --- 4 | --- Description : Fleece unit tests: basic set of tests --- 5 | --- Version : 0.3.1 / alpha --- 6 | --- Copyright : 2011 Henning Diedrich, Eonblast Corporation --- 7 | --- Author : H. Diedrich --- 8 | --- License : see file LICENSE --- 9 | --- Created : 05 Mar 2011 --- 10 | --- Changed : 05 Mar 2011 --- 11 | ------------------------------------------------------------------------------- 12 | --- --- 13 | --- Unit tests for Fleece. 14 | --- --- 15 | --- Use: lua test/bench-fleece.lua --- 16 | --- Or: make basic-tests # --- 17 | --- Or: make unit-tests # --- 18 | --- --- 19 | ------------------------------------------------------------------------------- 20 | 21 | function pre() 22 | print("Fleece Unit Tets") 23 | print("=========================================================") 24 | end 25 | 26 | package.cpath="src/?.so" 27 | fleece = require("fleece") 28 | -- package.path="etc/lunit/lunit.lua" 29 | -- require("lunit") 30 | 31 | module( "test", lunit.testcase, package.seeall ) 32 | 33 | function test_string() 34 | assert_equal( '"hello"', fleece.json("hello")) 35 | end 36 | 37 | function test_boolean() 38 | assert_equal( "true", fleece.json(true)) 39 | assert_equal( "false", fleece.json(false)) 40 | assert_not_equal( "true", fleece.json(True)) 41 | assert_not_equal( "false", fleece.json(False)) 42 | end 43 | 44 | function test_table() 45 | assert_equal( "[1]", fleece.json( {1} )) 46 | assert_equal( "[1,2,3]", fleece.json( {1,2,3} )) 47 | assert_equal( "[]", fleece.json( {} )) 48 | assert_equal( "[[]]", fleece.json( {{}} )) 49 | end 50 | --------------------------------------------------------------------------------