• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import {
17    fileIO, FILE_CONTENT, prepareFile, prepare200MFile, nextFileName, isIntNum, describe, it, expect,
18} from '../Common';
19
20const RAND_READ_BASELINE = 3.5;
21const RAND_WRITE_BASELINE = 0.5;
22const SEQUENCE_READ_BASELINE = 15;
23const SEQUENCE_WRITE_BASELINE = 5;
24
25export default function FileIOFilerw_test() {
26    describe('FileIO_fs_filerw_test', function () {
27
28        async function IOfunc(file, bf, total, rand, read, promise) {
29            let startTime = 0;
30            try {
31                let array = new ArrayBuffer(bf);
32                let count = total / bf;
33                startTime = new Date().getTime();
34                for (let i = 0; i < count; i++) {
35                    let offset = i * bf;
36                    if (rand) {
37                        offset = Math.floor(Math.random() * (total - bf));
38                    }
39                    if (promise) {
40                        if (read) {
41                            fileIO.read(file.fd, array, { length: bf, offset: offset }).then((readLen) => {
42                            }).catch((e) => {
43                                console.log('fileIO_test_read_promise has failed for ' + e.message + ', code: ' + e.code);
44                                return -1;
45                            });
46                        } else {
47                            fileIO.write(file.fd, array, { length: bf, offset: offset, encoding: 'utf-8' }).then((writeLen) => {
48                            }).catch((e) => {
49                                console.log('fileIO_test_write_promise has failed for ' + e.message + ', code: ' + e.code);
50                                return -1;
51                            });
52                        }
53                    } else {
54                        if (read) {
55                            fileIO.read(file.fd, array, { length: bf, offset: offset }, (err, readLen) => {
56                                if (err) {
57                                    console.log('fileIO_test_read_callback has failed for ' + err.message + ', code: ' + err.code);
58                                    return -1;
59                                }
60                            });
61                        } else {
62                            fileIO.write(file.fd, array, { length: bf, offset: offset, encoding: 'utf-8' }, (err, writeLen) => {
63                                if (err) {
64                                    console.log('fileIO_test_write_callback has failed for ' + err.message + ', code: ' + err.code);
65                                    return -1;
66                                }
67                            });
68                        }
69                    }
70                }
71                fileIO.fsyncSync(file.fd);
72            } catch (e) {
73                console.log('FileIO_fs_filerw_test has failed for ' + e.message + ', code: ' + e.code);
74            } finally {
75                let endTime = new Date().getTime();
76                console.log("FileIO_fs_filerw_test finally" + JSON.stringify(endTime))
77                var interval = endTime - startTime;
78                var average = total * 1000 / interval;
79                var averagerate = average / 0x100000;
80                return averagerate;
81            }
82        }
83
84        /**
85         * @tc.number SUB_DF_FILEIO_FILERW_READ_RANDOM_PROMISE_0000
86         * @tc.name fileIO_test_read_random_promise_000
87         * @tc.desc Test the read() interface. promise.
88         * Open the file, file read() in the random way.
89         * @tc.size MEDIUM
90         * @tc.type Function
91         * @tc.level Level 3
92         * @tc.require
93         */
94        it('fileIO_test_read_random_promise_000', 0, async function (done) {
95            let fpath = await nextFileName('fileIO_test_read_random_promise_000');
96            expect(prepare200MFile(fpath)).assertTrue;
97
98            try {
99                let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY);
100                expect(isIntNum(file.fd)).assertTrue();
101                let averagerate = await IOfunc(file, 0x1400, 0xC800000, true, true, true);
102                console.log("RAND_READ Rate is" + averagerate + "M/s");
103                expect(averagerate > RAND_READ_BASELINE).assertTrue();
104                fileIO.closeSync(file);
105                fileIO.unlinkSync(fpath);
106                done();
107            } catch (e) {
108                console.log('fileIO_test_read_random_promise_000 has failed for ' + e.message + ', code: ' + e.code);
109                expect(false).assertTrue();
110            }
111        });
112
113        /**
114         * @tc.number SUB_DF_FILEIO_FILERW_WRITE_RANDOM_PROMISE_0000
115         * @tc.name fileIO_test_write_random_promise_000
116         * @tc.desc Test the write() interface. promise.
117         * Open the file, file write() in the random way.
118         * @tc.size MEDIUM
119         * @tc.type Function
120         * @tc.level Level 3
121         * @tc.require
122         */
123        it('fileIO_test_write_random_promise_000', 0, async function (done) {
124            let fpath = await nextFileName('fileIO_test_write_random_promise_000');
125            expect(prepare200MFile(fpath)).assertTrue;
126
127            try {
128                let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE);
129                expect(isIntNum(file.fd)).assertTrue();
130                let averagerate = await IOfunc(file, 0x1400, 0xC800000, true, false, true);
131                console.log("RAND_WRITE Rate is" + averagerate + "M/s");
132                expect(averagerate > RAND_WRITE_BASELINE).assertTrue();
133                fileIO.closeSync(file);
134                fileIO.unlinkSync(fpath);
135                done();
136            } catch (e) {
137                console.log('fileIO_test_write_random_promise_000 has failed for ' + e.message + ', code: ' + e.code);
138                expect(false).assertTrue();
139            }
140        });
141
142        /**
143         * @tc.number SUB_DF_FILEIO_FILERW_READ_SEQUENCE_PROMISE_0000
144         * @tc.name fileIO_test_read_sequence_promise_000
145         * @tc.desc Test the read() interface. promise.
146         * Open the file, file read() in the sequence way.
147         * @tc.size MEDIUM
148         * @tc.type Function
149         * @tc.level Level 3
150         * @tc.require
151         */
152        it('fileIO_test_read_sequence_promise_000', 0, async function (done) {
153            let fpath = await nextFileName('fileIO_test_read_sequence_promise_000');
154            expect(prepare200MFile(fpath)).assertTrue;
155
156            try {
157                let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY);
158                expect(isIntNum(file.fd)).assertTrue();
159                let averagerate = await IOfunc(file, 0x1400, 0xC800000, false, true, true);
160                console.log("SEQUENCE_READ Rate is" + averagerate + "M/s");
161                expect(averagerate > SEQUENCE_READ_BASELINE).assertTrue();
162                fileIO.closeSync(file);
163                fileIO.unlinkSync(fpath);
164                done();
165            } catch (e) {
166                console.log('fileIO_test_read_sequence_promise_000 has failed for ' + e.message + ', code: ' + e.code);
167                expect(false).assertTrue();
168            }
169        });
170
171        /**
172         * @tc.number SUB_DF_FILEIO_FILERW_WRITE_SEQUENCE_PROMISE_0000
173         * @tc.name fileIO_test_write_sequence_promise_000
174         * @tc.desc Test the write() interface. promise.
175         * Open the file, file write() in the sequence way.
176         * @tc.size MEDIUM
177         * @tc.type Function
178         * @tc.level Level 3
179         * @tc.require
180         */
181        it('fileIO_test_write_sequence_promise_000', 0, async function (done) {
182            let fpath = await nextFileName('fileIO_test_write_sequence_promise_000');
183            expect(prepare200MFile(fpath)).assertTrue;
184
185            try {
186                let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE);
187                expect(isIntNum(file.fd)).assertTrue();
188                let averagerate = await IOfunc(file, 0x1400, 0xC800000, false, false, true);
189                console.log("SEQUENCE_WRITE Rate is" + averagerate + "M/s");
190                expect(averagerate > SEQUENCE_WRITE_BASELINE).assertTrue();
191                fileIO.closeSync(file);
192                fileIO.unlinkSync(fpath);
193                done();
194            } catch (e) {
195                console.log('fileIO_test_write_sequence_promise_000 has failed for ' + e.message + ', code: ' + e.code);
196                expect(false).assertTrue();
197            }
198        });
199
200        /**
201         * @tc.number SUB_DF_FILEIO_FILERW_READ_RANDOM_CALLBACK_0000
202         * @tc.name fileIO_test_read_random_callback_000
203         * @tc.desc Test the read() interface. callback.
204         * Open the file, file read() in the random way.
205         * @tc.size MEDIUM
206         * @tc.type Function
207         * @tc.level Level 3
208         * @tc.require
209         */
210        it('fileIO_test_read_random_callback_000', 0, async function (done) {
211            let fpath = await nextFileName('fileIO_test_read_random_callback_000');
212            expect(prepare200MFile(fpath)).assertTrue;
213
214            try {
215                let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY);
216                expect(isIntNum(file.fd)).assertTrue();
217                let averagerate = await IOfunc(file, 0x1400, 0xC800000, true, true, false);
218                console.log("RAND_READ Rate is" + averagerate + "M/s");
219                expect(averagerate > RAND_READ_BASELINE).assertTrue();
220                fileIO.closeSync(file);
221                fileIO.unlinkSync(fpath);
222                done();
223            } catch (e) {
224                console.log('fileIO_test_read_random_callback_000 has failed for ' + e.message + ', code: ' + e.code);
225                expect(false).assertTrue();
226            }
227        });
228
229        /**
230         * @tc.number SUB_DF_FILEIO_FILERW_WRITE_RANDOM_CALLBACK_0000
231         * @tc.name fileIO_test_write_random_callback_000
232         * @tc.desc Test the write() interface. callback.
233         * Open the file, file write() in the random way.
234         * @tc.size MEDIUM
235         * @tc.type Function
236         * @tc.level Level 3
237         * @tc.require
238         */
239        it('fileIO_test_write_random_callback_000', 0, async function (done) {
240            let fpath = await nextFileName('fileIO_test_write_random_callback_000');
241            expect(prepare200MFile(fpath)).assertTrue;
242
243            try {
244                let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE);
245                expect(isIntNum(file.fd)).assertTrue();
246                let averagerate = await IOfunc(file, 0x1400, 0xC800000, true, false, false);
247                console.log("RAND_WRITE Rate is" + averagerate + "M/s");
248                expect(averagerate > RAND_WRITE_BASELINE).assertTrue();
249                fileIO.closeSync(file);
250                fileIO.unlinkSync(fpath);
251                done();
252            } catch (e) {
253                console.log('fileIO_test_write_random_callback_000 has failed for ' + e.message + ', code: ' + e.code);
254                expect(false).assertTrue();
255            }
256        });
257
258        /**
259         * @tc.number SUB_DF_FILEIO_FILERW_READ_SEQUENCE_CALLBACK_0000
260         * @tc.name fileIO_test_read_sequence_callback_000
261         * @tc.desc Test the read() interface. callback.
262         * Open the file, file read() in the sequence way.
263         * @tc.size MEDIUM
264         * @tc.type Function
265         * @tc.level Level 3
266         * @tc.require
267         */
268        it('fileIO_test_read_sequence_callback_000', 0, async function (done) {
269            let fpath = await nextFileName('fileIO_test_read_sequence_callback_000');
270            expect(prepare200MFile(fpath)).assertTrue;
271
272            try {
273                let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_ONLY);
274                expect(isIntNum(file.fd)).assertTrue();
275                let averagerate = await IOfunc(file, 0x1400, 0xC800000, false, true, false);
276                console.log("SEQUENCE_READ Rate is" + averagerate + "M/s");
277                expect(averagerate > SEQUENCE_READ_BASELINE).assertTrue();
278                fileIO.closeSync(file);
279                fileIO.unlinkSync(fpath);
280                done();
281            } catch (e) {
282                console.log('fileIO_test_read_sequence_callback_000 has failed for ' + e.message + ', code: ' + e.code);
283                expect(false).assertTrue();
284            }
285        });
286
287        /**
288         * @tc.number SUB_DF_FILEIO_FILERW_WRITE_SEQUENCE_CALLBACK_0000
289         * @tc.name fileIO_test_write_sequence_callback_000
290         * @tc.desc Test the write() interface. callback.
291         * Open the file, file write() in the sequence way.
292         * @tc.size MEDIUM
293         * @tc.type Function
294         * @tc.level Level 3
295         * @tc.require
296         */
297        it('fileIO_test_write_sequence_callback_000', 0, async function (done) {
298            let fpath = await nextFileName('fileIO_test_write_sequence_callback_000');
299            expect(prepare200MFile(fpath)).assertTrue;
300
301            try {
302                let file = fileIO.openSync(fpath, fileIO.OpenMode.READ_WRITE);
303                expect(isIntNum(file.fd)).assertTrue();
304                let averagerate = await IOfunc(file, 0x1400, 0xC800000, false, false, false);
305                console.log("SEQUENCE_WRITE Rate is" + averagerate + "M/s");
306                expect(averagerate > SEQUENCE_WRITE_BASELINE).assertTrue();
307                fileIO.closeSync(file);
308                fileIO.unlinkSync(fpath);
309                done();
310            } catch (e) {
311                console.log('fileIO_test_write_sequence_callback_000 has failed for ' + e.message + ', code: ' + e.code);
312                expect(false).assertTrue();
313            }
314        });
315    });
316}