Skip to content
Permalink
Browse files

Refactor

  • Loading branch information...
Mec-iS committed Aug 18, 2019
1 parent dbed202 commit 3bd6158f1ada45cef0fab1dff2247961e055a6b6
@@ -1 +1,4 @@

"""
pyWasm
"""
from .src.utils import *
@@ -0,0 +1,54 @@
from shapely.geometry import MultiPoint

def py_simple_add(a, b):
return a + b

def py_fibonacci(n):
"""https://stackoverflow.com/a/52133289/2536357"""
i = 0
nextterm = 1
present = 1
previous = 0

while i < n:
nextterm = present + previous
present = previous
previous = nextterm
i = i + 1

return nextterm

def py_shapely_convex_hull(shp=None):
if shp is None:
shp = tuple([
(0.0, 0.0),
(4.0, 0.0),
(4.0, 1.0),
(1.0, 1.0),
(1.0, 4.0),
(0.0, 4.0),
(0.0, 0.0),
])

MultiPoint(shp).convex_hull

def py_reverse_string(string):
return string[::1]

try:
from pywasm.src.utils import allocate_bytes, write_to_memory, address_to_utf8
except:
import sys
from os.path import abspath, dirname
sys.path.insert(0, abspath(dirname(dirname(dirname(__file__)))))
from pywasm.src.utils import allocate_bytes, write_to_memory, address_to_utf8

def test_reverse(instance, func, bytestr):
"""A simple string reversing test using Wasm memory"""
mem_view = allocate_bytes(instance)

mem_view = write_to_memory(bytestr, mem_view, offset=0)

result = func(0, len(bytestr))

return address_to_utf8(mem_view, result, len(bytestr))
@@ -30,7 +30,7 @@ def fibo_wasm(n):
def fibo_py(n):
n = int(n)

from src_py import py_fibonacci
from examples import py_fibonacci

if n < 25:
try:
@@ -72,7 +72,7 @@ def convexhull_wasm(n=0):
def convexhull_py(n=0):
n = int(n)

from src_py import py_shapely_convex_hull
from examples import py_shapely_convex_hull

if n < 25:
try:

This file was deleted.

@@ -2,4 +2,4 @@


if __name__ == '__main__':
pytest.main(['examples/flask_basic/tests.py', '-s'])
pytest.main(['pywasm/examples/flask_basic/tests.py', '-s'])
@@ -7,15 +7,17 @@
import wasmer
from wasmer import Instance

from src_py import *
from examples import *

path = '../target/wasm32-unknown-unknown/release/rust_wasm_python_101.wasm'
path = join(dirname(dirname(abspath(__file__))), 'target', 'wasm32-unknown-unknown', 'release', 'rust_wasm_python_101.wasm')

print('>>>', path)

from os.path import join, dirname

context = '''
context = f'''
from wasmer import Instance
path = '../target/wasm32-unknown-unknown/release/rust_wasm_python_101.wasm'
path = "{path}"
wasm_bytes = open(path, 'rb').read()
instance = Instance(wasm_bytes)
simple_add = instance.exports.simple_add
@@ -46,21 +48,21 @@ def run_timing():

results['data'] = {}

t_py = timeit.timeit('[py_simple_add(n-1, n) for n in range(100, 1000)]', number=10000, setup='from src_py import py_simple_add')
t_py = timeit.timeit('[py_simple_add(n-1, n) for n in range(100, 1000)]', number=10000, setup='from examples import py_simple_add')
print('py add', t_py)

t_wasm = timeit.timeit('[simple_add(n-1, n) for n in range(100, 1000)]', number=10000, setup=context+'simple_add = instance.exports.simple_add')
print('t_wasm add', t_wasm)
results['data']['simple_add'] = { 'py': t_py, 'wasm': t_wasm }

t_py = timeit.timeit('[py_fibonacci(n) for n in range(100, 1000)]', number=1000, setup='from src_py import py_fibonacci')
t_py = timeit.timeit('[py_fibonacci(n) for n in range(100, 1000)]', number=1000, setup='from examples import py_fibonacci')
print('py fibo', t_py)

t_wasm = timeit.timeit('[fibo(n) for n in range(100, 1000)]', number=1000, setup=context+'fibo = instance.exports.fibo')
print('t_wasm fibo', t_wasm)
results['data']['fibonacci'] = { 'py': t_py, 'wasm': t_wasm }

