Created
December 18, 2011 15:22
-
-
Save buzztaiki/1493686 to your computer and use it in GitHub Desktop.
Benchmark of calling migemo with several ways from gjs.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const GLib = imports.gi.GLib; | |
const Gio = imports.gi.Gio; | |
function shell_quote(str) { | |
return "'" + str + "'"; | |
} | |
function chomp(str) { | |
return str.replace(/[\r\n]+$/, ''); | |
} | |
function MigemoClient() { | |
// Do nothing. | |
} | |
MigemoClient.prototype.query = function(query) { | |
let [res, out] = GLib.spawn_command_line_sync( | |
'sh -c "' | |
+ ['migemo-client', shell_quote(query), '|', 'nkf', '-w8'].join(' ') | |
+ '"' | |
); | |
return res ? chomp(out) : ''; | |
} | |
function CMigemo() { | |
// Do nothing. | |
} | |
CMigemo.prototype.query = function(query) { | |
let [res, out] = GLib.spawn_command_line_sync( | |
['cmigemo', '-d', '/usr/share/cmigemo/utf-8/migemo-dict', '-w', shell_quote(query)].join(' ')); | |
return res ? chomp(out) : ''; | |
} | |
function CMigemoProc() { | |
let [res, pid, in_fd, out_fd, err_fd] = GLib.spawn_async_with_pipes( | |
null, | |
['cmigemo', '-d', '/usr/share/cmigemo/utf-8/migemo-dict', '-q', '-n'], | |
null, GLib.SpawnFlags.SEARCH_PATH, null); | |
if (!res) { | |
throw "Failed to spwan cmigemo"; | |
} | |
this.pid = pid; | |
this.in_writer = new Gio.DataOutputStream({ | |
base_stream: new Gio.UnixOutputStream({fd: in_fd}) | |
}); | |
this.out_reader = new Gio.DataInputStream({ | |
base_stream: new Gio.UnixInputStream({fd: out_fd}) | |
}); | |
} | |
CMigemoProc.prototype.query = function(query) { | |
this.in_writer.put_string(query + '\n', null); | |
// XXX: Want to use cancellable. | |
let [out, size] = this.out_reader.read_line(null); | |
return out; | |
} | |
function bench(migemo) { | |
let start = new Date().getTime(); | |
let text = 'aiueo'; | |
for (let l = 1; l <= text.length; l++) { | |
migemo.query(text.slice(0, l)); | |
} | |
for (let l = text.length; l > 0; l--) { | |
migemo.query(text.slice(0, l)); | |
} | |
print(migemo.constructor.name + ':' + (new Date().getTime() - start)); | |
} | |
bench(new MigemoClient()); | |
bench(new CMigemo()); | |
bench(new CMigemoProc()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
$ gjs gjs-migemo-bench.js
** (gjs:16292): DEBUG: Command line: gjs gjs-migemo-bench.js
** (gjs:16292): DEBUG: Creating new context to eval console script
MigemoClient:332
CMigemo:1004
CMigemoProc:97