forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
120 lines (86 loc) · 3.06 KB
/
benchmark.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import logging
import pytest
import random
import utils
from concurrent import futures
from test_lightningd import NodeFactory
from time import time
from tqdm import tqdm
num_workers = 480
num_payments = 10000
@pytest.fixture
def executor():
ex = futures.ThreadPoolExecutor(max_workers=num_workers)
yield ex
ex.shutdown(wait=False)
@pytest.fixture(scope="module")
def bitcoind():
bitcoind = utils.BitcoinD(rpcport=28332)
bitcoind.start()
info = bitcoind.rpc.getblockchaininfo()
# Make sure we have segwit and some funds
if info['blocks'] < 432:
logging.debug("SegWit not active, generating some more blocks")
bitcoind.generate_block(432 - info['blocks'])
yield bitcoind
try:
bitcoind.rpc.stop()
except Exception:
bitcoind.proc.kill()
bitcoind.proc.wait()
@pytest.fixture
def node_factory(request, bitcoind, executor):
nf = NodeFactory(request.node.name, bitcoind, executor)
yield nf
nf.killall([False] * len(nf.nodes))
def test_single_hop(node_factory, executor):
l1 = node_factory.get_node()
l2 = node_factory.get_node()
l1.rpc.connect(l2.rpc.getinfo()['id'], 'localhost:%d' % l2.port)
l1.openchannel(l2, 4000000)
print("Collecting invoices")
fs = []
invoices = []
for i in tqdm(range(num_payments)):
invoices.append(l2.rpc.invoice(1000, 'invoice-%d' % (i), 'desc')['payment_hash'])
route = l1.rpc.getroute(l2.rpc.getinfo()['id'], 1000, 1)['route']
print("Sending payments")
start_time = time()
def do_pay(i):
p = l1.rpc.sendpay(route, i)
r = l1.rpc.waitsendpay(p['payment_hash'])
return r
for i in invoices:
fs.append(executor.submit(do_pay, i))
for f in tqdm(futures.as_completed(fs), total=len(fs)):
f.result()
diff = time() - start_time
print("Done. %d payments performed in %f seconds (%f payments per second)" % (num_payments, diff, num_payments / diff))
def test_single_payment(node_factory, benchmark):
l1 = node_factory.get_node()
l2 = node_factory.get_node()
l1.rpc.connect(l2.rpc.getinfo()['id'], 'localhost:%d' % l2.port)
l1.openchannel(l2, 4000000)
def do_pay(l1, l2):
invoice = l2.rpc.invoice(1000, 'invoice-{}'.format(random.random()), 'desc')['bolt11']
l1.rpc.pay(invoice)
benchmark(do_pay, l1, l2)
def test_invoice(node_factory, benchmark):
l1 = node_factory.get_node()
def bench_invoice():
l1.rpc.invoice(1000, 'invoice-{}'.format(time()), 'desc')
benchmark(bench_invoice)
def test_pay(node_factory, benchmark):
l1 = node_factory.get_node()
l2 = node_factory.get_node()
l1.rpc.connect(l2.rpc.getinfo()['id'], 'localhost:%d' % l2.port)
l1.openchannel(l2, 4000000)
invoices = []
for _ in range(1, 100):
invoice = l2.rpc.invoice(1000, 'invoice-{}'.format(random.random()), 'desc')['bolt11']
invoices.append(invoice)
def do_pay(l1, l2):
l1.rpc.pay(invoices.pop())
benchmark(do_pay, l1, l2)
def test_start(node_factory, benchmark):
benchmark(node_factory.get_node)