t_py = timeit.timeit('[py_shapely_convex_hull() for n in range(100, 1000)]', number=1000, setup='from src_py import py_shapely_convex_hull')
t_py = timeit.timeit('[py_shapely_convex_hull() for n in range(100, 1000)]', number=1000, setup='from examples import py_shapely_convex_hull')
print('py shapely convex hull', t_py)

t_wasm = timeit.timeit('[rust_geo_convex_hull() for n in range(100, 1000)]', number=1000, setup=context+'rust_geo_convex_hull = instance.exports.rust_geo_convex_hull')
@@ -1,10 +1,11 @@
from os.path import dirname, join
import timeit

from wasmer import Instance

from src_py import *
from examples import *

path = '../target/wasm32-unknown-unknown/release/rust_wasm_python_101.wasm'
path = join(dirname(dirname(__file__)), 'target/wasm32-unknown-unknown/release/rust_wasm_python_101.wasm')

def run_test():
with open(path, 'rb') as bytecode:
@@ -27,11 +28,10 @@ def run_test():
print("call simple_add(12, 12): ")
print(result)

from src_py.str_alloc import test_reverse
test_str = b'Test sTRing'
result = test_reverse(instance.memory, reverse_string, test_str)
result = test_reverse(instance, reverse_string, test_str)

print(f'Reversing {test_str} >')
print(f'Reversing {test_str} >>>')
print(result)


@@ -0,0 +1,9 @@
from distutils.core import setup

setup(
name='pywasm',
version='0.1dev',
packages=['pywasm',],
license='Apache 2.0',
long_description=open('README.md').read(),
)
No changes.
@@ -0,0 +1,23 @@
"""
Memeory Allocation Utilities
"""
def allocate_bytes(instance, size=8):
"""Create a view for an instance"""
mem = instance.memory
return mem.uint8_view()

def write_to_memory(bytestr, mem_view, offset=0):
"""Fill memory with values"""
for i, c in enumerate(bytestr):
mem_view[i] = c
return mem_view

def address_to_utf8(mem_view, address, length):
"""Read from address into UTF-8"""
codes = mem_view[address:address+length]

string_ = ''
for i, c in enumerate(codes):
string_ += chr(c)

return string_.encode()

This file was deleted.

This file was deleted.

@@ -7,3 +7,4 @@ timestap,python_wasmer_v,rustc_v,data
2019-07-26 15:24:15.230455,0.3.0|0.5.5,rustc 1.38.0-nightly (890881f8f 2019-07-25),"{'simple_add': {'py': 1.6466156440001214, 'wasm': 9.031503515000168}, 'fibonacci': {'py': 45.21274620200029, 'wasm': 1.2782949410002402}, 'convex_hull': {'py': 49.05368046700005, 'wasm': 2.2740365139998175}}"
2019-08-09 12:42:58.602595,0.3.0|0.5.5,rustc 1.38.0-nightly (2d1a551e1 2019-08-08),"{'simple_add': {'py': 1.2286460770001213, 'wasm': 8.336394050999843}, 'fibonacci': {'py': 45.084232722000706, 'wasm': 1.2134531949996017}, 'convex_hull': {'py': 45.251383527000144, 'wasm': 2.2230020449997028}}"
2019-08-17 12:36:29.856542,0.3.0|0.6.0,rustc 1.38.0-nightly (2d1a551e1 2019-08-08),"{'simple_add': {'py': 1.2221312599999692, 'wasm': 8.259179609000057}, 'fibonacci': {'py': 44.733623436000016, 'wasm': 1.2072560749998047}, 'convex_hull': {'py': 45.49324563399978, 'wasm': 2.14174346100026}}"
2019-08-18 16:14:00.663364,0.3.0|0.6.0,rustc 1.38.0-nightly (2d1a551e1 2019-08-08),"{'simple_add': {'py': 1.238140885001485, 'wasm': 8.309190371001023}, 'fibonacci': {'py': 41.6909912120027, 'wasm': 1.2667080979990715}, 'convex_hull': {'py': 48.262130475999584, 'wasm': 2.163790942002379}}"

0 comments on commit 3bd6158

Please sign in to comment.
You can’t perform that action at this time.