1
+ # -*- coding: utf-8 -*-
2
+
3
+ from __future__ import absolute_import , print_function , unicode_literals
4
+
5
+ from wolframclient .cli .utils import SimpleCommand
6
+ from wolframclient .language .expression import wl
7
+ from wolframclient .serializers import export
8
+ from wolframclient .utils .debug import timed
9
+ from wolframclient .utils .decorators import to_tuple
10
+ from wolframclient .utils .encoding import force_text
11
+ from wolframclient .utils .functional import first
12
+
13
+ import os
14
+ import tempfile
15
+
16
+ class Command (SimpleCommand ):
17
+
18
+ col_size = 8
19
+ repetitions = 10
20
+ complexity = [1 , 2 , 5 , 10 , 100 , 1000 ]
21
+
22
+ @to_tuple
23
+ def complexity_handler (self , complexity ):
24
+ for i in range (complexity ):
25
+ yield [wl .Symbol , {"a" : [1 , 2 , 3 ], 2 :2 }]
26
+
27
+ @timed
28
+ def export (self , * args , ** opts ):
29
+ return export (* args , ** opts )
30
+
31
+ def formatted_time (self , * args , ** opts ):
32
+
33
+ time = sum (
34
+ first (self .export (* args , ** opts ))
35
+ for i in range (self .repetitions )
36
+ )
37
+
38
+ return '%.5f' % (time / self .repetitions )
39
+
40
+ def table_line (self , * iterable ):
41
+ self .print (* (force_text (c ).ljust (self .col_size ) for c in iterable ))
42
+
43
+ def table_divider (self , length ):
44
+ self .print (* ("-" * self .col_size for i in range (length )))
45
+
46
+ def handle (self , * args ):
47
+
48
+ path = tempfile .gettempdir ()
49
+
50
+ benchmarks = [
51
+ (c , self .complexity_handler (c ))
52
+ for c in self .complexity
53
+ ]
54
+
55
+ self .print ('dumping results in' , path )
56
+
57
+ self .table_line ("" , * (force_text (c ).ljust (self .col_size ) for c in self .complexity ))
58
+ self .table_divider (len (self .complexity ) + 1 )
59
+
60
+ for export_format , opts in (
61
+ ("wl" , dict ()),
62
+ ("wxf" , dict ()),
63
+ ("wxf" , dict (compress = True )),
64
+ ):
65
+ self .table_line (export_format , * (
66
+ self .formatted_time (
67
+ expr ,
68
+ stream = os .path .join (path , 'benchmark-test-%s.%s' % (force_text (complexity ).zfill (7 ), export_format )),
69
+ format = export_format ,
70
+ ** opts
71
+ )
72
+ for complexity , expr in benchmarks
73
+ ))
74
+
75
+ self .table_line ()
0 commit comments