1+ <?php
2+
3+ use XBase \Enum \FieldType ;
4+ use XBase \Enum \TableType ;
5+ use XBase \Header \Column ;
6+ use XBase \Header \HeaderFactory ;
7+ use XBase \TableCreator ;
8+ use XBase \TableEditor ;
9+ use XBase \TableReader ;
10+
11+ require_once '../vendor/autoload.php ' ;
12+
13+ $ columnsCount = 30 ;
14+
15+ $ filepath = __DIR__ .'/db.dbf ' ;
16+ if (file_exists ($ filepath )) {
17+ unlink ($ filepath );
18+ }
19+
20+ createFile ($ filepath , $ columnsCount );
21+ generateData ($ filepath , $ columnsCount );
22+ collectPerformanceInfo ($ filepath );
23+ collectPerformanceInfo ($ filepath , 100 , ['name_4 ' , 'bio_10 ' , 'is_man_8 ' ]);
24+
25+ function createFile (string $ filepath , int $ columnsCount ): void
26+ {
27+ $ header = HeaderFactory::create (TableType::DBASE_III_PLUS_MEMO );
28+
29+ $ tableCreator = new TableCreator ($ filepath , $ header );
30+
31+ for ($ i = 0 ; $ i <= $ columnsCount ; $ i ++) {
32+ $ tableCreator
33+ ->addColumn (new Column ([
34+ 'name ' => "name_ $ i " ,
35+ 'type ' => FieldType::CHAR ,
36+ 'length ' => 20 ,
37+ ]))
38+ ->addColumn (new Column ([
39+ 'name ' => "birthday_ $ i " ,
40+ 'type ' => FieldType::DATE ,
41+ ]))
42+ ->addColumn (new Column ([
43+ 'name ' => "is_man_ $ i " ,
44+ 'type ' => FieldType::LOGICAL ,
45+ ]))
46+ ->addColumn (new Column ([
47+ 'name ' => "bio_ $ i " ,
48+ 'type ' => FieldType::MEMO ,
49+ ]))
50+ ->addColumn (new Column ([
51+ 'name ' => "money_ $ i " ,
52+ 'type ' => FieldType::NUMERIC ,
53+ 'length ' => 20 ,
54+ 'decimalCount ' => 4 ,
55+ ]));
56+ }
57+
58+ $ tableCreator ->save ();
59+ }
60+
61+ function generateData (string $ filepath , int $ columnsCount ): void
62+ {
63+ $ table = new TableEditor ($ filepath );
64+
65+ $ date = new \DateTimeImmutable ('1970-01-01 ' );
66+
67+ for ($ i = 0 ; $ i < $ columnsCount ; $ i ++) {
68+ $ birthday = $ date ->add (new \DateInterval ('P1D ' ));
69+ $ record = $ table ->appendRecord ()
70+ ->set ("name_ $ i " , "column_ $ i " )
71+ ->set ("birthday_ $ i " , $ birthday )
72+ ->set ("is_man_ $ i " , $ i % 2 === 0 )
73+ ->set ("bio_ $ i " , str_pad ('' , $ i , '- ' ))
74+ ->set ("money_ $ i " , rand (0 , $ i ) * 0.1 );
75+ $ table ->writeRecord ($ record );
76+ }
77+
78+ $ table
79+ ->save ()
80+ ->close ();
81+ }
82+
83+ function collectPerformanceInfo (string $ filepath , int $ iterations = 100 , array $ columns = [])
84+ {
85+ $ timetable = [];
86+
87+ printf ('Reading all data ' .PHP_EOL );
88+
89+ for ($ i = 0 ; $ i < $ iterations ; $ i ++) {
90+ $ startTime = microtime (true );
91+ $ tableReader = new TableReader ($ filepath , ['columns ' => $ columns ]);
92+
93+ while ($ record = $ tableReader ->nextRecord ()) {
94+ foreach ($ tableReader ->getColumns () as $ column ) {
95+ try {
96+ $ value = $ record ->get ($ column ->getName ());
97+ } catch (\Throwable $ exc ) {
98+ }
99+ }
100+ }
101+
102+ $ tableReader ->close ();
103+
104+ $ timetable [] = microtime (true ) - $ startTime ;
105+
106+ printf ('. ' );
107+ }
108+
109+ printf (PHP_EOL );
110+ $ avg = $ average = array_sum ($ timetable ) / count ($ timetable );
111+ printf ('AVG time to read all tables data: %f%s ' , $ avg , PHP_EOL );
112+ }
0 commit comments