S
saurabh hirani
Hi guys,
I am working on a logging module. I was just studying the existing
perl log modules and read that - syswrite is useful in a condition
like logging as during its write operation it prevents someone else
from writing at the same time. To verify that I wrote a small perl
program:
#!/usr/bin/perl -w
use strict;
use Fcntl;
use Data:umper;
$| = 1;
sub testme {
my ($flag, $data) = @_;
if ($flag == 1) {
sysopen(FILE, 'bangbang', O_WRONLY | O_APPEND | O_CREAT);
while (1) {
syswrite FILE, "$data\n";
}
} else {
open(FILE, ">>bangbang");
while (1) {
print FILE "$data\n";
}
}
close(FILE);
}
testme($ARGV[0], $ARGV[1]);
Testing both for syswrite and print - I found that $data printing in
file is corrupted when I run 2 instances of the same program together.
But using syswrite saves me from that. It has every line intact and no
line is a mix of different data sets of the 2 programs.
But what struck me more was the size of the files created when I ran
the following programs together:
1. ./program 1 first
2. ./program 1 second
this uses syswrite to write and check whether 'first' and 'second'
clobber each other. During this run, I got the following stats:
1st run - 30 seconds - file size 40 MB
2nd run - 50 seconds - file size 61 MB
Now for the print run,
1. ./program 2 first
2. ./program 2 second
this uses print to write and check whether 'first' and 'second'
clobber each other. During this run, I got the following stats:
1st run - 30 seconds - file size 315 MB
2nd run - 50 seconds - file size 586 MB
I was running a VM so I know that my profiling wouldn't be so
accurate. But still, I had done autoflush for both syswrite and print.
And I read somewhere that for output greater than 3 KB syswrite is
faster?
Is print this faster than syswrite? Is my testing flawed? Am I missing
something? What would you use if you had to write a logging module in
perl - print or syswrite? and why?
Thanks for going through it.
I am working on a logging module. I was just studying the existing
perl log modules and read that - syswrite is useful in a condition
like logging as during its write operation it prevents someone else
from writing at the same time. To verify that I wrote a small perl
program:
#!/usr/bin/perl -w
use strict;
use Fcntl;
use Data:umper;
$| = 1;
sub testme {
my ($flag, $data) = @_;
if ($flag == 1) {
sysopen(FILE, 'bangbang', O_WRONLY | O_APPEND | O_CREAT);
while (1) {
syswrite FILE, "$data\n";
}
} else {
open(FILE, ">>bangbang");
while (1) {
print FILE "$data\n";
}
}
close(FILE);
}
testme($ARGV[0], $ARGV[1]);
Testing both for syswrite and print - I found that $data printing in
file is corrupted when I run 2 instances of the same program together.
But using syswrite saves me from that. It has every line intact and no
line is a mix of different data sets of the 2 programs.
But what struck me more was the size of the files created when I ran
the following programs together:
1. ./program 1 first
2. ./program 1 second
this uses syswrite to write and check whether 'first' and 'second'
clobber each other. During this run, I got the following stats:
1st run - 30 seconds - file size 40 MB
2nd run - 50 seconds - file size 61 MB
Now for the print run,
1. ./program 2 first
2. ./program 2 second
this uses print to write and check whether 'first' and 'second'
clobber each other. During this run, I got the following stats:
1st run - 30 seconds - file size 315 MB
2nd run - 50 seconds - file size 586 MB
I was running a VM so I know that my profiling wouldn't be so
accurate. But still, I had done autoflush for both syswrite and print.
And I read somewhere that for output greater than 3 KB syswrite is
faster?
Is print this faster than syswrite? Is my testing flawed? Am I missing
something? What would you use if you had to write a logging module in
perl - print or syswrite? and why?
Thanks for going through it.